示例#1
0
/*!
  Draw a color bar into a rectangle

  \param painter Painter
  \param colorMap Color map
  \param interval Value range
  \param scaleMap Scale map
  \param orientation Orientation
  \param rect Traget rectangle
*/
void QwtPainter::drawColorBar( QPainter *painter,
        const QwtColorMap &colorMap, const QwtInterval &interval,
        const QwtScaleMap &scaleMap, Qt::Orientation orientation,
        const QRectF &rect )
{
    QVector<QRgb> colorTable;
    if ( colorMap.format() == QwtColorMap::Indexed )
        colorTable = colorMap.colorTable( interval );

    QColor c;

    const QRect devRect = rect.toAlignedRect();

    /*
      We paint to a pixmap first to have something scalable for printing
      ( f.e. in a Pdf document )
     */

    QPixmap pixmap( devRect.size() );
    QPainter pmPainter( &pixmap );
    pmPainter.translate( -devRect.x(), -devRect.y() );

    if ( orientation == Qt::Horizontal )
    {
        QwtScaleMap sMap = scaleMap;
        sMap.setPaintInterval( rect.left(), rect.right() );

        for ( int x = devRect.left(); x <= devRect.right(); x++ )
        {
            const double value = sMap.invTransform( x );

            if ( colorMap.format() == QwtColorMap::RGB )
                c.setRgb( colorMap.rgb( interval, value ) );
            else
                c = colorTable[colorMap.colorIndex( interval, value )];

            pmPainter.setPen( c );
            pmPainter.drawLine( x, devRect.top(), x, devRect.bottom() );
        }
    }
    else // Vertical
    {
        QwtScaleMap sMap = scaleMap;
        sMap.setPaintInterval( rect.bottom(), rect.top() );

        for ( int y = devRect.top(); y <= devRect.bottom(); y++ )
        {
            const double value = sMap.invTransform( y );

            if ( colorMap.format() == QwtColorMap::RGB )
                c.setRgb( colorMap.rgb( interval, value ) );
            else
                c = colorTable[colorMap.colorIndex( interval, value )];

            pmPainter.setPen( c );
            pmPainter.drawLine( devRect.left(), y, devRect.right(), y );
        }
    }
    pmPainter.end();

    drawPixmap( painter, rect, pixmap );
}
void QwtPainter::drawColorBar(QPainter *painter,
        const QwtColorMap &colorMap, const QwtDoubleInterval &interval,
        const QwtScaleMap &scaleMap, Qt::Orientation orientation,
        const QRect &rect)
{
    painter->save();

    QwtPainter::setClipRect(painter, rect);

#if QT_VERSION < 0x040000
    QValueVector<QRgb> colorTable;
#else
    QVector<QRgb> colorTable;
#endif
    if ( colorMap.format() == QwtColorMap::Indexed )
        colorTable = colorMap.colorTable(interval);

    QColor c;

    const QRect devRect = d_metricsMap.layoutToDevice(rect);

    if ( orientation == Qt::Horizontal )
    {
        QwtScaleMap sMap = scaleMap;
        sMap.setPaintInterval(devRect.left(), devRect.right());

        for ( int x = devRect.left(); x <= devRect.right(); x++ )
        {
            const double value = sMap.invTransform(x);

            if ( colorMap.format() == QwtColorMap::RGB )
                c.setRgb(colorMap.rgb(interval, value));
            else
                c = colorTable[colorMap.colorIndex(interval, value)];

            painter->setBrush(QBrush(c));

            const QRect r(x, devRect.top(), 1, devRect.height());
            QwtPainter::drawRect(painter, r);
            painter->setPen(c);
            painter->drawLine(x, devRect.top(), x, devRect.bottom() - 1);
        }
    }
    else // Vertical
    {
        QwtScaleMap sMap = scaleMap;
        sMap.setPaintInterval(devRect.bottom(), devRect.top());

        for ( int y = devRect.top(); y <= devRect.bottom(); y++ )
        {
            const double value = sMap.invTransform(y);

            if ( colorMap.format() == QwtColorMap::RGB )
                c.setRgb(colorMap.rgb(interval, value));
            else
                c = colorTable[colorMap.colorIndex(interval, value)];

            painter->setPen(c);
            painter->drawLine(devRect.left(), y, devRect.right() - 1, y);
        }
    }
    painter->restore();
}