⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qwt_painter.cpp

📁 QWT5.01用于Qt开发的二维图形库程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    drawText(painter, QRect(x, y, w, h), flags, text);
}

/*!
    Wrapper for QPainter::drawText()
*/
void QwtPainter::drawText(QPainter *painter, const QRect &rect, 
        int flags, const QString &text)
{
    QRect textRect = d_metricsMap.layoutToDevice(rect, painter);
#if QT_VERSION < 0x040000
    if ( d_SVGMode &&
        ( flags == 0 || flags & Qt::AlignVCenter ) 
        && painter->device()->devType() & QInternal::Picture )
    {
        /*
            Qt3 misalignes texts, when saving a text
            to a SVG image. 
         */
        textRect.setY(textRect.y() - painter->fontMetrics().height() / 4);
    }
#endif
    painter->drawText(textRect, flags, text);
}

#ifndef QT_NO_RICHTEXT

/*!
  Wrapper for QSimpleRichText::draw()
*/
#if QT_VERSION < 0x040000

void QwtPainter::drawSimpleRichText(QPainter *painter, const QRect &rect,
    int flags, QSimpleRichText &text)
{
    QColorGroup cg;
    cg.setColor(QColorGroup::Text, painter->pen().color());

    const QRect scaledRect = d_metricsMap.layoutToDevice(rect, painter);

    text.setWidth(painter, scaledRect.width());

    // QSimpleRichText is Qt::AlignTop by default

    int y = scaledRect.y();
    if (flags & Qt::AlignBottom)
        y += (scaledRect.height() - text.height());
    else if (flags & Qt::AlignVCenter)
        y += (scaledRect.height() - text.height())/2;

    text.draw(painter, scaledRect.x(), y, scaledRect, cg);
}
#else
void QwtPainter::drawSimpleRichText(QPainter *painter, const QRect &rect,
    int flags, QTextDocument &text)
{
    const QRect scaledRect = d_metricsMap.layoutToDevice(rect, painter);
    text.setPageSize(QSize(scaledRect.width(), QWIDGETSIZE_MAX));

    QAbstractTextDocumentLayout* layout = text.documentLayout();

    const int height = qRound(layout->documentSize().height());
    int y = scaledRect.y();
    if (flags & Qt::AlignBottom)
        y += (scaledRect.height() - height);
    else if (flags & Qt::AlignVCenter)
        y += (scaledRect.height() - height)/2;

    QAbstractTextDocumentLayout::PaintContext context;
    context.palette.setColor(QPalette::Text, painter->pen().color());

    painter->save();

    painter->translate(scaledRect.x(), scaledRect.y());
    layout->draw(painter, context);

    painter->restore();
}
#endif

#endif // !QT_NO_RICHTEXT


/*!
  Wrapper for QPainter::drawLine()
*/
void QwtPainter::drawLine(QPainter *painter, int x1, int y1, int x2, int y2)
{
    const bool deviceClipping = needDeviceClipping(painter, d_deviceClipping);

    if ( deviceClipping && 
        !(deviceClipRect().contains(x1, y1) && deviceClipRect().contains(x2, y2)) )
    {
        QwtPolygon pa(2);
        pa.setPoint(0, x1, y1);
        pa.setPoint(1, x2, y2);
        drawPolyline(painter, pa);
        return;
    }

    if ( d_metricsMap.isIdentity() )
    {
#if QT_VERSION >= 0x030200 && QT_VERSION < 0x040000
        if ( !painter->device()->isExtDev() )
#endif
        {
            painter->drawLine(x1, y1, x2, y2);
            return;
        }
    }

    const QPoint p1 = d_metricsMap.layoutToDevice(QPoint(x1, y1));
    const QPoint p2 = d_metricsMap.layoutToDevice(QPoint(x2, y2));

#if QT_VERSION >= 0x030200 && QT_VERSION < 0x040000
    if ( painter->device()->isExtDev() )
    {
        // Strange: the postscript driver of QPrinter adds an offset 
        // of 0.5 to the start/endpoint when using drawLine, but not
        // for lines painted with drawLineSegments.

        QwtPolygon pa(2);
        pa.setPoint(0, p1);
        pa.setPoint(1, p2);
        painter->drawLineSegments(pa);
    }
    else
        painter->drawLine(p1, p2);
#else
    painter->drawLine(p1, p2);
#endif
}

