A collection of QPainter workarounds. More...
#include <qwt_painter.h>
Static Public Member Functions | |
static void | setMetricsMap (const QPaintDevice *layout, const QPaintDevice *device) |
static void | setMetricsMap (const QwtMetricsMap &) |
static void | resetMetricsMap () |
static const QwtMetricsMap & | metricsMap () |
static void | setDeviceClipping (bool) |
En/Disable device clipping. | |
static bool | deviceClipping () |
static const QRect & | deviceClipRect () |
static void | setClipRect (QPainter *, const QRect &) |
static void | drawText (QPainter *, int x, int y, const QString &) |
static void | drawText (QPainter *, const QPoint &, const QString &) |
static void | drawText (QPainter *, int x, int y, int w, int h, int flags, const QString &) |
static void | drawText (QPainter *, const QRect &, int flags, const QString &) |
static void | drawSimpleRichText (QPainter *, const QRect &, int flags, QSimpleRichText &) |
static void | drawRect (QPainter *, int x, int y, int w, int h) |
static void | drawRect (QPainter *, const QRect &rect) |
static void | fillRect (QPainter *, const QRect &, const QBrush &) |
static void | drawEllipse (QPainter *, const QRect &) |
static void | drawPie (QPainter *, const QRect &r, int a, int alen) |
static void | drawLine (QPainter *, int x1, int y1, int x2, int y2) |
static void | drawLine (QPainter *, const QPoint &p1, const QPoint &p2) |
Wrapper for QPainter::drawLine() | |
static void | drawPolygon (QPainter *, const QwtPolygon &pa) |
static void | drawPolyline (QPainter *, const QwtPolygon &pa) |
static void | drawPoint (QPainter *, int x, int y) |
static void | drawRoundFrame (QPainter *, const QRect &, int width, const QColorGroup &cg, bool sunken) |
Draw a round frame. | |
static void | drawFocusRect (QPainter *, QWidget *) |
static void | drawFocusRect (QPainter *, QWidget *, const QRect &) |
static void | drawColorBar (QPainter *painter, const QwtColorMap &, const QwtDoubleInterval &, const QwtScaleMap &, Qt::Orientation, const QRect &) |
static void | setSVGMode (bool on) |
En/Disable SVG mode. | |
static bool | isSVGMode () |
static QPen | scaledPen (const QPen &) |
Scale a pen according to the layout metrics. |
A collection of QPainter workarounds.
1) Clipping to coordinate system limits (Qt3 only)
On X11 pixel coordinates are stored in shorts. Qt produces overruns when mapping QCOORDS to shorts.
2) Scaling to device metrics
QPainter scales fonts, line and fill patterns to the metrics of the paint device. Other values like the geometries of rects, points remain device independend. To enable a device independent widget implementation, QwtPainter adds scaling of these geometries. (Unfortunately QPainter::scale scales both types of paintings, so the objects of the first type would be scaled twice).
Definition at line 63 of file qwt_painter.h.
bool QwtPainter::deviceClipping | ( | ) | [inline, static] |
Returns whether device clipping is enabled. On X11 the default is enabled, otherwise it is disabled.
Definition at line 154 of file qwt_painter.h.
Referenced by drawEllipse(), drawLine(), drawPie(), drawPoint(), drawPolygon(), drawPolyline(), drawRect(), drawText(), fillRect(), and isClippingNeeded().
{
return d_deviceClipping;
}
const QRect & QwtPainter::deviceClipRect | ( | ) | [static] |
Returns rect for device clipping
Definition at line 102 of file qwt_painter.cpp.
References QWT_COORD_MAX, and QWT_COORD_MIN.
Referenced by isClippingNeeded().
{ static QRect clip; if ( !clip.isValid() ) { clip.setCoords(QWT_COORD_MIN, QWT_COORD_MIN, QWT_COORD_MAX, QWT_COORD_MAX); } return clip; }
void QwtPainter::drawColorBar | ( | QPainter * | painter, |
const QwtColorMap & | colorMap, | ||
const QwtDoubleInterval & | interval, | ||
const QwtScaleMap & | scaleMap, | ||
Qt::Orientation | orientation, | ||
const QRect & | rect | ||
) | [static] |
Definition at line 671 of file qwt_painter.cpp.
References QwtColorMap::colorIndex(), QwtColorMap::colorTable(), QwtColorMap::format(), QwtColorMap::Indexed, QwtScaleMap::invTransform(), QwtMetricsMap::layoutToDevice(), QwtColorMap::rgb(), QwtColorMap::RGB, and QwtScaleMap::setPaintInterval().
{ #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); /* 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(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)]; pmPainter.setPen(c); pmPainter.drawLine(x, devRect.top(), x, devRect.bottom()); } } 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)]; pmPainter.setPen(c); pmPainter.drawLine(devRect.left(), y, devRect.right(), y); } } pmPainter.end(); painter->drawPixmap(devRect, pixmap); }
void QwtPainter::drawEllipse | ( | QPainter * | painter, |
const QRect & | rect | ||
) | [static] |
Wrapper for QPainter::drawEllipse()
Definition at line 291 of file qwt_painter.cpp.
References deviceClipping(), isClippingNeeded(), and QwtMetricsMap::layoutToDevice().
Referenced by QwtSymbol::draw(), and QwtPicker::drawRubberBand().
{ QRect r = d_metricsMap.layoutToDevice(rect, painter); QRect clipRect; const bool deviceClipping = isClippingNeeded(painter, clipRect); if ( deviceClipping && !clipRect.contains(r) ) return; #if QT_VERSION >= 0x040000 if ( painter->pen().style() != Qt::NoPen && painter->pen().color().isValid() ) { // Qt4 adds the pen to the rect, Qt3 not. int pw = painter->pen().width(); if ( pw == 0 ) pw = 1; r.setWidth(r.width() - pw); r.setHeight(r.height() - pw); } #endif painter->drawEllipse(r); }
void QwtPainter::drawFocusRect | ( | QPainter * | painter, |
QWidget * | widget | ||
) | [static] |
Definition at line 598 of file qwt_painter.cpp.
Referenced by QwtWheel::draw(), QwtSlider::draw(), QwtKnob::draw(), QwtTextLabel::drawContents(), and QwtPlotCanvas::drawFocusIndicator().
{ drawFocusRect(painter, widget, widget->rect()); }
void QwtPainter::drawFocusRect | ( | QPainter * | painter, |
QWidget * | widget, | ||
const QRect & | rect | ||
) | [static] |
Definition at line 603 of file qwt_painter.cpp.
{ #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 }
void QwtPainter::drawLine | ( | QPainter * | painter, |
int | x1, | ||
int | y1, | ||
int | x2, | ||
int | y2 | ||
) | [static] |
Wrapper for QPainter::drawLine()
Definition at line 436 of file qwt_painter.cpp.
References deviceClipping(), drawPolyline(), isClippingNeeded(), QwtMetricsMap::isIdentity(), and QwtMetricsMap::layoutToDevice().
Referenced by QwtSymbol::draw(), QwtPlotMarker::drawAt(), QwtScaleDraw::drawBackbone(), QmitkHistogram::drawBar(), QwtPlotSpectrogram::drawContourLines(), QwtLegendItem::drawIdentifier(), drawLine(), QwtPicker::drawRubberBand(), QwtPlotCurve::drawSticks(), QwtScaleDraw::drawTick(), and QwtRoundScaleDraw::drawTick().
{ QRect clipRect; const bool deviceClipping = isClippingNeeded(painter, clipRect); if ( deviceClipping && !(clipRect.contains(x1, y1) && clipRect.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 }
void QwtPainter::drawLine | ( | QPainter * | painter, |
const QPoint & | p1, | ||
const QPoint & | p2 | ||
) | [inline, static] |
Wrapper for QPainter::drawLine()
Definition at line 143 of file qwt_painter.h.
References drawLine().
{ drawLine(painter, p1.x(), p1.y(), p2.x(), p2.y()); }
void QwtPainter::drawPie | ( | QPainter * | painter, |
const QRect & | rect, | ||
int | a, | ||
int | alen | ||
) | [static] |
Wrapper for QPainter::drawPie()
Definition at line 275 of file qwt_painter.cpp.
References deviceClipping(), isClippingNeeded(), and QwtMetricsMap::layoutToDevice().
{ const QRect r = d_metricsMap.layoutToDevice(rect, painter); QRect clipRect; const bool deviceClipping = isClippingNeeded(painter, clipRect); if ( deviceClipping && !clipRect.contains(r) ) return; painter->drawPie(r, a, alen); }
void QwtPainter::drawPoint | ( | QPainter * | painter, |
int | x, | ||
int | y | ||
) | [static] |
Wrapper for QPainter::drawPoint()
Definition at line 551 of file qwt_painter.cpp.
References deviceClipping(), isClippingNeeded(), and QwtMetricsMap::layoutToDevice().
Referenced by QwtPlotCurve::drawDots().
{ QRect clipRect; const bool deviceClipping = isClippingNeeded(painter, clipRect); const QPoint pos = d_metricsMap.layoutToDevice(QPoint(x, y)); if ( deviceClipping && !clipRect.contains(pos) ) return; painter->drawPoint(pos); }
void QwtPainter::drawPolygon | ( | QPainter * | painter, |
const QwtPolygon & | pa | ||
) | [static] |
Wrapper for QPainter::drawPolygon()
Definition at line 487 of file qwt_painter.cpp.
References QwtClipper::clipPolygon(), deviceClipping(), isClippingNeeded(), and QwtMetricsMap::layoutToDevice().
Referenced by QwtSymbol::draw(), and QwtPlotCurve::fillCurve().
{ QRect clipRect; const bool deviceClipping = isClippingNeeded(painter, clipRect); QwtPolygon cpa = d_metricsMap.layoutToDevice(pa); if ( deviceClipping ) { #ifdef __GNUC__ #endif cpa = QwtClipper::clipPolygon(clipRect, cpa); } painter->drawPolygon(cpa); }
void QwtPainter::drawPolyline | ( | QPainter * | painter, |
const QwtPolygon & | pa | ||
) | [static] |
Wrapper for QPainter::drawPolyline()
Definition at line 505 of file qwt_painter.cpp.
References QwtClipper::clipPolygon(), deviceClipping(), isClippingNeeded(), QwtMetricsMap::layoutToDevice(), and qwtMin.
Referenced by drawLine(), QwtPlotCurve::drawLines(), drawRect(), and QwtPlotCurve::drawSteps().
{ QRect clipRect; const bool deviceClipping = isClippingNeeded(painter, clipRect); QwtPolygon cpa = d_metricsMap.layoutToDevice(pa); if ( deviceClipping ) cpa = QwtClipper::clipPolygon(clipRect, cpa); #if QT_VERSION >= 0x040000 && QT_VERSION < 0x040400 bool doSplit = false; const QPaintEngine *pe = painter->paintEngine(); if ( pe && pe->type() == QPaintEngine::Raster && painter->pen().width() >= 2 ) { /* The raster paint engine seems to use some algo with O(n*n). ( Qt 4.3 is better than Qt 4.2, but remains unacceptable) To work around this problem, we have to split the polygon into smaller pieces. */ doSplit = true; } if ( doSplit ) { const int numPoints = cpa.size(); const QPoint *points = cpa.data(); const int splitSize = 20; for ( int i = 0; i < numPoints; i += splitSize ) { const int n = qwtMin(splitSize + 1, cpa.size() - i); painter->drawPolyline(points + i, n); } } else #endif painter->drawPolyline(cpa); }
void QwtPainter::drawRect | ( | QPainter * | painter, |
int | x, | ||
int | y, | ||
int | w, | ||
int | h | ||
) | [static] |
Wrapper for QPainter::drawRect()
Definition at line 190 of file qwt_painter.cpp.
Referenced by QwtText::draw(), QwtSymbol::draw(), QmitkHistogram::drawBar(), QwtPicker::drawRubberBand(), and QwtPlot::printCanvas().
void QwtPainter::drawRect | ( | QPainter * | painter, |
const QRect & | rect | ||
) | [static] |
Wrapper for QPainter::drawRect()
Definition at line 198 of file qwt_painter.cpp.
References deviceClipping(), drawPolyline(), fillRect(), isClippingNeeded(), and QwtMetricsMap::layoutToDevice().
{ const QRect r = d_metricsMap.layoutToDevice(rect, painter); QRect clipRect; const bool deviceClipping = isClippingNeeded(painter, clipRect); if ( deviceClipping ) { if ( !clipRect.intersects(r) ) return; if ( !clipRect.contains(r) ) { fillRect(painter, r & clipRect, painter->brush()); int pw = painter->pen().width(); pw = pw % 2 + pw / 2; QwtPolygon pa(5); pa.setPoint(0, r.left(), r.top()); pa.setPoint(1, r.right() - pw, r.top()); pa.setPoint(2, r.right() - pw, r.bottom() - pw); pa.setPoint(3, r.left(), r.bottom() - pw); pa.setPoint(4, r.left(), r.top()); painter->save(); painter->setBrush(Qt::NoBrush); drawPolyline(painter, pa); painter->restore(); return; } } painter->drawRect(r); }
void QwtPainter::drawRoundFrame | ( | QPainter * | painter, |
const QRect & | rect, | ||
int | width, | ||
const QColorGroup & | cg, | ||
bool | sunken | ||
) | [static] |
Draw a round frame.
Definition at line 623 of file qwt_painter.cpp.
Referenced by QwtDial::drawFrame().
{ #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::drawSimpleRichText | ( | QPainter * | painter, |
const QRect & | rect, | ||
int | flags, | ||
QSimpleRichText & | text | ||
) | [static] |
Wrapper for QSimpleRichText::draw()
Definition at line 382 of file qwt_painter.cpp.
References QwtMetricsMap::layoutToDevice().
Referenced by QwtRichTextEngine::draw().
{ 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); }
void QwtPainter::drawText | ( | QPainter * | painter, |
int | x, | ||
int | y, | ||
const QString & | text | ||
) | [static] |
Wrapper for QPainter::drawText()
Definition at line 321 of file qwt_painter.cpp.
Referenced by QwtPlainTextEngine::draw(), and drawText().
void QwtPainter::drawText | ( | QPainter * | painter, |
const QPoint & | pos, | ||
const QString & | text | ||
) | [static] |
Wrapper for QPainter::drawText()
Definition at line 330 of file qwt_painter.cpp.
References deviceClipping(), isClippingNeeded(), and QwtMetricsMap::layoutToDevice().
{ const QPoint p = d_metricsMap.layoutToDevice(pos, painter); QRect clipRect; const bool deviceClipping = isClippingNeeded(painter, clipRect); if ( deviceClipping && !clipRect.contains(p) ) return; painter->drawText(p, text); }
void QwtPainter::drawText | ( | QPainter * | painter, |
int | x, | ||
int | y, | ||
int | w, | ||
int | h, | ||
int | flags, | ||
const QString & | text | ||
) | [static] |
Wrapper for QPainter::drawText()
Definition at line 347 of file qwt_painter.cpp.
References drawText().
void QwtPainter::drawText | ( | QPainter * | painter, |
const QRect & | rect, | ||
int | flags, | ||
const QString & | text | ||
) | [static] |
Wrapper for QPainter::drawText()
Definition at line 356 of file qwt_painter.cpp.
References QwtMetricsMap::layoutToDevice().
{ 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); }
void QwtPainter::fillRect | ( | QPainter * | painter, |
const QRect & | rect, | ||
const QBrush & | brush | ||
) | [static] |
Wrapper for QPainter::fillRect()
Definition at line 239 of file qwt_painter.cpp.
References deviceClipping(), isClippingNeeded(), and QwtMetricsMap::layoutToDevice().
Referenced by drawRect(), and QwtPlot::printCanvas().
{ if ( !rect.isValid() ) return; QRect clipRect; const bool deviceClipping = isClippingNeeded(painter, clipRect); #if QT_VERSION >= 0x040000 /* Performance of Qt4 is horrible for non trivial brushs. Without clipping expect minutes or hours for repainting large rects (might result from zooming) */ if ( deviceClipping ) clipRect &= painter->window(); else clipRect = painter->window(); if ( painter->hasClipping() ) clipRect &= painter->clipRegion().boundingRect(); #endif QRect r = d_metricsMap.layoutToDevice(rect, painter); if ( deviceClipping ) r = r.intersect(clipRect); if ( r.isValid() ) painter->fillRect(r, brush); }
bool QwtPainter::isSVGMode | ( | ) | [static] |
Definition at line 132 of file qwt_painter.cpp.
{
return d_SVGMode;
}
const QwtMetricsMap & QwtPainter::metricsMap | ( | ) | [static] |
Definition at line 174 of file qwt_painter.cpp.
Referenced by QwtText::draw(), QwtSymbol::draw(), QwtLegendItem::drawIdentifier(), QwtLegendItem::drawItem(), QwtScaleDraw::drawLabel(), QwtPlotCurve::drawSymbols(), QwtScaleDraw::drawTick(), QwtScaleWidget::drawTitle(), QwtText::heightForWidth(), QwtPlot::print(), QwtPlot::printScale(), scaledPen(), and QwtText::textSize().
{
return d_metricsMap;
}
void QwtPainter::resetMetricsMap | ( | ) | [static] |
Reset the metrics map to the ratio 1:1
Definition at line 166 of file qwt_painter.cpp.
Referenced by QwtScaleDraw::drawLabel(), QwtScaleDraw::drawTick(), QwtScaleWidget::drawTitle(), and QwtPlot::print().
{ d_metricsMap = QwtMetricsMap(); }
QPen QwtPainter::scaledPen | ( | const QPen & | pen ) | [static] |
Scale a pen according to the layout metrics.
The width of non cosmetic pens is scaled from screen to layout metrics, so that they look similar on paint devices with different resolutions.
pen | Unscaled pen |
Definition at line 747 of file qwt_painter.cpp.
References metricsMap().
Referenced by QwtText::draw(), QwtSymbol::draw(), QwtPlotGrid::draw(), QwtPlotCurve::draw(), QwtPlotMarker::drawAt(), QwtPlotSpectrogram::drawContourLines(), QwtLegendItem::drawIdentifier(), and QwtPlotCurve::drawSymbols().
{ #if QT_VERSION < 0x040000 return pen; #else QPen sPen = pen; if ( !pen.isCosmetic() ) { int pw = pen.width(); if ( pw == 0 ) pw = 1; sPen.setWidth(QwtPainter::metricsMap().screenToLayoutX(pw)); sPen.setCosmetic(true); } return sPen; #endif }
void QwtPainter::setClipRect | ( | QPainter * | painter, |
const QRect & | rect | ||
) | [static] |
Wrapper for QPainter::setClipRect()
Definition at line 182 of file qwt_painter.cpp.
References QwtMetricsMap::layoutToDevice().
Referenced by QwtPlot::printCanvas(), and QwtPlot::printLegend().
{ painter->setClipRect(d_metricsMap.layoutToDevice(rect, painter)); }
void QwtPainter::setDeviceClipping | ( | bool | enable ) | [static] |
En/Disable device clipping.
On X11 the default for device clipping is enabled, otherwise it is disabled.
Definition at line 93 of file qwt_painter.cpp.
{ d_deviceClipping = enable; }
void QwtPainter::setMetricsMap | ( | const QPaintDevice * | layout, |
const QPaintDevice * | device | ||
) | [static] |
Scale all QwtPainter drawing operations using the ratio QwtPaintMetrics(from).logicalDpiX() / QwtPaintMetrics(to).logicalDpiX() and QwtPaintMetrics(from).logicalDpiY() / QwtPaintMetrics(to).logicalDpiY()
Definition at line 147 of file qwt_painter.cpp.
References QwtMetricsMap::setMetrics().
Referenced by QwtScaleDraw::drawLabel(), QwtScaleDraw::drawTick(), QwtScaleWidget::drawTitle(), and QwtPlot::print().
{ d_metricsMap.setMetrics(layout, device); }
void QwtPainter::setMetricsMap | ( | const QwtMetricsMap & | map ) | [static] |
void QwtPainter::setSVGMode | ( | bool | on ) | [static] |
En/Disable SVG mode.
When saving a QPicture to a SVG some texts are misaligned. In SVGMode QwtPainter tries to fix them.
Definition at line 127 of file qwt_painter.cpp.
{ d_SVGMode = on; }