void QwtPlotTextLabel::draw( QPainter *painter,
    const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    const QRectF &canvasRect ) const
{
    Q_UNUSED( xMap );
    Q_UNUSED( yMap );

    const int m = d_data->margin;

    const QRectF adjustedRect = canvasRect.adjusted( m, m, -m, -m );

    const bool doAlign = QwtPainter::roundingAlignment( painter );

    if ( doAlign )
    {
        // when the paint device is aligning it is not one
        // where scalability matters ( PDF, SVG ).
        // As rendering a text label is an expensive operation
        // we use a cache.

        const QSize sz = d_data->text.textSize( painter->font() ).toSize();

        if ( d_data->pixmap.isNull() || sz != d_data->pixmap.size()  )
        {
            d_data->pixmap = QPixmap( sz );
            d_data->pixmap.fill( Qt::transparent );

            const QRect cacheRect( QPoint(), sz );

            QPainter pmPainter( &d_data->pixmap );
            d_data->text.draw( &pmPainter, cacheRect );
        }

        const QRect r = qwtItemRect( d_data->text.renderFlags(),
            adjustedRect.toRect(), d_data->pixmap.size() );

        painter->drawPixmap( r, d_data->pixmap );
    }
    else
    {
        d_data->text.draw( painter, adjustedRect );
    }
}
static QPixmap createBubblePixmap()
{
    QPixmap pixmap(124, 48);
    pixmap.fill(Qt::transparent);
    QPainter pmPainter(&pixmap);
    pmPainter.setRenderHint(QPainter::Antialiasing);
    pmPainter.setOpacity(0.85);
    pmPainter.translate(0.5, 0.5);
    pmPainter.setPen(Qt::NoPen);
    pmPainter.setBrush(QColor(0, 0, 0, 40));
    pmPainter.drawRoundedRect(QRect(0, 0, 124, 48), 8, 8);
    QLinearGradient gradient(QPoint(0, 0), QPoint(0, 44));
    gradient.setColorAt(0.0, QColor(70, 70, 70));
    gradient.setColorAt(1.0, QColor(10, 10, 10));
    pmPainter.setBrush(gradient);
    pmPainter.setPen(QColor(60, 60, 60));
    pmPainter.drawRoundedRect(QRect(2, 1, 120, 45), 5, 5);
    pmPainter.setBrush(Qt::NoBrush);
    pmPainter.setPen(QColor(255, 255, 255, 140));
    pmPainter.drawRoundedRect(QRect(3, 2, 118, 43), 5, 5);
    pmPainter.end();
    return pixmap;
}
Example #3
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 );
}
Example #4
0
static void drawSegmentControlSegmentSegment(const QStyleOption *option, QPainter *painter, QWidget *widget)
{
    // ### Change to qstyleoption_cast!
    if (const QtStyleOptionSegmentControlSegment *segment
            = static_cast<const QtStyleOptionSegmentControlSegment *>(option)) {
#ifdef Q_WS_MAC
        if (qobject_cast<QMacStyle *>(widget->style())) {
            CGContextRef cg = qt_mac_cg_context(painter->device());
            HIThemeSegmentDrawInfo sgi;
            bool selected = (segment->state & QStyle::State_Selected);
            sgi.version = 0;
            // Things look the same regardless of enabled.
            sgi.state = getDrawState(segment->state | QStyle::State_Enabled);
            sgi.value = selected ? kThemeButtonOn : kThemeButtonOff;
            sgi.size = kHIThemeSegmentSizeNormal;
            sgi.kind = kHIThemeSegmentKindNormal;
            sgi.adornment = kHIThemeSegmentAdornmentNone;
            switch (segment->position) {
            case QtStyleOptionSegmentControlSegment::Beginning:
                sgi.position = kHIThemeSegmentPositionFirst;
                if (segment->selectedPositions == QtStyleOptionSegmentControlSegment::NotAdjacent
                    || selected)
                    sgi.adornment |= kHIThemeSegmentAdornmentTrailingSeparator;
                break;
            case QtStyleOptionSegmentControlSegment::Middle:
                sgi.position = kHIThemeSegmentPositionMiddle;
                if (selected && !(segment->selectedPositions & QtStyleOptionSegmentControlSegment::PreviousIsSelected))
                    sgi.adornment |= kHIThemeSegmentAdornmentLeadingSeparator;
                if (selected || !(segment->selectedPositions & QtStyleOptionSegmentControlSegment::NextIsSelected)) // Also when we're selected.
                    sgi.adornment |= kHIThemeSegmentAdornmentTrailingSeparator;
                break;
            case QStyleOptionTab::End:
                sgi.position = kHIThemeSegmentPositionLast;
                if (selected && !(segment->selectedPositions & QtStyleOptionSegmentControlSegment::PreviousIsSelected))
                    sgi.adornment |= kHIThemeSegmentAdornmentLeadingSeparator;
                break;
            case QStyleOptionTab::OnlyOneTab:
                sgi.position = kHIThemeSegmentPositionOnly;
                break;
            }

            HIRect hirect = CGRectMake(segment->rect.x(), segment->rect.y(),
                                       segment->rect.width(), segment->rect.height());
            HIThemeDrawSegment(&hirect, &sgi, cg, kHIThemeOrientationNormal);
            CGContextRelease(cg);
        } else
#endif
        {
            Q_UNUSED(widget);
            painter->save();

            bool selected = (segment->state & QStyle::State_Selected);

            QPixmap pm;

            QSize buttonSize = widget->rect().size();
            QString key = QString("qt_segment %0 %1 %2").arg(option->state).arg(buttonSize.width()).arg(buttonSize.height());

            if (!QPixmapCache::find(key, pm)) {
                pm = QPixmap(buttonSize);
                pm.fill(Qt::transparent);
                QPainter pmPainter(&pm);
                QStyleOptionButton btnOpt;
                btnOpt.QStyleOption::operator =(*option);
                btnOpt.state &= ~QStyle::State_HasFocus;
                btnOpt.rect = QRect(QPoint(0, 0), buttonSize);;
                btnOpt.state = option->state;

                if (selected)
                    btnOpt.state |= QStyle::State_Sunken;
                else
                    btnOpt.state |= QStyle::State_Raised;
                widget->style()->drawPrimitive(QStyle::PE_PanelButtonCommand, &btnOpt, &pmPainter, widget);
                pmPainter.end();
                QPixmapCache::insert(key, pm);
            }
            int margin = widget->style()->pixelMetric(QStyle::PM_DefaultFrameWidth, option, widget);
            switch (segment->position) {
            case QtStyleOptionSegmentControlSegment::Beginning:
                painter->setClipRect(option->rect);
                painter->drawPixmap(0, 0, pm);
                painter->setOpacity(0.6);
                painter->setPen(option->palette.dark().color());
                painter->drawLine(option->rect.topRight() + QPoint(-1, margin), option->rect.bottomRight() + QPoint(-1, -margin));
                break;
            case QtStyleOptionSegmentControlSegment::Middle:
                painter->setClipRect(option->rect);
                painter->drawPixmap(0, 0, pm);
                painter->setPen(option->palette.dark().color());
                painter->drawLine(option->rect.topRight() + QPoint(-1, margin), option->rect.bottomRight() + QPoint(-1, -margin));
                break;
            case QStyleOptionTab::End:
                painter->setClipRect(option->rect);
                painter->drawPixmap(0, 0, pm);
                break;
            case QStyleOptionTab::OnlyOneTab:
                painter->setClipRect(option->rect);
                painter->drawPixmap(0, 0, pm);
                break;
            }
            painter->restore();
        }
    }
}
Example #5
0
void RichTextDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItemV4 opt = option;
    initStyleOption(&opt, index);

    if (!opt.text.contains(QLatin1Char('<'))) {
        QStyledItemDelegate::paint(painter, option, index);
        return;
    }

    QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();

    QString text = opt.text;
    opt.text.clear();
    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget);

    QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &opt, opt.widget);

    QString key = QLatin1String("RichTextDelegate-") + text;
    QPixmap pm;
    if (!QPixmapCache::find(key, &pm)) {
        m_doc->setDefaultFont(opt.font);

        QTextOption textOpt = m_doc->defaultTextOption();
        textOpt.setAlignment(QStyle::visualAlignment(opt.direction, opt.displayAlignment));
        bool wrapText = opt.features & QStyleOptionViewItemV2::WrapText;
        textOpt.setWrapMode(wrapText ? QTextOption::WordWrap : QTextOption::ManualWrap);
        textOpt.setTextDirection(opt.direction);
        m_doc->setDefaultTextOption(textOpt);

        QTextFrameFormat fmt = m_doc->rootFrame()->frameFormat();
        fmt.setMargin(0);
        m_doc->rootFrame()->setFrameFormat(fmt);

        m_doc->setTextWidth(textRect.width());
        m_doc->setHtml(text);

        QAbstractTextDocumentLayout::PaintContext context;
        context.palette = opt.palette;
        context.palette.setColor(QPalette::Text, context.palette.light().color());

        pm = QPixmap(m_doc->size().width(), m_doc->size().height());
        pm.fill(Qt::transparent);
        QPainter pmPainter(&pm);
        m_doc->documentLayout()->draw(&pmPainter, context);

        QPixmapCache::insert(key, pm);
    }

    int hDelta = textRect.width() - pm.width();
    int vDelta = textRect.height() - pm.height();
    int x = textRect.left();
    int y = textRect.top();

    if (opt.displayAlignment & Qt::AlignVCenter)
        y += vDelta / 2;
    else if (opt.displayAlignment & Qt::AlignBottom)
        y += vDelta;

    if (opt.displayAlignment & Qt::AlignHCenter)
        x += hDelta / 2;
    else if (opt.displayAlignment & Qt::AlignRight)
        x += hDelta;

    painter->drawPixmap(x, y, pm);
}
Example #6
0
void QwtPlotTextLabel::draw( QPainter *painter,
    const QwtScaleMap &xMap, const QwtScaleMap &yMap,
    const QRectF &canvasRect ) const
{
    Q_UNUSED( xMap );
    Q_UNUSED( yMap );

    const int m = d_data->margin;

    const QRectF rect = textRect( canvasRect.adjusted( m, m, -m, -m ),
        d_data->text.textSize( painter->font() ) );

    bool doCache = QwtPainter::roundingAlignment( painter );
    if ( doCache )
    {
        switch( painter->paintEngine()->type() )
        {
            case QPaintEngine::Picture:
            case QPaintEngine::User: // usually QwtGraphic
            {
                // don't use a cache for record/replay devices
                doCache = false;
                break;
            }
            default:;
        }
    }

    if ( doCache )
    {
        // when the paint device is aligning it is not one
        // where scalability matters ( PDF, SVG ).
        // As rendering a text label is an expensive operation
        // we use a cache.

        int pw = 0;
        if ( d_data->text.borderPen().style() != Qt::NoPen )
            pw = qMax( d_data->text.borderPen().width(), 1 );

        QRect pixmapRect;
        pixmapRect.setLeft( qFloor( rect.left() ) - pw );
        pixmapRect.setTop( qFloor( rect.top() ) - pw );
        pixmapRect.setRight( qCeil( rect.right() ) + pw );
        pixmapRect.setBottom( qCeil( rect.bottom() ) + pw );

#define QWT_HIGH_DPI 1

#if QT_VERSION >= 0x050100 && QWT_HIGH_DPI
        const qreal pixelRatio = painter->device()->devicePixelRatio();
        const QSize scaledSize = pixmapRect.size() * pixelRatio;
#else
        const QSize scaledSize = pixmapRect.size();
#endif
        if ( d_data->pixmap.isNull() ||
            ( scaledSize != d_data->pixmap.size() )  )
        {
            d_data->pixmap = QPixmap( scaledSize );
#if QT_VERSION >= 0x050100 && QWT_HIGH_DPI
            d_data->pixmap.setDevicePixelRatio( pixelRatio );
#endif
            d_data->pixmap.fill( Qt::transparent );

            const QRect r( pw, pw,
                pixmapRect.width() - 2 * pw, pixmapRect.height() - 2 * pw );

            QPainter pmPainter( &d_data->pixmap );
            d_data->text.draw( &pmPainter, r );
        }

        painter->drawPixmap( pixmapRect, d_data->pixmap );
    }
    else
    {
        d_data->text.draw( painter, rect );
    }
}