/*!
  Wrapper for QPainter::drawPolygon()
*/
void QwtPainter::drawPolygon(QPainter *painter, const QwtPolygon &pa)
{
    const bool deviceClipping = needDeviceClipping(painter, d_deviceClipping);

    QwtPolygon cpa = d_metricsMap.layoutToDevice(pa);
    if ( deviceClipping )
    {
#ifdef __GNUC__
#endif
        cpa = clip(cpa);
    }
    painter->drawPolygon(cpa);
}

/*!
    Wrapper for QPainter::drawPolyline()
*/
void QwtPainter::drawPolyline(QPainter *painter, const QwtPolygon &pa)
{
    const bool deviceClipping = needDeviceClipping(painter, d_deviceClipping);

    QwtPolygon cpa = d_metricsMap.layoutToDevice(pa);
    if ( deviceClipping )
        cpa = clip(cpa);
    painter->drawPolyline(cpa);
}

/*!
    Wrapper for QPainter::drawPoint()
*/

void QwtPainter::drawPoint(QPainter *painter, int x, int y)
{
    const bool deviceClipping = needDeviceClipping(painter, d_deviceClipping);

    const QPoint pos = d_metricsMap.layoutToDevice(QPoint(x, y));

    if ( deviceClipping && !deviceClipRect().contains(pos) )
        return;

    painter->drawPoint(pos);
}

void QwtPainter::drawColoredArc(QPainter *painter, const QRect &rect, 
    int peak, int arc, int interval, const QColor &c1, const QColor &c2)
{
    int h1, s1, v1;
    int h2, s2, v2;

#if QT_VERSION < 0x040000
    c1.hsv(&h1, &s1, &v1);
    c2.hsv(&h2, &s2, &v2);
#else
    c1.getHsv(&h1, &s1, &v1);
    c2.getHsv(&h2, &s2, &v2);
#endif
    
    arc /= 2;
    for ( int angle = -arc; angle < arc; angle += interval)
    {
        double ratio;
        if ( angle >= 0 )
            ratio = 1.0 - angle / double(arc);
        else
            ratio = 1.0 + angle / double(arc);
            

        QColor c;
        c.setHsv( h1 + qRound(ratio * (h2 - h1)),
            s1 + qRound(ratio * (s2 - s1)),
            v1 + qRound(ratio * (v2 - v1)) );

        painter->setPen(QPen(c, painter->pen().width()));
        painter->drawArc(rect, (peak + angle) * 16, interval * 16);
    }
}

void QwtPainter::drawFocusRect(QPainter *painter, QWidget *widget)
{
    drawFocusRect(painter, widget, widget->rect());
}

void QwtPainter::drawFocusRect(QPainter *painter, QWidget *widget,
    const QRect &rect)
{
#if QT_VERSION < 0x040000
        widget->style().drawPrimitive(QStyle::PE_FocusRect, painter,
            rect, widget->colorGroup());
#else
        QStyleOptionFocusRect opt;
        opt.init(widget);
        opt.rect = rect;
        opt.state |= QStyle::State_HasFocus;

        widget->style()->drawPrimitive(QStyle::PE_FrameFocusRect, 
            &opt, painter, widget);
#endif

}

//!  Draw a round frame
#if QT_VERSION < 0x040000
void QwtPainter::drawRoundFrame(QPainter *painter, const QRect &rect,
    int width, const QColorGroup &cg, bool sunken)
#else
void QwtPainter::drawRoundFrame(QPainter *painter, const QRect &rect,
    int width, const QPalette &palette, bool sunken)
#endif
{

#if QT_VERSION < 0x040000
    QColor c0 = cg.mid();
    QColor c1, c2;
    if ( sunken )
    {
        c1 = cg.dark();
        c2 = cg.light();
    }
    else
    {
        c1 = cg.light();
        c2 = cg.dark();
    }
#else
    QColor c0 = palette.color(QPalette::Mid);
    QColor c1, c2;
    if ( sunken )
    {
        c1 = palette.color(QPalette::Dark);
        c2 = palette.color(QPalette::Light);
    }
    else
    {
        c1 = palette.color(QPalette::Light);
        c2 = palette.color(QPalette::Dark);
    }
#endif

    painter->setPen(QPen(c0, width));
    painter->drawArc(rect, 0, 360 * 16); // full

    const int peak = 150;
    const int interval = 2;

    if ( c0 != c1 )
        drawColoredArc(painter, rect, peak, 160, interval, c0, c1);
    if ( c0 != c2 )
        drawColoredArc(painter, rect, peak + 180, 120, interval, c0, c2);
}

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();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -