1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
50 #include "qpaintdevice.h"
51 #include "qpaintengine.h"
53 #include "qpainter_p.h"
54 #include "qpainterpath.h"
56 #include "qpixmapcache.h"
58 #include "qtextlayout.h"
60 #include "qapplication.h"
63 #include "qvarlengtharray.h"
64 #include "qstatictext.h"
65 #include "qglyphrun.h"
67 #include <private/qfontengine_p.h>
68 #include <private/qpaintengine_p.h>
69 #include <private/qemulationpaintengine_p.h>
70 #include <private/qpainterpath_p.h>
71 #include <private/qtextengine_p.h>
72 #include <private/qwidget_p.h>
73 #include <private/qpaintengine_raster_p.h>
74 #include <private/qmath_p.h>
75 #include <private/qstatictext_p.h>
76 #include <private/qglyphrun_p.h>
77 #include <private/qstylehelper_p.h>
78 #include <private/qrawfont_p.h>
82 #define QGradient_StretchToDevice 0x10000000
83 #define QPaintEngine_OpaqueBackground 0x40000000
85 // #define QT_DEBUG_DRAW
87 bool qt_show_painter_debug_output = true;
90 extern QPixmap qt_pixmapForBrush(int style, bool invert);
92 void qt_format_text(const QFont &font,
93 const QRectF &_r, int tf, const QTextOption *option, const QString& str, QRectF *brect,
94 int tabstops, int* tabarray, int tabarraylen,
96 static void drawTextItemDecoration(QPainter *painter, const QPointF &pos, const QFontEngine *fe,
97 QTextCharFormat::UnderlineStyle underlineStyle,
98 QTextItem::RenderFlags flags, qreal width,
99 const QTextCharFormat &charFormat);
100 // Helper function to calculate left most position, width and flags for decoration drawing
101 Q_GUI_EXPORT void qt_draw_decoration_for_glyphs(QPainter *painter, const glyph_t *glyphArray,
102 const QFixedPoint *positions, int glyphCount,
103 QFontEngine *fontEngine, const QFont &font,
104 const QTextCharFormat &charFormat);
106 static inline QGradient::CoordinateMode coordinateMode(const QBrush &brush)
108 switch (brush.style()) {
109 case Qt::LinearGradientPattern:
110 case Qt::RadialGradientPattern:
111 case Qt::ConicalGradientPattern:
112 return brush.gradient()->coordinateMode();
116 return QGradient::LogicalMode;
119 /* Returns true if the gradient requires stretch to device...*/
120 static inline bool check_gradient(const QBrush &brush)
122 return coordinateMode(brush) == QGradient::StretchToDeviceMode;
125 extern bool qHasPixmapTexture(const QBrush &);
127 static inline bool is_brush_transparent(const QBrush &brush) {
128 Qt::BrushStyle s = brush.style();
129 bool brushBitmap = qHasPixmapTexture(brush)
130 ? brush.texture().isQBitmap()
131 : (brush.textureImage().depth() == 1);
132 return ((s >= Qt::Dense1Pattern && s <= Qt::DiagCrossPattern)
133 || (s == Qt::TexturePattern && brushBitmap));
136 static inline bool is_pen_transparent(const QPen &pen) {
137 return pen.style() > Qt::SolidLine || is_brush_transparent(pen.brush());
140 /* Discards the emulation flags that are not relevant for line drawing
141 and returns the result
143 static inline uint line_emulation(uint emulation)
145 return emulation & (QPaintEngine::PrimitiveTransform
146 | QPaintEngine::AlphaBlend
147 | QPaintEngine::Antialiasing
148 | QPaintEngine::BrushStroke
149 | QPaintEngine::ConstantOpacity
150 | QGradient_StretchToDevice
151 | QPaintEngine::ObjectBoundingModeGradients
152 | QPaintEngine_OpaqueBackground);
156 static bool qt_painter_thread_test(int devType, const char *what, bool extraCondition = false)
159 case QInternal::Image:
160 case QInternal::Printer:
161 case QInternal::Picture:
162 // can be drawn onto these devices safely from any thread
169 if (QApplication::testAttribute(Qt::AA_X11InitThreads))
172 if (!extraCondition && QThread::currentThread() != qApp->thread()) {
173 qWarning("QPainter: It is not safe to use %s outside the GUI thread", what);
182 void QPainterPrivate::checkEmulation()
185 if (extended->flags() & QPaintEngineEx::DoNotEmulate)
188 bool doEmulation = false;
189 if (state->bgMode == Qt::OpaqueMode)
192 const QGradient *bg = state->brush.gradient();
193 if (bg && bg->coordinateMode() > QGradient::LogicalMode)
196 const QGradient *pg = qpen_brush(state->pen).gradient();
197 if (pg && pg->coordinateMode() > QGradient::LogicalMode)
201 if (extended != emulationEngine) {
202 if (!emulationEngine)
203 emulationEngine = new QEmulationPaintEngine(extended);
204 extended = emulationEngine;
205 extended->setState(state);
207 } else if (emulationEngine == extended) {
208 extended = emulationEngine->real_engine;
213 QPainterPrivate::~QPainterPrivate()
215 delete emulationEngine;
216 for (int i=0; i<states.size(); ++i)
224 QTransform QPainterPrivate::viewTransform() const
227 qreal scaleW = qreal(state->vw)/qreal(state->ww);
228 qreal scaleH = qreal(state->vh)/qreal(state->wh);
229 return QTransform(scaleW, 0, 0, scaleH,
230 state->vx - state->wx*scaleW, state->vy - state->wy*scaleH);
238 Returns true if using a shared painter; otherwise false.
240 bool QPainterPrivate::attachPainterPrivate(QPainter *q, QPaintDevice *pdev)
245 if (pdev->devType() != QInternal::Widget)
248 QWidget *widget = static_cast<QWidget *>(pdev);
251 // Someone either called QPainter::setRedirected in the widget's paint event
252 // right before this painter was created (or begin was called) or
253 // sent a paint event directly to the widget.
254 if (!widget->d_func()->redirectDev)
257 QPainter *sp = widget->d_func()->sharedPainter();
258 if (!sp || !sp->isActive())
261 if (sp->paintEngine()->paintDevice() != widget->d_func()->redirectDev)
264 // Check if we're attempting to paint outside a paint event.
265 if (!sp->d_ptr->engine->hasFeature(QPaintEngine::PaintOutsidePaintEvent)
266 && !widget->testAttribute(Qt::WA_PaintOutsidePaintEvent)
267 && !widget->testAttribute(Qt::WA_WState_InPaintEvent)) {
269 qWarning("QPainter::begin: Widget painting can only begin as a result of a paintEvent");
273 // Save the current state of the shared painter and assign
274 // the current d_ptr to the shared painter's d_ptr.
276 if (!sp->d_ptr->d_ptrs) {
277 // Allocate space for 4 d-pointers (enough for up to 4 sub-sequent
278 // redirections within the same paintEvent(), which should be enough
279 // in 99% of all cases). E.g: A renders B which renders C which renders D.
280 sp->d_ptr->d_ptrs_size = 4;
281 sp->d_ptr->d_ptrs = (QPainterPrivate **)malloc(4 * sizeof(QPainterPrivate *));
282 Q_CHECK_PTR(sp->d_ptr->d_ptrs);
283 } else if (sp->d_ptr->refcount - 1 == sp->d_ptr->d_ptrs_size) {
284 // However, to support corner cases we grow the array dynamically if needed.
285 sp->d_ptr->d_ptrs_size <<= 1;
286 const int newSize = sp->d_ptr->d_ptrs_size * sizeof(QPainterPrivate *);
287 sp->d_ptr->d_ptrs = q_check_ptr((QPainterPrivate **)realloc(sp->d_ptr->d_ptrs, newSize));
289 sp->d_ptr->d_ptrs[++sp->d_ptr->refcount - 2] = q->d_ptr.data();
291 q->d_ptr.reset(sp->d_ptr.data());
293 Q_ASSERT(q->d_ptr->state);
295 // Now initialize the painter with correct widget properties.
298 widget->d_func()->redirected(&offset);
299 offset += q->d_ptr->engine->coordinateOffset();
301 // Update system rect.
302 q->d_ptr->state->ww = q->d_ptr->state->vw = widget->width();
303 q->d_ptr->state->wh = q->d_ptr->state->vh = widget->height();
306 if (q->d_ptr->state->WxF) {
307 q->d_ptr->state->redirectionMatrix = q->d_ptr->state->matrix;
308 q->d_ptr->state->redirectionMatrix.translate(-offset.x(), -offset.y());
309 q->d_ptr->state->worldMatrix = QTransform();
310 q->d_ptr->state->WxF = false;
312 q->d_ptr->state->redirectionMatrix = QTransform::fromTranslate(-offset.x(), -offset.y());
314 q->d_ptr->updateMatrix();
316 QPaintEnginePrivate *enginePrivate = q->d_ptr->engine->d_func();
317 if (enginePrivate->currentClipWidget == widget) {
318 enginePrivate->systemStateChanged();
322 // Update system transform and clip.
323 enginePrivate->currentClipWidget = widget;
324 enginePrivate->setSystemTransform(q->d_ptr->state->matrix);
328 void QPainterPrivate::detachPainterPrivate(QPainter *q)
330 Q_ASSERT(refcount > 1);
333 QPainterPrivate *original = d_ptrs[--refcount - 1];
335 inDestructor = false;
337 original->inDestructor = true;
338 } else if (!original) {
339 original = new QPainterPrivate(q);
342 d_ptrs[refcount - 1] = 0;
345 q->d_ptr.reset(original);
347 if (emulationEngine) {
348 extended = emulationEngine->real_engine;
349 delete emulationEngine;
355 void QPainterPrivate::draw_helper(const QPainterPath &originalPath, DrawOperation op)
358 if (qt_show_painter_debug_output) {
359 printf("QPainter::drawHelper\n");
363 if (originalPath.isEmpty())
366 QPaintEngine::PaintEngineFeatures gradientStretch =
367 QPaintEngine::PaintEngineFeatures(QGradient_StretchToDevice
368 | QPaintEngine::ObjectBoundingModeGradients);
370 const bool mustEmulateObjectBoundingModeGradients = extended
371 || ((state->emulationSpecifier & QPaintEngine::ObjectBoundingModeGradients)
372 && !engine->hasFeature(QPaintEngine::PatternTransform));
374 if (!(state->emulationSpecifier & ~gradientStretch)
375 && !mustEmulateObjectBoundingModeGradients) {
376 drawStretchedGradient(originalPath, op);
378 } else if (state->emulationSpecifier & QPaintEngine_OpaqueBackground) {
379 drawOpaqueBackground(originalPath, op);
385 qreal strokeOffsetX = 0, strokeOffsetY = 0;
387 QPainterPath path = originalPath * state->matrix;
388 QRectF pathBounds = path.boundingRect();
390 bool doStroke = (op & StrokeDraw) && (state->pen.style() != Qt::NoPen);
392 qreal penWidth = state->pen.widthF();
397 // In case of complex xform
398 if (state->matrix.type() > QTransform::TxScale) {
399 QPainterPathStroker stroker;
400 stroker.setWidth(penWidth);
401 stroker.setJoinStyle(state->pen.joinStyle());
402 stroker.setCapStyle(state->pen.capStyle());
403 QPainterPath stroke = stroker.createStroke(originalPath);
404 strokeBounds = (stroke * state->matrix).boundingRect();
406 strokeOffsetX = qAbs(penWidth * state->matrix.m11() / 2.0);
407 strokeOffsetY = qAbs(penWidth * state->matrix.m22() / 2.0);
413 if (!strokeBounds.isEmpty()) {
414 absPathRect = strokeBounds.intersected(QRectF(0, 0, device->width(), device->height())).toAlignedRect();
416 absPathRect = pathBounds.adjusted(-strokeOffsetX, -strokeOffsetY, strokeOffsetX, strokeOffsetY)
417 .intersected(QRectF(0, 0, device->width(), device->height())).toAlignedRect();
420 if (q->hasClipping()) {
421 bool hasPerspectiveTransform = false;
422 for (int i = 0; i < state->clipInfo.size(); ++i) {
423 const QPainterClipInfo &info = state->clipInfo.at(i);
424 if (info.matrix.type() == QTransform::TxProject) {
425 hasPerspectiveTransform = true;
429 // avoid mapping QRegions with perspective transforms
430 if (!hasPerspectiveTransform) {
431 // The trick with txinv and invMatrix is done in order to
432 // avoid transforming the clip to logical coordinates, and
433 // then back to device coordinates. This is a problem with
434 // QRegion/QRect based clips, since they use integer
435 // coordinates and converting to/from logical coordinates will
437 bool old_txinv = txinv;
438 QTransform old_invMatrix = invMatrix;
440 invMatrix = QTransform();
441 QPainterPath clipPath = q->clipPath();
442 QRectF r = clipPath.boundingRect().intersected(absPathRect);
443 absPathRect = r.toAlignedRect();
445 invMatrix = old_invMatrix;
449 // qDebug("\nQPainterPrivate::draw_helper(), x=%d, y=%d, w=%d, h=%d",
450 // devMinX, devMinY, device->width(), device->height());
451 // qDebug() << " - matrix" << state->matrix;
452 // qDebug() << " - originalPath.bounds" << originalPath.boundingRect();
453 // qDebug() << " - path.bounds" << path.boundingRect();
455 if (absPathRect.width() <= 0 || absPathRect.height() <= 0)
458 QImage image(absPathRect.width(), absPathRect.height(), QImage::Format_ARGB32_Premultiplied);
463 p.d_ptr->helper_device = helper_device;
465 p.setOpacity(state->opacity);
466 p.translate(-absPathRect.x(), -absPathRect.y());
467 p.setTransform(state->matrix, true);
468 p.setPen(doStroke ? state->pen : QPen(Qt::NoPen));
469 p.setBrush((op & FillDraw) ? state->brush : QBrush(Qt::NoBrush));
470 p.setBackground(state->bgBrush);
471 p.setBackgroundMode(state->bgMode);
472 p.setBrushOrigin(state->brushOrigin);
474 p.setRenderHint(QPainter::Antialiasing, state->renderHints & QPainter::Antialiasing);
475 p.setRenderHint(QPainter::SmoothPixmapTransform,
476 state->renderHints & QPainter::SmoothPixmapTransform);
478 p.drawPath(originalPath);
481 static bool do_fallback_overlay = qgetenv("QT_PAINT_FALLBACK_OVERLAY").size() > 0;
482 if (do_fallback_overlay) {
483 QImage block(8, 8, QImage::Format_ARGB32_Premultiplied);
485 pt.fillRect(0, 0, 8, 8, QColor(196, 0, 196));
486 pt.drawLine(0, 0, 8, 8);
489 p.setCompositionMode(QPainter::CompositionMode_SourceAtop);
491 p.fillRect(0, 0, image.width(), image.height(), QBrush(block));
498 state->matrix = QTransform();
500 extended->transformChanged();
502 state->dirtyFlags |= QPaintEngine::DirtyTransform;
505 engine->drawImage(absPathRect,
507 QRectF(0, 0, absPathRect.width(), absPathRect.height()),
508 Qt::OrderedDither | Qt::OrderedAlphaDither);
512 void QPainterPrivate::drawOpaqueBackground(const QPainterPath &path, DrawOperation op)
516 q->setBackgroundMode(Qt::TransparentMode);
518 if (op & FillDraw && state->brush.style() != Qt::NoBrush) {
519 q->fillPath(path, state->bgBrush.color());
520 q->fillPath(path, state->brush);
523 if (op & StrokeDraw && state->pen.style() != Qt::NoPen) {
524 q->strokePath(path, QPen(state->bgBrush.color(), state->pen.width()));
525 q->strokePath(path, state->pen);
528 q->setBackgroundMode(Qt::OpaqueMode);
531 static inline QBrush stretchGradientToUserSpace(const QBrush &brush, const QRectF &boundingRect)
533 Q_ASSERT(brush.style() >= Qt::LinearGradientPattern
534 && brush.style() <= Qt::ConicalGradientPattern);
536 QTransform gradientToUser(boundingRect.width(), 0, 0, boundingRect.height(),
537 boundingRect.x(), boundingRect.y());
539 QGradient g = *brush.gradient();
540 g.setCoordinateMode(QGradient::LogicalMode);
543 b.setTransform(gradientToUser * b.transform());
547 void QPainterPrivate::drawStretchedGradient(const QPainterPath &path, DrawOperation op)
551 const qreal sw = helper_device->width();
552 const qreal sh = helper_device->height();
554 bool changedPen = false;
555 bool changedBrush = false;
556 bool needsFill = false;
558 const QPen pen = state->pen;
559 const QBrush brush = state->brush;
561 const QGradient::CoordinateMode penMode = coordinateMode(pen.brush());
562 const QGradient::CoordinateMode brushMode = coordinateMode(brush);
566 // Draw the xformed fill if the brush is a stretch gradient.
567 if ((op & FillDraw) && brush.style() != Qt::NoBrush) {
568 if (brushMode == QGradient::StretchToDeviceMode) {
569 q->setPen(Qt::NoPen);
570 changedPen = pen.style() != Qt::NoPen;
574 const qreal isw = 1.0 / sw;
575 const qreal ish = 1.0 / sh;
576 QTransform inv(isw, 0, 0, ish, 0, 0);
577 engine->drawPath(path * inv);
582 if (brushMode == QGradient::ObjectBoundingMode) {
583 Q_ASSERT(engine->hasFeature(QPaintEngine::PatternTransform));
584 boundingRect = path.boundingRect();
585 q->setBrush(stretchGradientToUserSpace(brush, boundingRect));
591 if ((op & StrokeDraw) && pen.style() != Qt::NoPen) {
592 // Draw the xformed outline if the pen is a stretch gradient.
593 if (penMode == QGradient::StretchToDeviceMode) {
594 q->setPen(Qt::NoPen);
599 engine->drawPath(path);
603 q->setBrush(pen.brush());
607 QPainterPathStroker stroker;
608 stroker.setDashPattern(pen.style());
609 stroker.setWidth(pen.widthF());
610 stroker.setJoinStyle(pen.joinStyle());
611 stroker.setCapStyle(pen.capStyle());
612 stroker.setMiterLimit(pen.miterLimit());
613 QPainterPath stroke = stroker.createStroke(path);
615 const qreal isw = 1.0 / sw;
616 const qreal ish = 1.0 / sh;
617 QTransform inv(isw, 0, 0, ish, 0, 0);
618 engine->drawPath(stroke * inv);
621 if (!needsFill && brush.style() != Qt::NoBrush) {
622 q->setBrush(Qt::NoBrush);
626 if (penMode == QGradient::ObjectBoundingMode) {
627 Q_ASSERT(engine->hasFeature(QPaintEngine::PatternTransform));
629 // avoid computing the bounding rect twice
630 if (!needsFill || brushMode != QGradient::ObjectBoundingMode)
631 boundingRect = path.boundingRect();
634 p.setBrush(stretchGradientToUserSpace(pen.brush(), boundingRect));
637 } else if (changedPen) {
643 engine->drawPath(path);
645 } else if (needsFill) {
646 if (pen.style() != Qt::NoPen) {
647 q->setPen(Qt::NoPen);
652 engine->drawPath(path);
662 void QPainterPrivate::updateMatrix()
664 state->matrix = state->WxF ? state->worldMatrix : QTransform();
666 state->matrix *= viewTransform();
668 txinv = false; // no inverted matrix
669 state->matrix *= state->redirectionMatrix;
671 extended->transformChanged();
673 state->dirtyFlags |= QPaintEngine::DirtyTransform;
675 // printf("VxF=%d, WxF=%d\n", state->VxF, state->WxF);
676 // qDebug() << " --- using matrix" << state->matrix << redirection_offset;
680 void QPainterPrivate::updateInvMatrix()
682 Q_ASSERT(txinv == false);
683 txinv = true; // creating inverted matrix
684 invMatrix = state->matrix.inverted();
687 extern bool qt_isExtendedRadialGradient(const QBrush &brush);
689 void QPainterPrivate::updateEmulationSpecifier(QPainterState *s)
692 bool linearGradient = false;
693 bool radialGradient = false;
694 bool extendedRadialGradient = false;
695 bool conicalGradient = false;
696 bool patternBrush = false;
698 bool complexXform = false;
702 // Pen and brush properties (we have to check both if one changes because the
703 // one that's unchanged can still be in a state which requires emulation)
704 if (s->state() & (QPaintEngine::DirtyPen | QPaintEngine::DirtyBrush | QPaintEngine::DirtyHints)) {
705 // Check Brush stroke emulation
706 if (!s->pen.isSolid() && !engine->hasFeature(QPaintEngine::BrushStroke))
707 s->emulationSpecifier |= QPaintEngine::BrushStroke;
709 s->emulationSpecifier &= ~QPaintEngine::BrushStroke;
713 QBrush penBrush = (qpen_style(s->pen) == Qt::NoPen) ? QBrush(Qt::NoBrush) : qpen_brush(s->pen);
714 Qt::BrushStyle brushStyle = qbrush_style(s->brush);
715 Qt::BrushStyle penBrushStyle = qbrush_style(penBrush);
716 alpha = (penBrushStyle != Qt::NoBrush
717 && (penBrushStyle < Qt::LinearGradientPattern && penBrush.color().alpha() != 255)
718 && !penBrush.isOpaque())
719 || (brushStyle != Qt::NoBrush
720 && (brushStyle < Qt::LinearGradientPattern && s->brush.color().alpha() != 255)
721 && !s->brush.isOpaque());
722 linearGradient = ((penBrushStyle == Qt::LinearGradientPattern) ||
723 (brushStyle == Qt::LinearGradientPattern));
724 radialGradient = ((penBrushStyle == Qt::RadialGradientPattern) ||
725 (brushStyle == Qt::RadialGradientPattern));
726 extendedRadialGradient = radialGradient && (qt_isExtendedRadialGradient(penBrush) || qt_isExtendedRadialGradient(s->brush));
727 conicalGradient = ((penBrushStyle == Qt::ConicalGradientPattern) ||
728 (brushStyle == Qt::ConicalGradientPattern));
729 patternBrush = (((penBrushStyle > Qt::SolidPattern
730 && penBrushStyle < Qt::LinearGradientPattern)
731 || penBrushStyle == Qt::TexturePattern) ||
732 ((brushStyle > Qt::SolidPattern
733 && brushStyle < Qt::LinearGradientPattern)
734 || brushStyle == Qt::TexturePattern));
736 bool penTextureAlpha = false;
737 if (penBrush.style() == Qt::TexturePattern)
738 penTextureAlpha = qHasPixmapTexture(penBrush)
739 ? (penBrush.texture().depth() > 1) && penBrush.texture().hasAlpha()
740 : penBrush.textureImage().hasAlphaChannel();
741 bool brushTextureAlpha = false;
742 if (s->brush.style() == Qt::TexturePattern) {
743 brushTextureAlpha = qHasPixmapTexture(s->brush)
744 ? (s->brush.texture().depth() > 1) && s->brush.texture().hasAlpha()
745 : s->brush.textureImage().hasAlphaChannel();
747 if (((penBrush.style() == Qt::TexturePattern && penTextureAlpha)
748 || (s->brush.style() == Qt::TexturePattern && brushTextureAlpha))
749 && !engine->hasFeature(QPaintEngine::MaskedBrush))
750 s->emulationSpecifier |= QPaintEngine::MaskedBrush;
752 s->emulationSpecifier &= ~QPaintEngine::MaskedBrush;
755 if (s->state() & (QPaintEngine::DirtyHints
756 | QPaintEngine::DirtyOpacity
757 | QPaintEngine::DirtyBackgroundMode)) {
765 qDebug("QPainterPrivate::updateEmulationSpecifier, state=%p\n"
767 " - linearGradient: %d\n"
768 " - radialGradient: %d\n"
769 " - conicalGradient: %d\n"
770 " - patternBrush: %d\n"
779 uint(s->renderHints),
784 if (s->state() & QPaintEngine::DirtyTransform) {
785 xform = !s->matrix.isIdentity();
786 complexXform = !s->matrix.isAffine();
787 } else if (s->matrix.type() >= QTransform::TxTranslate) {
789 complexXform = !s->matrix.isAffine();
792 const bool brushXform = (!s->brush.transform().type() == QTransform::TxNone);
793 const bool penXform = (!s->pen.brush().transform().type() == QTransform::TxNone);
795 const bool patternXform = patternBrush && (xform || brushXform || penXform);
797 // Check alphablending
798 if (alpha && !engine->hasFeature(QPaintEngine::AlphaBlend))
799 s->emulationSpecifier |= QPaintEngine::AlphaBlend;
801 s->emulationSpecifier &= ~QPaintEngine::AlphaBlend;
803 // Linear gradient emulation
804 if (linearGradient && !engine->hasFeature(QPaintEngine::LinearGradientFill))
805 s->emulationSpecifier |= QPaintEngine::LinearGradientFill;
807 s->emulationSpecifier &= ~QPaintEngine::LinearGradientFill;
809 // Radial gradient emulation
810 if (extendedRadialGradient || (radialGradient && !engine->hasFeature(QPaintEngine::RadialGradientFill)))
811 s->emulationSpecifier |= QPaintEngine::RadialGradientFill;
813 s->emulationSpecifier &= ~QPaintEngine::RadialGradientFill;
815 // Conical gradient emulation
816 if (conicalGradient && !engine->hasFeature(QPaintEngine::ConicalGradientFill))
817 s->emulationSpecifier |= QPaintEngine::ConicalGradientFill;
819 s->emulationSpecifier &= ~QPaintEngine::ConicalGradientFill;
822 if (patternBrush && !engine->hasFeature(QPaintEngine::PatternBrush))
823 s->emulationSpecifier |= QPaintEngine::PatternBrush;
825 s->emulationSpecifier &= ~QPaintEngine::PatternBrush;
828 if (patternXform && !engine->hasFeature(QPaintEngine::PatternTransform))
829 s->emulationSpecifier |= QPaintEngine::PatternTransform;
831 s->emulationSpecifier &= ~QPaintEngine::PatternTransform;
834 if (xform && !engine->hasFeature(QPaintEngine::PrimitiveTransform))
835 s->emulationSpecifier |= QPaintEngine::PrimitiveTransform;
837 s->emulationSpecifier &= ~QPaintEngine::PrimitiveTransform;
839 // Perspective XForms
840 if (complexXform && !engine->hasFeature(QPaintEngine::PerspectiveTransform))
841 s->emulationSpecifier |= QPaintEngine::PerspectiveTransform;
843 s->emulationSpecifier &= ~QPaintEngine::PerspectiveTransform;
846 if (state->opacity != 1 && !engine->hasFeature(QPaintEngine::ConstantOpacity))
847 s->emulationSpecifier |= QPaintEngine::ConstantOpacity;
849 s->emulationSpecifier &= ~QPaintEngine::ConstantOpacity;
851 bool gradientStretch = false;
852 bool objectBoundingMode = false;
853 if (linearGradient || conicalGradient || radialGradient) {
854 QGradient::CoordinateMode brushMode = coordinateMode(s->brush);
855 QGradient::CoordinateMode penMode = coordinateMode(s->pen.brush());
857 gradientStretch |= (brushMode == QGradient::StretchToDeviceMode);
858 gradientStretch |= (penMode == QGradient::StretchToDeviceMode);
860 objectBoundingMode |= (brushMode == QGradient::ObjectBoundingMode);
861 objectBoundingMode |= (penMode == QGradient::ObjectBoundingMode);
864 s->emulationSpecifier |= QGradient_StretchToDevice;
866 s->emulationSpecifier &= ~QGradient_StretchToDevice;
868 if (objectBoundingMode && !engine->hasFeature(QPaintEngine::ObjectBoundingModeGradients))
869 s->emulationSpecifier |= QPaintEngine::ObjectBoundingModeGradients;
871 s->emulationSpecifier &= ~QPaintEngine::ObjectBoundingModeGradients;
873 // Opaque backgrounds...
874 if (s->bgMode == Qt::OpaqueMode &&
875 (is_pen_transparent(s->pen) || is_brush_transparent(s->brush)))
876 s->emulationSpecifier |= QPaintEngine_OpaqueBackground;
878 s->emulationSpecifier &= ~QPaintEngine_OpaqueBackground;
881 //won't be correct either way because the device can already have
882 // something rendered to it in which case subsequent emulation
883 // on a fully transparent qimage and then blitting the results
884 // won't produce correct results
886 if (state->composition_mode > QPainter::CompositionMode_Xor &&
887 !engine->hasFeature(QPaintEngine::BlendModes))
888 s->emulationSpecifier |= QPaintEngine::BlendModes;
890 s->emulationSpecifier &= ~QPaintEngine::BlendModes;
894 void QPainterPrivate::updateStateImpl(QPainterState *newState)
896 // ### we might have to call QPainter::begin() here...
897 if (!engine->state) {
898 engine->state = newState;
899 engine->setDirty(QPaintEngine::AllDirty);
902 if (engine->state->painter() != newState->painter)
903 // ### this could break with clip regions vs paths.
904 engine->setDirty(QPaintEngine::AllDirty);
906 // Upon restore, revert all changes since last save
907 else if (engine->state != newState)
908 newState->dirtyFlags |= QPaintEngine::DirtyFlags(static_cast<QPainterState *>(engine->state)->changeFlags);
910 // We need to store all changes made so that restore can deal with them
912 newState->changeFlags |= newState->dirtyFlags;
914 updateEmulationSpecifier(newState);
916 // Unset potential dirty background mode
917 newState->dirtyFlags &= ~(QPaintEngine::DirtyBackgroundMode
918 | QPaintEngine::DirtyBackground);
920 engine->state = newState;
921 engine->updateState(*newState);
922 engine->clearDirty(QPaintEngine::AllDirty);
926 void QPainterPrivate::updateState(QPainterState *newState)
930 engine->state = newState;
932 } else if (newState->state() || engine->state!=newState) {
933 bool setNonCosmeticPen = (newState->renderHints & QPainter::NonCosmeticDefaultPen)
934 && newState->pen.widthF() == 0;
935 if (setNonCosmeticPen) {
936 // Override the default pen's cosmetic state if the
937 // NonCosmeticDefaultPen render hint is used.
938 QPen oldPen = newState->pen;
939 newState->pen.setWidth(1);
940 newState->pen.setCosmetic(false);
941 newState->dirtyFlags |= QPaintEngine::DirtyPen;
943 updateStateImpl(newState);
945 // Restore the state pen back to its default to preserve visible
947 newState->pen = oldPen;
949 updateStateImpl(newState);
957 \brief The QPainter class performs low-level painting on widgets and
964 QPainter provides highly optimized functions to do most of the
965 drawing GUI programs require. It can draw everything from simple
966 lines to complex shapes like pies and chords. It can also draw
967 aligned text and pixmaps. Normally, it draws in a "natural"
968 coordinate system, but it can also do view and world
969 transformation. QPainter can operate on any object that inherits
970 the QPaintDevice class.
972 The common use of QPainter is inside a widget's paint event:
973 Construct and customize (e.g. set the pen or the brush) the
974 painter. Then draw. Remember to destroy the QPainter object after
975 drawing. For example:
977 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 0
979 The core functionality of QPainter is drawing, but the class also
980 provide several functions that allows you to customize QPainter's
981 settings and its rendering quality, and others that enable
982 clipping. In addition you can control how different shapes are
983 merged together by specifying the painter's composition mode.
985 The isActive() function indicates whether the painter is active. A
986 painter is activated by the begin() function and the constructor
987 that takes a QPaintDevice argument. The end() function, and the
988 destructor, deactivates it.
990 Together with the QPaintDevice and QPaintEngine classes, QPainter
991 form the basis for Qt's paint system. QPainter is the class used
992 to perform drawing operations. QPaintDevice represents a device
993 that can be painted on using a QPainter. QPaintEngine provides the
994 interface that the painter uses to draw onto different types of
995 devices. If the painter is active, device() returns the paint
996 device on which the painter paints, and paintEngine() returns the
997 paint engine that the painter is currently operating on. For more
998 information, see the \l {Paint System}.
1000 Sometimes it is desirable to make someone else paint on an unusual
1001 QPaintDevice. QPainter supports a static function to do this,
1004 \warning When the paintdevice is a widget, QPainter can only be
1005 used inside a paintEvent() function or in a function called by
1006 paintEvent(); that is unless the Qt::WA_PaintOutsidePaintEvent
1007 widget attribute is set. On Mac OS X and Windows, you can only
1008 paint in a paintEvent() function regardless of this attribute's
1015 There are several settings that you can customize to make QPainter
1016 draw according to your preferences:
1020 \o font() is the font used for drawing text. If the painter
1021 isActive(), you can retrieve information about the currently set
1022 font, and its metrics, using the fontInfo() and fontMetrics()
1023 functions respectively.
1025 \o brush() defines the color or pattern that is used for filling
1028 \o pen() defines the color or stipple that is used for drawing
1029 lines or boundaries.
1031 \o backgroundMode() defines whether there is a background() or
1032 not, i.e it is either Qt::OpaqueMode or Qt::TransparentMode.
1034 \o background() only applies when backgroundMode() is \l
1035 Qt::OpaqueMode and pen() is a stipple. In that case, it
1036 describes the color of the background pixels in the stipple.
1038 \o brushOrigin() defines the origin of the tiled brushes, normally
1039 the origin of widget's background.
1041 \o viewport(), window(), worldTransform() make up the painter's coordinate
1042 transformation system. For more information, see the \l
1043 {Coordinate Transformations} section and the \l {Coordinate
1044 System} documentation.
1046 \o hasClipping() tells whether the painter clips at all. (The paint
1047 device clips, too.) If the painter clips, it clips to clipRegion().
1049 \o layoutDirection() defines the layout direction used by the
1050 painter when drawing text.
1052 \o worldMatrixEnabled() tells whether world transformation is enabled.
1054 \o viewTransformEnabled() tells whether view transformation is
1059 Note that some of these settings mirror settings in some paint
1060 devices, e.g. QWidget::font(). The QPainter::begin() function (or
1061 equivalently the QPainter constructor) copies these attributes
1062 from the paint device.
1064 You can at any time save the QPainter's state by calling the
1065 save() function which saves all the available settings on an
1066 internal stack. The restore() function pops them back.
1070 QPainter provides functions to draw most primitives: drawPoint(),
1071 drawPoints(), drawLine(), drawRect(), drawRoundedRect(),
1072 drawEllipse(), drawArc(), drawPie(), drawChord(), drawPolyline(),
1073 drawPolygon(), drawConvexPolygon() and drawCubicBezier(). The two
1074 convenience functions, drawRects() and drawLines(), draw the given
1075 number of rectangles or lines in the given array of \l
1076 {QRect}{QRects} or \l {QLine}{QLines} using the current pen and
1079 The QPainter class also provides the fillRect() function which
1080 fills the given QRect, with the given QBrush, and the eraseRect()
1081 function that erases the area inside the given rectangle.
1083 All of these functions have both integer and floating point
1088 \o \inlineimage qpainter-basicdrawing.png
1090 \bold {Basic Drawing Example}
1092 The \l {painting/basicdrawing}{Basic Drawing} example shows how to
1093 display basic graphics primitives in a variety of styles using the
1098 If you need to draw a complex shape, especially if you need to do
1099 so repeatedly, consider creating a QPainterPath and drawing it
1105 \bold {Painter Paths example}
1107 The QPainterPath class provides a container for painting
1108 operations, enabling graphical shapes to be constructed and
1111 The \l {painting/painterpaths}{Painter Paths} example shows how
1112 painter paths can be used to build complex shapes for rendering.
1114 \o \inlineimage qpainter-painterpaths.png
1117 QPainter also provides the fillPath() function which fills the
1118 given QPainterPath with the given QBrush, and the strokePath()
1119 function that draws the outline of the given path (i.e. strokes
1122 See also the \l {demos/deform}{Vector Deformation} demo which
1123 shows how to use advanced vector techniques to draw text using a
1124 QPainterPath, the \l {demos/gradients}{Gradients} demo which shows
1125 the different types of gradients that are available in Qt, and the \l
1126 {demos/pathstroke}{Path Stroking} demo which shows Qt's built-in
1127 dash patterns and shows how custom patterns can be used to extend
1128 the range of available patterns.
1132 \o \l {demos/deform}{Vector Deformation}
1133 \o \l {demos/gradients}{Gradients}
1134 \o \l {demos/pathstroke}{Path Stroking}
1136 \o \inlineimage qpainter-vectordeformation.png
1137 \o \inlineimage qpainter-gradients.png
1138 \o \inlineimage qpainter-pathstroking.png
1142 There are functions to draw pixmaps/images, namely drawPixmap(),
1143 drawImage() and drawTiledPixmap(). Both drawPixmap() and drawImage()
1144 produce the same result, except that drawPixmap() is faster
1145 on-screen while drawImage() may be faster on a QPrinter or other
1148 Text drawing is done using drawText(). When you need
1149 fine-grained positioning, boundingRect() tells you where a given
1150 drawText() command will draw.
1152 There is a drawPicture() function that draws the contents of an
1153 entire QPicture. The drawPicture() function is the only function
1154 that disregards all the painter's settings as QPicture has its own
1157 \section1 Rendering Quality
1159 To get the optimal rendering result using QPainter, you should use
1160 the platform independent QImage as paint device; i.e. using QImage
1161 will ensure that the result has an identical pixel representation
1164 The QPainter class also provides a means of controlling the
1165 rendering quality through its RenderHint enum and the support for
1166 floating point precision: All the functions for drawing primitives
1167 has a floating point version. These are often used in combination
1168 with the \l {RenderHint}{QPainter::Antialiasing} render hint.
1172 \o \inlineimage qpainter-concentriccircles.png
1174 \bold {Concentric Circles Example}
1176 The \l {painting/concentriccircles}{Concentric Circles} example
1177 shows the improved rendering quality that can be obtained using
1178 floating point precision and anti-aliasing when drawing custom
1181 The application's main window displays several widgets which are
1182 drawn using the various combinations of precision and
1187 The RenderHint enum specifies flags to QPainter that may or may
1188 not be respected by any given engine. \l
1189 {RenderHint}{QPainter::Antialiasing} indicates that the engine
1190 should antialias edges of primitives if possible, \l
1191 {RenderHint}{QPainter::TextAntialiasing} indicates that the engine
1192 should antialias text if possible, and the \l
1193 {RenderHint}{QPainter::SmoothPixmapTransform} indicates that the
1194 engine should use a smooth pixmap transformation algorithm.
1195 \l {RenderHint}{HighQualityAntialiasing} is an OpenGL-specific rendering hint
1196 indicating that the engine should use fragment programs and offscreen
1197 rendering for antialiasing.
1199 The renderHints() function returns a flag that specifies the
1200 rendering hints that are set for this painter. Use the
1201 setRenderHint() function to set or clear the currently set
1204 \section1 Coordinate Transformations
1206 Normally, the QPainter operates on the device's own coordinate
1207 system (usually pixels), but QPainter has good support for
1208 coordinate transformations.
1212 \o nop \o rotate() \o scale() \o translate()
1214 \o \inlineimage qpainter-clock.png
1215 \o \inlineimage qpainter-rotation.png
1216 \o \inlineimage qpainter-scale.png
1217 \o \inlineimage qpainter-translation.png
1220 The most commonly used transformations are scaling, rotation,
1221 translation and shearing. Use the scale() function to scale the
1222 coordinate system by a given offset, the rotate() function to
1223 rotate it clockwise and translate() to translate it (i.e. adding a
1224 given offset to the points). You can also twist the coordinate
1225 system around the origin using the shear() function. See the \l
1226 {demos/affine}{Affine Transformations} demo for a visualization of
1227 a sheared coordinate system.
1229 See also the \l {painting/transformations}{Transformations}
1230 example which shows how transformations influence the way that
1231 QPainter renders graphics primitives. In particular it shows how
1232 the order of transformations affects the result.
1237 \bold {Affine Transformations Demo}
1239 The \l {demos/affine}{Affine Transformations} demo show Qt's
1240 ability to perform affine transformations on painting
1241 operations. The demo also allows the user to experiment with the
1242 transformation operations and see the results immediately.
1244 \o \inlineimage qpainter-affinetransformations.png
1247 All the tranformation operations operate on the transformation
1248 worldTransform(). A matrix transforms a point in the plane to another
1249 point. For more information about the transformation matrix, see
1250 the \l {Coordinate System} and QTransform documentation.
1252 The setWorldTransform() function can replace or add to the currently
1253 set worldTransform(). The resetTransform() function resets any
1254 transformations that were made using translate(), scale(),
1255 shear(), rotate(), setWorldTransform(), setViewport() and setWindow()
1256 functions. The deviceTransform() returns the matrix that transforms
1257 from logical coordinates to device coordinates of the platform
1258 dependent paint device. The latter function is only needed when
1259 using platform painting commands on the platform dependent handle,
1260 and the platform does not do transformations nativly.
1262 When drawing with QPainter, we specify points using logical
1263 coordinates which then are converted into the physical coordinates
1264 of the paint device. The mapping of the logical coordinates to the
1265 physical coordinates are handled by QPainter's combinedTransform(), a
1266 combination of viewport() and window() and worldTransform(). The
1267 viewport() represents the physical coordinates specifying an
1268 arbitrary rectangle, the window() describes the same rectangle in
1269 logical coordinates, and the worldTransform() is identical with the
1270 transformation matrix.
1272 See also \l {Coordinate System}
1276 QPainter can clip any drawing operation to a rectangle, a region,
1277 or a vector path. The current clip is available using the
1278 functions clipRegion() and clipPath(). Whether paths or regions are
1279 preferred (faster) depends on the underlying paintEngine(). For
1280 example, the QImage paint engine prefers paths while the X11 paint
1281 engine prefers regions. Setting a clip is done in the painters
1282 logical coordinates.
1284 After QPainter's clipping, the paint device may also clip. For
1285 example, most widgets clip away the pixels used by child widgets,
1286 and most printers clip away an area near the edges of the paper.
1287 This additional clipping is not reflected by the return value of
1288 clipRegion() or hasClipping().
1290 \section1 Composition Modes
1291 \target Composition Modes
1293 QPainter provides the CompositionMode enum which defines the
1294 Porter-Duff rules for digital image compositing; it describes a
1295 model for combining the pixels in one image, the source, with the
1296 pixels in another image, the destination.
1298 The two most common forms of composition are \l
1299 {QPainter::CompositionMode}{Source} and \l
1300 {QPainter::CompositionMode}{SourceOver}. \l
1301 {QPainter::CompositionMode}{Source} is used to draw opaque objects
1302 onto a paint device. In this mode, each pixel in the source
1303 replaces the corresponding pixel in the destination. In \l
1304 {QPainter::CompositionMode}{SourceOver} composition mode, the
1305 source object is transparent and is drawn on top of the
1308 Note that composition transformation operates pixelwise. For that
1309 reason, there is a difference between using the graphic primitive
1310 itself and its bounding rectangle: The bounding rect contains
1311 pixels with alpha == 0 (i.e the pixels surrounding the
1312 primitive). These pixels will overwrite the other image's pixels,
1313 affectively clearing those, while the primitive only overwrites
1318 \o \inlineimage qpainter-compositiondemo.png
1321 \bold {Composition Modes Demo}
1323 The \l {demos/composition}{Composition Modes} demo, available in
1324 Qt's demo directory, allows you to experiment with the various
1325 composition modes and see the results immediately.
1329 \section1 Limitations
1332 If you are using coordinates with Qt's raster-based paint engine, it is
1333 important to note that, while coordinates greater than +/- 2\sup 15 can
1334 be used, any painting performed with coordinates outside this range is not
1335 guaranteed to be shown; the drawing may be clipped. This is due to the
1336 use of \c{short int} in the implementation.
1338 The outlines generated by Qt's stroker are only an approximation when dealing
1339 with curved shapes. It is in most cases impossible to represent the outline of
1340 a bezier curve segment using another bezier curve segment, and so Qt approximates
1341 the curve outlines by using several smaller curves. For performance reasons there
1342 is a limit to how many curves Qt uses for these outlines, and thus when using
1343 large pen widths or scales the outline error increases. To generate outlines with
1344 smaller errors it is possible to use the QPainterPathStroker class, which has the
1345 setCurveThreshold member function which let's the user specify the error tolerance.
1346 Another workaround is to convert the paths to polygons first and then draw the
1349 \section1 Performance
1351 QPainter is a rich framework that allows developers to do a great
1352 variety of graphical operations, such as gradients, composition
1353 modes and vector graphics. And QPainter can do this across a
1354 variety of different hardware and software stacks. Naturally the
1355 underlying combination of hardware and software has some
1356 implications for performance, and ensuring that every single
1357 operation is fast in combination with all the various combinations
1358 of composition modes, brushes, clipping, transformation, etc, is
1359 close to an impossible task because of the number of
1360 permutations. As a compromise we have selected a subset of the
1361 QPainter API and backends, where performance is guaranteed to be as
1362 good as we can sensibly get it for the given combination of
1363 hardware and software.
1365 The backends we focus on as high-performance engines are:
1369 \o Raster - This backend implements all rendering in pure software
1370 and is always used to render into QImages. For optimal performance
1371 only use the format types QImage::Format_ARGB32_Premultiplied,
1372 QImage::Format_RGB32 or QImage::Format_RGB16. Any other format,
1373 including QImage::Format_ARGB32, has significantly worse
1374 performance. This engine is also used by default on Windows and on
1375 QWS. It can be used as default graphics system on any
1376 OS/hardware/software combination by passing \c {-graphicssystem
1377 raster} on the command line
1379 \o OpenGL 2.0 (ES) - This backend is the primary backend for
1380 hardware accelerated graphics. It can be run on desktop machines
1381 and embedded devices supporting the OpenGL 2.0 or OpenGL/ES 2.0
1382 specification. This includes most graphics chips produced in the
1383 last couple of years. The engine can be enabled by using QPainter
1384 onto a QGLWidget or by passing \c {-graphicssystem opengl} on the
1385 command line when the underlying system supports it.
1387 \o OpenVG - This backend implements the Khronos standard for 2D
1388 and Vector Graphics. It is primarily for embedded devices with
1389 hardware support for OpenVG. The engine can be enabled by
1390 passing \c {-graphicssystem openvg} on the command line when
1391 the underlying system supports it.
1395 These operations are:
1399 \o Simple transformations, meaning translation and scaling, pluss
1400 0, 90, 180, 270 degree rotations.
1402 \o \c drawPixmap() in combination with simple transformations and
1403 opacity with non-smooth transformation mode
1404 (\c QPainter::SmoothPixmapTransform not enabled as a render hint).
1406 \o Rectangle fills with solid color, two-color linear gradients
1407 and simple transforms.
1409 \o Rectangular clipping with simple transformations and intersect
1412 \o Composition Modes \c QPainter::CompositionMode_Source and
1413 QPainter::CompositionMode_SourceOver
1415 \o Rounded rectangle filling using solid color and two-color
1416 linear gradients fills.
1418 \o 3x3 patched pixmaps, via qDrawBorderPixmap.
1422 This list gives an indication of which features to safely use in
1423 an application where performance is critical. For certain setups,
1424 other operations may be fast too, but before making extensive use
1425 of them, it is recommended to benchmark and verify them on the
1426 system where the software will run in the end. There are also
1427 cases where expensive operations are ok to use, for instance when
1428 the result is cached in a QPixmap.
1430 \sa QPaintDevice, QPaintEngine, {QtSvg Module}, {Basic Drawing Example},
1431 {Drawing Utility Functions}
1435 \enum QPainter::RenderHint
1437 Renderhints are used to specify flags to QPainter that may or
1438 may not be respected by any given engine.
1440 \value Antialiasing Indicates that the engine should antialias
1441 edges of primitives if possible.
1443 \value TextAntialiasing Indicates that the engine should antialias
1444 text if possible. To forcibly disable antialiasing for text, do not
1445 use this hint. Instead, set QFont::NoAntialias on your font's style
1448 \value SmoothPixmapTransform Indicates that the engine should use
1449 a smooth pixmap transformation algorithm (such as bilinear) rather
1450 than nearest neighbor.
1452 \value HighQualityAntialiasing An OpenGL-specific rendering hint
1453 indicating that the engine should use fragment programs and offscreen
1454 rendering for antialiasing.
1456 \value NonCosmeticDefaultPen The engine should interpret pens with a width
1457 of 0 (which otherwise enables QPen::isCosmetic()) as being a non-cosmetic
1458 pen with a width of 1.
1460 \sa renderHints(), setRenderHint(), {QPainter#Rendering
1461 Quality}{Rendering Quality}, {Concentric Circles Example}
1466 Constructs a painter.
1471 QPainter::QPainter()
1472 : d_ptr(new QPainterPrivate(this))
1477 \fn QPainter::QPainter(QPaintDevice *device)
1479 Constructs a painter that begins painting the paint \a device
1482 This constructor is convenient for short-lived painters, e.g. in a
1483 QWidget::paintEvent() and should be used only once. The
1484 constructor calls begin() for you and the QPainter destructor
1485 automatically calls end().
1487 Here's an example using begin() and end():
1488 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 1
1490 The same example using this constructor:
1491 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 2
1493 Since the constructor cannot provide feedback when the initialization
1494 of the painter failed you should rather use begin() and end() to paint
1495 on external devices, e.g. printers.
1500 QPainter::QPainter(QPaintDevice *pd)
1504 if (!QPainterPrivate::attachPainterPrivate(this, pd)) {
1505 d_ptr.reset(new QPainterPrivate(this));
1512 Destroys the painter.
1514 QPainter::~QPainter()
1516 d_ptr->inDestructor = true;
1520 else if (d_ptr->refcount > 1)
1521 d_ptr->detachPainterPrivate(this);
1523 // don't throw anything in the destructor.
1526 // Make sure we haven't messed things up.
1527 Q_ASSERT(d_ptr->inDestructor);
1528 d_ptr->inDestructor = false;
1529 Q_ASSERT(d_ptr->refcount == 1);
1531 free(d_ptr->d_ptrs);
1536 Returns the paint device on which this painter is currently
1537 painting, or 0 if the painter is not active.
1542 QPaintDevice *QPainter::device() const
1544 Q_D(const QPainter);
1545 if (isActive() && d->engine->d_func()->currentClipWidget)
1546 return d->engine->d_func()->currentClipWidget;
1547 return d->original_device;
1551 Returns true if begin() has been called and end() has not yet been
1552 called; otherwise returns false.
1554 \sa begin(), QPaintDevice::paintingActive()
1557 bool QPainter::isActive() const
1559 Q_D(const QPainter);
1564 Initializes the painters pen, background and font to the same as
1565 the given \a widget. This function is called automatically when the
1566 painter is opened on a QWidget.
1568 \sa begin(), {QPainter#Settings}{Settings}
1570 void QPainter::initFrom(const QWidget *widget)
1572 Q_ASSERT_X(widget, "QPainter::initFrom(const QWidget *widget)", "Widget cannot be 0");
1575 qWarning("QPainter::initFrom: Painter not active, aborted");
1579 const QPalette &pal = widget->palette();
1580 d->state->pen = QPen(pal.brush(widget->foregroundRole()), 0);
1581 d->state->bgBrush = pal.brush(widget->backgroundRole());
1582 d->state->deviceFont = QFont(widget->font(), const_cast<QWidget*> (widget));
1583 d->state->font = d->state->deviceFont;
1585 d->extended->penChanged();
1586 } else if (d->engine) {
1587 d->engine->setDirty(QPaintEngine::DirtyPen);
1588 d->engine->setDirty(QPaintEngine::DirtyBrush);
1589 d->engine->setDirty(QPaintEngine::DirtyFont);
1595 Saves the current painter state (pushes the state onto a stack). A
1596 save() must be followed by a corresponding restore(); the end()
1597 function unwinds the stack.
1602 void QPainter::save()
1604 #ifdef QT_DEBUG_DRAW
1605 if (qt_show_painter_debug_output)
1606 printf("QPainter::save()\n");
1610 qWarning("QPainter::save: Painter not active");
1615 d->state = d->extended->createState(d->states.back());
1616 d->extended->setState(d->state);
1618 d->updateState(d->state);
1619 d->state = new QPainterState(d->states.back());
1620 d->engine->state = d->state;
1622 d->states.push_back(d->state);
1626 Restores the current painter state (pops a saved state off the
1632 void QPainter::restore()
1634 #ifdef QT_DEBUG_DRAW
1635 if (qt_show_painter_debug_output)
1636 printf("QPainter::restore()\n");
1639 if (d->states.size()<=1) {
1640 qWarning("QPainter::restore: Unbalanced save/restore");
1642 } else if (!d->engine) {
1643 qWarning("QPainter::restore: Painter not active");
1647 QPainterState *tmp = d->state;
1648 d->states.pop_back();
1649 d->state = d->states.back();
1653 d->checkEmulation();
1654 d->extended->setState(d->state);
1659 // trigger clip update if the clip path/region has changed since
1661 if (!d->state->clipInfo.isEmpty()
1662 && (tmp->changeFlags & (QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyClipPath))) {
1663 // reuse the tmp state to avoid any extra allocs...
1664 tmp->dirtyFlags = QPaintEngine::DirtyClipPath;
1665 tmp->clipOperation = Qt::NoClip;
1666 tmp->clipPath = QPainterPath();
1667 d->engine->updateState(*tmp);
1668 // replay the list of clip states,
1669 for (int i=0; i<d->state->clipInfo.size(); ++i) {
1670 const QPainterClipInfo &info = d->state->clipInfo.at(i);
1671 tmp->matrix = info.matrix;
1672 tmp->matrix *= d->state->redirectionMatrix;
1673 tmp->clipOperation = info.operation;
1674 if (info.clipType == QPainterClipInfo::RectClip) {
1675 tmp->dirtyFlags = QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyTransform;
1676 tmp->clipRegion = info.rect;
1677 } else if (info.clipType == QPainterClipInfo::RegionClip) {
1678 tmp->dirtyFlags = QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyTransform;
1679 tmp->clipRegion = info.region;
1680 } else { // clipType == QPainterClipInfo::PathClip
1681 tmp->dirtyFlags = QPaintEngine::DirtyClipPath | QPaintEngine::DirtyTransform;
1682 tmp->clipPath = info.path;
1684 d->engine->updateState(*tmp);
1688 //Since we've updated the clip region anyway, pretend that the clip path hasn't changed:
1689 d->state->dirtyFlags &= ~(QPaintEngine::DirtyClipPath | QPaintEngine::DirtyClipRegion);
1690 tmp->changeFlags &= ~(QPaintEngine::DirtyClipPath | QPaintEngine::DirtyClipRegion);
1691 tmp->changeFlags |= QPaintEngine::DirtyTransform;
1694 d->updateState(d->state);
1701 \fn bool QPainter::begin(QPaintDevice *device)
1703 Begins painting the paint \a device and returns true if
1704 successful; otherwise returns false.
1706 Notice that all painter settings (setPen(), setBrush() etc.) are reset
1707 to default values when begin() is called.
1709 The errors that can occur are serious problems, such as these:
1711 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 3
1713 Note that most of the time, you can use one of the constructors
1714 instead of begin(), and that end() is automatically done at
1717 \warning A paint device can only be painted by one painter at a
1720 \warning Painting on a QImage with the format
1721 QImage::Format_Indexed8 is not supported.
1723 \sa end(), QPainter()
1726 static inline void qt_cleanup_painter_state(QPainterPrivate *d)
1735 bool QPainter::begin(QPaintDevice *pd)
1739 if (pd->painters > 0) {
1740 qWarning("QPainter::begin: A paint device can only be painted by one painter at a time.");
1744 if (d_ptr->engine) {
1745 qWarning("QPainter::begin: Painter already active");
1749 if (QPainterPrivate::attachPainterPrivate(this, pd))
1754 d->helper_device = pd;
1755 d->original_device = pd;
1756 QPaintDevice *rpd = 0;
1758 QPoint redirectionOffset;
1759 // We know for sure that redirection is broken when the widget is inside
1760 // its paint event, so it's safe to use our hard-coded redirection. However,
1761 // there IS one particular case we still need to support, and that's
1762 // when people call QPainter::setRedirected in the widget's paint event right
1763 // before any painter is created (or QPainter::begin is called). In that
1764 // particular case our hard-coded redirection is restored and the redirection
1765 // is retrieved from QPainter::redirected (as before).
1766 if (pd->devType() == QInternal::Widget)
1767 rpd = static_cast<QWidget *>(pd)->d_func()->redirected(&redirectionOffset);
1770 rpd = redirected(pd, &redirectionOffset);
1775 #ifdef QT_DEBUG_DRAW
1776 if (qt_show_painter_debug_output)
1777 printf("QPainter::begin(), device=%p, type=%d\n", pd, pd->devType());
1780 if (pd->devType() == QInternal::Pixmap)
1781 static_cast<QPixmap *>(pd)->detach();
1782 else if (pd->devType() == QInternal::Image)
1783 static_cast<QImage *>(pd)->detach();
1785 d->engine = pd->paintEngine();
1788 qWarning("QPainter::begin: Paint device returned engine == 0, type: %d", pd->devType());
1794 d->extended = d->engine->isExtended() ? static_cast<QPaintEngineEx *>(d->engine) : 0;
1795 if (d->emulationEngine)
1796 d->emulationEngine->real_engine = d->extended;
1798 // Setup new state...
1799 Q_ASSERT(!d->state);
1800 d->state = d->extended ? d->extended->createState(0) : new QPainterState;
1801 d->state->painter = this;
1802 d->states.push_back(d->state);
1804 d->state->redirectionMatrix.translate(-redirectionOffset.x(), -redirectionOffset.y());
1805 d->state->brushOrigin = QPointF();
1807 // Slip a painter state into the engine before we do any other operations
1809 d->extended->setState(d->state);
1811 d->engine->state = d->state;
1813 switch (pd->devType()) {
1814 case QInternal::Widget:
1816 const QWidget *widget = static_cast<const QWidget *>(pd);
1819 const bool paintOutsidePaintEvent = widget->testAttribute(Qt::WA_PaintOutsidePaintEvent);
1820 const bool inPaintEvent = widget->testAttribute(Qt::WA_WState_InPaintEvent);
1821 if(!d->engine->hasFeature(QPaintEngine::PaintOutsidePaintEvent)
1822 && !paintOutsidePaintEvent && !inPaintEvent) {
1823 qWarning("QPainter::begin: Widget painting can only begin as a "
1824 "result of a paintEvent");
1825 qt_cleanup_painter_state(d);
1829 // Adjust offset for alien widgets painting outside the paint event.
1830 if (!inPaintEvent && paintOutsidePaintEvent && !widget->internalWinId()
1831 && widget->testAttribute(Qt::WA_WState_Created)) {
1832 const QPoint offset = widget->mapTo(widget->nativeParentWidget(), QPoint());
1833 d->state->redirectionMatrix.translate(offset.x(), offset.y());
1837 case QInternal::Pixmap:
1839 QPixmap *pm = static_cast<QPixmap *>(pd);
1842 qWarning("QPainter::begin: Cannot paint on a null pixmap");
1843 qt_cleanup_painter_state(d);
1847 if (pm->depth() == 1) {
1848 d->state->pen = QPen(Qt::color1);
1849 d->state->brush = QBrush(Qt::color0);
1853 case QInternal::Image:
1855 QImage *img = static_cast<QImage *>(pd);
1857 if (img->isNull()) {
1858 qWarning("QPainter::begin: Cannot paint on a null image");
1859 qt_cleanup_painter_state(d);
1861 } else if (img->format() == QImage::Format_Indexed8) {
1862 // Painting on indexed8 images is not supported.
1863 qWarning("QPainter::begin: Cannot paint on an image with the QImage::Format_Indexed8 format");
1864 qt_cleanup_painter_state(d);
1867 if (img->depth() == 1) {
1868 d->state->pen = QPen(Qt::color1);
1869 d->state->brush = QBrush(Qt::color0);
1876 if (d->state->ww == 0) // For compat with 3.x painter defaults
1877 d->state->ww = d->state->wh = d->state->vw = d->state->vh = 1024;
1879 d->engine->setPaintDevice(pd);
1881 bool begun = d->engine->begin(pd);
1883 qWarning("QPainter::begin(): Returned false");
1884 if (d->engine->isActive()) {
1887 qt_cleanup_painter_state(d);
1891 d->engine->setActive(begun);
1894 // Copy painter properties from original paint device,
1895 // required for QPixmap::grabWidget()
1896 if (d->original_device->devType() == QInternal::Widget) {
1897 QWidget *widget = static_cast<QWidget *>(d->original_device);
1900 d->state->layoutDirection = Qt::LayoutDirectionAuto;
1901 // make sure we have a font compatible with the paintdevice
1902 d->state->deviceFont = d->state->font = QFont(d->state->deviceFont, device());
1905 QRect systemRect = d->engine->systemRect();
1906 if (!systemRect.isEmpty()) {
1907 d->state->ww = d->state->vw = systemRect.width();
1908 d->state->wh = d->state->vh = systemRect.height();
1910 d->state->ww = d->state->vw = pd->metric(QPaintDevice::PdmWidth);
1911 d->state->wh = d->state->vh = pd->metric(QPaintDevice::PdmHeight);
1914 const QPoint coordinateOffset = d->engine->coordinateOffset();
1915 d->state->redirectionMatrix.translate(-coordinateOffset.x(), -coordinateOffset.y());
1917 Q_ASSERT(d->engine->isActive());
1919 if (!d->state->redirectionMatrix.isIdentity())
1922 Q_ASSERT(d->engine->isActive());
1923 d->state->renderHints = QPainter::TextAntialiasing;
1924 ++d->device->painters;
1926 d->state->emulationSpecifier = 0;
1932 Ends painting. Any resources used while painting are released. You
1933 don't normally need to call this since it is called by the
1936 Returns true if the painter is no longer active; otherwise returns false.
1938 \sa begin(), isActive()
1941 bool QPainter::end()
1943 #ifdef QT_DEBUG_DRAW
1944 if (qt_show_painter_debug_output)
1945 printf("QPainter::end()\n");
1950 qWarning("QPainter::end: Painter not active, aborted");
1951 qt_cleanup_painter_state(d);
1955 if (d->refcount > 1) {
1956 d->detachPainterPrivate(this);
1962 if (d->engine->isActive()) {
1963 ended = d->engine->end();
1966 --d->device->painters;
1967 if (d->device->painters == 0) {
1968 d->engine->setPaintDevice(0);
1969 d->engine->setActive(false);
1973 if (d->states.size() > 1) {
1974 qWarning("QPainter::end: Painter ended with %d saved states",
1978 if (d->engine->autoDestruct()) {
1982 if (d->emulationEngine) {
1983 delete d->emulationEngine;
1984 d->emulationEngine = 0;
1991 qt_cleanup_painter_state(d);
1998 Returns the paint engine that the painter is currently operating
1999 on if the painter is active; otherwise 0.
2003 QPaintEngine *QPainter::paintEngine() const
2005 Q_D(const QPainter);
2012 Flushes the painting pipeline and prepares for the user issuing commands
2013 directly to the underlying graphics context. Must be followed by a call to
2014 endNativePainting().
2016 Note that only the states the underlying paint engine changes will be reset
2017 to their respective default states. The states we reset may change from
2018 release to release. The following states are currently reset in the OpenGL
2022 \i blending is disabled
2023 \i the depth, stencil and scissor tests are disabled
2024 \i the active texture unit is reset to 0
2025 \i the depth mask, depth function and the clear depth are reset to their
2027 \i the stencil mask, stencil operation and stencil function are reset to
2028 their default values
2029 \i the current color is reset to solid white
2032 If, for example, the OpenGL polygon mode is changed by the user inside a
2033 beginNativePaint()/endNativePainting() block, it will not be reset to the
2034 default state by endNativePainting(). Here is an example that shows
2035 intermixing of painter commands and raw OpenGL commands:
2037 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 21
2039 \sa endNativePainting()
2041 void QPainter::beginNativePainting()
2045 qWarning("QPainter::beginNativePainting: Painter not active");
2050 d->extended->beginNativePainting();
2056 Restores the painter after manually issuing native painting commands. Lets
2057 the painter restore any native state that it relies on before calling any
2058 other painter commands.
2060 \sa beginNativePainting()
2062 void QPainter::endNativePainting()
2064 Q_D(const QPainter);
2066 qWarning("QPainter::beginNativePainting: Painter not active");
2071 d->extended->endNativePainting();
2073 d->engine->syncState();
2077 Returns the font metrics for the painter if the painter is
2078 active. Otherwise, the return value is undefined.
2080 \sa font(), isActive(), {QPainter#Settings}{Settings}
2083 QFontMetrics QPainter::fontMetrics() const
2085 Q_D(const QPainter);
2087 qWarning("QPainter::fontMetrics: Painter not active");
2088 return QFontMetrics(QFont());
2090 return QFontMetrics(d->state->font);
2095 Returns the font info for the painter if the painter is
2096 active. Otherwise, the return value is undefined.
2098 \sa font(), isActive(), {QPainter#Settings}{Settings}
2101 QFontInfo QPainter::fontInfo() const
2103 Q_D(const QPainter);
2105 qWarning("QPainter::fontInfo: Painter not active");
2106 return QFontInfo(QFont());
2108 return QFontInfo(d->state->font);
2114 Returns the opacity of the painter. The default value is
2118 qreal QPainter::opacity() const
2120 Q_D(const QPainter);
2122 qWarning("QPainter::opacity: Painter not active");
2125 return d->state->opacity;
2131 Sets the opacity of the painter to \a opacity. The value should
2132 be in the range 0.0 to 1.0, where 0.0 is fully transparent and
2133 1.0 is fully opaque.
2135 Opacity set on the painter will apply to all drawing operations
2139 void QPainter::setOpacity(qreal opacity)
2144 qWarning("QPainter::setOpacity: Painter not active");
2148 opacity = qMin(qreal(1), qMax(qreal(0), opacity));
2150 if (opacity == d->state->opacity)
2153 d->state->opacity = opacity;
2156 d->extended->opacityChanged();
2158 d->state->dirtyFlags |= QPaintEngine::DirtyOpacity;
2163 Returns the currently set brush origin.
2165 \sa setBrushOrigin(), {QPainter#Settings}{Settings}
2168 QPoint QPainter::brushOrigin() const
2170 Q_D(const QPainter);
2172 qWarning("QPainter::brushOrigin: Painter not active");
2175 return QPointF(d->state->brushOrigin).toPoint();
2179 \fn void QPainter::setBrushOrigin(const QPointF &position)
2181 Sets the brush origin to \a position.
2183 The brush origin specifies the (0, 0) coordinate of the painter's
2186 Note that while the brushOrigin() was necessary to adopt the
2187 parent's background for a widget in Qt 3, this is no longer the
2188 case since the Qt 4 painter doesn't paint the background unless
2189 you explicitly tell it to do so by setting the widget's \l
2190 {QWidget::autoFillBackground}{autoFillBackground} property to
2193 \sa brushOrigin(), {QPainter#Settings}{Settings}
2196 void QPainter::setBrushOrigin(const QPointF &p)
2199 #ifdef QT_DEBUG_DRAW
2200 if (qt_show_painter_debug_output)
2201 printf("QPainter::setBrushOrigin(), (%.2f,%.2f)\n", p.x(), p.y());
2205 qWarning("QPainter::setBrushOrigin: Painter not active");
2209 d->state->brushOrigin = p;
2212 d->extended->brushOriginChanged();
2216 d->state->dirtyFlags |= QPaintEngine::DirtyBrushOrigin;
2220 \fn void QPainter::setBrushOrigin(const QPoint &position)
2223 Sets the brush's origin to the given \a position.
2227 \fn void QPainter::setBrushOrigin(int x, int y)
2231 Sets the brush's origin to point (\a x, \a y).
2235 \enum QPainter::CompositionMode
2237 Defines the modes supported for digital image compositing.
2238 Composition modes are used to specify how the pixels in one image,
2239 the source, are merged with the pixel in another image, the
2242 Please note that the bitwise raster operation modes, denoted with
2243 a RasterOp prefix, are only natively supported in the X11 and
2244 raster paint engines. This means that the only way to utilize
2245 these modes on the Mac is via a QImage. The RasterOp denoted blend
2246 modes are \e not supported for pens and brushes with alpha
2247 components. Also, turning on the QPainter::Antialiasing render
2248 hint will effectively disable the RasterOp modes.
2251 \image qpainter-compositionmode1.png
2252 \image qpainter-compositionmode2.png
2254 The most common type is SourceOver (often referred to as just
2255 alpha blending) where the source pixel is blended on top of the
2256 destination pixel in such a way that the alpha component of the
2257 source defines the translucency of the pixel.
2259 When the paint device is a QImage, the image format must be set to
2260 \l {QImage::Format}{Format_ARGB32Premultiplied} or
2261 \l {QImage::Format}{Format_ARGB32} for the composition modes to have
2262 any effect. For performance the premultiplied version is the preferred
2265 When a composition mode is set it applies to all painting
2266 operator, pens, brushes, gradients and pixmap/image drawing.
2268 \value CompositionMode_SourceOver This is the default mode. The
2269 alpha of the source is used to blend the pixel on top of the
2272 \value CompositionMode_DestinationOver The alpha of the
2273 destination is used to blend it on top of the source pixels. This
2274 mode is the inverse of CompositionMode_SourceOver.
2276 \value CompositionMode_Clear The pixels in the destination are
2277 cleared (set to fully transparent) independent of the source.
2279 \value CompositionMode_Source The output is the source
2280 pixel. (This means a basic copy operation and is identical to
2281 SourceOver when the source pixel is opaque).
2283 \value CompositionMode_Destination The output is the destination
2284 pixel. This means that the blending has no effect. This mode is
2285 the inverse of CompositionMode_Source.
2287 \value CompositionMode_SourceIn The output is the source, where
2288 the alpha is reduced by that of the destination.
2290 \value CompositionMode_DestinationIn The output is the
2291 destination, where the alpha is reduced by that of the
2292 source. This mode is the inverse of CompositionMode_SourceIn.
2294 \value CompositionMode_SourceOut The output is the source, where
2295 the alpha is reduced by the inverse of destination.
2297 \value CompositionMode_DestinationOut The output is the
2298 destination, where the alpha is reduced by the inverse of the
2299 source. This mode is the inverse of CompositionMode_SourceOut.
2301 \value CompositionMode_SourceAtop The source pixel is blended on
2302 top of the destination, with the alpha of the source pixel reduced
2303 by the alpha of the destination pixel.
2305 \value CompositionMode_DestinationAtop The destination pixel is
2306 blended on top of the source, with the alpha of the destination
2307 pixel is reduced by the alpha of the destination pixel. This mode
2308 is the inverse of CompositionMode_SourceAtop.
2310 \value CompositionMode_Xor The source, whose alpha is reduced with
2311 the inverse of the destination alpha, is merged with the
2312 destination, whose alpha is reduced by the inverse of the source
2313 alpha. CompositionMode_Xor is not the same as the bitwise Xor.
2315 \value CompositionMode_Plus Both the alpha and color of the source
2316 and destination pixels are added together.
2318 \value CompositionMode_Multiply The output is the source color
2319 multiplied by the destination. Multiplying a color with white
2320 leaves the color unchanged, while multiplying a color
2321 with black produces black.
2323 \value CompositionMode_Screen The source and destination colors
2324 are inverted and then multiplied. Screening a color with white
2325 produces white, whereas screening a color with black leaves the
2328 \value CompositionMode_Overlay Multiplies or screens the colors
2329 depending on the destination color. The destination color is mixed
2330 with the source color to reflect the lightness or darkness of the
2333 \value CompositionMode_Darken The darker of the source and
2334 destination colors is selected.
2336 \value CompositionMode_Lighten The lighter of the source and
2337 destination colors is selected.
2339 \value CompositionMode_ColorDodge The destination color is
2340 brightened to reflect the source color. A black source color
2341 leaves the destination color unchanged.
2343 \value CompositionMode_ColorBurn The destination color is darkened
2344 to reflect the source color. A white source color leaves the
2345 destination color unchanged.
2347 \value CompositionMode_HardLight Multiplies or screens the colors
2348 depending on the source color. A light source color will lighten
2349 the destination color, whereas a dark source color will darken the
2352 \value CompositionMode_SoftLight Darkens or lightens the colors
2353 depending on the source color. Similar to
2354 CompositionMode_HardLight.
2356 \value CompositionMode_Difference Subtracts the darker of the
2357 colors from the lighter. Painting with white inverts the
2358 destination color, whereas painting with black leaves the
2359 destination color unchanged.
2361 \value CompositionMode_Exclusion Similar to
2362 CompositionMode_Difference, but with a lower contrast. Painting
2363 with white inverts the destination color, whereas painting with
2364 black leaves the destination color unchanged.
2366 \value RasterOp_SourceOrDestination Does a bitwise OR operation on
2367 the source and destination pixels (src OR dst).
2369 \value RasterOp_SourceAndDestination Does a bitwise AND operation
2370 on the source and destination pixels (src AND dst).
2372 \value RasterOp_SourceXorDestination Does a bitwise XOR operation
2373 on the source and destination pixels (src XOR dst).
2375 \value RasterOp_NotSourceAndNotDestination Does a bitwise NOR
2376 operation on the source and destination pixels ((NOT src) AND (NOT
2379 \value RasterOp_NotSourceOrNotDestination Does a bitwise NAND
2380 operation on the source and destination pixels ((NOT src) OR (NOT
2383 \value RasterOp_NotSourceXorDestination Does a bitwise operation
2384 where the source pixels are inverted and then XOR'ed with the
2385 destination ((NOT src) XOR dst).
2387 \value RasterOp_NotSource Does a bitwise operation where the
2388 source pixels are inverted (NOT src).
2390 \value RasterOp_NotSourceAndDestination Does a bitwise operation
2391 where the source is inverted and then AND'ed with the destination
2392 ((NOT src) AND dst).
2394 \value RasterOp_SourceAndNotDestination Does a bitwise operation
2395 where the source is AND'ed with the inverted destination pixels
2396 (src AND (NOT dst)).
2398 \sa compositionMode(), setCompositionMode(), {QPainter#Composition
2399 Modes}{Composition Modes}, {Image Composition Example}
2403 Sets the composition mode to the given \a mode.
2405 \warning Only a QPainter operating on a QImage fully supports all
2406 composition modes. The RasterOp modes are supported for X11 as
2407 described in compositionMode().
2409 \sa compositionMode()
2411 void QPainter::setCompositionMode(CompositionMode mode)
2415 qWarning("QPainter::setCompositionMode: Painter not active");
2418 if (d->state->composition_mode == mode)
2421 d->state->composition_mode = mode;
2422 d->extended->compositionModeChanged();
2426 if (mode >= QPainter::RasterOp_SourceOrDestination) {
2427 if (!d->engine->hasFeature(QPaintEngine::RasterOpModes)) {
2428 qWarning("QPainter::setCompositionMode: "
2429 "Raster operation modes not supported on device");
2432 } else if (mode >= QPainter::CompositionMode_Plus) {
2433 if (!d->engine->hasFeature(QPaintEngine::BlendModes)) {
2434 qWarning("QPainter::setCompositionMode: "
2435 "Blend modes not supported on device");
2438 } else if (!d->engine->hasFeature(QPaintEngine::PorterDuff)) {
2439 if (mode != CompositionMode_Source && mode != CompositionMode_SourceOver) {
2440 qWarning("QPainter::setCompositionMode: "
2441 "PorterDuff modes not supported on device");
2446 d->state->composition_mode = mode;
2447 d->state->dirtyFlags |= QPaintEngine::DirtyCompositionMode;
2451 Returns the current composition mode.
2453 \sa CompositionMode, setCompositionMode()
2455 QPainter::CompositionMode QPainter::compositionMode() const
2457 Q_D(const QPainter);
2459 qWarning("QPainter::compositionMode: Painter not active");
2460 return QPainter::CompositionMode_SourceOver;
2462 return d->state->composition_mode;
2466 Returns the current background brush.
2468 \sa setBackground(), {QPainter#Settings}{Settings}
2471 const QBrush &QPainter::background() const
2473 Q_D(const QPainter);
2475 qWarning("QPainter::background: Painter not active");
2476 return d->fakeState()->brush;
2478 return d->state->bgBrush;
2483 Returns true if clipping has been set; otherwise returns false.
2485 \sa setClipping(), {QPainter#Clipping}{Clipping}
2488 bool QPainter::hasClipping() const
2490 Q_D(const QPainter);
2492 qWarning("QPainter::hasClipping: Painter not active");
2495 return d->state->clipEnabled && d->state->clipOperation != Qt::NoClip;
2500 Enables clipping if \a enable is true, or disables clipping if \a
2503 \sa hasClipping(), {QPainter#Clipping}{Clipping}
2506 void QPainter::setClipping(bool enable)
2509 #ifdef QT_DEBUG_DRAW
2510 if (qt_show_painter_debug_output)
2511 printf("QPainter::setClipping(), enable=%s, was=%s\n",
2512 enable ? "on" : "off",
2513 hasClipping() ? "on" : "off");
2516 qWarning("QPainter::setClipping: Painter not active, state will be reset by begin");
2520 if (hasClipping() == enable)
2523 // we can't enable clipping if we don't have a clip
2525 && (d->state->clipInfo.isEmpty() || d->state->clipInfo.last().operation == Qt::NoClip))
2527 d->state->clipEnabled = enable;
2530 d->extended->clipEnabledChanged();
2534 d->state->dirtyFlags |= QPaintEngine::DirtyClipEnabled;
2535 d->updateState(d->state);
2540 Returns the currently set clip region. Note that the clip region
2541 is given in logical coordinates.
2543 \warning QPainter does not store the combined clip explicitly as
2544 this is handled by the underlying QPaintEngine, so the path is
2545 recreated on demand and transformed to the current logical
2546 coordinate system. This is potentially an expensive operation.
2548 \sa setClipRegion(), clipPath(), setClipping()
2551 QRegion QPainter::clipRegion() const
2553 Q_D(const QPainter);
2555 qWarning("QPainter::clipRegion: Painter not active");
2560 bool lastWasNothing = true;
2563 const_cast<QPainter *>(this)->d_ptr->updateInvMatrix();
2565 // ### Falcon: Use QPainterPath
2566 for (int i=0; i<d->state->clipInfo.size(); ++i) {
2567 const QPainterClipInfo &info = d->state->clipInfo.at(i);
2568 switch (info.clipType) {
2570 case QPainterClipInfo::RegionClip: {
2571 QTransform matrix = (info.matrix * d->invMatrix);
2572 if (lastWasNothing) {
2573 region = info.region * matrix;
2574 lastWasNothing = false;
2577 if (info.operation == Qt::IntersectClip)
2578 region &= info.region * matrix;
2579 else if (info.operation == Qt::UniteClip)
2580 region |= info.region * matrix;
2581 else if (info.operation == Qt::NoClip) {
2582 lastWasNothing = true;
2585 region = info.region * matrix;
2589 case QPainterClipInfo::PathClip: {
2590 QTransform matrix = (info.matrix * d->invMatrix);
2591 if (lastWasNothing) {
2592 region = QRegion((info.path * matrix).toFillPolygon().toPolygon(),
2593 info.path.fillRule());
2594 lastWasNothing = false;
2597 if (info.operation == Qt::IntersectClip) {
2598 region &= QRegion((info.path * matrix).toFillPolygon().toPolygon(),
2599 info.path.fillRule());
2600 } else if (info.operation == Qt::UniteClip) {
2601 region |= QRegion((info.path * matrix).toFillPolygon().toPolygon(),
2602 info.path.fillRule());
2603 } else if (info.operation == Qt::NoClip) {
2604 lastWasNothing = true;
2607 region = QRegion((info.path * matrix).toFillPolygon().toPolygon(),
2608 info.path.fillRule());
2613 case QPainterClipInfo::RectClip: {
2614 QTransform matrix = (info.matrix * d->invMatrix);
2615 if (lastWasNothing) {
2616 region = QRegion(info.rect) * matrix;
2617 lastWasNothing = false;
2620 if (info.operation == Qt::IntersectClip) {
2621 // Use rect intersection if possible.
2622 if (matrix.type() <= QTransform::TxScale)
2623 region &= matrix.mapRect(info.rect);
2625 region &= matrix.map(QRegion(info.rect));
2626 } else if (info.operation == Qt::UniteClip) {
2627 region |= QRegion(info.rect) * matrix;
2628 } else if (info.operation == Qt::NoClip) {
2629 lastWasNothing = true;
2632 region = QRegion(info.rect) * matrix;
2637 case QPainterClipInfo::RectFClip: {
2638 QTransform matrix = (info.matrix * d->invMatrix);
2639 if (lastWasNothing) {
2640 region = QRegion(info.rectf.toRect()) * matrix;
2641 lastWasNothing = false;
2644 if (info.operation == Qt::IntersectClip) {
2645 // Use rect intersection if possible.
2646 if (matrix.type() <= QTransform::TxScale)
2647 region &= matrix.mapRect(info.rectf.toRect());
2649 region &= matrix.map(QRegion(info.rectf.toRect()));
2650 } else if (info.operation == Qt::UniteClip) {
2651 region |= QRegion(info.rectf.toRect()) * matrix;
2652 } else if (info.operation == Qt::NoClip) {
2653 lastWasNothing = true;
2656 region = QRegion(info.rectf.toRect()) * matrix;
2666 extern QPainterPath qt_regionToPath(const QRegion ®ion);
2669 Returns the currently clip as a path. Note that the clip path is
2670 given in logical coordinates.
2672 \warning QPainter does not store the combined clip explicitly as
2673 this is handled by the underlying QPaintEngine, so the path is
2674 recreated on demand and transformed to the current logical
2675 coordinate system. This is potentially an expensive operation.
2677 \sa setClipPath(), clipRegion(), setClipping()
2679 QPainterPath QPainter::clipPath() const
2681 Q_D(const QPainter);
2683 // ### Since we do not support path intersections and path unions yet,
2684 // we just use clipRegion() here...
2686 qWarning("QPainter::clipPath: Painter not active");
2687 return QPainterPath();
2690 // No clip, return empty
2691 if (d->state->clipInfo.size() == 0) {
2692 return QPainterPath();
2695 // Update inverse matrix, used below.
2697 const_cast<QPainter *>(this)->d_ptr->updateInvMatrix();
2699 // For the simple case avoid conversion.
2700 if (d->state->clipInfo.size() == 1
2701 && d->state->clipInfo.at(0).clipType == QPainterClipInfo::PathClip) {
2702 QTransform matrix = (d->state->clipInfo.at(0).matrix * d->invMatrix);
2703 return d->state->clipInfo.at(0).path * matrix;
2705 } else if (d->state->clipInfo.size() == 1
2706 && d->state->clipInfo.at(0).clipType == QPainterClipInfo::RectClip) {
2707 QTransform matrix = (d->state->clipInfo.at(0).matrix * d->invMatrix);
2709 path.addRect(d->state->clipInfo.at(0).rect);
2710 return path * matrix;
2712 // Fallback to clipRegion() for now, since we don't have isect/unite for paths
2713 return qt_regionToPath(clipRegion());
2719 Returns the bounding rectangle of the current clip if there is a clip;
2720 otherwise returns an empty rectangle. Note that the clip region is
2721 given in logical coordinates.
2723 The bounding rectangle is not guaranteed to be tight.
2725 \sa setClipRect(), setClipPath(), setClipRegion()
2730 QRectF QPainter::clipBoundingRect() const
2732 Q_D(const QPainter);
2735 qWarning("QPainter::clipBoundingRect: Painter not active");
2739 // Accumulate the bounding box in device space. This is not 100%
2740 // precise, but it fits within the guarantee and it is resonably
2743 for (int i=0; i<d->state->clipInfo.size(); ++i) {
2745 const QPainterClipInfo &info = d->state->clipInfo.at(i);
2747 if (info.clipType == QPainterClipInfo::RectClip)
2749 else if (info.clipType == QPainterClipInfo::RectFClip)
2751 else if (info.clipType == QPainterClipInfo::RegionClip)
2752 r = info.region.boundingRect();
2754 r = info.path.boundingRect();
2756 r = info.matrix.mapRect(r);
2760 else if (info.operation == Qt::IntersectClip)
2762 else if (info.operation == Qt::UniteClip)
2767 // Map the rectangle back into logical space using the inverse
2770 const_cast<QPainter *>(this)->d_ptr->updateInvMatrix();
2772 return d->invMatrix.mapRect(bounds);
2776 \fn void QPainter::setClipRect(const QRectF &rectangle, Qt::ClipOperation operation)
2778 Enables clipping, and sets the clip region to the given \a
2779 rectangle using the given clip \a operation. The default operation
2780 is to replace the current clip rectangle.
2782 Note that the clip rectangle is specified in logical (painter)
2785 \sa clipRegion(), setClipping(), {QPainter#Clipping}{Clipping}
2787 void QPainter::setClipRect(const QRectF &rect, Qt::ClipOperation op)
2792 if ((!d->state->clipEnabled && op != Qt::NoClip) || (d->state->clipOperation == Qt::NoClip && op == Qt::UniteClip))
2793 op = Qt::ReplaceClip;
2796 qWarning("QPainter::setClipRect: Painter not active");
2799 qreal right = rect.x() + rect.width();
2800 qreal bottom = rect.y() + rect.height();
2801 qreal pts[] = { rect.x(), rect.y(),
2805 QVectorPath vp(pts, 4, 0, QVectorPath::RectangleHint);
2806 d->state->clipEnabled = true;
2807 d->extended->clip(vp, op);
2808 if (op == Qt::ReplaceClip || op == Qt::NoClip)
2809 d->state->clipInfo.clear();
2810 d->state->clipInfo << QPainterClipInfo(rect, op, d->state->matrix);
2811 d->state->clipOperation = op;
2815 if (qreal(int(rect.top())) == rect.top()
2816 && qreal(int(rect.bottom())) == rect.bottom()
2817 && qreal(int(rect.left())) == rect.left()
2818 && qreal(int(rect.right())) == rect.right())
2820 setClipRect(rect.toRect(), op);
2824 if (rect.isEmpty()) {
2825 setClipRegion(QRegion(), op);
2831 setClipPath(path, op);
2835 \fn void QPainter::setClipRect(const QRect &rectangle, Qt::ClipOperation operation)
2838 Enables clipping, and sets the clip region to the given \a rectangle using the given
2841 void QPainter::setClipRect(const QRect &rect, Qt::ClipOperation op)
2846 qWarning("QPainter::setClipRect: Painter not active");
2850 if ((!d->state->clipEnabled && op != Qt::NoClip) || (d->state->clipOperation == Qt::NoClip && op == Qt::UniteClip))
2851 op = Qt::ReplaceClip;
2854 d->state->clipEnabled = true;
2855 d->extended->clip(rect, op);
2856 if (op == Qt::ReplaceClip || op == Qt::NoClip)
2857 d->state->clipInfo.clear();
2858 d->state->clipInfo << QPainterClipInfo(rect, op, d->state->matrix);
2859 d->state->clipOperation = op;
2863 d->state->clipRegion = rect;
2864 d->state->clipOperation = op;
2865 if (op == Qt::NoClip || op == Qt::ReplaceClip)
2866 d->state->clipInfo.clear();
2867 d->state->clipInfo << QPainterClipInfo(rect, op, d->state->matrix);
2868 d->state->clipEnabled = true;
2869 d->state->dirtyFlags |= QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyClipEnabled;
2870 d->updateState(d->state);
2874 \fn void QPainter::setClipRect(int x, int y, int width, int height, Qt::ClipOperation operation)
2876 Enables clipping, and sets the clip region to the rectangle beginning at (\a x, \a y)
2877 with the given \a width and \a height.
2881 \fn void QPainter::setClipRegion(const QRegion ®ion, Qt::ClipOperation operation)
2883 Sets the clip region to the given \a region using the specified clip
2884 \a operation. The default clip operation is to replace the current
2887 Note that the clip region is given in logical coordinates.
2889 \sa clipRegion(), setClipRect(), {QPainter#Clipping}{Clipping}
2891 void QPainter::setClipRegion(const QRegion &r, Qt::ClipOperation op)
2894 #ifdef QT_DEBUG_DRAW
2895 QRect rect = r.boundingRect();
2896 if (qt_show_painter_debug_output)
2897 printf("QPainter::setClipRegion(), size=%d, [%d,%d,%d,%d]\n",
2898 r.rects().size(), rect.x(), rect.y(), rect.width(), rect.height());
2901 qWarning("QPainter::setClipRegion: Painter not active");
2905 if ((!d->state->clipEnabled && op != Qt::NoClip) || (d->state->clipOperation == Qt::NoClip && op == Qt::UniteClip))
2906 op = Qt::ReplaceClip;
2909 d->state->clipEnabled = true;
2910 d->extended->clip(r, op);
2911 if (op == Qt::NoClip || op == Qt::ReplaceClip)
2912 d->state->clipInfo.clear();
2913 d->state->clipInfo << QPainterClipInfo(r, op, d->state->matrix);
2914 d->state->clipOperation = op;
2918 d->state->clipRegion = r;
2919 d->state->clipOperation = op;
2920 if (op == Qt::NoClip || op == Qt::ReplaceClip)
2921 d->state->clipInfo.clear();
2922 d->state->clipInfo << QPainterClipInfo(r, op, d->state->matrix);
2923 d->state->clipEnabled = true;
2924 d->state->dirtyFlags |= QPaintEngine::DirtyClipRegion | QPaintEngine::DirtyClipEnabled;
2925 d->updateState(d->state);
2932 Sets the transformation matrix to \a matrix and enables transformations.
2934 \note It is advisable to use setWorldTransform() instead of this function to
2935 preserve the properties of perspective transformations.
2937 If \a combine is true, then \a matrix is combined with the current
2938 transformation matrix; otherwise \a matrix replaces the current
2939 transformation matrix.
2941 If \a matrix is the identity matrix and \a combine is false, this
2942 function calls setWorldMatrixEnabled(false). (The identity matrix is the
2943 matrix where QMatrix::m11() and QMatrix::m22() are 1.0 and the
2946 The following functions can transform the coordinate system without using
2955 They operate on the painter's worldMatrix() and are implemented like this:
2957 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 4
2959 Note that when using setWorldMatrix() function you should always have
2960 \a combine be true when you are drawing into a QPicture. Otherwise
2961 it may not be possible to replay the picture with additional
2962 transformations; using the translate(), scale(), etc. convenience
2965 For more information about the coordinate system, transformations
2966 and window-viewport conversion, see \l {Coordinate System}.
2968 \sa setWorldTransform(), QTransform
2971 void QPainter::setWorldMatrix(const QMatrix &matrix, bool combine)
2973 setWorldTransform(QTransform(matrix), combine);
2980 Returns the world transformation matrix.
2982 It is advisable to use worldTransform() because worldMatrix() does not
2983 preserve the properties of perspective transformations.
2985 \sa {QPainter#Coordinate Transformations}{Coordinate Transformations},
2989 const QMatrix &QPainter::worldMatrix() const
2991 Q_D(const QPainter);
2993 qWarning("QPainter::worldMatrix: Painter not active");
2994 return d->fakeState()->transform.toAffine();
2996 return d->state->worldMatrix.toAffine();
3002 Use setWorldTransform() instead.
3004 \sa setWorldTransform()
3007 void QPainter::setMatrix(const QMatrix &matrix, bool combine)
3009 setWorldTransform(QTransform(matrix), combine);
3015 Use worldTransform() instead.
3017 \sa worldTransform()
3020 const QMatrix &QPainter::matrix() const
3022 return worldMatrix();
3030 Returns the transformation matrix combining the current
3031 window/viewport and world transformation.
3033 It is advisable to use combinedTransform() instead of this
3034 function to preserve the properties of perspective transformations.
3036 \sa setWorldTransform(), setWindow(), setViewport()
3038 QMatrix QPainter::combinedMatrix() const
3040 return combinedTransform().toAffine();
3047 Returns the matrix that transforms from logical coordinates to
3048 device coordinates of the platform dependent paint device.
3050 \note It is advisable to use deviceTransform() instead of this
3051 function to preserve the properties of perspective transformations.
3053 This function is \e only needed when using platform painting
3054 commands on the platform dependent handle (Qt::HANDLE), and the
3055 platform does not do transformations nativly.
3057 The QPaintEngine::PaintEngineFeature enum can be queried to
3058 determine whether the platform performs the transformations or
3061 \sa worldMatrix(), QPaintEngine::hasFeature(),
3063 const QMatrix &QPainter::deviceMatrix() const
3065 Q_D(const QPainter);
3067 qWarning("QPainter::deviceMatrix: Painter not active");
3068 return d->fakeState()->transform.toAffine();
3070 return d->state->matrix.toAffine();
3076 Resets any transformations that were made using translate(), scale(),
3077 shear(), rotate(), setWorldMatrix(), setViewport() and
3080 It is advisable to use resetTransform() instead of this function
3081 to preserve the properties of perspective transformations.
3083 \sa {QPainter#Coordinate Transformations}{Coordinate
3087 void QPainter::resetMatrix()
3096 Enables transformations if \a enable is true, or disables
3097 transformations if \a enable is false. The world transformation
3098 matrix is not changed.
3100 \sa worldMatrixEnabled(), worldTransform(), {QPainter#Coordinate
3101 Transformations}{Coordinate Transformations}
3104 void QPainter::setWorldMatrixEnabled(bool enable)
3107 #ifdef QT_DEBUG_DRAW
3108 if (qt_show_painter_debug_output)
3109 printf("QPainter::setMatrixEnabled(), enable=%d\n", enable);
3113 qWarning("QPainter::setMatrixEnabled: Painter not active");
3116 if (enable == d->state->WxF)
3119 d->state->WxF = enable;
3126 Returns true if world transformation is enabled; otherwise returns
3129 \sa setWorldMatrixEnabled(), worldTransform(), {Coordinate System}
3132 bool QPainter::worldMatrixEnabled() const
3134 Q_D(const QPainter);
3136 qWarning("QPainter::worldMatrixEnabled: Painter not active");
3139 return d->state->WxF;
3145 Use setWorldMatrixEnabled() instead.
3147 \sa setWorldMatrixEnabled()
3150 void QPainter::setMatrixEnabled(bool enable)
3152 setWorldMatrixEnabled(enable);
3158 Use worldMatrixEnabled() instead
3160 \sa worldMatrixEnabled()
3163 bool QPainter::matrixEnabled() const
3165 return worldMatrixEnabled();
3169 Scales the coordinate system by (\a{sx}, \a{sy}).
3171 \sa setWorldTransform() {QPainter#Coordinate Transformations}{Coordinate
3175 void QPainter::scale(qreal sx, qreal sy)
3177 #ifdef QT_DEBUG_DRAW
3178 if (qt_show_painter_debug_output)
3179 printf("QPainter::scale(), sx=%f, sy=%f\n", sx, sy);
3183 qWarning("QPainter::scale: Painter not active");
3187 d->state->worldMatrix.scale(sx,sy);
3188 d->state->WxF = true;
3193 Shears the coordinate system by (\a{sh}, \a{sv}).
3195 \sa setWorldTransform(), {QPainter#Coordinate Transformations}{Coordinate
3199 void QPainter::shear(qreal sh, qreal sv)
3201 #ifdef QT_DEBUG_DRAW
3202 if (qt_show_painter_debug_output)
3203 printf("QPainter::shear(), sh=%f, sv=%f\n", sh, sv);
3207 qWarning("QPainter::shear: Painter not active");
3211 d->state->worldMatrix.shear(sh, sv);
3212 d->state->WxF = true;
3217 \fn void QPainter::rotate(qreal angle)
3219 Rotates the coordinate system the given \a angle clockwise.
3221 \sa setWorldTransform(), {QPainter#Coordinate Transformations}{Coordinate
3225 void QPainter::rotate(qreal a)
3227 #ifdef QT_DEBUG_DRAW
3228 if (qt_show_painter_debug_output)
3229 printf("QPainter::rotate(), angle=%f\n", a);
3233 qWarning("QPainter::rotate: Painter not active");
3237 d->state->worldMatrix.rotate(a);
3238 d->state->WxF = true;
3243 Translates the coordinate system by the given \a offset; i.e. the
3244 given \a offset is added to points.
3246 \sa setWorldTransform(), {QPainter#Coordinate Transformations}{Coordinate
3249 void QPainter::translate(const QPointF &offset)
3251 qreal dx = offset.x();
3252 qreal dy = offset.y();
3253 #ifdef QT_DEBUG_DRAW
3254 if (qt_show_painter_debug_output)
3255 printf("QPainter::translate(), dx=%f, dy=%f\n", dx, dy);
3259 qWarning("QPainter::translate: Painter not active");
3263 d->state->worldMatrix.translate(dx, dy);
3264 d->state->WxF = true;
3269 \fn void QPainter::translate(const QPoint &offset)
3272 Translates the coordinate system by the given \a offset.
3276 \fn void QPainter::translate(qreal dx, qreal dy)
3279 Translates the coordinate system by the vector (\a dx, \a dy).
3283 \fn void QPainter::setClipPath(const QPainterPath &path, Qt::ClipOperation operation)
3285 Enables clipping, and sets the clip path for the painter to the
3286 given \a path, with the clip \a operation.
3288 Note that the clip path is specified in logical (painter)
3291 \sa clipPath(), clipRegion(), {QPainter#Clipping}{Clipping}
3294 void QPainter::setClipPath(const QPainterPath &path, Qt::ClipOperation op)
3296 #ifdef QT_DEBUG_DRAW
3297 if (qt_show_painter_debug_output) {
3298 QRectF b = path.boundingRect();
3299 printf("QPainter::setClipPath(), size=%d, op=%d, bounds=[%.2f,%.2f,%.2f,%.2f]\n",
3300 path.elementCount(), op, b.x(), b.y(), b.width(), b.height());
3306 qWarning("QPainter::setClipPath: Painter not active");
3310 if ((!d->state->clipEnabled && op != Qt::NoClip) || (d->state->clipOperation == Qt::NoClip && op == Qt::UniteClip))
3311 op = Qt::ReplaceClip;
3314 d->state->clipEnabled = true;
3315 d->extended->clip(path, op);
3316 if (op == Qt::NoClip || op == Qt::ReplaceClip)
3317 d->state->clipInfo.clear();
3318 d->state->clipInfo << QPainterClipInfo(path, op, d->state->matrix);
3319 d->state->clipOperation = op;
3323 d->state->clipPath = path;
3324 d->state->clipOperation = op;
3325 if (op == Qt::NoClip || op == Qt::ReplaceClip)
3326 d->state->clipInfo.clear();
3327 d->state->clipInfo << QPainterClipInfo(path, op, d->state->matrix);
3328 d->state->clipEnabled = true;
3329 d->state->dirtyFlags |= QPaintEngine::DirtyClipPath | QPaintEngine::DirtyClipEnabled;
3330 d->updateState(d->state);
3334 Draws the outline (strokes) the path \a path with the pen specified
3337 \sa fillPath(), {QPainter#Drawing}{Drawing}
3339 void QPainter::strokePath(const QPainterPath &path, const QPen &pen)
3344 qWarning("QPainter::strokePath: Painter not active");
3352 const QGradient *g = qpen_brush(pen).gradient();
3353 if (!g || g->coordinateMode() == QGradient::LogicalMode) {
3354 d->extended->stroke(qtVectorPathForPath(path), pen);
3359 QBrush oldBrush = d->state->brush;
3360 QPen oldPen = d->state->pen;
3363 setBrush(Qt::NoBrush);
3373 Fills the given \a path using the given \a brush. The outline is
3376 Alternatively, you can specify a QColor instead of a QBrush; the
3377 QBrush constructor (taking a QColor argument) will automatically
3378 create a solid pattern brush.
3382 void QPainter::fillPath(const QPainterPath &path, const QBrush &brush)
3387 qWarning("QPainter::fillPath: Painter not active");
3395 const QGradient *g = brush.gradient();
3396 if (!g || g->coordinateMode() == QGradient::LogicalMode) {
3397 d->extended->fill(qtVectorPathForPath(path), brush);
3402 QBrush oldBrush = d->state->brush;
3403 QPen oldPen = d->state->pen;
3416 Draws the given painter \a path using the current pen for outline
3417 and the current brush for filling.
3421 \o \inlineimage qpainter-path.png
3423 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 5
3426 \sa {painting/painterpaths}{the Painter Paths
3427 example},{demos/deform}{the Vector Deformation demo}
3429 void QPainter::drawPath(const QPainterPath &path)
3431 #ifdef QT_DEBUG_DRAW
3432 QRectF pathBounds = path.boundingRect();
3433 if (qt_show_painter_debug_output)
3434 printf("QPainter::drawPath(), size=%d, [%.2f,%.2f,%.2f,%.2f]\n",
3435 path.elementCount(),
3436 pathBounds.x(), pathBounds.y(), pathBounds.width(), pathBounds.height());
3442 qWarning("QPainter::drawPath: Painter not active");
3447 d->extended->drawPath(path);
3450 d->updateState(d->state);
3452 if (d->engine->hasFeature(QPaintEngine::PainterPaths) && d->state->emulationSpecifier == 0) {
3453 d->engine->drawPath(path);
3455 d->draw_helper(path);
3460 \fn void QPainter::drawLine(const QLineF &line)
3462 Draws a line defined by \a line.
3466 \o \inlineimage qpainter-line.png
3468 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 6
3471 \sa drawLines(), drawPolyline(), {Coordinate System}
3475 \fn void QPainter::drawLine(const QLine &line)
3478 Draws a line defined by \a line.
3482 \fn void QPainter::drawLine(const QPoint &p1, const QPoint &p2)
3485 Draws a line from \a p1 to \a p2.
3489 \fn void QPainter::drawLine(const QPointF &p1, const QPointF &p2)
3492 Draws a line from \a p1 to \a p2.
3496 \fn void QPainter::drawLine(int x1, int y1, int x2, int y2)
3499 Draws a line from (\a x1, \a y1) to (\a x2, \a y2) and sets the
3500 current pen position to (\a x2, \a y2).
3504 \fn void QPainter::drawRect(const QRectF &rectangle)
3506 Draws the current \a rectangle with the current pen and brush.
3508 A filled rectangle has a size of \a{rectangle}.size(). A stroked
3509 rectangle has a size of \a{rectangle}.size() plus the pen width.
3513 \o \inlineimage qpainter-rectangle.png
3515 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 7
3518 \sa drawRects(), drawPolygon(), {Coordinate System}
3522 \fn void QPainter::drawRect(const QRect &rectangle)
3526 Draws the current \a rectangle with the current pen and brush.
3530 \fn void QPainter::drawRect(int x, int y, int width, int height)
3534 Draws a rectangle with upper left corner at (\a{x}, \a{y}) and
3535 with the given \a width and \a height.
3539 \fn void QPainter::drawRects(const QRectF *rectangles, int rectCount)
3541 Draws the first \a rectCount of the given \a rectangles using the
3542 current pen and brush.
3546 void QPainter::drawRects(const QRectF *rects, int rectCount)
3548 #ifdef QT_DEBUG_DRAW
3549 if (qt_show_painter_debug_output)
3550 printf("QPainter::drawRects(), count=%d\n", rectCount);
3555 qWarning("QPainter::drawRects: Painter not active");
3563 d->extended->drawRects(rects, rectCount);
3567 d->updateState(d->state);
3569 if (!d->state->emulationSpecifier) {
3570 d->engine->drawRects(rects, rectCount);
3574 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
3575 && d->state->matrix.type() == QTransform::TxTranslate) {
3576 for (int i=0; i<rectCount; ++i) {
3577 QRectF r(rects[i].x() + d->state->matrix.dx(),
3578 rects[i].y() + d->state->matrix.dy(),
3581 d->engine->drawRects(&r, 1);
3584 if (d->state->brushNeedsResolving() || d->state->penNeedsResolving()) {
3585 for (int i=0; i<rectCount; ++i) {
3586 QPainterPath rectPath;
3587 rectPath.addRect(rects[i]);
3588 d->draw_helper(rectPath, QPainterPrivate::StrokeAndFillDraw);
3591 QPainterPath rectPath;
3592 for (int i=0; i<rectCount; ++i)
3593 rectPath.addRect(rects[i]);
3594 d->draw_helper(rectPath, QPainterPrivate::StrokeAndFillDraw);
3600 \fn void QPainter::drawRects(const QRect *rectangles, int rectCount)
3603 Draws the first \a rectCount of the given \a rectangles using the
3604 current pen and brush.
3606 void QPainter::drawRects(const QRect *rects, int rectCount)
3608 #ifdef QT_DEBUG_DRAW
3609 if (qt_show_painter_debug_output)
3610 printf("QPainter::drawRects(), count=%d\n", rectCount);
3615 qWarning("QPainter::drawRects: Painter not active");
3623 d->extended->drawRects(rects, rectCount);
3627 d->updateState(d->state);
3629 if (!d->state->emulationSpecifier) {
3630 d->engine->drawRects(rects, rectCount);
3634 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
3635 && d->state->matrix.type() == QTransform::TxTranslate) {
3636 for (int i=0; i<rectCount; ++i) {
3637 QRectF r(rects[i].x() + d->state->matrix.dx(),
3638 rects[i].y() + d->state->matrix.dy(),
3642 d->engine->drawRects(&r, 1);
3645 if (d->state->brushNeedsResolving() || d->state->penNeedsResolving()) {
3646 for (int i=0; i<rectCount; ++i) {
3647 QPainterPath rectPath;
3648 rectPath.addRect(rects[i]);
3649 d->draw_helper(rectPath, QPainterPrivate::StrokeAndFillDraw);
3652 QPainterPath rectPath;
3653 for (int i=0; i<rectCount; ++i)
3654 rectPath.addRect(rects[i]);
3656 d->draw_helper(rectPath, QPainterPrivate::StrokeAndFillDraw);
3662 \fn void QPainter::drawRects(const QVector<QRectF> &rectangles)
3665 Draws the given \a rectangles using the current pen and brush.
3669 \fn void QPainter::drawRects(const QVector<QRect> &rectangles)
3673 Draws the given \a rectangles using the current pen and brush.
3677 \fn void QPainter::drawPoint(const QPointF &position)
3679 Draws a single point at the given \a position using the current
3682 \sa {Coordinate System}
3686 \fn void QPainter::drawPoint(const QPoint &position)
3689 Draws a single point at the given \a position using the current
3693 /*! \fn void QPainter::drawPoint(int x, int y)
3697 Draws a single point at position (\a x, \a y).
3701 Draws the first \a pointCount points in the array \a points using
3702 the current pen's color.
3704 \sa {Coordinate System}
3706 void QPainter::drawPoints(const QPointF *points, int pointCount)
3708 #ifdef QT_DEBUG_DRAW
3709 if (qt_show_painter_debug_output)
3710 printf("QPainter::drawPoints(), count=%d\n", pointCount);
3715 qWarning("QPainter::drawPoints: Painter not active");
3719 if (pointCount <= 0)
3723 d->extended->drawPoints(points, pointCount);
3727 d->updateState(d->state);
3729 if (!d->state->emulationSpecifier) {
3730 d->engine->drawPoints(points, pointCount);
3734 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
3735 && d->state->matrix.type() == QTransform::TxTranslate) {
3736 // ### use drawPoints function
3737 for (int i=0; i<pointCount; ++i) {
3738 QPointF pt(points[i].x() + d->state->matrix.dx(),
3739 points[i].y() + d->state->matrix.dy());
3740 d->engine->drawPoints(&pt, 1);
3743 QPen pen = d->state->pen;
3744 bool flat_pen = pen.capStyle() == Qt::FlatCap;
3747 pen.setCapStyle(Qt::SquareCap);
3751 for (int i=0; i<pointCount; ++i) {
3752 path.moveTo(points[i].x(), points[i].y());
3753 path.lineTo(points[i].x() + 0.0001, points[i].y());
3755 d->draw_helper(path, QPainterPrivate::StrokeDraw);
3764 Draws the first \a pointCount points in the array \a points using
3765 the current pen's color.
3768 void QPainter::drawPoints(const QPoint *points, int pointCount)
3770 #ifdef QT_DEBUG_DRAW
3771 if (qt_show_painter_debug_output)
3772 printf("QPainter::drawPoints(), count=%d\n", pointCount);
3777 qWarning("QPainter::drawPoints: Painter not active");
3781 if (pointCount <= 0)
3785 d->extended->drawPoints(points, pointCount);
3789 d->updateState(d->state);
3791 if (!d->state->emulationSpecifier) {
3792 d->engine->drawPoints(points, pointCount);
3796 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
3797 && d->state->matrix.type() == QTransform::TxTranslate) {
3798 // ### use drawPoints function
3799 for (int i=0; i<pointCount; ++i) {
3800 QPointF pt(points[i].x() + d->state->matrix.dx(),
3801 points[i].y() + d->state->matrix.dy());
3802 d->engine->drawPoints(&pt, 1);
3805 QPen pen = d->state->pen;
3806 bool flat_pen = (pen.capStyle() == Qt::FlatCap);
3809 pen.setCapStyle(Qt::SquareCap);
3813 for (int i=0; i<pointCount; ++i) {
3814 path.moveTo(points[i].x(), points[i].y());
3815 path.lineTo(points[i].x() + 0.0001, points[i].y());
3817 d->draw_helper(path, QPainterPrivate::StrokeDraw);
3824 \fn void QPainter::drawPoints(const QPolygonF &points)
3828 Draws the points in the vector \a points.
3832 \fn void QPainter::drawPoints(const QPolygon &points)
3836 Draws the points in the vector \a points.
3840 \fn void QPainter::drawPoints(const QPolygon &polygon, int index,
3846 Draws \a count points in the vector \a polygon starting on \a index
3847 using the current pen.
3849 Use drawPoints() combined with QPolygon::constData() instead.
3852 QPainter painter(this);
3853 painter.drawPoints(polygon, index, count);
3855 int pointCount = (count == -1) ? polygon.size() - index : count;
3857 QPainter painter(this);
3858 painter.drawPoints(polygon.constData() + index, pointCount);
3863 Sets the background mode of the painter to the given \a mode
3865 Qt::TransparentMode (the default) draws stippled lines and text
3866 without setting the background pixels. Qt::OpaqueMode fills these
3867 space with the current background color.
3869 Note that in order to draw a bitmap or pixmap transparently, you
3870 must use QPixmap::setMask().
3872 \sa backgroundMode(), setBackground(),
3873 {QPainter#Settings}{Settings}
3876 void QPainter::setBackgroundMode(Qt::BGMode mode)
3878 #ifdef QT_DEBUG_DRAW
3879 if (qt_show_painter_debug_output)
3880 printf("QPainter::setBackgroundMode(), mode=%d\n", mode);
3885 qWarning("QPainter::setBackgroundMode: Painter not active");
3888 if (d->state->bgMode == mode)
3891 d->state->bgMode = mode;
3893 d->checkEmulation();
3895 d->state->dirtyFlags |= QPaintEngine::DirtyBackgroundMode;
3900 Returns the current background mode.
3902 \sa setBackgroundMode(), {QPainter#Settings}{Settings}
3904 Qt::BGMode QPainter::backgroundMode() const
3906 Q_D(const QPainter);
3908 qWarning("QPainter::backgroundMode: Painter not active");
3909 return Qt::TransparentMode;
3911 return d->state->bgMode;
3918 Sets the painter's pen to have style Qt::SolidLine, width 0 and the
3922 void QPainter::setPen(const QColor &color)
3924 #ifdef QT_DEBUG_DRAW
3925 if (qt_show_painter_debug_output)
3926 printf("QPainter::setPen(), color=%04x\n", color.rgb());
3930 qWarning("QPainter::setPen: Painter not active");
3934 if (d->state->pen.style() == Qt::SolidLine
3935 && d->state->pen.widthF() == 0
3936 && d->state->pen.isSolid()
3937 && d->state->pen.color() == color)
3940 QPen pen(color.isValid() ? color : QColor(Qt::black), 0, Qt::SolidLine);
3942 d->state->pen = pen;
3944 d->extended->penChanged();
3946 d->state->dirtyFlags |= QPaintEngine::DirtyPen;
3950 Sets the painter's pen to be the given \a pen.
3952 The \a pen defines how to draw lines and outlines, and it also
3953 defines the text color.
3955 \sa pen(), {QPainter#Settings}{Settings}
3958 void QPainter::setPen(const QPen &pen)
3961 #ifdef QT_DEBUG_DRAW
3962 if (qt_show_painter_debug_output)
3963 printf("QPainter::setPen(), color=%04x, (brushStyle=%d) style=%d, cap=%d, join=%d\n",
3964 pen.color().rgb(), pen.brush().style(), pen.style(), pen.capStyle(), pen.joinStyle());
3968 qWarning("QPainter::setPen: Painter not active");
3972 if (d->state->pen == pen)
3975 d->state->pen = pen;
3978 d->checkEmulation();
3979 d->extended->penChanged();
3983 d->state->dirtyFlags |= QPaintEngine::DirtyPen;
3989 Sets the painter's pen to have the given \a style, width 0 and
3993 void QPainter::setPen(Qt::PenStyle style)
3997 qWarning("QPainter::setPen: Painter not active");
4001 if (d->state->pen.style() == style
4002 && (style == Qt::NoPen || (d->state->pen.widthF() == 0
4003 && d->state->pen.isSolid()
4004 && d->state->pen.color() == QColor(Qt::black))))
4007 // QPen(Qt::NoPen) is to avoid creating QPenData, including its brush (from the color)
4008 // Note that this works well as long as QPen(Qt::NoPen) returns a black, zero-width pen
4009 d->state->pen = (style == Qt::NoPen) ? QPen(Qt::NoPen) : QPen(Qt::black, 0, style);
4012 d->extended->penChanged();
4014 d->state->dirtyFlags |= QPaintEngine::DirtyPen;
4019 Returns the painter's current pen.
4021 \sa setPen(), {QPainter#Settings}{Settings}
4024 const QPen &QPainter::pen() const
4026 Q_D(const QPainter);
4028 qWarning("QPainter::pen: Painter not active");
4029 return d->fakeState()->pen;
4031 return d->state->pen;
4036 Sets the painter's brush to the given \a brush.
4038 The painter's brush defines how shapes are filled.
4040 \sa brush(), {QPainter#Settings}{Settings}
4043 void QPainter::setBrush(const QBrush &brush)
4045 #ifdef QT_DEBUG_DRAW
4046 if (qt_show_painter_debug_output)
4047 printf("QPainter::setBrush(), color=%04x, style=%d\n", brush.color().rgb(), brush.style());
4051 qWarning("QPainter::setBrush: Painter not active");
4055 if (d->state->brush.d == brush.d)
4059 d->state->brush = brush;
4060 d->checkEmulation();
4061 d->extended->brushChanged();
4065 d->state->brush = brush;
4066 d->state->dirtyFlags |= QPaintEngine::DirtyBrush;
4073 Sets the painter's brush to black color and the specified \a
4077 void QPainter::setBrush(Qt::BrushStyle style)
4081 qWarning("QPainter::setBrush: Painter not active");
4084 if (d->state->brush.style() == style &&
4085 (style == Qt::NoBrush
4086 || (style == Qt::SolidPattern && d->state->brush.color() == QColor(0, 0, 0))))
4088 d->state->brush = QBrush(Qt::black, style);
4090 d->extended->brushChanged();
4092 d->state->dirtyFlags |= QPaintEngine::DirtyBrush;
4096 Returns the painter's current brush.
4098 \sa QPainter::setBrush(), {QPainter#Settings}{Settings}
4101 const QBrush &QPainter::brush() const
4103 Q_D(const QPainter);
4105 qWarning("QPainter::brush: Painter not active");
4106 return d->fakeState()->brush;
4108 return d->state->brush;
4112 \fn void QPainter::setBackground(const QBrush &brush)
4114 Sets the background brush of the painter to the given \a brush.
4116 The background brush is the brush that is filled in when drawing
4117 opaque text, stippled lines and bitmaps. The background brush has
4118 no effect in transparent background mode (which is the default).
4120 \sa background(), setBackgroundMode(),
4121 {QPainter#Settings}{Settings}
4124 void QPainter::setBackground(const QBrush &bg)
4126 #ifdef QT_DEBUG_DRAW
4127 if (qt_show_painter_debug_output)
4128 printf("QPainter::setBackground(), color=%04x, style=%d\n", bg.color().rgb(), bg.style());
4133 qWarning("QPainter::setBackground: Painter not active");
4136 d->state->bgBrush = bg;
4138 d->state->dirtyFlags |= QPaintEngine::DirtyBackground;
4142 Sets the painter's font to the given \a font.
4144 This font is used by subsequent drawText() functions. The text
4145 color is the same as the pen color.
4147 If you set a font that isn't available, Qt finds a close match.
4148 font() will return what you set using setFont() and fontInfo() returns the
4149 font actually being used (which may be the same).
4151 \sa font(), drawText(), {QPainter#Settings}{Settings}
4154 void QPainter::setFont(const QFont &font)
4158 #ifdef QT_DEBUG_DRAW
4159 if (qt_show_painter_debug_output)
4160 printf("QPainter::setFont(), family=%s, pointSize=%d\n", font.family().toLatin1().constData(), font.pointSize());
4164 qWarning("QPainter::setFont: Painter not active");
4168 d->state->font = QFont(font.resolve(d->state->deviceFont), device());
4170 d->state->dirtyFlags |= QPaintEngine::DirtyFont;
4174 Returns the currently set font used for drawing text.
4176 \sa setFont(), drawText(), {QPainter#Settings}{Settings}
4178 const QFont &QPainter::font() const
4180 Q_D(const QPainter);
4182 qWarning("QPainter::font: Painter not active");
4183 return d->fakeState()->font;
4185 return d->state->font;
4191 Draws the given rectangle \a rect with rounded corners.
4193 The \a xRadius and \a yRadius arguments specify the radii
4194 of the ellipses defining the corners of the rounded rectangle.
4195 When \a mode is Qt::RelativeSize, \a xRadius and
4196 \a yRadius are specified in percentage of half the rectangle's
4197 width and height respectively, and should be in the range
4200 A filled rectangle has a size of rect.size(). A stroked rectangle
4201 has a size of rect.size() plus the pen width.
4205 \o \inlineimage qpainter-roundrect.png
4207 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 8
4210 \sa drawRect(), QPen
4212 void QPainter::drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode)
4214 #ifdef QT_DEBUG_DRAW
4215 if (qt_show_painter_debug_output)
4216 printf("QPainter::drawRoundedRect(), [%.2f,%.2f,%.2f,%.2f]\n", rect.x(), rect.y(), rect.width(), rect.height());
4223 if (xRadius <= 0 || yRadius <= 0) { // draw normal rectangle
4229 d->extended->drawRoundedRect(rect, xRadius, yRadius, mode);
4234 path.addRoundedRect(rect, xRadius, yRadius, mode);
4239 \fn void QPainter::drawRoundedRect(const QRect &rect, qreal xRadius, qreal yRadius,
4240 Qt::SizeMode mode = Qt::AbsoluteSize);
4244 Draws the given rectangle \a rect with rounded corners.
4248 \fn void QPainter::drawRoundedRect(int x, int y, int w, int h, qreal xRadius, qreal yRadius,
4249 Qt::SizeMode mode = Qt::AbsoluteSize);
4253 Draws the given rectangle \a x, \a y, \a w, \a h with rounded corners.
4259 Draws a rectangle \a r with rounded corners.
4261 The \a xRnd and \a yRnd arguments specify how rounded the corners
4262 should be. 0 is angled corners, 99 is maximum roundedness.
4264 A filled rectangle has a size of r.size(). A stroked rectangle
4265 has a size of r.size() plus the pen width.
4267 \sa drawRoundedRect()
4269 void QPainter::drawRoundRect(const QRectF &r, int xRnd, int yRnd)
4271 drawRoundedRect(r, xRnd, yRnd, Qt::RelativeSize);
4276 \fn void QPainter::drawRoundRect(const QRect &r, int xRnd = 25, int yRnd = 25)
4281 Draws the rectangle \a r with rounded corners.
4287 \fn QPainter::drawRoundRect(int x, int y, int w, int h, int xRnd, int yRnd)
4291 Draws the rectangle \a x, \a y, \a w, \a h with rounded corners.
4295 \fn void QPainter::drawEllipse(const QRectF &rectangle)
4297 Draws the ellipse defined by the given \a rectangle.
4299 A filled ellipse has a size of \a{rectangle}.\l
4300 {QRect::size()}{size()}. A stroked ellipse has a size of
4301 \a{rectangle}.\l {QRect::size()}{size()} plus the pen width.
4305 \o \inlineimage qpainter-ellipse.png
4307 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 9
4310 \sa drawPie(), {Coordinate System}
4312 void QPainter::drawEllipse(const QRectF &r)
4314 #ifdef QT_DEBUG_DRAW
4315 if (qt_show_painter_debug_output)
4316 printf("QPainter::drawEllipse(), [%.2f,%.2f,%.2f,%.2f]\n", r.x(), r.y(), r.width(), r.height());
4323 QRectF rect(r.normalized());
4326 d->extended->drawEllipse(rect);
4330 d->updateState(d->state);
4331 if (d->state->emulationSpecifier) {
4332 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
4333 && d->state->matrix.type() == QTransform::TxTranslate) {
4334 rect.translate(QPointF(d->state->matrix.dx(), d->state->matrix.dy()));
4337 path.addEllipse(rect);
4338 d->draw_helper(path, QPainterPrivate::StrokeAndFillDraw);
4343 d->engine->drawEllipse(rect);
4347 \fn QPainter::drawEllipse(const QRect &rectangle)
4351 Draws the ellipse defined by the given \a rectangle.
4353 void QPainter::drawEllipse(const QRect &r)
4355 #ifdef QT_DEBUG_DRAW
4356 if (qt_show_painter_debug_output)
4357 printf("QPainter::drawEllipse(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
4364 QRect rect(r.normalized());
4367 d->extended->drawEllipse(rect);
4371 d->updateState(d->state);
4373 if (d->state->emulationSpecifier) {
4374 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
4375 && d->state->matrix.type() == QTransform::TxTranslate) {
4376 rect.translate(QPoint(qRound(d->state->matrix.dx()), qRound(d->state->matrix.dy())));
4379 path.addEllipse(rect);
4380 d->draw_helper(path, QPainterPrivate::StrokeAndFillDraw);
4385 d->engine->drawEllipse(rect);
4389 \fn QPainter::drawEllipse(int x, int y, int width, int height)
4393 Draws the ellipse defined by the rectangle beginning at (\a{x},
4394 \a{y}) with the given \a width and \a height.
4400 \fn QPainter::drawEllipse(const QPointF ¢er, qreal rx, qreal ry)
4404 Draws the ellipse positioned at \a{center} with radii \a{rx} and \a{ry}.
4410 \fn QPainter::drawEllipse(const QPoint ¢er, int rx, int ry)
4414 Draws the ellipse positioned at \a{center} with radii \a{rx} and \a{ry}.
4418 \fn void QPainter::drawArc(const QRectF &rectangle, int startAngle, int spanAngle)
4420 Draws the arc defined by the given \a rectangle, \a startAngle and
4423 The \a startAngle and \a spanAngle must be specified in 1/16th of
4424 a degree, i.e. a full circle equals 5760 (16 * 360). Positive
4425 values for the angles mean counter-clockwise while negative values
4426 mean the clockwise direction. Zero degrees is at the 3 o'clock
4431 \o \inlineimage qpainter-arc.png
4433 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 10
4436 \sa drawPie(), drawChord(), {Coordinate System}
4439 void QPainter::drawArc(const QRectF &r, int a, int alen)
4441 #ifdef QT_DEBUG_DRAW
4442 if (qt_show_painter_debug_output)
4443 printf("QPainter::drawArc(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
4444 r.x(), r.y(), r.width(), r.height(), a/16, alen/16);
4451 QRectF rect = r.normalized();
4454 path.arcMoveTo(rect, a/16.0);
4455 path.arcTo(rect, a/16.0, alen/16.0);
4456 strokePath(path, d->state->pen);
4459 /*! \fn void QPainter::drawArc(const QRect &rectangle, int startAngle,
4464 Draws the arc defined by the given \a rectangle, \a startAngle and
4469 \fn void QPainter::drawArc(int x, int y, int width, int height,
4470 int startAngle, int spanAngle)
4474 Draws the arc defined by the rectangle beginning at (\a x, \a y)
4475 with the specified \a width and \a height, and the given \a
4476 startAngle and \a spanAngle.
4480 \fn void QPainter::drawPie(const QRectF &rectangle, int startAngle, int spanAngle)
4482 Draws a pie defined by the given \a rectangle, \a startAngle and
4485 The pie is filled with the current brush().
4487 The startAngle and spanAngle must be specified in 1/16th of a
4488 degree, i.e. a full circle equals 5760 (16 * 360). Positive values
4489 for the angles mean counter-clockwise while negative values mean
4490 the clockwise direction. Zero degrees is at the 3 o'clock
4495 \o \inlineimage qpainter-pie.png
4497 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 11
4500 \sa drawEllipse(), drawChord(), {Coordinate System}
4502 void QPainter::drawPie(const QRectF &r, int a, int alen)
4504 #ifdef QT_DEBUG_DRAW
4505 if (qt_show_painter_debug_output)
4506 printf("QPainter::drawPie(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
4507 r.x(), r.y(), r.width(), r.height(), a/16, alen/16);
4518 if (a < 0) a += (360*16);
4521 QRectF rect = r.normalized();
4524 path.moveTo(rect.center());
4525 path.arcTo(rect.x(), rect.y(), rect.width(), rect.height(), a/16.0, alen/16.0);
4526 path.closeSubpath();
4532 \fn void QPainter::drawPie(const QRect &rectangle, int startAngle, int spanAngle)
4535 Draws a pie defined by the given \a rectangle, \a startAngle and
4540 \fn void QPainter::drawPie(int x, int y, int width, int height, int
4541 startAngle, int spanAngle)
4545 Draws the pie defined by the rectangle beginning at (\a x, \a y) with
4546 the specified \a width and \a height, and the given \a startAngle and
4551 \fn void QPainter::drawChord(const QRectF &rectangle, int startAngle, int spanAngle)
4553 Draws the chord defined by the given \a rectangle, \a startAngle and
4554 \a spanAngle. The chord is filled with the current brush().
4556 The startAngle and spanAngle must be specified in 1/16th of a
4557 degree, i.e. a full circle equals 5760 (16 * 360). Positive values
4558 for the angles mean counter-clockwise while negative values mean
4559 the clockwise direction. Zero degrees is at the 3 o'clock
4564 \o \inlineimage qpainter-chord.png
4566 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 12
4569 \sa drawArc(), drawPie(), {Coordinate System}
4571 void QPainter::drawChord(const QRectF &r, int a, int alen)
4573 #ifdef QT_DEBUG_DRAW
4574 if (qt_show_painter_debug_output)
4575 printf("QPainter::drawChord(), [%.2f,%.2f,%.2f,%.2f], angle=%d, sweep=%d\n",
4576 r.x(), r.y(), r.width(), r.height(), a/16, alen/16);
4583 QRectF rect = r.normalized();
4586 path.arcMoveTo(rect, a/16.0);
4587 path.arcTo(rect, a/16.0, alen/16.0);
4588 path.closeSubpath();
4592 \fn void QPainter::drawChord(const QRect &rectangle, int startAngle, int spanAngle)
4596 Draws the chord defined by the given \a rectangle, \a startAngle and
4601 \fn void QPainter::drawChord(int x, int y, int width, int height, int
4602 startAngle, int spanAngle)
4606 Draws the chord defined by the rectangle beginning at (\a x, \a y)
4607 with the specified \a width and \a height, and the given \a
4608 startAngle and \a spanAngle.
4613 \fn void QPainter::drawLineSegments(const QPolygon &polygon, int
4616 Draws \a count separate lines from points defined by the \a
4617 polygon, starting at \a{polygon}\e{[index]} (\a index defaults to
4618 0). If \a count is -1 (the default) all points until the end of
4621 Use drawLines() combined with QPolygon::constData() instead.
4624 QPainter painter(this);
4625 painter.drawLineSegments(polygon, index, count);
4627 int lineCount = (count == -1) ? (polygon.size() - index) / 2 : count;
4629 QPainter painter(this);
4630 painter.drawLines(polygon.constData() + index * 2, lineCount);
4634 void QPainter::drawLineSegments(const QPolygon &a, int index, int nlines)
4636 #ifdef QT_DEBUG_DRAW
4637 if (qt_show_painter_debug_output)
4638 printf("QPainter::drawLineSegments(), count=%d\n", a.size()/2);
4646 nlines = a.size()/2 - index/2;
4647 if (index + nlines*2 > (int)a.size())
4648 nlines = (a.size() - index)/2;
4649 if (nlines < 1 || index < 0)
4653 // FALCON: Use QVectorPath
4654 QVector<QLineF> lines;
4655 for (int i=index; i<index + nlines*2; i+=2)
4656 lines << QLineF(a.at(i), a.at(i+1));
4657 d->extended->drawLines(lines.data(), lines.size());
4661 d->updateState(d->state);
4663 QVector<QLineF> lines;
4664 if (d->state->emulationSpecifier) {
4665 if (d->state->emulationSpecifier == QPaintEngine::PrimitiveTransform
4666 && d->state->matrix.type() == QTransform::TxTranslate) {
4667 QPointF offset(d->state->matrix.dx(), d->state->matrix.dy());
4668 for (int i=index; i<index + nlines*2; i+=2)
4669 lines << QLineF(a.at(i) + offset, a.at(i+1) + offset);
4671 QPainterPath linesPath;
4672 for (int i=index; i<index + nlines*2; i+=2) {
4673 linesPath.moveTo(a.at(i));
4674 linesPath.lineTo(a.at(i+1));
4676 d->draw_helper(linesPath, QPainterPrivate::StrokeDraw);
4680 for (int i=index; i<index + nlines*2; i+=2)
4681 lines << QLineF(a.at(i), a.at(i+1));
4684 d->engine->drawLines(lines.data(), lines.size());
4686 #endif // QT3_SUPPORT
4689 Draws the first \a lineCount lines in the array \a lines
4690 using the current pen.
4692 \sa drawLine(), drawPolyline()
4694 void QPainter::drawLines(const QLineF *lines, int lineCount)
4696 #ifdef QT_DEBUG_DRAW
4697 if (qt_show_painter_debug_output)
4698 printf("QPainter::drawLines(), line count=%d\n", lineCount);
4703 if (!d->engine || lineCount < 1)
4707 d->extended->drawLines(lines, lineCount);
4711 d->updateState(d->state);
4713 uint lineEmulation = line_emulation(d->state->emulationSpecifier);
4715 if (lineEmulation) {
4716 if (lineEmulation == QPaintEngine::PrimitiveTransform
4717 && d->state->matrix.type() == QTransform::TxTranslate) {
4718 for (int i = 0; i < lineCount; ++i) {
4719 QLineF line = lines[i];
4720 line.translate(d->state->matrix.dx(), d->state->matrix.dy());
4721 d->engine->drawLines(&line, 1);
4724 QPainterPath linePath;
4725 for (int i = 0; i < lineCount; ++i) {
4726 linePath.moveTo(lines[i].p1());
4727 linePath.lineTo(lines[i].p2());
4729 d->draw_helper(linePath, QPainterPrivate::StrokeDraw);
4733 d->engine->drawLines(lines, lineCount);
4737 \fn void QPainter::drawLines(const QLine *lines, int lineCount)
4740 Draws the first \a lineCount lines in the array \a lines
4741 using the current pen.
4743 void QPainter::drawLines(const QLine *lines, int lineCount)
4745 #ifdef QT_DEBUG_DRAW
4746 if (qt_show_painter_debug_output)
4747 printf("QPainter::drawLine(), line count=%d\n", lineCount);
4752 if (!d->engine || lineCount < 1)
4756 d->extended->drawLines(lines, lineCount);
4760 d->updateState(d->state);
4762 uint lineEmulation = line_emulation(d->state->emulationSpecifier);
4764 if (lineEmulation) {
4765 if (lineEmulation == QPaintEngine::PrimitiveTransform
4766 && d->state->matrix.type() == QTransform::TxTranslate) {
4767 for (int i = 0; i < lineCount; ++i) {
4768 QLineF line = lines[i];
4769 line.translate(d->state->matrix.dx(), d->state->matrix.dy());
4770 d->engine->drawLines(&line, 1);
4773 QPainterPath linePath;
4774 for (int i = 0; i < lineCount; ++i) {
4775 linePath.moveTo(lines[i].p1());
4776 linePath.lineTo(lines[i].p2());
4778 d->draw_helper(linePath, QPainterPrivate::StrokeDraw);
4782 d->engine->drawLines(lines, lineCount);
4788 Draws the first \a lineCount lines in the array \a pointPairs
4789 using the current pen. The lines are specified as pairs of points
4790 so the number of entries in \a pointPairs must be at least \a
4793 void QPainter::drawLines(const QPointF *pointPairs, int lineCount)
4795 Q_ASSERT(sizeof(QLineF) == 2*sizeof(QPointF));
4797 drawLines((QLineF*)pointPairs, lineCount);
4803 Draws the first \a lineCount lines in the array \a pointPairs
4804 using the current pen.
4806 void QPainter::drawLines(const QPoint *pointPairs, int lineCount)
4808 Q_ASSERT(sizeof(QLine) == 2*sizeof(QPoint));
4810 drawLines((QLine*)pointPairs, lineCount);
4815 \fn void QPainter::drawLines(const QVector<QPointF> &pointPairs)
4818 Draws a line for each pair of points in the vector \a pointPairs
4819 using the current pen. If there is an odd number of points in the
4820 array, the last point will be ignored.
4824 \fn void QPainter::drawLines(const QVector<QPoint> &pointPairs)
4827 Draws a line for each pair of points in the vector \a pointPairs
4828 using the current pen.
4832 \fn void QPainter::drawLines(const QVector<QLineF> &lines)
4835 Draws the set of lines defined by the list \a lines using the
4836 current pen and brush.
4840 \fn void QPainter::drawLines(const QVector<QLine> &lines)
4843 Draws the set of lines defined by the list \a lines using the
4844 current pen and brush.
4848 Draws the polyline defined by the first \a pointCount points in \a
4849 points using the current pen.
4851 Note that unlike the drawPolygon() function the last point is \e
4852 not connected to the first, neither is the polyline filled.
4857 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 13
4860 \sa drawLines(), drawPolygon(), {Coordinate System}
4862 void QPainter::drawPolyline(const QPointF *points, int pointCount)
4864 #ifdef QT_DEBUG_DRAW
4865 if (qt_show_painter_debug_output)
4866 printf("QPainter::drawPolyline(), count=%d\n", pointCount);
4870 if (!d->engine || pointCount < 2)
4874 d->extended->drawPolygon(points, pointCount, QPaintEngine::PolylineMode);
4878 d->updateState(d->state);
4880 uint lineEmulation = line_emulation(d->state->emulationSpecifier);
4882 if (lineEmulation) {
4884 // if (lineEmulation == QPaintEngine::PrimitiveTransform
4885 // && d->state->matrix.type() == QTransform::TxTranslate) {
4887 QPainterPath polylinePath(points[0]);
4888 for (int i=1; i<pointCount; ++i)
4889 polylinePath.lineTo(points[i]);
4890 d->draw_helper(polylinePath, QPainterPrivate::StrokeDraw);
4893 d->engine->drawPolygon(points, pointCount, QPaintEngine::PolylineMode);
4900 Draws the polyline defined by the first \a pointCount points in \a
4901 points using the current pen.
4903 void QPainter::drawPolyline(const QPoint *points, int pointCount)
4905 #ifdef QT_DEBUG_DRAW
4906 if (qt_show_painter_debug_output)
4907 printf("QPainter::drawPolyline(), count=%d\n", pointCount);
4911 if (!d->engine || pointCount < 2)
4915 d->extended->drawPolygon(points, pointCount, QPaintEngine::PolylineMode);
4919 d->updateState(d->state);
4921 uint lineEmulation = line_emulation(d->state->emulationSpecifier);
4923 if (lineEmulation) {
4925 // if (lineEmulation == QPaintEngine::PrimitiveTransform
4926 // && d->state->matrix.type() == QTransform::TxTranslate) {
4928 QPainterPath polylinePath(points[0]);
4929 for (int i=1; i<pointCount; ++i)
4930 polylinePath.lineTo(points[i]);
4931 d->draw_helper(polylinePath, QPainterPrivate::StrokeDraw);
4934 d->engine->drawPolygon(points, pointCount, QPaintEngine::PolylineMode);
4939 \fn void QPainter::drawPolyline(const QPolygon &polygon, int index, int
4945 Draws the polyline defined by the \a count lines of the given \a
4946 polygon starting at \a index (\a index defaults to 0).
4948 Use drawPolyline() combined with QPolygon::constData() instead.
4951 QPainter painter(this);
4952 painter.drawPolyline(polygon, index, count);
4954 int pointCount = (count == -1) ? polygon.size() - index : count;
4956 QPainter painter(this);
4957 painter.drawPolyline(polygon.constData() + index, pointCount);
4962 \fn void QPainter::drawPolyline(const QPolygonF &points)
4966 Draws the polyline defined by the given \a points using the
4971 \fn void QPainter::drawPolyline(const QPolygon &points)
4975 Draws the polyline defined by the given \a points using the
4980 Draws the polygon defined by the first \a pointCount points in the
4981 array \a points using the current pen and brush.
4985 \o \inlineimage qpainter-polygon.png
4987 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 14
4990 The first point is implicitly connected to the last point, and the
4991 polygon is filled with the current brush().
4993 If \a fillRule is Qt::WindingFill, the polygon is filled using the
4994 winding fill algorithm. If \a fillRule is Qt::OddEvenFill, the
4995 polygon is filled using the odd-even fill algorithm. See
4996 \l{Qt::FillRule} for a more detailed description of these fill
4999 \sa drawConvexPolygon(), drawPolyline(), {Coordinate System}
5001 void QPainter::drawPolygon(const QPointF *points, int pointCount, Qt::FillRule fillRule)
5003 #ifdef QT_DEBUG_DRAW
5004 if (qt_show_painter_debug_output)
5005 printf("QPainter::drawPolygon(), count=%d\n", pointCount);
5010 if (!d->engine || pointCount < 2)
5014 d->extended->drawPolygon(points, pointCount, QPaintEngine::PolygonDrawMode(fillRule));
5018 d->updateState(d->state);
5020 uint emulationSpecifier = d->state->emulationSpecifier;
5022 if (emulationSpecifier) {
5023 QPainterPath polygonPath(points[0]);
5024 for (int i=1; i<pointCount; ++i)
5025 polygonPath.lineTo(points[i]);
5026 polygonPath.closeSubpath();
5027 polygonPath.setFillRule(fillRule);
5028 d->draw_helper(polygonPath);
5032 d->engine->drawPolygon(points, pointCount, QPaintEngine::PolygonDrawMode(fillRule));
5037 Draws the polygon defined by the first \a pointCount points in the
5040 void QPainter::drawPolygon(const QPoint *points, int pointCount, Qt::FillRule fillRule)
5042 #ifdef QT_DEBUG_DRAW
5043 if (qt_show_painter_debug_output)
5044 printf("QPainter::drawPolygon(), count=%d\n", pointCount);
5049 if (!d->engine || pointCount < 2)
5053 d->extended->drawPolygon(points, pointCount, QPaintEngine::PolygonDrawMode(fillRule));
5057 d->updateState(d->state);
5059 uint emulationSpecifier = d->state->emulationSpecifier;
5061 if (emulationSpecifier) {
5062 QPainterPath polygonPath(points[0]);
5063 for (int i=1; i<pointCount; ++i)
5064 polygonPath.lineTo(points[i]);
5065 polygonPath.closeSubpath();
5066 polygonPath.setFillRule(fillRule);
5067 d->draw_helper(polygonPath);
5071 d->engine->drawPolygon(points, pointCount, QPaintEngine::PolygonDrawMode(fillRule));
5074 /*! \fn void QPainter::drawPolygon(const QPolygonF &polygon, bool winding, int index = 0,
5079 Use drawPolygon() combined with QPolygonF::constData() instead.
5082 QPainter painter(this);
5083 painter.drawPolygon(polygon, winding, index, count);
5085 int pointCount = (count == -1) ? polygon.size() - index : count;
5086 int fillRule = winding ? Qt::WindingFill : Qt::OddEvenFill;
5088 QPainter painter(this);
5089 painter.drawPolygon( polygon.constData() + index, pointCount, fillRule);
5093 /*! \fn void QPainter::drawPolygon(const QPolygon &polygon, bool winding,
5094 int index = 0, int count = -1)
5099 Use drawPolygon() combined with QPolygon::constData() instead.
5102 QPainter painter(this);
5103 painter.drawPolygon(polygon, winding, index, count);
5105 int pointCount = (count == -1) ? polygon.size() - index : count;
5106 int fillRule = winding ? Qt::WindingFill : Qt::OddEvenFill;
5108 QPainter painter(this);
5109 painter.drawPolygon( polygon.constData() + index, pointCount, fillRule);
5113 /*! \fn void QPainter::drawPolygon(const QPolygonF &points, Qt::FillRule fillRule)
5117 Draws the polygon defined by the given \a points using the fill
5121 /*! \fn void QPainter::drawPolygon(const QPolygon &points, Qt::FillRule fillRule)
5125 Draws the polygon defined by the given \a points using the fill
5130 \fn void QPainter::drawConvexPolygon(const QPointF *points, int pointCount)
5132 Draws the convex polygon defined by the first \a pointCount points
5133 in the array \a points using the current pen.
5137 \o \inlineimage qpainter-polygon.png
5139 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 15
5142 The first point is implicitly connected to the last point, and the
5143 polygon is filled with the current brush(). If the supplied
5144 polygon is not convex, i.e. it contains at least one angle larger
5145 than 180 degrees, the results are undefined.
5147 On some platforms (e.g. X11), the drawConvexPolygon() function can
5148 be faster than the drawPolygon() function.
5150 \sa drawPolygon(), drawPolyline(), {Coordinate System}
5154 \fn void QPainter::drawConvexPolygon(const QPoint *points, int pointCount)
5157 Draws the convex polygon defined by the first \a pointCount points
5158 in the array \a points using the current pen.
5162 \fn void QPainter::drawConvexPolygon(const QPolygonF &polygon)
5166 Draws the convex polygon defined by \a polygon using the current
5171 \fn void QPainter::drawConvexPolygon(const QPolygon &polygon)
5174 Draws the convex polygon defined by \a polygon using the current
5179 \fn void QPainter::drawConvexPolygon(const QPolygonF &polygon, int
5185 Use drawConvexPolygon() combined with QPolygonF::constData()
5189 QPainter painter(this);
5190 painter.drawConvexPolygon(polygon, index, count);
5192 int pointCount = (count == -1) ? polygon.size() - index : count;
5194 QPainter painter(this);
5195 painter.drawConvexPolygon(polygon.constData() + index, pointCount);
5200 \fn void QPainter::drawConvexPolygon(const QPolygon &polygon, int
5206 Use drawConvexPolygon() combined with QPolygon::constData()
5210 QPainter painter(this);
5211 painter.drawConvexPolygon(polygon, index, count);
5213 int pointCount = (count == -1) ? polygon.size() - index : count;
5215 QPainter painter(this);
5216 painter.drawConvexPolygon(polygon.constData() + index, pointCount);
5220 void QPainter::drawConvexPolygon(const QPoint *points, int pointCount)
5222 #ifdef QT_DEBUG_DRAW
5223 if (qt_show_painter_debug_output)
5224 printf("QPainter::drawConvexPolygon(), count=%d\n", pointCount);
5229 if (!d->engine || pointCount < 2)
5233 d->extended->drawPolygon(points, pointCount, QPaintEngine::ConvexMode);
5237 d->updateState(d->state);
5239 uint emulationSpecifier = d->state->emulationSpecifier;
5241 if (emulationSpecifier) {
5242 QPainterPath polygonPath(points[0]);
5243 for (int i=1; i<pointCount; ++i)
5244 polygonPath.lineTo(points[i]);
5245 polygonPath.closeSubpath();
5246 polygonPath.setFillRule(Qt::WindingFill);
5247 d->draw_helper(polygonPath);
5251 d->engine->drawPolygon(points, pointCount, QPaintEngine::ConvexMode);
5254 void QPainter::drawConvexPolygon(const QPointF *points, int pointCount)
5256 #ifdef QT_DEBUG_DRAW
5257 if (qt_show_painter_debug_output)
5258 printf("QPainter::drawConvexPolygon(), count=%d\n", pointCount);
5263 if (!d->engine || pointCount < 2)
5267 d->extended->drawPolygon(points, pointCount, QPaintEngine::ConvexMode);
5271 d->updateState(d->state);
5273 uint emulationSpecifier = d->state->emulationSpecifier;
5275 if (emulationSpecifier) {
5276 QPainterPath polygonPath(points[0]);
5277 for (int i=1; i<pointCount; ++i)
5278 polygonPath.lineTo(points[i]);
5279 polygonPath.closeSubpath();
5280 polygonPath.setFillRule(Qt::WindingFill);
5281 d->draw_helper(polygonPath);
5285 d->engine->drawPolygon(points, pointCount, QPaintEngine::ConvexMode);
5288 static inline QPointF roundInDeviceCoordinates(const QPointF &p, const QTransform &m)
5290 return m.inverted().map(QPointF(m.map(p).toPoint()));
5294 \fn void QPainter::drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
5296 Draws the rectangular portion \a source of the given \a pixmap
5297 into the given \a target in the paint device.
5299 \note The pixmap is scaled to fit the rectangle, if both the pixmap and rectangle size disagree.
5304 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 16
5307 If \a pixmap is a QBitmap it is drawn with the bits that are "set"
5308 using the pens color. If backgroundMode is Qt::OpaqueMode, the
5309 "unset" bits are drawn using the color of the background brush; if
5310 backgroundMode is Qt::TransparentMode, the "unset" bits are
5311 transparent. Drawing bitmaps with gradient or texture colors is
5316 void QPainter::drawPixmap(const QPointF &p, const QPixmap &pm)
5318 #if defined QT_DEBUG_DRAW
5319 if (qt_show_painter_debug_output)
5320 printf("QPainter::drawPixmap(), p=[%.2f,%.2f], pix=[%d,%d]\n",
5322 pm.width(), pm.height());
5327 if (!d->engine || pm.isNull())
5331 qt_painter_thread_test(d->device->devType(), "drawPixmap()", true);
5335 d->extended->drawPixmap(p, pm);
5343 int h = pm.height();
5348 // Emulate opaque background for bitmaps
5349 if (d->state->bgMode == Qt::OpaqueMode && pm.isQBitmap()) {
5350 fillRect(QRectF(x, y, w, h), d->state->bgBrush.color());
5353 d->updateState(d->state);
5355 if ((d->state->matrix.type() > QTransform::TxTranslate
5356 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
5357 || (!d->state->matrix.isAffine() && !d->engine->hasFeature(QPaintEngine::PerspectiveTransform))
5358 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity)))
5361 // If there is no rotation involved we have to make sure we use the
5362 // antialiased and not the aliased coordinate system by rounding the coordinates.
5363 if (d->state->matrix.type() <= QTransform::TxScale) {
5364 const QPointF p = roundInDeviceCoordinates(QPointF(x, y), d->state->matrix);
5369 setBackgroundMode(Qt::TransparentMode);
5370 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
5371 QBrush brush(d->state->pen.color(), pm);
5374 setBrushOrigin(QPointF(0, 0));
5376 drawRect(pm.rect());
5379 if (!d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
5380 x += d->state->matrix.dx();
5381 y += d->state->matrix.dy();
5383 d->engine->drawPixmap(QRectF(x, y, w, h), pm, QRectF(0, 0, w, h));
5387 void QPainter::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
5389 #if defined QT_DEBUG_DRAW
5390 if (qt_show_painter_debug_output)
5391 printf("QPainter::drawPixmap(), target=[%.2f,%.2f,%.2f,%.2f], pix=[%d,%d], source=[%.2f,%.2f,%.2f,%.2f]\n",
5392 r.x(), r.y(), r.width(), r.height(),
5393 pm.width(), pm.height(),
5394 sr.x(), sr.y(), sr.width(), sr.height());
5398 if (!d->engine || pm.isNull())
5401 qt_painter_thread_test(d->device->devType(), "drawPixmap()", true);
5406 qreal w = r.width();
5407 qreal h = r.height();
5410 qreal sw = sr.width();
5411 qreal sh = sr.height();
5413 // Sanity-check clipping
5415 sw = pm.width() - sx;
5418 sh = pm.height() - sy;
5426 qreal w_ratio = sx * w/sw;
5434 qreal h_ratio = sy * h/sh;
5441 if (sw + sx > pm.width()) {
5442 qreal delta = sw - (pm.width() - sx);
5443 qreal w_ratio = delta * w/sw;
5448 if (sh + sy > pm.height()) {
5449 qreal delta = sh - (pm.height() - sy);
5450 qreal h_ratio = delta * h/sh;
5455 if (w == 0 || h == 0 || sw <= 0 || sh <= 0)
5459 d->extended->drawPixmap(QRectF(x, y, w, h), pm, QRectF(sx, sy, sw, sh));
5463 // Emulate opaque background for bitmaps
5464 if (d->state->bgMode == Qt::OpaqueMode && pm.isQBitmap())
5465 fillRect(QRectF(x, y, w, h), d->state->bgBrush.color());
5467 d->updateState(d->state);
5469 if ((d->state->matrix.type() > QTransform::TxTranslate
5470 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
5471 || (!d->state->matrix.isAffine() && !d->engine->hasFeature(QPaintEngine::PerspectiveTransform))
5472 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity))
5473 || ((sw != w || sh != h) && !d->engine->hasFeature(QPaintEngine::PixmapTransform)))
5476 // If there is no rotation involved we have to make sure we use the
5477 // antialiased and not the aliased coordinate system by rounding the coordinates.
5478 if (d->state->matrix.type() <= QTransform::TxScale) {
5479 const QPointF p = roundInDeviceCoordinates(QPointF(x, y), d->state->matrix);
5484 if (d->state->matrix.type() <= QTransform::TxTranslate && sw == w && sh == h) {
5492 scale(w / sw, h / sh);
5493 setBackgroundMode(Qt::TransparentMode);
5494 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
5497 if (sw == pm.width() && sh == pm.height())
5498 brush = QBrush(d->state->pen.color(), pm);
5500 brush = QBrush(d->state->pen.color(), pm.copy(sx, sy, sw, sh));
5505 drawRect(QRectF(0, 0, sw, sh));
5508 if (!d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
5509 x += d->state->matrix.dx();
5510 y += d->state->matrix.dy();
5512 d->engine->drawPixmap(QRectF(x, y, w, h), pm, QRectF(sx, sy, sw, sh));
5518 \fn void QPainter::drawPixmap(const QRect &target, const QPixmap &pixmap,
5519 const QRect &source)
5522 Draws the rectangular portion \a source of the given \a pixmap
5523 into the given \a target in the paint device.
5525 \note The pixmap is scaled to fit the rectangle, if both the pixmap and rectangle size disagree.
5529 \fn void QPainter::drawPixmap(const QPointF &point, const QPixmap &pixmap,
5530 const QRectF &source)
5533 Draws the rectangular portion \a source of the given \a pixmap
5534 with its origin at the given \a point.
5538 \fn void QPainter::drawPixmap(const QPoint &point, const QPixmap &pixmap,
5539 const QRect &source)
5543 Draws the rectangular portion \a source of the given \a pixmap
5544 with its origin at the given \a point.
5548 \fn void QPainter::drawPixmap(const QPointF &point, const QPixmap &pixmap)
5551 Draws the given \a pixmap with its origin at the given \a point.
5555 \fn void QPainter::drawPixmap(const QPoint &point, const QPixmap &pixmap)
5558 Draws the given \a pixmap with its origin at the given \a point.
5562 \fn void QPainter::drawPixmap(int x, int y, const QPixmap &pixmap)
5566 Draws the given \a pixmap at position (\a{x}, \a{y}).
5570 \fn void QPainter::drawPixmap(const QRect &rectangle, const QPixmap &pixmap)
5573 Draws the given \a pixmap into the given \a rectangle.
5575 \note The pixmap is scaled to fit the rectangle, if both the pixmap and rectangle size disagree.
5579 \fn void QPainter::drawPixmap(int x, int y, int width, int height,
5580 const QPixmap &pixmap)
5584 Draws the \a pixmap into the rectangle at position (\a{x}, \a{y})
5585 with the given \a width and \a height.
5589 \fn void QPainter::drawPixmap(int x, int y, int w, int h, const QPixmap &pixmap,
5590 int sx, int sy, int sw, int sh)
5594 Draws the rectangular portion with the origin (\a{sx}, \a{sy}),
5595 width \a sw and height \a sh, of the given \a pixmap , at the
5596 point (\a{x}, \a{y}), with a width of \a w and a height of \a h.
5597 If sw or sh are equal to zero the width/height of the pixmap
5598 is used and adjusted by the offset sx/sy;
5602 \fn void QPainter::drawPixmap(int x, int y, const QPixmap &pixmap,
5603 int sx, int sy, int sw, int sh)
5607 Draws a pixmap at (\a{x}, \a{y}) by copying a part of the given \a
5608 pixmap into the paint device.
5610 (\a{x}, \a{y}) specifies the top-left point in the paint device that is
5611 to be drawn onto. (\a{sx}, \a{sy}) specifies the top-left point in \a
5612 pixmap that is to be drawn. The default is (0, 0).
5614 (\a{sw}, \a{sh}) specifies the size of the pixmap that is to be drawn.
5615 The default, (0, 0) (and negative) means all the way to the
5616 bottom-right of the pixmap.
5619 void QPainter::drawImage(const QPointF &p, const QImage &image)
5623 if (!d->engine || image.isNull())
5627 d->extended->drawImage(p, image);
5634 int w = image.width();
5635 int h = image.height();
5637 d->updateState(d->state);
5639 if (((d->state->matrix.type() > QTransform::TxTranslate)
5640 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
5641 || (!d->state->matrix.isAffine() && !d->engine->hasFeature(QPaintEngine::PerspectiveTransform))
5642 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity)))
5645 // If there is no rotation involved we have to make sure we use the
5646 // antialiased and not the aliased coordinate system by rounding the coordinates.
5647 if (d->state->matrix.type() <= QTransform::TxScale) {
5648 const QPointF p = roundInDeviceCoordinates(QPointF(x, y), d->state->matrix);
5653 setBackgroundMode(Qt::TransparentMode);
5654 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
5655 QBrush brush(image);
5658 setBrushOrigin(QPointF(0, 0));
5660 drawRect(image.rect());
5665 if (d->state->matrix.type() == QTransform::TxTranslate
5666 && !d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
5667 x += d->state->matrix.dx();
5668 y += d->state->matrix.dy();
5671 d->engine->drawImage(QRectF(x, y, w, h), image, QRectF(0, 0, w, h), Qt::AutoColor);
5674 void QPainter::drawImage(const QRectF &targetRect, const QImage &image, const QRectF &sourceRect,
5675 Qt::ImageConversionFlags flags)
5679 if (!d->engine || image.isNull())
5682 qreal x = targetRect.x();
5683 qreal y = targetRect.y();
5684 qreal w = targetRect.width();
5685 qreal h = targetRect.height();
5686 qreal sx = sourceRect.x();
5687 qreal sy = sourceRect.y();
5688 qreal sw = sourceRect.width();
5689 qreal sh = sourceRect.height();
5691 // Sanity-check clipping
5693 sw = image.width() - sx;
5696 sh = image.height() - sy;
5704 qreal w_ratio = sx * w/sw;
5712 qreal h_ratio = sy * h/sh;
5719 if (sw + sx > image.width()) {
5720 qreal delta = sw - (image.width() - sx);
5721 qreal w_ratio = delta * w/sw;
5726 if (sh + sy > image.height()) {
5727 qreal delta = sh - (image.height() - sy);
5728 qreal h_ratio = delta * h/sh;
5733 if (w == 0 || h == 0 || sw <= 0 || sh <= 0)
5737 d->extended->drawImage(QRectF(x, y, w, h), image, QRectF(sx, sy, sw, sh), flags);
5741 d->updateState(d->state);
5743 if (((d->state->matrix.type() > QTransform::TxTranslate || (sw != w || sh != h))
5744 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
5745 || (!d->state->matrix.isAffine() && !d->engine->hasFeature(QPaintEngine::PerspectiveTransform))
5746 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity)))
5749 // If there is no rotation involved we have to make sure we use the
5750 // antialiased and not the aliased coordinate system by rounding the coordinates.
5751 if (d->state->matrix.type() <= QTransform::TxScale) {
5752 const QPointF p = roundInDeviceCoordinates(QPointF(x, y), d->state->matrix);
5757 if (d->state->matrix.type() <= QTransform::TxTranslate && sw == w && sh == h) {
5764 scale(w / sw, h / sh);
5765 setBackgroundMode(Qt::TransparentMode);
5766 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
5767 QBrush brush(image);
5770 setBrushOrigin(QPointF(-sx, -sy));
5772 drawRect(QRectF(0, 0, sw, sh));
5777 if (d->state->matrix.type() == QTransform::TxTranslate
5778 && !d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
5779 x += d->state->matrix.dx();
5780 y += d->state->matrix.dy();
5783 d->engine->drawImage(QRectF(x, y, w, h), image, QRectF(sx, sy, sw, sh), flags);
5787 Draws the glyphs represented by \a glyphs at \a position. The \a position gives the
5788 edge of the baseline for the string of glyphs. The glyphs will be retrieved from the font
5789 selected on \a glyphs and at offsets given by the positions in \a glyphs.
5793 \sa QGlyphRun::setRawFont(), QGlyphRun::setPositions(), QGlyphRun::setGlyphIndexes()
5795 #if !defined(QT_NO_RAWFONT)
5796 void QPainter::drawGlyphRun(const QPointF &position, const QGlyphRun &glyphRun)
5800 QRawFont font = glyphRun.rawFont();
5801 if (!font.isValid())
5804 QGlyphRunPrivate *glyphRun_d = QGlyphRunPrivate::get(glyphRun);
5806 const quint32 *glyphIndexes = glyphRun_d->glyphIndexData;
5807 const QPointF *glyphPositions = glyphRun_d->glyphPositionData;
5809 int count = qMin(glyphRun_d->glyphIndexDataSize, glyphRun_d->glyphPositionDataSize);
5810 QVarLengthArray<QFixedPoint, 128> fixedPointPositions(count);
5812 QRawFontPrivate *fontD = QRawFontPrivate::get(font);
5813 bool supportsTransformations;
5814 if (d->extended != 0) {
5815 supportsTransformations = d->extended->supportsTransformations(fontD->fontEngine->fontDef.pixelSize,
5818 supportsTransformations = d->engine->type() == QPaintEngine::CoreGraphics
5819 || d->state->matrix.isAffine();
5822 for (int i=0; i<count; ++i) {
5823 QPointF processedPosition = position + glyphPositions[i];
5824 if (!supportsTransformations)
5825 processedPosition = d->state->transform().map(processedPosition);
5826 fixedPointPositions[i] = QFixedPoint::fromPointF(processedPosition);
5829 d->drawGlyphs(glyphIndexes, fixedPointPositions.data(), count, font, glyphRun.overline(),
5830 glyphRun.underline(), glyphRun.strikeOut());
5833 void QPainterPrivate::drawGlyphs(const quint32 *glyphArray, QFixedPoint *positions,
5835 const QRawFont &font, bool overline, bool underline,
5842 QRawFontPrivate *fontD = QRawFontPrivate::get(font);
5843 QFontEngine *fontEngine = fontD->fontEngine;
5848 for (int i=0; i<glyphCount; ++i) {
5849 glyph_metrics_t gm = fontEngine->boundingBox(glyphArray[i]);
5850 if (i == 0 || leftMost > positions[i].x)
5851 leftMost = positions[i].x;
5853 // We don't support glyphs that do not share a common baseline. If this turns out to
5854 // be a relevant use case, then we need to find clusters of glyphs that share a baseline
5855 // and do a drawTextItemDecorations call per cluster.
5856 if (i == 0 || baseLine < positions[i].y)
5857 baseLine = positions[i].y;
5859 // We use the advance rather than the actual bounds to match the algorithm in drawText()
5860 if (i == 0 || rightMost < positions[i].x + gm.xoff)
5861 rightMost = positions[i].x + gm.xoff;
5864 QFixed width = rightMost - leftMost;
5866 if (extended != 0 && state->matrix.isAffine()) {
5867 QStaticTextItem staticTextItem;
5868 staticTextItem.color = state->pen.color();
5869 staticTextItem.font = state->font;
5870 staticTextItem.setFontEngine(fontEngine);
5871 staticTextItem.numGlyphs = glyphCount;
5872 staticTextItem.glyphs = reinterpret_cast<glyph_t *>(const_cast<glyph_t *>(glyphArray));
5873 staticTextItem.glyphPositions = positions;
5875 extended->drawStaticTextItem(&staticTextItem);
5877 QTextItemInt textItem;
5878 textItem.fontEngine = fontEngine;
5880 QVarLengthArray<QFixed, 128> advances(glyphCount);
5881 QVarLengthArray<QGlyphJustification, 128> glyphJustifications(glyphCount);
5882 QVarLengthArray<HB_GlyphAttributes, 128> glyphAttributes(glyphCount);
5883 qMemSet(glyphAttributes.data(), 0, glyphAttributes.size() * sizeof(HB_GlyphAttributes));
5884 qMemSet(advances.data(), 0, advances.size() * sizeof(QFixed));
5885 qMemSet(glyphJustifications.data(), 0, glyphJustifications.size() * sizeof(QGlyphJustification));
5887 textItem.glyphs.numGlyphs = glyphCount;
5888 textItem.glyphs.glyphs = reinterpret_cast<HB_Glyph *>(const_cast<quint32 *>(glyphArray));
5889 textItem.glyphs.offsets = positions;
5890 textItem.glyphs.advances_x = advances.data();
5891 textItem.glyphs.advances_y = advances.data();
5892 textItem.glyphs.justifications = glyphJustifications.data();
5893 textItem.glyphs.attributes = glyphAttributes.data();
5895 engine->drawTextItem(QPointF(0, 0), textItem);
5898 QTextItemInt::RenderFlags flags;
5900 flags |= QTextItemInt::Underline;
5902 flags |= QTextItemInt::Overline;
5904 flags |= QTextItemInt::StrikeOut;
5906 drawTextItemDecoration(q, QPointF(leftMost.toReal(), baseLine.toReal()),
5909 ? QTextCharFormat::SingleUnderline
5910 : QTextCharFormat::NoUnderline),
5911 flags, width.toReal(), QTextCharFormat());
5913 #endif // QT_NO_RAWFONT
5917 \fn void QPainter::drawStaticText(const QPoint &topLeftPosition, const QStaticText &staticText)
5921 Draws the \a staticText at the \a topLeftPosition.
5923 \note The y-position is used as the top of the font.
5928 \fn void QPainter::drawStaticText(int left, int top, const QStaticText &staticText)
5932 Draws the \a staticText at coordinates \a left and \a top.
5934 \note The y-position is used as the top of the font.
5938 \fn void QPainter::drawText(const QPointF &position, const QString &text)
5940 Draws the given \a text with the currently defined text direction,
5941 beginning at the given \a position.
5943 This function does not handle the newline character (\n), as it cannot
5944 break text into multiple lines, and it cannot display the newline character.
5945 Use the QPainter::drawText() overload that takes a rectangle instead
5946 if you want to draw multiple lines of text with the newline character, or
5947 if you want the text to be wrapped.
5949 By default, QPainter draws text anti-aliased.
5951 \note The y-position is used as the baseline of the font.
5954 void QPainter::drawText(const QPointF &p, const QString &str)
5956 drawText(p, str, 0, 0);
5962 Draws the given \a staticText at the given \a topLeftPosition.
5964 The text will be drawn using the font and the transformation set on the painter. If the
5965 font and/or transformation set on the painter are different from the ones used to initialize
5966 the layout of the QStaticText, then the layout will have to be recalculated. Use
5967 QStaticText::prepare() to initialize \a staticText with the font and transformation with which
5968 it will later be drawn.
5970 If \a topLeftPosition is not the same as when \a staticText was initialized, or when it was
5971 last drawn, then there will be a slight overhead when translating the text to its new position.
5973 \note If the painter's transformation is not affine, then \a staticText will be drawn using
5974 regular calls to drawText(), losing any potential for performance improvement.
5976 \note The y-position is used as the top of the font.
5980 void QPainter::drawStaticText(const QPointF &topLeftPosition, const QStaticText &staticText)
5983 if (!d->engine || staticText.text().isEmpty() || pen().style() == Qt::NoPen)
5986 QStaticTextPrivate *staticText_d =
5987 const_cast<QStaticTextPrivate *>(QStaticTextPrivate::get(&staticText));
5989 if (font() != staticText_d->font) {
5990 staticText_d->font = font();
5991 staticText_d->needsRelayout = true;
5994 // If we don't have an extended paint engine, or if the painter is projected,
5995 // we go through standard code path
5996 if (d->extended == 0 || !d->state->matrix.isAffine()) {
5997 staticText_d->paintText(topLeftPosition, this);
6001 bool supportsTransformations = d->extended->supportsTransformations(staticText_d->font.pixelSize(),
6003 if (supportsTransformations && !staticText_d->untransformedCoordinates) {
6004 staticText_d->untransformedCoordinates = true;
6005 staticText_d->needsRelayout = true;
6006 } else if (!supportsTransformations && staticText_d->untransformedCoordinates) {
6007 staticText_d->untransformedCoordinates = false;
6008 staticText_d->needsRelayout = true;
6011 // Don't recalculate entire layout because of translation, rather add the dx and dy
6012 // into the position to move each text item the correct distance.
6013 QPointF transformedPosition = topLeftPosition;
6014 if (!staticText_d->untransformedCoordinates)
6015 transformedPosition = transformedPosition * d->state->matrix;
6016 QTransform oldMatrix;
6018 // The translation has been applied to transformedPosition. Remove translation
6019 // component from matrix.
6020 if (d->state->matrix.isTranslating() && !staticText_d->untransformedCoordinates) {
6021 qreal m11 = d->state->matrix.m11();
6022 qreal m12 = d->state->matrix.m12();
6023 qreal m13 = d->state->matrix.m13();
6024 qreal m21 = d->state->matrix.m21();
6025 qreal m22 = d->state->matrix.m22();
6026 qreal m23 = d->state->matrix.m23();
6027 qreal m33 = d->state->matrix.m33();
6029 oldMatrix = d->state->matrix;
6030 d->state->matrix.setMatrix(m11, m12, m13,
6035 // If the transform is not identical to the text transform,
6036 // we have to relayout the text (for other transformations than plain translation)
6037 bool staticTextNeedsReinit = staticText_d->needsRelayout;
6038 if (!staticText_d->untransformedCoordinates && staticText_d->matrix != d->state->matrix) {
6039 staticText_d->matrix = d->state->matrix;
6040 staticTextNeedsReinit = true;
6043 // Recreate the layout of the static text because the matrix or font has changed
6044 if (staticTextNeedsReinit)
6045 staticText_d->init();
6047 if (transformedPosition != staticText_d->position) { // Translate to actual position
6048 QFixed fx = QFixed::fromReal(transformedPosition.x());
6049 QFixed fy = QFixed::fromReal(transformedPosition.y());
6050 QFixed oldX = QFixed::fromReal(staticText_d->position.x());
6051 QFixed oldY = QFixed::fromReal(staticText_d->position.y());
6052 for (int item=0; item<staticText_d->itemCount;++item) {
6053 QStaticTextItem *textItem = staticText_d->items + item;
6054 for (int i=0; i<textItem->numGlyphs; ++i) {
6055 textItem->glyphPositions[i].x += fx - oldX;
6056 textItem->glyphPositions[i].y += fy - oldY;
6058 textItem->userDataNeedsUpdate = true;
6061 staticText_d->position = transformedPosition;
6064 QPen oldPen = d->state->pen;
6065 QColor currentColor = oldPen.color();
6066 for (int i=0; i<staticText_d->itemCount; ++i) {
6067 QStaticTextItem *item = staticText_d->items + i;
6068 if (item->color.isValid() && currentColor != item->color) {
6069 setPen(item->color);
6070 currentColor = item->color;
6072 d->extended->drawStaticTextItem(item);
6074 qt_draw_decoration_for_glyphs(this, item->glyphs, item->glyphPositions,
6075 item->numGlyphs, item->fontEngine(), staticText_d->font,
6078 if (currentColor != oldPen.color())
6081 if (!staticText_d->untransformedCoordinates && oldMatrix.isTranslating())
6082 d->state->matrix = oldMatrix;
6088 void QPainter::drawText(const QPointF &p, const QString &str, int tf, int justificationPadding)
6090 #ifdef QT_DEBUG_DRAW
6091 if (qt_show_painter_debug_output)
6092 printf("QPainter::drawText(), pos=[%.2f,%.2f], str='%s'\n", p.x(), p.y(), str.toLatin1().constData());
6097 if (!d->engine || str.isEmpty() || pen().style() == Qt::NoPen)
6100 if (tf & Qt::TextBypassShaping) {
6101 // Skip harfbuzz complex shaping, shape using glyph advances only
6102 int len = str.length();
6103 int numGlyphs = len;
6104 QVarLengthGlyphLayoutArray glyphs(len);
6105 QFontEngine *fontEngine = d->state->font.d->engineForScript(QUnicodeTables::Common);
6106 if (!fontEngine->stringToCMap(str.data(), len, &glyphs, &numGlyphs, 0)) {
6107 glyphs.resize(numGlyphs);
6108 if (!fontEngine->stringToCMap(str.data(), len, &glyphs, &numGlyphs, 0))
6109 Q_ASSERT_X(false, Q_FUNC_INFO, "stringToCMap shouldn't fail twice");
6112 QTextItemInt gf(glyphs, &d->state->font, str.data(), len, fontEngine);
6113 drawTextItem(p, gf);
6117 QStackTextEngine engine(str, d->state->font);
6118 engine.option.setTextDirection(d->state->layoutDirection);
6119 if (tf & (Qt::TextForceLeftToRight|Qt::TextForceRightToLeft)) {
6120 engine.ignoreBidi = true;
6121 engine.option.setTextDirection((tf & Qt::TextForceLeftToRight) ? Qt::LeftToRight : Qt::RightToLeft);
6125 line.length = str.length();
6126 engine.shapeLine(line);
6128 int nItems = engine.layoutData->items.size();
6129 QVarLengthArray<int> visualOrder(nItems);
6130 QVarLengthArray<uchar> levels(nItems);
6131 for (int i = 0; i < nItems; ++i)
6132 levels[i] = engine.layoutData->items[i].analysis.bidiLevel;
6133 QTextEngine::bidiReorder(nItems, levels.data(), visualOrder.data());
6135 if (justificationPadding > 0) {
6136 engine.option.setAlignment(Qt::AlignJustify);
6137 engine.forceJustification = true;
6138 // this works because justify() is only interested in the difference between width and textWidth
6139 line.width = justificationPadding;
6140 engine.justify(line);
6142 QFixed x = QFixed::fromReal(p.x());
6144 for (int i = 0; i < nItems; ++i) {
6145 int item = visualOrder[i];
6146 const QScriptItem &si = engine.layoutData->items.at(item);
6147 if (si.analysis.flags >= QScriptAnalysis::TabOrObject) {
6151 QFont f = engine.font(si);
6152 QTextItemInt gf(si, &f);
6153 gf.glyphs = engine.shapedGlyphs(&si);
6154 gf.chars = engine.layoutData->string.unicode() + si.position;
6155 gf.num_chars = engine.length(item);
6156 if (engine.forceJustification) {
6157 for (int j=0; j<gf.glyphs.numGlyphs; ++j)
6158 gf.width += gf.glyphs.effectiveAdvance(j);
6160 gf.width = si.width;
6162 gf.logClusters = engine.logClusters(&si);
6164 drawTextItem(QPointF(x.toReal(), p.y()), gf);
6170 void QPainter::drawText(const QRect &r, int flags, const QString &str, QRect *br)
6172 #ifdef QT_DEBUG_DRAW
6173 if (qt_show_painter_debug_output)
6174 printf("QPainter::drawText(), r=[%d,%d,%d,%d], flags=%d, str='%s'\n",
6175 r.x(), r.y(), r.width(), r.height(), flags, str.toLatin1().constData());
6180 if (!d->engine || str.length() == 0 || pen().style() == Qt::NoPen)
6184 d->updateState(d->state);
6187 qt_format_text(d->state->font, r, flags, 0, str, br ? &bounds : 0, 0, 0, 0, this);
6189 *br = bounds.toAlignedRect();
6193 \fn void QPainter::drawText(const QPoint &position, const QString &text)
6197 Draws the given \a text with the currently defined text direction,
6198 beginning at the given \a position.
6200 By default, QPainter draws text anti-aliased.
6202 \note The y-position is used as the baseline of the font.
6207 \fn void QPainter::drawText(const QRectF &rectangle, int flags, const QString &text, QRectF *boundingRect)
6210 Draws the given \a text within the provided \a rectangle.
6214 \o \inlineimage qpainter-text.png
6216 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 17
6219 The \a boundingRect (if not null) is set to the what the bounding rectangle
6220 should be in order to enclose the whole text. The \a flags argument is a bitwise
6221 OR of the following flags:
6233 \o Qt::TextSingleLine
6234 \o Qt::TextExpandTabs
6235 \o Qt::TextShowMnemonic
6237 \o Qt::TextIncludeTrailingSpaces
6240 \sa Qt::AlignmentFlag, Qt::TextFlag, boundingRect(), layoutDirection()
6242 By default, QPainter draws text anti-aliased.
6244 \note The y-coordinate of \a rectangle is used as the top of the font.
6246 void QPainter::drawText(const QRectF &r, int flags, const QString &str, QRectF *br)
6248 #ifdef QT_DEBUG_DRAW
6249 if (qt_show_painter_debug_output)
6250 printf("QPainter::drawText(), r=[%.2f,%.2f,%.2f,%.2f], flags=%d, str='%s'\n",
6251 r.x(), r.y(), r.width(), r.height(), flags, str.toLatin1().constData());
6256 if (!d->engine || str.length() == 0 || pen().style() == Qt::NoPen)
6260 d->updateState(d->state);
6262 qt_format_text(d->state->font, r, flags, 0, str, br, 0, 0, 0, this);
6266 \fn void QPainter::drawText(const QRect &rectangle, int flags, const QString &text, QRect *boundingRect)
6269 Draws the given \a text within the provided \a rectangle according
6270 to the specified \a flags. The \a boundingRect (if not null) is set to
6271 the what the bounding rectangle should be in order to enclose the whole text.
6273 By default, QPainter draws text anti-aliased.
6275 \note The y-coordinate of \a rectangle is used as the top of the font.
6279 \fn void QPainter::drawText(int x, int y, const QString &text)
6283 Draws the given \a text at position (\a{x}, \a{y}), using the painter's
6284 currently defined text direction.
6286 By default, QPainter draws text anti-aliased.
6288 \note The y-position is used as the baseline of the font.
6293 \fn void QPainter::drawText(int x, int y, int width, int height, int flags,
6294 const QString &text, QRect *boundingRect)
6298 Draws the given \a text within the rectangle with origin (\a{x},
6299 \a{y}), \a width and \a height.
6301 The \a boundingRect (if not null) is set to the actual bounding
6302 rectangle of the output. The \a flags argument is a bitwise OR of
6303 the following flags:
6314 \o Qt::TextSingleLine
6315 \o Qt::TextExpandTabs
6316 \o Qt::TextShowMnemonic
6320 By default, QPainter draws text anti-aliased.
6322 \note The y-position is used as the top of the font.
6324 \sa Qt::AlignmentFlag, Qt::TextFlag
6328 \fn void QPainter::drawText(const QRectF &rectangle, const QString &text,
6329 const QTextOption &option)
6332 Draws the given \a text in the \a rectangle specified using the \a option
6333 to control its positioning and orientation.
6335 By default, QPainter draws text anti-aliased.
6337 \note The y-coordinate of \a rectangle is used as the top of the font.
6339 void QPainter::drawText(const QRectF &r, const QString &text, const QTextOption &o)
6341 #ifdef QT_DEBUG_DRAW
6342 if (qt_show_painter_debug_output)
6343 printf("QPainter::drawText(), r=[%.2f,%.2f,%.2f,%.2f], str='%s'\n",
6344 r.x(), r.y(), r.width(), r.height(), text.toLatin1().constData());
6349 if (!d->engine || text.length() == 0 || pen().style() == Qt::NoPen)
6353 d->updateState(d->state);
6355 qt_format_text(d->state->font, r, 0, &o, text, 0, 0, 0, 0, this);
6359 \fn void QPainter::drawTextItem(int x, int y, const QTextItem &ti)
6366 \fn void QPainter::drawTextItem(const QPoint &p, const QTextItem &ti)
6371 Draws the text item \a ti at position \a p.
6375 \fn void QPainter::drawTextItem(const QPointF &p, const QTextItem &ti)
6380 Draws the text item \a ti at position \a p.
6382 This method ignores the painters background mode and
6383 color. drawText and qt_format_text have to do it themselves, as
6384 only they know the extents of the complete string.
6386 It ignores the font set on the painter as the text item has one of its own.
6388 The underline and strikeout parameters of the text items font are
6389 ignored aswell. You'll need to pass in the correct flags to get
6390 underlining and strikeout.
6393 static QPixmap generateWavyPixmap(qreal maxRadius, const QPen &pen)
6395 const qreal radiusBase = qMax(qreal(1), maxRadius);
6397 QString key = QLatin1Literal("WaveUnderline-")
6398 % pen.color().name()
6399 % HexString<qreal>(radiusBase);
6402 if (QPixmapCache::find(key, pixmap))
6405 const qreal halfPeriod = qMax(qreal(2), qreal(radiusBase * 1.61803399)); // the golden ratio
6406 const int width = qCeil(100 / (2 * halfPeriod)) * (2 * halfPeriod);
6407 const int radius = qFloor(radiusBase);
6414 while (xs < width) {
6417 path.quadTo(xs - halfPeriod / 2, ys, xs, 0);
6420 pixmap = QPixmap(width, radius * 2);
6421 pixmap.fill(Qt::transparent);
6424 wavePen.setCapStyle(Qt::SquareCap);
6426 // This is to protect against making the line too fat, as happens on Mac OS X
6427 // due to it having a rather thick width for the regular underline.
6428 const qreal maxPenWidth = .8 * radius;
6429 if (wavePen.widthF() > maxPenWidth)
6430 wavePen.setWidth(maxPenWidth);
6432 QPainter imgPainter(&pixmap);
6433 imgPainter.setPen(wavePen);
6434 imgPainter.setRenderHint(QPainter::Antialiasing);
6435 imgPainter.translate(0, radius);
6436 imgPainter.drawPath(path);
6439 QPixmapCache::insert(key, pixmap);
6444 static void drawTextItemDecoration(QPainter *painter, const QPointF &pos, const QFontEngine *fe,
6445 QTextCharFormat::UnderlineStyle underlineStyle,
6446 QTextItem::RenderFlags flags, qreal width,
6447 const QTextCharFormat &charFormat)
6449 if (underlineStyle == QTextCharFormat::NoUnderline
6450 && !(flags & (QTextItem::StrikeOut | QTextItem::Overline)))
6453 const QPen oldPen = painter->pen();
6454 const QBrush oldBrush = painter->brush();
6455 painter->setBrush(Qt::NoBrush);
6457 pen.setStyle(Qt::SolidLine);
6458 pen.setWidthF(fe->lineThickness().toReal());
6459 pen.setCapStyle(Qt::FlatCap);
6461 QLineF line(pos.x(), pos.y(), pos.x() + qFloor(width), pos.y());
6463 const qreal underlineOffset = fe->underlinePosition().toReal();
6464 // deliberately ceil the offset to avoid the underline coming too close to
6465 // the text above it.
6466 const qreal aliasedCoordinateDelta = 0.5 - 0.015625;
6467 const qreal underlinePos = pos.y() + qCeil(underlineOffset) - aliasedCoordinateDelta;
6469 if (underlineStyle == QTextCharFormat::SpellCheckUnderline) {
6470 underlineStyle = QTextCharFormat::UnderlineStyle(QApplication::style()->styleHint(QStyle::SH_SpellCheckUnderlineStyle));
6473 if (underlineStyle == QTextCharFormat::WaveUnderline) {
6475 painter->translate(0, pos.y() + 1);
6477 QColor uc = charFormat.underlineColor();
6481 // Adapt wave to underlineOffset or pen width, whatever is larger, to make it work on all platforms
6482 const QPixmap wave = generateWavyPixmap(qMax(underlineOffset, pen.widthF()), pen);
6483 const int descent = (int) fe->descent().toReal();
6485 painter->setBrushOrigin(painter->brushOrigin().x(), 0);
6486 painter->fillRect(pos.x(), 0, qCeil(width), qMin(wave.height(), descent), wave);
6488 } else if (underlineStyle != QTextCharFormat::NoUnderline) {
6489 QLineF underLine(line.x1(), underlinePos, line.x2(), underlinePos);
6491 QColor uc = charFormat.underlineColor();
6495 pen.setStyle((Qt::PenStyle)(underlineStyle));
6496 painter->setPen(pen);
6497 painter->drawLine(underLine);
6500 pen.setStyle(Qt::SolidLine);
6501 pen.setColor(oldPen.color());
6503 if (flags & QTextItem::StrikeOut) {
6504 QLineF strikeOutLine = line;
6505 strikeOutLine.translate(0., - fe->ascent().toReal() / 3.);
6506 painter->setPen(pen);
6507 painter->drawLine(strikeOutLine);
6510 if (flags & QTextItem::Overline) {
6511 QLineF overLine = line;
6512 overLine.translate(0., - fe->ascent().toReal());
6513 painter->setPen(pen);
6514 painter->drawLine(overLine);
6517 painter->setPen(oldPen);
6518 painter->setBrush(oldBrush);
6521 Q_GUI_EXPORT void qt_draw_decoration_for_glyphs(QPainter *painter, const glyph_t *glyphArray,
6522 const QFixedPoint *positions, int glyphCount,
6523 QFontEngine *fontEngine, const QFont &font,
6524 const QTextCharFormat &charFormat)
6526 if (!(font.underline() || font.strikeOut() || font.overline()))
6532 for (int i=0; i<glyphCount; ++i) {
6533 glyph_metrics_t gm = fontEngine->boundingBox(glyphArray[i]);
6534 if (i == 0 || leftMost > positions[i].x)
6535 leftMost = positions[i].x;
6537 // We don't support glyphs that do not share a common baseline. If this turns out to
6538 // be a relevant use case, then we need to find clusters of glyphs that share a baseline
6539 // and do a drawTextItemDecorations call per cluster.
6540 if (i == 0 || baseLine < positions[i].y)
6541 baseLine = positions[i].y;
6543 // We use the advance rather than the actual bounds to match the algorithm in drawText()
6544 if (i == 0 || rightMost < positions[i].x + gm.xoff)
6545 rightMost = positions[i].x + gm.xoff;
6548 QFixed width = rightMost - leftMost;
6549 QTextItem::RenderFlags flags = 0;
6551 if (font.underline())
6552 flags |= QTextItem::Underline;
6553 if (font.overline())
6554 flags |= QTextItem::Overline;
6555 if (font.strikeOut())
6556 flags |= QTextItem::StrikeOut;
6558 drawTextItemDecoration(painter, QPointF(leftMost.toReal(), baseLine.toReal()),
6560 font.underline() ? QTextCharFormat::SingleUnderline
6561 : QTextCharFormat::NoUnderline, flags,
6562 width.toReal(), charFormat);
6565 void QPainter::drawTextItem(const QPointF &p, const QTextItem &_ti)
6567 #ifdef QT_DEBUG_DRAW
6568 if (qt_show_painter_debug_output)
6569 printf("QPainter::drawTextItem(), pos=[%.f,%.f], str='%s'\n",
6570 p.x(), p.y(), qPrintable(_ti.text()));
6579 qt_painter_thread_test(d->device->devType(),
6581 QFontDatabase::supportsThreadedFontRendering());
6584 QTextItemInt &ti = const_cast<QTextItemInt &>(static_cast<const QTextItemInt &>(_ti));
6586 if (!d->extended && d->state->bgMode == Qt::OpaqueMode) {
6587 QRectF rect(p.x(), p.y() - ti.ascent.toReal(), ti.width.toReal(), (ti.ascent + ti.descent + 1).toReal());
6588 fillRect(rect, d->state->bgBrush);
6591 if (pen().style() == Qt::NoPen)
6594 const RenderHints oldRenderHints = d->state->renderHints;
6595 if (!d->state->renderHints & QPainter::Antialiasing && d->state->matrix.type() >= QTransform::TxScale) {
6596 // draw antialias decoration (underline/overline/strikeout) with
6600 const QTransform &m = d->state->matrix;
6601 if (d->state->matrix.type() < QTransform::TxShear) {
6602 bool isPlain90DegreeRotation =
6603 (qFuzzyIsNull(m.m11())
6604 && qFuzzyIsNull(m.m12() - qreal(1))
6605 && qFuzzyIsNull(m.m21() + qreal(1))
6606 && qFuzzyIsNull(m.m22())
6609 (qFuzzyIsNull(m.m11() + qreal(1))
6610 && qFuzzyIsNull(m.m12())
6611 && qFuzzyIsNull(m.m21())
6612 && qFuzzyIsNull(m.m22() + qreal(1))
6615 (qFuzzyIsNull(m.m11())
6616 && qFuzzyIsNull(m.m12() + qreal(1))
6617 && qFuzzyIsNull(m.m21() - qreal(1))
6618 && qFuzzyIsNull(m.m22())
6621 aa = !isPlain90DegreeRotation;
6624 setRenderHint(QPainter::Antialiasing, true);
6628 d->updateState(d->state);
6630 if (!ti.glyphs.numGlyphs) {
6632 } else if (ti.fontEngine->type() == QFontEngine::Multi) {
6633 QFontEngineMulti *multi = static_cast<QFontEngineMulti *>(ti.fontEngine);
6635 const QGlyphLayout &glyphs = ti.glyphs;
6636 int which = glyphs.glyphs[0] >> 24;
6643 for (end = 0; end < ti.glyphs.numGlyphs; ++end) {
6644 const int e = glyphs.glyphs[end] >> 24;
6649 QTextItemInt ti2 = ti.midItem(multi->engine(which), start, end - start);
6651 // set the high byte to zero and calc the width
6652 for (i = start; i < end; ++i) {
6653 glyphs.glyphs[i] = glyphs.glyphs[i] & 0xffffff;
6654 ti2.width += ti.glyphs.effectiveAdvance(i);
6657 d->engine->drawTextItem(QPointF(x, y), ti2);
6659 // reset the high byte for all glyphs and advance to the next sub-string
6660 const int hi = which << 24;
6661 for (i = start; i < end; ++i) {
6662 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
6664 x += ti2.width.toReal();
6671 QTextItemInt ti2 = ti.midItem(multi->engine(which), start, end - start);
6673 // set the high byte to zero and calc the width
6674 for (i = start; i < end; ++i) {
6675 glyphs.glyphs[i] = glyphs.glyphs[i] & 0xffffff;
6676 ti2.width += ti.glyphs.effectiveAdvance(i);
6680 d->extended->drawTextItem(QPointF(x, y), ti2);
6682 d->engine->drawTextItem(QPointF(x,y), ti2);
6684 // reset the high byte for all glyphs
6685 const int hi = which << 24;
6686 for (i = start; i < end; ++i)
6687 glyphs.glyphs[i] = hi | glyphs.glyphs[i];
6691 d->extended->drawTextItem(p, ti);
6693 d->engine->drawTextItem(p, ti);
6695 drawTextItemDecoration(this, p, ti.fontEngine, ti.underlineStyle, ti.flags, ti.width.toReal(),
6698 if (d->state->renderHints != oldRenderHints) {
6699 d->state->renderHints = oldRenderHints;
6701 d->extended->renderHintsChanged();
6703 d->state->dirtyFlags |= QPaintEngine::DirtyHints;
6708 \fn QRectF QPainter::boundingRect(const QRectF &rectangle, int flags, const QString &text)
6710 Returns the bounding rectangle of the \a text as it will appear
6711 when drawn inside the given \a rectangle with the specified \a
6712 flags using the currently set font(); i.e the function tells you
6713 where the drawText() function will draw when given the same
6716 If the \a text does not fit within the given \a rectangle using
6717 the specified \a flags, the function returns the required
6720 The \a flags argument is a bitwise OR of the following flags:
6729 \o Qt::TextSingleLine
6730 \o Qt::TextExpandTabs
6731 \o Qt::TextShowMnemonic
6733 \o Qt::TextIncludeTrailingSpaces
6735 If several of the horizontal or several of the vertical alignment
6736 flags are set, the resulting alignment is undefined.
6738 \sa drawText(), Qt::Alignment, Qt::TextFlag
6742 \fn QRect QPainter::boundingRect(const QRect &rectangle, int flags,
6743 const QString &text)
6747 Returns the bounding rectangle of the \a text as it will appear
6748 when drawn inside the given \a rectangle with the specified \a
6749 flags using the currently set font().
6753 \fn QRect QPainter::boundingRect(int x, int y, int w, int h, int flags,
6754 const QString &text);
6758 Returns the bounding rectangle of the given \a text as it will
6759 appear when drawn inside the rectangle beginning at the point
6760 (\a{x}, \a{y}) with width \a w and height \a h.
6762 QRect QPainter::boundingRect(const QRect &rect, int flags, const QString &str)
6765 return QRect(rect.x(),rect.y(), 0,0);
6767 drawText(rect, flags | Qt::TextDontPrint, str, &brect);
6773 QRectF QPainter::boundingRect(const QRectF &rect, int flags, const QString &str)
6776 return QRectF(rect.x(),rect.y(), 0,0);
6778 drawText(rect, flags | Qt::TextDontPrint, str, &brect);
6783 \fn QRectF QPainter::boundingRect(const QRectF &rectangle,
6784 const QString &text, const QTextOption &option)
6788 Instead of specifying flags as a bitwise OR of the
6789 Qt::AlignmentFlag and Qt::TextFlag, this overloaded function takes
6790 an \a option argument. The QTextOption class provides a
6791 description of general rich text properties.
6795 QRectF QPainter::boundingRect(const QRectF &r, const QString &text, const QTextOption &o)
6799 if (!d->engine || text.length() == 0)
6800 return QRectF(r.x(),r.y(), 0,0);
6803 qt_format_text(d->state->font, r, Qt::TextDontPrint, &o, text, &br, 0, 0, 0, this);
6808 \fn void QPainter::drawTiledPixmap(const QRectF &rectangle, const QPixmap &pixmap, const QPointF &position)
6810 Draws a tiled \a pixmap, inside the given \a rectangle with its
6811 origin at the given \a position.
6813 Calling drawTiledPixmap() is similar to calling drawPixmap()
6814 several times to fill (tile) an area with a pixmap, but is
6815 potentially much more efficient depending on the underlying window
6820 void QPainter::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &sp)
6822 #ifdef QT_DEBUG_DRAW
6823 if (qt_show_painter_debug_output)
6824 printf("QPainter::drawTiledPixmap(), target=[%.2f,%.2f,%.2f,%.2f], pix=[%d,%d], offset=[%.2f,%.2f]\n",
6825 r.x(), r.y(), r.width(), r.height(),
6826 pixmap.width(), pixmap.height(),
6831 if (!d->engine || pixmap.isNull() || r.isEmpty())
6835 qt_painter_thread_test(d->device->devType(), "drawTiledPixmap()", true);
6838 qreal sw = pixmap.width();
6839 qreal sh = pixmap.height();
6843 sx = qRound(sw) - qRound(-sx) % qRound(sw);
6845 sx = qRound(sx) % qRound(sw);
6847 sy = qRound(sh) - -qRound(sy) % qRound(sh);
6849 sy = qRound(sy) % qRound(sh);
6853 d->extended->drawTiledPixmap(r, pixmap, QPointF(sx, sy));
6857 if (d->state->bgMode == Qt::OpaqueMode && pixmap.isQBitmap())
6858 fillRect(r, d->state->bgBrush);
6860 d->updateState(d->state);
6861 if ((d->state->matrix.type() > QTransform::TxTranslate
6862 && !d->engine->hasFeature(QPaintEngine::PixmapTransform))
6863 || (d->state->opacity != 1.0 && !d->engine->hasFeature(QPaintEngine::ConstantOpacity)))
6866 setBackgroundMode(Qt::TransparentMode);
6867 setRenderHint(Antialiasing, renderHints() & SmoothPixmapTransform);
6868 setBrush(QBrush(d->state->pen.color(), pixmap));
6871 // If there is no rotation involved we have to make sure we use the
6872 // antialiased and not the aliased coordinate system by rounding the coordinates.
6873 if (d->state->matrix.type() <= QTransform::TxScale) {
6874 const QPointF p = roundInDeviceCoordinates(r.topLeft(), d->state->matrix);
6876 if (d->state->matrix.type() <= QTransform::TxTranslate) {
6881 setBrushOrigin(QPointF(r.x()-sx, r.y()-sy));
6882 drawRect(QRectF(p, r.size()));
6884 setBrushOrigin(QPointF(r.x()-sx, r.y()-sy));
6893 if (d->state->matrix.type() == QTransform::TxTranslate
6894 && !d->engine->hasFeature(QPaintEngine::PixmapTransform)) {
6895 x += d->state->matrix.dx();
6896 y += d->state->matrix.dy();
6899 d->engine->drawTiledPixmap(QRectF(x, y, r.width(), r.height()), pixmap, QPointF(sx, sy));
6903 \fn QPainter::drawTiledPixmap(const QRect &rectangle, const QPixmap &pixmap,
6904 const QPoint &position = QPoint())
6907 Draws a tiled \a pixmap, inside the given \a rectangle with its
6908 origin at the given \a position.
6912 \fn void QPainter::drawTiledPixmap(int x, int y, int width, int height, const
6913 QPixmap &pixmap, int sx, int sy);
6916 Draws a tiled \a pixmap in the specified rectangle.
6918 (\a{x}, \a{y}) specifies the top-left point in the paint device
6919 that is to be drawn onto; with the given \a width and \a
6920 height. (\a{sx}, \a{sy}) specifies the top-left point in the \a
6921 pixmap that is to be drawn; this defaults to (0, 0).
6924 #ifndef QT_NO_PICTURE
6927 \fn void QPainter::drawPicture(const QPointF &point, const QPicture &picture)
6929 Replays the given \a picture at the given \a point.
6931 The QPicture class is a paint device that records and replays
6932 QPainter commands. A picture serializes the painter commands to an
6933 IO device in a platform-independent format. Everything that can be
6934 painted on a widget or pixmap can also be stored in a picture.
6936 This function does exactly the same as QPicture::play() when
6937 called with \a point = QPoint(0, 0).
6942 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 18
6945 \sa QPicture::play()
6948 void QPainter::drawPicture(const QPointF &p, const QPicture &picture)
6956 d->updateState(d->state);
6960 const_cast<QPicture *>(&picture)->play(this);
6965 \fn void QPainter::drawPicture(const QPoint &point, const QPicture &picture)
6968 Replays the given \a picture at the given \a point.
6972 \fn void QPainter::drawPicture(int x, int y, const QPicture &picture)
6975 Draws the given \a picture at point (\a x, \a y).
6978 #endif // QT_NO_PICTURE
6981 \fn void QPainter::eraseRect(const QRectF &rectangle)
6983 Erases the area inside the given \a rectangle. Equivalent to
6985 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 19
6989 void QPainter::eraseRect(const QRectF &r)
6993 fillRect(r, d->state->bgBrush);
6996 static inline bool needsResolving(const QBrush &brush)
6998 Qt::BrushStyle s = brush.style();
6999 return ((s == Qt::LinearGradientPattern || s == Qt::RadialGradientPattern ||
7000 s == Qt::ConicalGradientPattern) &&
7001 brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode);
7005 \fn void QPainter::eraseRect(const QRect &rectangle)
7008 Erases the area inside the given \a rectangle.
7012 \fn void QPainter::eraseRect(int x, int y, int width, int height)
7015 Erases the area inside the rectangle beginning at (\a x, \a y)
7016 with the given \a width and \a height.
7021 \fn void QPainter::fillRect(int x, int y, int width, int height, Qt::BrushStyle style)
7024 Fills the rectangle beginning at (\a{x}, \a{y}) with the given \a
7025 width and \a height, using the brush \a style specified.
7031 \fn void QPainter::fillRect(const QRect &rectangle, Qt::BrushStyle style)
7034 Fills the given \a rectangle with the brush \a style specified.
7040 \fn void QPainter::fillRect(const QRectF &rectangle, Qt::BrushStyle style)
7043 Fills the given \a rectangle with the brush \a style specified.
7049 \fn void QPainter::fillRect(const QRectF &rectangle, const QBrush &brush)
7051 Fills the given \a rectangle with the \a brush specified.
7053 Alternatively, you can specify a QColor instead of a QBrush; the
7054 QBrush constructor (taking a QColor argument) will automatically
7055 create a solid pattern brush.
7059 void QPainter::fillRect(const QRectF &r, const QBrush &brush)
7067 const QGradient *g = brush.gradient();
7068 if (!g || g->coordinateMode() == QGradient::LogicalMode) {
7069 d->extended->fillRect(r, brush);
7074 QPen oldPen = pen();
7075 QBrush oldBrush = this->brush();
7077 if (brush.style() == Qt::SolidPattern) {
7078 d->colorBrush.setStyle(Qt::SolidPattern);
7079 d->colorBrush.setColor(brush.color());
7080 setBrush(d->colorBrush);
7091 \fn void QPainter::fillRect(const QRect &rectangle, const QBrush &brush)
7094 Fills the given \a rectangle with the specified \a brush.
7097 void QPainter::fillRect(const QRect &r, const QBrush &brush)
7105 const QGradient *g = brush.gradient();
7106 if (!g || g->coordinateMode() == QGradient::LogicalMode) {
7107 d->extended->fillRect(r, brush);
7112 QPen oldPen = pen();
7113 QBrush oldBrush = this->brush();
7115 if (brush.style() == Qt::SolidPattern) {
7116 d->colorBrush.setStyle(Qt::SolidPattern);
7117 d->colorBrush.setColor(brush.color());
7118 setBrush(d->colorBrush);
7131 \fn void QPainter::fillRect(const QRect &rectangle, const QColor &color)
7134 Fills the given \a rectangle with the \a color specified.
7138 void QPainter::fillRect(const QRect &r, const QColor &color)
7146 d->extended->fillRect(r, color);
7150 fillRect(r, QBrush(color));
7155 \fn void QPainter::fillRect(const QRectF &rectangle, const QColor &color)
7158 Fills the given \a rectangle with the \a color specified.
7162 void QPainter::fillRect(const QRectF &r, const QColor &color)
7170 d->extended->fillRect(r, color);
7174 fillRect(r, QBrush(color));
7178 \fn void QPainter::fillRect(int x, int y, int width, int height, const QBrush &brush)
7182 Fills the rectangle beginning at (\a{x}, \a{y}) with the given \a
7183 width and \a height, using the given \a brush.
7187 \fn void QPainter::fillRect(int x, int y, int width, int height, const QColor &color)
7191 Fills the rectangle beginning at (\a{x}, \a{y}) with the given \a
7192 width and \a height, using the given \a color.
7198 \fn void QPainter::fillRect(int x, int y, int width, int height, Qt::GlobalColor color)
7202 Fills the rectangle beginning at (\a{x}, \a{y}) with the given \a
7203 width and \a height, using the given \a color.
7209 \fn void QPainter::fillRect(const QRect &rectangle, Qt::GlobalColor color);
7213 Fills the given \a rectangle with the specified \a color.
7219 \fn void QPainter::fillRect(const QRectF &rectangle, Qt::GlobalColor color);
7223 Fills the given \a rectangle with the specified \a color.
7229 Sets the given render \a hint on the painter if \a on is true;
7230 otherwise clears the render hint.
7232 \sa setRenderHints(), renderHints(), {QPainter#Rendering
7233 Quality}{Rendering Quality}
7235 void QPainter::setRenderHint(RenderHint hint, bool on)
7237 #ifdef QT_DEBUG_DRAW
7238 if (qt_show_painter_debug_output)
7239 printf("QPainter::setRenderHint: hint=%x, %s\n", hint, on ? "on" : "off");
7243 static const bool antialiasingDisabled = qgetenv("QT_NO_ANTIALIASING").toInt();
7244 if (hint == QPainter::Antialiasing && antialiasingDisabled)
7248 setRenderHints(hint, on);
7254 Sets the given render \a hints on the painter if \a on is true;
7255 otherwise clears the render hints.
7257 \sa setRenderHint(), renderHints(), {QPainter#Rendering
7258 Quality}{Rendering Quality}
7261 void QPainter::setRenderHints(RenderHints hints, bool on)
7266 qWarning("QPainter::setRenderHint: Painter must be active to set rendering hints");
7271 d->state->renderHints |= hints;
7273 d->state->renderHints &= ~hints;
7276 d->extended->renderHintsChanged();
7278 d->state->dirtyFlags |= QPaintEngine::DirtyHints;
7282 Returns a flag that specifies the rendering hints that are set for
7285 \sa testRenderHint(), {QPainter#Rendering Quality}{Rendering Quality}
7287 QPainter::RenderHints QPainter::renderHints() const
7289 Q_D(const QPainter);
7294 return d->state->renderHints;
7298 \fn bool QPainter::testRenderHint(RenderHint hint) const
7301 Returns true if \a hint is set; otherwise returns false.
7303 \sa renderHints(), setRenderHint()
7307 Returns true if view transformation is enabled; otherwise returns
7310 \sa setViewTransformEnabled(), worldTransform()
7313 bool QPainter::viewTransformEnabled() const
7315 Q_D(const QPainter);
7317 qWarning("QPainter::viewTransformEnabled: Painter not active");
7320 return d->state->VxF;
7324 \fn void QPainter::setWindow(const QRect &rectangle)
7326 Sets the painter's window to the given \a rectangle, and enables
7327 view transformations.
7329 The window rectangle is part of the view transformation. The
7330 window specifies the logical coordinate system. Its sister, the
7331 viewport(), specifies the device coordinate system.
7333 The default window rectangle is the same as the device's
7336 \sa window(), viewTransformEnabled(), {Coordinate
7337 System#Window-Viewport Conversion}{Window-Viewport Conversion}
7341 \fn void QPainter::setWindow(int x, int y, int width, int height)
7344 Sets the painter's window to the rectangle beginning at (\a x, \a
7345 y) and the given \a width and \a height.
7348 void QPainter::setWindow(const QRect &r)
7350 #ifdef QT_DEBUG_DRAW
7351 if (qt_show_painter_debug_output)
7352 printf("QPainter::setWindow(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
7358 qWarning("QPainter::setWindow: Painter not active");
7362 d->state->wx = r.x();
7363 d->state->wy = r.y();
7364 d->state->ww = r.width();
7365 d->state->wh = r.height();
7367 d->state->VxF = true;
7372 Returns the window rectangle.
7374 \sa setWindow(), setViewTransformEnabled()
7377 QRect QPainter::window() const
7379 Q_D(const QPainter);
7381 qWarning("QPainter::window: Painter not active");
7384 return QRect(d->state->wx, d->state->wy, d->state->ww, d->state->wh);
7388 \fn void QPainter::setViewport(const QRect &rectangle)
7390 Sets the painter's viewport rectangle to the given \a rectangle,
7391 and enables view transformations.
7393 The viewport rectangle is part of the view transformation. The
7394 viewport specifies the device coordinate system. Its sister, the
7395 window(), specifies the logical coordinate system.
7397 The default viewport rectangle is the same as the device's
7400 \sa viewport(), viewTransformEnabled() {Coordinate
7401 System#Window-Viewport Conversion}{Window-Viewport Conversion}
7405 \fn void QPainter::setViewport(int x, int y, int width, int height)
7408 Sets the painter's viewport rectangle to be the rectangle
7409 beginning at (\a x, \a y) with the given \a width and \a height.
7412 void QPainter::setViewport(const QRect &r)
7414 #ifdef QT_DEBUG_DRAW
7415 if (qt_show_painter_debug_output)
7416 printf("QPainter::setViewport(), [%d,%d,%d,%d]\n", r.x(), r.y(), r.width(), r.height());
7422 qWarning("QPainter::setViewport: Painter not active");
7426 d->state->vx = r.x();
7427 d->state->vy = r.y();
7428 d->state->vw = r.width();
7429 d->state->vh = r.height();
7431 d->state->VxF = true;
7436 Returns the viewport rectangle.
7438 \sa setViewport(), setViewTransformEnabled()
7441 QRect QPainter::viewport() const
7443 Q_D(const QPainter);
7445 qWarning("QPainter::viewport: Painter not active");
7448 return QRect(d->state->vx, d->state->vy, d->state->vw, d->state->vh);
7451 /*! \fn bool QPainter::hasViewXForm() const
7454 Use viewTransformEnabled() instead.
7457 /*! \fn bool QPainter::hasWorldXForm() const
7460 Use worldMatrixEnabled() instead.
7463 /*! \fn void QPainter::resetXForm()
7466 Use resetTransform() instead.
7469 /*! \fn void QPainter::setViewXForm(bool enabled)
7472 Use setViewTransformEnabled() instead.
7475 /*! \fn void QPainter::setWorldXForm(bool enabled)
7478 Use setWorldMatrixEnabled() instead.
7481 Enables view transformations if \a enable is true, or disables
7482 view transformations if \a enable is false.
7484 \sa viewTransformEnabled(), {Coordinate System#Window-Viewport
7485 Conversion}{Window-Viewport Conversion}
7488 void QPainter::setViewTransformEnabled(bool enable)
7490 #ifdef QT_DEBUG_DRAW
7491 if (qt_show_painter_debug_output)
7492 printf("QPainter::setViewTransformEnabled(), enable=%d\n", enable);
7498 qWarning("QPainter::setViewTransformEnabled: Painter not active");
7502 if (enable == d->state->VxF)
7505 d->state->VxF = enable;
7514 Use the worldTransform() combined with QTransform::dx() instead.
7517 QPainter painter(this);
7518 qreal x = painter.translationX();
7520 QPainter painter(this);
7521 qreal x = painter.worldTransform().dx();
7524 qreal QPainter::translationX() const
7526 Q_D(const QPainter);
7528 qWarning("QPainter::translationX: Painter not active");
7531 return d->state->worldMatrix.dx();
7537 Use the worldTransform() combined with QTransform::dy() instead.
7540 QPainter painter(this);
7541 qreal y = painter.translationY();
7543 QPainter painter(this);
7544 qreal y = painter.worldTransform().dy();
7547 qreal QPainter::translationY() const
7549 Q_D(const QPainter);
7551 qWarning("QPainter::translationY: Painter not active");
7554 return d->state->worldMatrix.dy();
7558 \fn void QPainter::map(int x, int y, int *rx, int *ry) const
7562 Sets (\a{rx}, \a{ry}) to the point that results from applying the
7563 painter's current transformation on the point (\a{x}, \a{y}).
7565 void QPainter::map(int x, int y, int *rx, int *ry) const
7568 p = p * combinedMatrix();
7574 \fn QPoint QPainter::xForm(const QPoint &point) const
7576 Use combinedTransform() instead.
7579 QPoint QPainter::xForm(const QPoint &p) const
7581 Q_D(const QPainter);
7583 qWarning("QPainter::xForm: Painter not active");
7586 if (d->state->matrix.type() == QTransform::TxNone)
7588 return p * combinedMatrix();
7593 \fn QRect QPainter::xForm(const QRect &rectangle) const
7596 Use combinedTransform() instead of this function and call
7597 mapRect() on the result to obtain a QRect.
7600 QRect QPainter::xForm(const QRect &r) const
7602 Q_D(const QPainter);
7604 qWarning("QPainter::xForm: Painter not active");
7607 if (d->state->matrix.type() == QTransform::TxNone)
7609 return combinedMatrix().mapRect(r);
7613 \fn QPolygon QPainter::xForm(const QPolygon &polygon) const
7616 Use combinedTransform() instead.
7619 QPolygon QPainter::xForm(const QPolygon &a) const
7621 Q_D(const QPainter);
7623 qWarning("QPainter::xForm: Painter not active");
7626 if (d->state->matrix.type() == QTransform::TxNone)
7628 return a * combinedMatrix();
7632 \fn QPolygon QPainter::xForm(const QPolygon &polygon, int index, int count) const
7635 Use combinedTransform() combined with QPolygon::mid() instead.
7638 QPainter painter(this);
7639 QPolygon transformed = painter.xForm(polygon, index, count)
7641 QPainter painter(this);
7642 QPolygon transformed = polygon.mid(index, count) * painter.combinedTransform();
7646 QPolygon QPainter::xForm(const QPolygon &av, int index, int npoints) const
7648 int lastPoint = npoints < 0 ? av.size() : index+npoints;
7649 QPolygon a(lastPoint-index);
7650 memcpy(a.data(), av.data()+index, (lastPoint-index)*sizeof(QPoint));
7651 return a * combinedMatrix();
7655 \fn QPoint QPainter::xFormDev(const QPoint &point) const
7659 Use combinedTransform() combined with QTransform::inverted() instead.
7662 QPainter painter(this);
7663 QPoint transformed = painter.xFormDev(point);
7665 QPainter painter(this);
7666 QPoint transformed = point * painter.combinedTransform().inverted();
7670 QPoint QPainter::xFormDev(const QPoint &p) const
7672 Q_D(const QPainter);
7674 qWarning("QPainter::xFormDev: Painter not active");
7677 if(d->state->matrix.type() == QTransform::TxNone)
7679 return p * combinedMatrix().inverted();
7683 \fn QRect QPainter::xFormDev(const QRect &rectangle) const
7687 Use combinedTransform() combined with QTransform::inverted() instead.
7690 QPainter painter(this);
7691 QRect transformed = painter.xFormDev(rectangle);
7693 QPainter painter(this);
7694 QRegion region = QRegion(rectangle) * painter.combinedTransform().inverted();
7695 QRect transformed = region.boundingRect();
7699 QRect QPainter::xFormDev(const QRect &r) const
7701 Q_D(const QPainter);
7703 qWarning("QPainter::xFormDev: Painter not active");
7706 if (d->state->matrix.type() == QTransform::TxNone)
7708 return combinedMatrix().inverted().mapRect(r);
7714 \fn QPoint QPainter::xFormDev(const QPolygon &polygon) const
7717 Use combinedTransform() combined with QTransform::inverted() instead.
7720 QPainter painter(this);
7721 QPolygon transformed = painter.xFormDev(rectangle);
7723 QPainter painter(this);
7724 QPolygon transformed = polygon * painter.combinedTransform().inverted();
7728 QPolygon QPainter::xFormDev(const QPolygon &a) const
7730 Q_D(const QPainter);
7732 qWarning("QPainter::xFormDev: Painter not active");
7735 if (d->state->matrix.type() == QTransform::TxNone)
7737 return a * combinedMatrix().inverted();
7741 \fn QPolygon QPainter::xFormDev(const QPolygon &polygon, int index, int count) const
7745 Use combinedTransform() combined with QPolygon::mid() and QTransform::inverted() instead.
7748 QPainter painter(this);
7749 QPolygon transformed = painter.xFormDev(polygon, index, count);
7751 QPainter painter(this);
7752 QPolygon transformed = polygon.mid(index, count) * painter.combinedTransform().inverted();
7756 QPolygon QPainter::xFormDev(const QPolygon &ad, int index, int npoints) const
7758 Q_D(const QPainter);
7759 int lastPoint = npoints < 0 ? ad.size() : index+npoints;
7760 QPolygon a(lastPoint-index);
7761 memcpy(a.data(), ad.data()+index, (lastPoint-index)*sizeof(QPoint));
7762 if (d->state->matrix.type() == QTransform::TxNone)
7764 return a * combinedMatrix().inverted();
7768 \fn void QPainter::drawCubicBezier(const QPolygon &controlPoints, int index)
7770 Draws a cubic Bezier curve defined by the \a controlPoints,
7771 starting at \a{controlPoints}\e{[index]} (\a index defaults to 0).
7772 Points after \a{controlPoints}\e{[index + 3]} are ignored. Nothing
7773 happens if there aren't enough control points.
7775 Use strokePath() instead.
7778 QPainter painter(this);
7779 painter.drawCubicBezier(controlPoints, index)
7782 path.moveTo(controlPoints.at(index));
7783 path.cubicTo(controlPoints.at(index+1),
7784 controlPoints.at(index+2),
7785 controlPoints.at(index+3));
7787 QPainter painter(this);
7788 painter.strokePath(path, painter.pen());
7791 void QPainter::drawCubicBezier(const QPolygon &a, int index)
7798 if ((int)a.size() - index < 4) {
7799 qWarning("QPainter::drawCubicBezier: Cubic Bezier needs 4 control "
7805 path.moveTo(a.at(index));
7806 path.cubicTo(a.at(index+1), a.at(index+2), a.at(index+3));
7807 strokePath(path, d->state->pen);
7811 struct QPaintDeviceRedirection
7813 QPaintDeviceRedirection() : device(0), replacement(0), internalWidgetRedirectionIndex(-1) {}
7814 QPaintDeviceRedirection(const QPaintDevice *device, QPaintDevice *replacement,
7815 const QPoint& offset, int internalWidgetRedirectionIndex)
7816 : device(device), replacement(replacement), offset(offset),
7817 internalWidgetRedirectionIndex(internalWidgetRedirectionIndex) { }
7818 const QPaintDevice *device;
7819 QPaintDevice *replacement;
7821 int internalWidgetRedirectionIndex;
7822 bool operator==(const QPaintDevice *pdev) const { return device == pdev; }
7823 Q_DUMMY_COMPARISON_OPERATOR(QPaintDeviceRedirection)
7826 typedef QList<QPaintDeviceRedirection> QPaintDeviceRedirectionList;
7827 Q_GLOBAL_STATIC(QPaintDeviceRedirectionList, globalRedirections)
7828 Q_GLOBAL_STATIC(QMutex, globalRedirectionsMutex)
7829 Q_GLOBAL_STATIC(QAtomicInt, globalRedirectionAtomic)
7836 Please use QWidget::render() instead.
7838 Redirects all paint commands for the given paint \a device, to the
7839 \a replacement device. The optional point \a offset defines an
7840 offset within the source device.
7842 The redirection will not be effective until the begin() function
7843 has been called; make sure to call end() for the given \a
7844 device's painter (if any) before redirecting. Call
7845 restoreRedirected() to restore the previous redirection.
7847 \warning Making use of redirections in the QPainter API implies
7848 that QPainter::begin() and QPaintDevice destructors need to hold
7849 a mutex for a short period. This can impact performance. Use of
7850 QWidget::render is strongly encouraged.
7852 \sa redirected(), restoreRedirected()
7854 void QPainter::setRedirected(const QPaintDevice *device,
7855 QPaintDevice *replacement,
7856 const QPoint &offset)
7858 Q_ASSERT(device != 0);
7860 bool hadInternalWidgetRedirection = false;
7861 if (device->devType() == QInternal::Widget) {
7862 const QWidgetPrivate *widgetPrivate = static_cast<const QWidget *>(device)->d_func();
7863 // This is the case when the widget is in a paint event.
7864 if (widgetPrivate->redirectDev) {
7865 // Remove internal redirection and put it back into the global redirection list.
7867 QPaintDevice *oldReplacement = widgetPrivate->redirected(&oldOffset);
7868 const_cast<QWidgetPrivate *>(widgetPrivate)->restoreRedirected();
7869 setRedirected(device, oldReplacement, oldOffset);
7870 hadInternalWidgetRedirection = true;
7875 QPaintDevice *rdev = redirected(replacement, &roffset);
7877 QMutexLocker locker(globalRedirectionsMutex());
7878 QPaintDeviceRedirectionList *redirections = globalRedirections();
7879 Q_ASSERT(redirections != 0);
7880 *redirections += QPaintDeviceRedirection(device, rdev ? rdev : replacement, offset + roffset,
7881 hadInternalWidgetRedirection ? redirections->size() - 1 : -1);
7882 globalRedirectionAtomic()->ref();
7890 Using QWidget::render() obsoletes the use of this function.
7892 Restores the previous redirection for the given \a device after a
7893 call to setRedirected().
7895 \warning Making use of redirections in the QPainter API implies
7896 that QPainter::begin() and QPaintDevice destructors need to hold
7897 a mutex for a short period. This can impact performance. Use of
7898 QWidget::render is strongly encouraged.
7902 void QPainter::restoreRedirected(const QPaintDevice *device)
7904 Q_ASSERT(device != 0);
7905 QMutexLocker locker(globalRedirectionsMutex());
7906 QPaintDeviceRedirectionList *redirections = globalRedirections();
7907 Q_ASSERT(redirections != 0);
7908 for (int i = redirections->size()-1; i >= 0; --i) {
7909 if (redirections->at(i) == device) {
7910 globalRedirectionAtomic()->deref();
7911 const int internalWidgetRedirectionIndex = redirections->at(i).internalWidgetRedirectionIndex;
7912 redirections->removeAt(i);
7913 // Restore the internal widget redirection, i.e. remove it from the global
7914 // redirection list and put it back into QWidgetPrivate. The index is only set when
7915 // someone call QPainter::setRedirected in a widget's paint event and we internally
7916 // have a redirection set (typically set in QWidgetPrivate::drawWidget).
7917 if (internalWidgetRedirectionIndex >= 0) {
7918 Q_ASSERT(internalWidgetRedirectionIndex < redirections->size());
7919 const QPaintDeviceRedirection &redirectionDevice = redirections->at(internalWidgetRedirectionIndex);
7920 QWidget *widget = static_cast<QWidget *>(const_cast<QPaintDevice *>(device));
7921 widget->d_func()->setRedirected(redirectionDevice.replacement, redirectionDevice.offset);
7922 redirections->removeAt(internalWidgetRedirectionIndex);
7934 Using QWidget::render() obsoletes the use of this function.
7936 Returns the replacement for given \a device. The optional out
7937 parameter \a offset returns the offset within the replaced device.
7939 \warning Making use of redirections in the QPainter API implies
7940 that QPainter::begin() and QPaintDevice destructors need to hold
7941 a mutex for a short period. This can impact performance. Use of
7942 QWidget::render is strongly encouraged.
7944 \sa setRedirected(), restoreRedirected()
7946 QPaintDevice *QPainter::redirected(const QPaintDevice *device, QPoint *offset)
7948 Q_ASSERT(device != 0);
7950 if (device->devType() == QInternal::Widget) {
7951 const QWidgetPrivate *widgetPrivate = static_cast<const QWidget *>(device)->d_func();
7952 if (widgetPrivate->redirectDev)
7953 return widgetPrivate->redirected(offset);
7956 if (!globalRedirectionAtomic() || *globalRedirectionAtomic() == 0)
7959 QMutexLocker locker(globalRedirectionsMutex());
7960 QPaintDeviceRedirectionList *redirections = globalRedirections();
7961 Q_ASSERT(redirections != 0);
7962 for (int i = redirections->size()-1; i >= 0; --i)
7963 if (redirections->at(i) == device) {
7965 *offset = redirections->at(i).offset;
7966 return redirections->at(i).replacement;
7969 *offset = QPoint(0, 0);
7974 void qt_painter_removePaintDevice(QPaintDevice *dev)
7976 if (!globalRedirectionAtomic() || *globalRedirectionAtomic() == 0)
7981 mutex = globalRedirectionsMutex();
7983 // ignore the missing mutex, since we could be called from
7984 // a destructor, and destructors shall not throw
7986 QMutexLocker locker(mutex);
7987 QPaintDeviceRedirectionList *redirections = 0;
7989 redirections = globalRedirections();
7991 // do nothing - code below is safe with redirections being 0.
7994 for (int i = 0; i < redirections->size(); ) {
7995 if(redirections->at(i) == dev || redirections->at(i).replacement == dev)
7996 redirections->removeAt(i);
8003 void qt_format_text(const QFont &fnt, const QRectF &_r,
8004 int tf, const QString& str, QRectF *brect,
8005 int tabstops, int *ta, int tabarraylen,
8008 qt_format_text(fnt, _r,
8010 tabstops, ta, tabarraylen,
8013 void qt_format_text(const QFont &fnt, const QRectF &_r,
8014 int tf, const QTextOption *option, const QString& str, QRectF *brect,
8015 int tabstops, int *ta, int tabarraylen,
8019 Q_ASSERT( !((tf & ~Qt::TextDontPrint)!=0 && option!=0) ); // we either have an option or flags
8022 tf |= option->alignment();
8023 if (option->wrapMode() != QTextOption::NoWrap)
8024 tf |= Qt::TextWordWrap;
8026 if (option->flags() & QTextOption::IncludeTrailingSpaces)
8027 tf |= Qt::TextIncludeTrailingSpaces;
8029 if (option->tabStop() >= 0 || !option->tabArray().isEmpty())
8030 tf |= Qt::TextExpandTabs;
8033 // we need to copy r here to protect against the case (&r == brect).
8036 bool dontclip = (tf & Qt::TextDontClip);
8037 bool wordwrap = (tf & Qt::TextWordWrap) || (tf & Qt::TextWrapAnywhere);
8038 bool singleline = (tf & Qt::TextSingleLine);
8039 bool showmnemonic = (tf & Qt::TextShowMnemonic);
8040 bool hidemnmemonic = (tf & Qt::TextHideMnemonic);
8042 Qt::LayoutDirection layout_direction;
8043 if (tf & Qt::TextForceLeftToRight)
8044 layout_direction = Qt::LeftToRight;
8045 else if (tf & Qt::TextForceRightToLeft)
8046 layout_direction = Qt::RightToLeft;
8048 layout_direction = option->textDirection();
8050 layout_direction = painter->layoutDirection();
8052 layout_direction = Qt::LeftToRight;
8054 tf = QStyle::visualAlignment(layout_direction, QFlag(tf));
8056 bool isRightToLeft = layout_direction == Qt::RightToLeft;
8057 bool expandtabs = ((tf & Qt::TextExpandTabs) &&
8058 (((tf & Qt::AlignLeft) && !isRightToLeft) ||
8059 ((tf & Qt::AlignRight) && isRightToLeft)));
8062 tf |= Qt::TextDontPrint;
8064 uint maxUnderlines = 0;
8065 int numUnderlines = 0;
8066 QVarLengthArray<int, 32> underlinePositions(1);
8068 QFontMetricsF fm(fnt);
8071 start_lengthVariant:
8072 bool hasMoreLengthVariants = false;
8073 // compatible behaviour to the old implementation. Replace
8075 int old_offset = offset;
8076 for (; offset < text.length(); offset++) {
8077 QChar chr = text.at(offset);
8078 if (chr == QLatin1Char('\r') || (singleline && chr == QLatin1Char('\n'))) {
8079 text[offset] = QLatin1Char(' ');
8080 } else if (chr == QLatin1Char('\n')) {
8081 text[offset] = QChar::LineSeparator;
8082 } else if (chr == QLatin1Char('&')) {
8084 } else if (chr == QLatin1Char('\t')) {
8086 text[offset] = QLatin1Char(' ');
8087 } else if (!tabarraylen && !tabstops) {
8088 tabstops = qRound(fm.width(QLatin1Char('x'))*8);
8090 } else if (chr == QChar(ushort(0x9c))) {
8091 // string with multiple length variants
8092 hasMoreLengthVariants = true;
8097 int length = offset - old_offset;
8098 if ((hidemnmemonic || showmnemonic) && maxUnderlines > 0) {
8099 underlinePositions.resize(maxUnderlines + 1);
8101 QChar *cout = text.data() + old_offset;
8105 if (*cin == QLatin1Char('&')) {
8111 if (*cin != QLatin1Char('&') && !hidemnmemonic)
8112 underlinePositions[numUnderlines++] = cout - text.data() - old_offset;
8121 // no need to do extra work for underlines if we don't paint
8122 if (tf & Qt::TextDontPrint)
8125 underlinePositions[numUnderlines] = -1;
8129 QString finalText = text.mid(old_offset, length);
8130 QStackTextEngine engine(finalText, fnt);
8132 engine.option = *option;
8135 if (engine.option.tabStop() < 0 && tabstops > 0)
8136 engine.option.setTabStop(tabstops);
8138 if (engine.option.tabs().isEmpty() && ta) {
8140 for (int i = 0; i < tabarraylen; i++)
8141 tabs.append(qreal(ta[i]));
8142 engine.option.setTabArray(tabs);
8145 engine.option.setTextDirection(layout_direction);
8146 if (tf & Qt::AlignJustify)
8147 engine.option.setAlignment(Qt::AlignJustify);
8149 engine.option.setAlignment(Qt::AlignLeft); // do not do alignment twice
8151 if (!option && (tf & Qt::TextWrapAnywhere))
8152 engine.option.setWrapMode(QTextOption::WrapAnywhere);
8154 if (tf & Qt::TextJustificationForced)
8155 engine.forceJustification = true;
8156 QTextLayout textLayout(&engine);
8157 textLayout.setCacheEnabled(true);
8158 textLayout.engine()->underlinePositions = underlinePositions.data();
8160 if (finalText.isEmpty()) {
8161 height = fm.height();
8163 tf |= Qt::TextDontPrint;
8165 qreal lineWidth = 0x01000000;
8166 if (wordwrap || (tf & Qt::TextJustificationForced))
8167 lineWidth = qMax<qreal>(0, r.width());
8169 tf |= Qt::TextIncludeTrailingSpaces;
8170 textLayout.engine()->ignoreBidi = bool(tf & Qt::TextDontPrint);
8171 textLayout.beginLayout();
8173 qreal leading = fm.leading();
8177 QTextLine l = textLayout.createLine();
8181 l.setLineWidth(lineWidth);
8183 l.setPosition(QPointF(0., height));
8184 height += l.height();
8185 width = qMax(width, l.naturalTextWidth());
8186 if (!dontclip && !brect && height >= r.height())
8189 textLayout.endLayout();
8194 if (tf & Qt::AlignBottom) {
8195 yoff = r.height() - height;
8196 } else if (tf & Qt::AlignVCenter) {
8197 yoff = (r.height() - height)/2;
8199 QTransform::TransformationType type = painter->transform().type();
8200 if (type <= QTransform::TxScale) {
8201 // do the rounding manually to work around inconsistencies
8202 // in the paint engines when drawing on floating point offsets
8203 const qreal scale = painter->transform().m22();
8205 yoff = -qRound(-yoff * scale) / scale;
8209 if (tf & Qt::AlignRight) {
8210 xoff = r.width() - width;
8211 } else if (tf & Qt::AlignHCenter) {
8212 xoff = (r.width() - width)/2;
8214 QTransform::TransformationType type = painter->transform().type();
8215 if (type <= QTransform::TxScale) {
8216 // do the rounding manually to work around inconsistencies
8217 // in the paint engines when drawing on floating point offsets
8218 const qreal scale = painter->transform().m11();
8220 xoff = qRound(xoff * scale) / scale;
8224 QRectF bounds = QRectF(r.x() + xoff, r.y() + yoff, width, height);
8226 if (hasMoreLengthVariants && !(tf & Qt::TextLongestVariant) && !r.contains(bounds)) {
8228 goto start_lengthVariant;
8233 if (!(tf & Qt::TextDontPrint)) {
8234 bool restore = false;
8235 if (!dontclip && !r.contains(bounds)) {
8238 painter->setClipRect(r, Qt::IntersectClip);
8241 for (int i = 0; i < textLayout.lineCount(); i++) {
8242 QTextLine line = textLayout.lineAt(i);
8244 qreal advance = line.horizontalAdvance();
8246 if (tf & Qt::AlignRight) {
8247 QTextEngine *eng = textLayout.engine();
8248 xoff = r.width() - advance -
8249 eng->leadingSpaceWidth(eng->lines[line.lineNumber()]).toReal();
8251 else if (tf & Qt::AlignHCenter)
8252 xoff = (r.width() - advance) / 2;
8254 line.draw(painter, QPointF(r.x() + xoff, r.y() + yoff));
8264 Sets the layout direction used by the painter when drawing text,
8265 to the specified \a direction.
8267 The default is Qt::LayoutDirectionAuto, which will implicitly determine the
8268 direction from the text drawn.
8270 \sa QTextOption::setTextDirection(), layoutDirection(), drawText(), {QPainter#Settings}{Settings}
8272 void QPainter::setLayoutDirection(Qt::LayoutDirection direction)
8276 d->state->layoutDirection = direction;
8280 Returns the layout direction used by the painter when drawing text.
8282 \sa QTextOption::textDirection(), setLayoutDirection(), drawText(), {QPainter#Settings}{Settings}
8284 Qt::LayoutDirection QPainter::layoutDirection() const
8286 Q_D(const QPainter);
8287 return d->state ? d->state->layoutDirection : Qt::LayoutDirectionAuto;
8290 QPainterState::QPainterState(const QPainterState *s)
8291 : brushOrigin(s->brushOrigin), font(s->font), deviceFont(s->deviceFont),
8292 pen(s->pen), brush(s->brush), bgBrush(s->bgBrush),
8293 clipRegion(s->clipRegion), clipPath(s->clipPath),
8294 clipOperation(s->clipOperation),
8295 renderHints(s->renderHints), clipInfo(s->clipInfo),
8296 worldMatrix(s->worldMatrix), matrix(s->matrix), redirectionMatrix(s->redirectionMatrix),
8297 wx(s->wx), wy(s->wy), ww(s->ww), wh(s->wh),
8298 vx(s->vx), vy(s->vy), vw(s->vw), vh(s->vh),
8299 opacity(s->opacity), WxF(s->WxF), VxF(s->VxF),
8300 clipEnabled(s->clipEnabled), bgMode(s->bgMode), painter(s->painter),
8301 layoutDirection(s->layoutDirection),
8302 composition_mode(s->composition_mode),
8303 emulationSpecifier(s->emulationSpecifier), changeFlags(0)
8305 dirtyFlags = s->dirtyFlags;
8308 QPainterState::QPainterState()
8309 : brushOrigin(0, 0), bgBrush(Qt::white), clipOperation(Qt::NoClip),
8311 wx(0), wy(0), ww(0), wh(0), vx(0), vy(0), vw(0), vh(0),
8312 opacity(1), WxF(false), VxF(false), clipEnabled(true),
8313 bgMode(Qt::TransparentMode), painter(0),
8314 layoutDirection(QApplication::layoutDirection()),
8315 composition_mode(QPainter::CompositionMode_SourceOver),
8316 emulationSpecifier(0), changeFlags(0)
8321 QPainterState::~QPainterState()
8325 void QPainterState::init(QPainter *p) {
8326 bgBrush = Qt::white;
8327 bgMode = Qt::TransparentMode;
8331 wx = wy = ww = wh = 0;
8332 vx = vy = vw = vh = 0;
8335 brushOrigin = QPointF(0, 0);
8337 font = deviceFont = QFont();
8338 clipRegion = QRegion();
8339 clipPath = QPainterPath();
8340 clipOperation = Qt::NoClip;
8342 worldMatrix.reset();
8344 layoutDirection = QApplication::layoutDirection();
8345 composition_mode = QPainter::CompositionMode_SourceOver;
8346 emulationSpecifier = 0;
8354 static void bitBlt_helper(QPaintDevice *dst, const QPoint &dp,
8355 const QPaintDevice *src, const QRect &sr, bool)
8360 if (src->devType() == QInternal::Pixmap) {
8361 const QPixmap *pixmap = static_cast<const QPixmap *>(src);
8363 pt.drawPixmap(dp, *pixmap, sr);
8366 qWarning("QPainter: bitBlt only works when source is of type pixmap");
8370 void bitBlt(QPaintDevice *dst, int dx, int dy,
8371 const QPaintDevice *src, int sx, int sy, int sw, int sh,
8374 bitBlt_helper(dst, QPoint(dx, dy), src, QRect(sx, sy, sw, sh), ignoreMask);
8377 void bitBlt(QPaintDevice *dst, const QPoint &dp, const QPaintDevice *src, const QRect &sr, bool ignoreMask)
8379 bitBlt_helper(dst, dp, src, sr, ignoreMask);
8382 void bitBlt(QPaintDevice *dst, int dx, int dy,
8383 const QImage *src, int sx, int sy, int sw, int sh, int fl)
8385 Qt::ImageConversionFlags flags(fl);
8386 QPixmap srcPixmap = QPixmap::fromImage(*src, flags);
8387 bitBlt_helper(dst, QPoint(dx, dy), &srcPixmap, QRect(sx, sy, sw, sh), false);
8390 #endif // QT3_SUPPORT
8393 \fn void QPainter::setBackgroundColor(const QColor &color)
8395 Use setBackground() instead.
8399 \fn const QColor &QPainter::backgroundColor() const
8401 Use background() and QBrush::color() instead.
8404 QColor myColor = backgroundColor();
8406 QColor myColor = background().color();
8409 Note that the background can be a complex brush such as a texture
8414 \fn void QPainter::drawText(int x, int y, const QString &text, int pos, int length)
8417 Use drawText() combined with QString::mid() instead.
8420 QPainter painter(this);
8421 painter.drawText(x, y, text, pos, length);
8423 QPainter painter(this);
8424 painter.drawText(x, y, text.mid(pos, length));
8429 \fn void QPainter::drawText(const QPoint &point, const QString &text, int pos, int length)
8432 Use drawText() combined with QString::mid() instead.
8435 QPainter painter(this);
8436 painter.drawText(point, text, pos, length);
8438 QPainter painter(this);
8439 painter.drawText(point, text.mid(pos, length));
8444 \fn void QPainter::drawText(int x, int y, const QString &text, int length)
8447 Use drawText() combined with QString::left() instead.
8450 QPainter painter(this);
8451 painter.drawText(x, y, text, length);
8453 QPainter painter(this);
8454 painter.drawText(x, y, text.left(length));
8459 \fn void QPainter::drawText(const QPoint &point, const QString &text, int length)
8462 Use drawText() combined with QString::left() instead.
8465 QPainter painter(this);
8466 painter.drawText(point, text, length);
8468 QPainter painter(this);
8469 painter.drawText(point, text.left(length));
8474 \fn bool QPainter::begin(QPaintDevice *device, const QWidget *init)
8477 Use begin() instead.
8479 If the paint \a device is a QWidget, QPainter is initialized after
8480 the widget's settings automatically. Otherwise, you must call the
8481 initFrom() function to initialize the painters pen, background and
8482 font to the same as any given widget.
8485 QPainter painter(this);
8486 painter.begin(device, init);
8488 QPainter painter(this);
8489 painter.begin(device);
8490 painter.initFrom(init);
8495 \fn void QPainter::drawImage(const QRectF &target, const QImage &image, const QRectF &source,
8496 Qt::ImageConversionFlags flags)
8498 Draws the rectangular portion \a source of the given \a image
8499 into the \a target rectangle in the paint device.
8501 \note The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
8503 If the image needs to be modified to fit in a lower-resolution
8504 result (e.g. converting from 32-bit to 8-bit), use the \a flags to
8505 specify how you would prefer this to happen.
8510 \snippet doc/src/snippets/code/src_gui_painting_qpainter.cpp 20
8517 \fn void QPainter::drawImage(const QRect &target, const QImage &image, const QRect &source,
8518 Qt::ImageConversionFlags flags)
8521 Draws the rectangular portion \a source of the given \a image
8522 into the \a target rectangle in the paint device.
8524 \note The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
8528 \fn void QPainter::drawImage(const QPointF &point, const QImage &image)
8532 Draws the given \a image at the given \a point.
8536 \fn void QPainter::drawImage(const QPoint &point, const QImage &image)
8540 Draws the given \a image at the given \a point.
8544 \fn void QPainter::drawImage(const QPointF &point, const QImage &image, const QRectF &source,
8545 Qt::ImageConversionFlags flags = 0)
8549 Draws the rectangular portion \a source of the given \a image with
8550 its origin at the given \a point.
8554 \fn void QPainter::drawImage(const QPoint &point, const QImage &image, const QRect &source,
8555 Qt::ImageConversionFlags flags = 0)
8558 Draws the rectangular portion \a source of the given \a image with
8559 its origin at the given \a point.
8563 \fn void QPainter::drawImage(const QRectF &rectangle, const QImage &image)
8567 Draws the given \a image into the given \a rectangle.
8569 \note The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
8573 \fn void QPainter::drawImage(const QRect &rectangle, const QImage &image)
8577 Draws the given \a image into the given \a rectangle.
8579 \note The image is scaled to fit the rectangle, if both the image and rectangle size disagree.
8583 \fn void QPainter::drawImage(int x, int y, const QImage &image,
8584 int sx, int sy, int sw, int sh,
8585 Qt::ImageConversionFlags flags)
8588 Draws an image at (\a{x}, \a{y}) by copying a part of \a image into
8591 (\a{x}, \a{y}) specifies the top-left point in the paint device that is
8592 to be drawn onto. (\a{sx}, \a{sy}) specifies the top-left point in \a
8593 image that is to be drawn. The default is (0, 0).
8595 (\a{sw}, \a{sh}) specifies the size of the image that is to be drawn.
8596 The default, (0, 0) (and negative) means all the way to the
8597 bottom-right of the image.
8601 \fn void QPainter::redirect(QPaintDevice *pdev, QPaintDevice *replacement)
8603 Use setRedirected() instead.
8607 \fn QPaintDevice *QPainter::redirect(QPaintDevice *pdev)
8609 Use redirected() instead.
8613 \fn QRect QPainter::boundingRect(const QRect &rectangle, int flags,
8614 const QString &text, int length)
8617 Returns the bounding rectangle for the given \a length of the \a
8618 text constrained by the provided \a rectangle.
8620 Use boundingRect() combined with QString::left() instead.
8623 QRect rectangle = boundingRect(rect, flags, text, length);
8625 QRect rectangle = boundingRect(rect, flags, text.left(length));
8630 \fn void QPainter::drawText(const QRect &rectangle, int flags, const QString &text,
8631 int length, QRect *br)
8634 Use drawText() combined with QString::left() instead.
8637 QPainter painter(this);
8638 painter.drawText(rectangle, flags, text, length, br );
8640 QPainter painter(this);
8641 painter.drawText(rectangle, flags, text.left(length), br );
8646 \fn QRect QPainter::boundingRect(int x, int y, int width, int height, int flags,
8647 const QString &text, int length);
8651 Returns the bounding rectangle for the given \a length of the \a
8652 text constrained by the rectangle that begins at point (\a{x},
8653 \a{y}) with the given \a width and \a height.
8655 Use boundingRect() combined with QString::left() instead.
8658 QRect rectangle = boundingRect(x, y, width, height, flags, text, length);
8660 QRect rectangle = boundingRect(x, y, width, height, flags, text.left(length));
8665 \fn void QPainter::drawText(int x, int y, int width, int height, int flags,
8666 const QString &text, int length, QRect *br)
8670 Use drawText() combined with QString::left() instead.
8673 QPainter painter(this);
8674 painter.drawText(x, y, width, height, flags, text, length, br );
8676 QPainter painter(this);
8677 painter.drawText(x, y, width, height, flags, text.left(length), br );
8683 \class QPaintEngineState
8686 \brief The QPaintEngineState class provides information about the
8687 active paint engine's current state.
8690 QPaintEngineState records which properties that have changed since
8691 the last time the paint engine was updated, as well as their
8694 Which properties that have changed can at any time be retrieved
8695 using the state() function. This function returns an instance of
8696 the QPaintEngine::DirtyFlags type which stores an OR combination
8697 of QPaintEngine::DirtyFlag values. The QPaintEngine::DirtyFlag
8698 enum defines whether a property has changed since the last update
8701 If a property is marked with a dirty flag, its current value can
8702 be retrieved using the corresponding get function:
8707 \header \o Property Flag \o Current Property Value
8708 \row \o QPaintEngine::DirtyBackground \o backgroundBrush()
8709 \row \o QPaintEngine::DirtyBackgroundMode \o backgroundMode()
8710 \row \o QPaintEngine::DirtyBrush \o brush()
8711 \row \o QPaintEngine::DirtyBrushOrigin \o brushOrigin()
8712 \row \o QPaintEngine::DirtyClipRegion \e or QPaintEngine::DirtyClipPath
8714 \row \o QPaintEngine::DirtyClipPath \o clipPath()
8715 \row \o QPaintEngine::DirtyClipRegion \o clipRegion()
8716 \row \o QPaintEngine::DirtyCompositionMode \o compositionMode()
8717 \row \o QPaintEngine::DirtyFont \o font()
8718 \row \o QPaintEngine::DirtyTransform \o transform()
8719 \row \o QPaintEngine::DirtyClipEnabled \o isClipEnabled()
8720 \row \o QPaintEngine::DirtyPen \o pen()
8721 \row \o QPaintEngine::DirtyHints \o renderHints()
8724 The QPaintEngineState class also provide the painter() function
8725 which returns a pointer to the painter that is currently updating
8728 An instance of this class, representing the current state of the
8729 active paint engine, is passed as argument to the
8730 QPaintEngine::updateState() function. The only situation in which
8731 you will have to use this class directly is when implementing your
8739 \fn QPaintEngine::DirtyFlags QPaintEngineState::state() const
8741 Returns a combination of flags identifying the set of properties
8742 that need to be updated when updating the paint engine's state
8743 (i.e. during a call to the QPaintEngine::updateState() function).
8745 \sa QPaintEngine::updateState()
8750 Returns the pen in the current paint engine state.
8752 This variable should only be used when the state() returns a
8753 combination which includes the QPaintEngine::DirtyPen flag.
8755 \sa state(), QPaintEngine::updateState()
8758 QPen QPaintEngineState::pen() const
8760 return static_cast<const QPainterState *>(this)->pen;
8764 Returns the brush in the current paint engine state.
8766 This variable should only be used when the state() returns a
8767 combination which includes the QPaintEngine::DirtyBrush flag.
8769 \sa state(), QPaintEngine::updateState()
8772 QBrush QPaintEngineState::brush() const
8774 return static_cast<const QPainterState *>(this)->brush;
8778 Returns the brush origin in the current paint engine state.
8780 This variable should only be used when the state() returns a
8781 combination which includes the QPaintEngine::DirtyBrushOrigin flag.
8783 \sa state(), QPaintEngine::updateState()
8786 QPointF QPaintEngineState::brushOrigin() const
8788 return static_cast<const QPainterState *>(this)->brushOrigin;
8792 Returns the background brush in the current paint engine state.
8794 This variable should only be used when the state() returns a
8795 combination which includes the QPaintEngine::DirtyBackground flag.
8797 \sa state(), QPaintEngine::updateState()
8800 QBrush QPaintEngineState::backgroundBrush() const
8802 return static_cast<const QPainterState *>(this)->bgBrush;
8806 Returns the background mode in the current paint engine
8809 This variable should only be used when the state() returns a
8810 combination which includes the QPaintEngine::DirtyBackgroundMode flag.
8812 \sa state(), QPaintEngine::updateState()
8815 Qt::BGMode QPaintEngineState::backgroundMode() const
8817 return static_cast<const QPainterState *>(this)->bgMode;
8821 Returns the font in the current paint engine
8824 This variable should only be used when the state() returns a
8825 combination which includes the QPaintEngine::DirtyFont flag.
8827 \sa state(), QPaintEngine::updateState()
8830 QFont QPaintEngineState::font() const
8832 return static_cast<const QPainterState *>(this)->font;
8839 Returns the matrix in the current paint engine
8842 \note It is advisable to use transform() instead of this function to
8843 preserve the properties of perspective transformations.
8845 This variable should only be used when the state() returns a
8846 combination which includes the QPaintEngine::DirtyTransform flag.
8848 \sa state(), QPaintEngine::updateState()
8851 QMatrix QPaintEngineState::matrix() const
8853 const QPainterState *st = static_cast<const QPainterState *>(this);
8855 return st->matrix.toAffine();
8861 Returns the matrix in the current paint engine state.
8863 This variable should only be used when the state() returns a
8864 combination which includes the QPaintEngine::DirtyTransform flag.
8866 \sa state(), QPaintEngine::updateState()
8870 QTransform QPaintEngineState::transform() const
8872 const QPainterState *st = static_cast<const QPainterState *>(this);
8879 Returns the clip operation in the current paint engine
8882 This variable should only be used when the state() returns a
8883 combination which includes either the QPaintEngine::DirtyClipPath
8884 or the QPaintEngine::DirtyClipRegion flag.
8886 \sa state(), QPaintEngine::updateState()
8889 Qt::ClipOperation QPaintEngineState::clipOperation() const
8891 return static_cast<const QPainterState *>(this)->clipOperation;
8897 Returns whether the coordinate of the fill have been specified
8898 as bounded by the current rendering operation and have to be
8899 resolved (about the currently rendered primitive).
8901 bool QPaintEngineState::brushNeedsResolving() const
8903 const QBrush &brush = static_cast<const QPainterState *>(this)->brush;
8904 return needsResolving(brush);
8911 Returns whether the coordinate of the stroke have been specified
8912 as bounded by the current rendering operation and have to be
8913 resolved (about the currently rendered primitive).
8915 bool QPaintEngineState::penNeedsResolving() const
8917 const QPen &pen = static_cast<const QPainterState *>(this)->pen;
8918 return needsResolving(pen.brush());
8922 Returns the clip region in the current paint engine state.
8924 This variable should only be used when the state() returns a
8925 combination which includes the QPaintEngine::DirtyClipRegion flag.
8927 \sa state(), QPaintEngine::updateState()
8930 QRegion QPaintEngineState::clipRegion() const
8932 return static_cast<const QPainterState *>(this)->clipRegion;
8936 Returns the clip path in the current paint engine state.
8938 This variable should only be used when the state() returns a
8939 combination which includes the QPaintEngine::DirtyClipPath flag.
8941 \sa state(), QPaintEngine::updateState()
8944 QPainterPath QPaintEngineState::clipPath() const
8946 return static_cast<const QPainterState *>(this)->clipPath;
8950 Returns whether clipping is enabled or not in the current paint
8953 This variable should only be used when the state() returns a
8954 combination which includes the QPaintEngine::DirtyClipEnabled
8957 \sa state(), QPaintEngine::updateState()
8960 bool QPaintEngineState::isClipEnabled() const
8962 return static_cast<const QPainterState *>(this)->clipEnabled;
8966 Returns the render hints in the current paint engine state.
8968 This variable should only be used when the state() returns a
8969 combination which includes the QPaintEngine::DirtyHints
8972 \sa state(), QPaintEngine::updateState()
8975 QPainter::RenderHints QPaintEngineState::renderHints() const
8977 return static_cast<const QPainterState *>(this)->renderHints;
8981 Returns the composition mode in the current paint engine state.
8983 This variable should only be used when the state() returns a
8984 combination which includes the QPaintEngine::DirtyCompositionMode
8987 \sa state(), QPaintEngine::updateState()
8990 QPainter::CompositionMode QPaintEngineState::compositionMode() const
8992 return static_cast<const QPainterState *>(this)->composition_mode;
8997 Returns a pointer to the painter currently updating the paint
9001 QPainter *QPaintEngineState::painter() const
9003 return static_cast<const QPainterState *>(this)->painter;
9010 Returns the opacity in the current paint engine state.
9013 qreal QPaintEngineState::opacity() const
9015 return static_cast<const QPainterState *>(this)->opacity;
9021 Sets the world transformation matrix.
9022 If \a combine is true, the specified \a transform is combined with
9023 the current matrix; otherwise it replaces the current matrix.
9025 \sa transform() setWorldTransform()
9028 void QPainter::setTransform(const QTransform &transform, bool combine )
9030 setWorldTransform(transform, combine);
9034 Returns the world transformation matrix.
9036 \sa worldTransform()
9039 const QTransform & QPainter::transform() const
9041 return worldTransform();
9046 Returns the matrix that transforms from logical coordinates to
9047 device coordinates of the platform dependent paint device.
9049 This function is \e only needed when using platform painting
9050 commands on the platform dependent handle (Qt::HANDLE), and the
9051 platform does not do transformations nativly.
9053 The QPaintEngine::PaintEngineFeature enum can be queried to
9054 determine whether the platform performs the transformations or
9057 \sa worldTransform(), QPaintEngine::hasFeature(),
9060 const QTransform & QPainter::deviceTransform() const
9062 Q_D(const QPainter);
9064 qWarning("QPainter::deviceTransform: Painter not active");
9065 return d->fakeState()->transform;
9067 return d->state->matrix;
9072 Resets any transformations that were made using translate(),
9073 scale(), shear(), rotate(), setWorldTransform(), setViewport()
9076 \sa {Coordinate Transformations}
9079 void QPainter::resetTransform()
9082 #ifdef QT_DEBUG_DRAW
9083 if (qt_show_painter_debug_output)
9084 printf("QPainter::resetMatrix()\n");
9087 qWarning("QPainter::resetMatrix: Painter not active");
9091 d->state->wx = d->state->wy = d->state->vx = d->state->vy = 0; // default view origins
9092 d->state->ww = d->state->vw = d->device->metric(QPaintDevice::PdmWidth);
9093 d->state->wh = d->state->vh = d->device->metric(QPaintDevice::PdmHeight);
9094 d->state->worldMatrix = QTransform();
9095 setMatrixEnabled(false);
9096 setViewTransformEnabled(false);
9098 d->extended->transformChanged();
9100 d->state->dirtyFlags |= QPaintEngine::DirtyTransform;
9104 Sets the world transformation matrix.
9105 If \a combine is true, the specified \a matrix is combined with the current matrix;
9106 otherwise it replaces the current matrix.
9108 \sa transform(), setTransform()
9111 void QPainter::setWorldTransform(const QTransform &matrix, bool combine )
9116 qWarning("QPainter::setWorldTransform: Painter not active");
9121 d->state->worldMatrix = matrix * d->state->worldMatrix; // combines
9123 d->state->worldMatrix = matrix; // set new matrix
9125 d->state->WxF = true;
9130 Returns the world transformation matrix.
9133 const QTransform & QPainter::worldTransform() const
9135 Q_D(const QPainter);
9137 qWarning("QPainter::worldTransform: Painter not active");
9138 return d->fakeState()->transform;
9140 return d->state->worldMatrix;
9144 Returns the transformation matrix combining the current
9145 window/viewport and world transformation.
9147 \sa setWorldTransform(), setWindow(), setViewport()
9150 QTransform QPainter::combinedTransform() const
9152 Q_D(const QPainter);
9154 qWarning("QPainter::combinedTransform: Painter not active");
9155 return QTransform();
9157 return d->state->worldMatrix * d->viewTransform();
9163 This function is used to draw \a pixmap, or a sub-rectangle of \a pixmap,
9164 at multiple positions with different scale, rotation and opacity. \a
9165 fragments is an array of \a fragmentCount elements specifying the
9166 parameters used to draw each pixmap fragment. The \a hints
9167 parameter can be used to pass in drawing hints.
9169 This function is potentially faster than multiple calls to drawPixmap(),
9170 since the backend can optimize state changes.
9172 \sa QPainter::PixmapFragment, QPainter::PixmapFragmentHint
9175 void QPainter::drawPixmapFragments(const PixmapFragment *fragments, int fragmentCount,
9176 const QPixmap &pixmap, PixmapFragmentHints hints)
9180 if (!d->engine || pixmap.isNull())
9184 for (int i = 0; i < fragmentCount; ++i) {
9185 QRectF sourceRect(fragments[i].sourceLeft, fragments[i].sourceTop,
9186 fragments[i].width, fragments[i].height);
9187 if (!(QRectF(pixmap.rect()).contains(sourceRect)))
9188 qWarning("QPainter::drawPixmapFragments - the source rect is not contained by the pixmap's rectangle");
9192 if (d->engine->isExtended()) {
9193 d->extended->drawPixmapFragments(fragments, fragmentCount, pixmap, hints);
9195 qreal oldOpacity = opacity();
9196 QTransform oldTransform = transform();
9198 for (int i = 0; i < fragmentCount; ++i) {
9199 QTransform transform = oldTransform;
9202 if (fragments[i].rotation == 0) {
9203 xOffset = fragments[i].x;
9204 yOffset = fragments[i].y;
9206 transform.translate(fragments[i].x, fragments[i].y);
9207 transform.rotate(fragments[i].rotation);
9209 setOpacity(oldOpacity * fragments[i].opacity);
9210 setTransform(transform);
9212 qreal w = fragments[i].scaleX * fragments[i].width;
9213 qreal h = fragments[i].scaleY * fragments[i].height;
9214 QRectF sourceRect(fragments[i].sourceLeft, fragments[i].sourceTop,
9215 fragments[i].width, fragments[i].height);
9216 drawPixmap(QRectF(-0.5 * w + xOffset, -0.5 * h + yOffset, w, h), pixmap, sourceRect);
9219 setOpacity(oldOpacity);
9220 setTransform(oldTransform);
9226 \class QPainter::PixmapFragment
9228 \brief This class is used in conjunction with the
9229 QPainter::drawPixmapFragments() function to specify how a pixmap, or
9230 sub-rect of a pixmap, is drawn.
9232 The \a sourceLeft, \a sourceTop, \a width and \a height variables are used
9233 as a source rectangle within the pixmap passed into the
9234 QPainter::drawPixmapFragments() function. The variables \a x, \a y, \a
9235 width and \a height are used to calculate the target rectangle that is
9236 drawn. \a x and \a y denotes the center of the target rectangle. The \a
9237 width and \a height in the target rectangle is scaled by the \a scaleX and
9238 \a scaleY values. The resulting target rectangle is then rotated \a
9239 rotation degrees around the \a x, \a y center point.
9241 \sa QPainter::drawPixmapFragments()
9247 This is a convenience function that returns a QPainter::PixmapFragment that is
9248 initialized with the \a pos, \a sourceRect, \a scaleX, \a scaleY, \a
9249 rotation, \a opacity parameters.
9252 QPainter::PixmapFragment QPainter::PixmapFragment::create(const QPointF &pos, const QRectF &sourceRect,
9253 qreal scaleX, qreal scaleY, qreal rotation,
9256 PixmapFragment fragment = {pos.x(), pos.y(), sourceRect.x(), sourceRect.y(), sourceRect.width(),
9257 sourceRect.height(), scaleX, scaleY, rotation, opacity};
9262 \variable QPainter::PixmapFragment::x
9263 \brief the x coordinate of center point in the target rectangle.
9267 \variable QPainter::PixmapFragment::y
9268 \brief the y coordinate of the center point in the target rectangle.
9272 \variable QPainter::PixmapFragment::sourceLeft
9273 \brief the left coordinate of the source rectangle.
9277 \variable QPainter::PixmapFragment::sourceTop
9278 \brief the top coordinate of the source rectangle.
9282 \variable QPainter::PixmapFragment::width
9284 \brief the width of the source rectangle and is used to calculate the width
9285 of the target rectangle.
9289 \variable QPainter::PixmapFragment::height
9291 \brief the height of the source rectangle and is used to calculate the
9292 height of the target rectangle.
9296 \variable QPainter::PixmapFragment::scaleX
9297 \brief the horizontal scale of the target rectangle.
9301 \variable QPainter::PixmapFragment::scaleY
9302 \brief the vertical scale of the target rectangle.
9306 \variable QPainter::PixmapFragment::rotation
9308 \brief the rotation of the target rectangle in degrees. The target
9309 rectangle is rotated after it has been scaled.
9313 \variable QPainter::PixmapFragment::opacity
9315 \brief the opacity of the target rectangle, where 0.0 is fully transparent
9316 and 1.0 is fully opaque.
9322 \enum QPainter::PixmapFragmentHint
9324 \value OpaqueHint Indicates that the pixmap fragments to be drawn are
9325 opaque. Opaque fragments are potentially faster to draw.
9327 \sa QPainter::drawPixmapFragments(), QPainter::PixmapFragment
9330 void qt_draw_helper(QPainterPrivate *p, const QPainterPath &path, QPainterPrivate::DrawOperation operation)
9332 p->draw_helper(path, operation);
9335 /*! \fn Display *QPaintDevice::x11Display() const
9336 Use QX11Info::display() instead.
9339 Display *display = widget->x11Display();
9341 Display *display = QX11Info::display();
9344 \sa QWidget::x11Info(), QX11Info::display()
9347 /*! \fn int QPaintDevice::x11Screen() const
9348 Use QX11Info::screen() instead.
9351 int screen = widget->x11Screen();
9353 int screen = widget->x11Info().screen();
9356 \sa QWidget::x11Info(), QPixmap::x11Info()
9359 /*! \fn void *QPaintDevice::x11Visual() const
9360 Use QX11Info::visual() instead.
9363 void *visual = widget->x11Visual();
9365 void *visual = widget->x11Info().visual();
9368 \sa QWidget::x11Info(), QPixmap::x11Info()
9371 /*! \fn int QPaintDevice::x11Depth() const
9372 Use QX11Info::depth() instead.
9375 int depth = widget->x11Depth();
9377 int depth = widget->x11Info().depth();
9380 \sa QWidget::x11Info(), QPixmap::x11Info()
9383 /*! \fn int QPaintDevice::x11Cells() const
9384 Use QX11Info::cells() instead.
9387 int cells = widget->x11Cells();
9389 int cells = widget->x11Info().cells();
9392 \sa QWidget::x11Info(), QPixmap::x11Info()
9395 /*! \fn Qt::HANDLE QPaintDevice::x11Colormap() const
9396 Use QX11Info::colormap() instead.
9399 unsigned long screen = widget->x11Colormap();
9401 unsigned long screen = widget->x11Info().colormap();
9404 \sa QWidget::x11Info(), QPixmap::x11Info()
9407 /*! \fn bool QPaintDevice::x11DefaultColormap() const
9408 Use QX11Info::defaultColormap() instead.
9411 bool isDefault = widget->x11DefaultColormap();
9413 bool isDefault = widget->x11Info().defaultColormap();
9416 \sa QWidget::x11Info(), QPixmap::x11Info()
9419 /*! \fn bool QPaintDevice::x11DefaultVisual() const
9420 Use QX11Info::defaultVisual() instead.
9423 bool isDefault = widget->x11DefaultVisual();
9425 bool isDefault = widget->x11Info().defaultVisual();
9428 \sa QWidget::x11Info(), QPixmap::x11Info()
9431 /*! \fn void *QPaintDevice::x11AppVisual(int screen)
9432 Use QX11Info::visual() instead.
9435 void *visual = QPaintDevice::x11AppVisual(screen);
9437 void *visual = qApp->x11Info(screen).visual();
9440 \sa QWidget::x11Info(), QPixmap::x11Info()
9443 /*! \fn Qt::HANDLE QPaintDevice::x11AppColormap(int screen)
9444 Use QX11Info::colormap() instead.
9447 unsigned long colormap = QPaintDevice::x11AppColormap(screen);
9449 unsigned long colormap = qApp->x11Info(screen).colormap();
9452 \sa QWidget::x11Info(), QPixmap::x11Info()
9455 /*! \fn Display *QPaintDevice::x11AppDisplay()
9456 Use QX11Info::display() instead.
9459 Display *display = QPaintDevice::x11AppDisplay();
9461 Display *display = qApp->x11Info().display();
9464 \sa QWidget::x11Info(), QPixmap::x11Info()
9467 /*! \fn int QPaintDevice::x11AppScreen()
9468 Use QX11Info::screen() instead.
9471 int screen = QPaintDevice::x11AppScreen();
9473 int screen = qApp->x11Info().screen();
9476 \sa QWidget::x11Info(), QPixmap::x11Info()
9479 /*! \fn int QPaintDevice::x11AppDepth(int screen)
9480 Use QX11Info::depth() instead.
9483 int depth = QPaintDevice::x11AppDepth(screen);
9485 int depth = qApp->x11Info(screen).depth();
9488 \sa QWidget::x11Info(), QPixmap::x11Info()
9491 /*! \fn int QPaintDevice::x11AppCells(int screen)
9492 Use QX11Info::cells() instead.
9495 int cells = QPaintDevice::x11AppCells(screen);
9497 int cells = qApp->x11Info(screen).cells();
9500 \sa QWidget::x11Info(), QPixmap::x11Info()
9503 /*! \fn Qt::HANDLE QPaintDevice::x11AppRootWindow(int screen)
9504 Use QX11Info::appRootWindow() instead.
9507 unsigned long window = QPaintDevice::x11AppRootWindow(screen);
9509 unsigned long window = qApp->x11Info(screen).appRootWindow();
9512 \sa QWidget::x11Info(), QPixmap::x11Info()
9515 /*! \fn bool QPaintDevice::x11AppDefaultColormap(int screen)
9516 Use QX11Info::defaultColormap() instead.
9519 bool isDefault = QPaintDevice::x11AppDefaultColormap(screen);
9521 bool isDefault = qApp->x11Info(screen).defaultColormap();
9524 \sa QWidget::x11Info(), QPixmap::x11Info()
9527 /*! \fn bool QPaintDevice::x11AppDefaultVisual(int screen)
9528 Use QX11Info::defaultVisual() instead.
9531 bool isDefault = QPaintDevice::x11AppDefaultVisual(screen);
9533 bool isDefault = qApp->x11Info(screen).defaultVisual();
9536 \sa QWidget::x11Info(), QPixmap::x11Info()
9539 /*! \fn void QPaintDevice::x11SetAppDpiX(int dpi, int screen)
9540 Use QX11Info::setAppDpiX() instead.
9543 /*! \fn void QPaintDevice::x11SetAppDpiY(int dpi, int screen)
9544 Use QX11Info::setAppDpiY() instead.
9547 /*! \fn int QPaintDevice::x11AppDpiX(int screen)
9548 Use QX11Info::appDpiX() instead.
9551 bool isDefault = QPaintDevice::x11AppDpiX(screen);
9553 bool isDefault = qApp->x11Info(screen).appDpiX();
9556 \sa QWidget::x11Info(), QPixmap::x11Info()
9559 /*! \fn int QPaintDevice::x11AppDpiY(int screen)
9560 Use QX11Info::appDpiY() instead.
9563 bool isDefault = QPaintDevice::x11AppDpiY(screen);
9565 bool isDefault = qApp->x11Info(screen).appDpiY();
9568 \sa QWidget::x11Info(), QPixmap::x11Info()
9571 /*! \fn HDC QPaintDevice::getDC() const
9575 /*! \fn void QPaintDevice::releaseDC(HDC) const
9579 /*! \fn QWSDisplay *QPaintDevice::qwsDisplay()