📄 qpaintengine.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qpaintengine.h"#include "qpaintengine_p.h"#include "qpainter_p.h"#include "qpolygon.h"#include "qbitmap.h"#include "qapplication.h"#include <qdebug.h>#include <private/qtextengine_p.h>#include <private/qmath_p.h>#include <qvarlengtharray.h>/*! \class QTextItem \internal*/qreal QTextItem::descent() const{ const QTextItemInt *ti = static_cast<const QTextItemInt *>(this); return ti->descent.toReal();}qreal QTextItem::ascent() const{ const QTextItemInt *ti = static_cast<const QTextItemInt *>(this); return ti->ascent.toReal();}qreal QTextItem::width() const{ const QTextItemInt *ti = static_cast<const QTextItemInt *>(this); return ti->width.toReal();}QTextItem::RenderFlags QTextItem::renderFlags() const{ const QTextItemInt *ti = static_cast<const QTextItemInt *>(this); return ti->flags;}QString QTextItem::text() const{ const QTextItemInt *ti = static_cast<const QTextItemInt *>(this); return QString(ti->chars, ti->num_chars);}QFont QTextItem::font() const{ const QTextItemInt *ti = static_cast<const QTextItemInt *>(this); return ti->f ? *ti->f : QApplication::font();}#include <private/qfontengine_p.h>/*! \class QPaintEngine \ingroup multimedia \brief The QPaintEngine class provides an abstract definition of how QPainter draws to a given device on a given platform. Qt 4.0 provides several premade implementations of QPaintEngine for the different painter backends we support. We provide one paint engine for each window system and painting framework we support. This includes X11 on Unix/Linux and CoreGraphics on Mac OS X. In addition we provide QPaintEngine implementations for OpenGL (accessible through QGLWidget) and PostScript (accessible through QPSPrinter on X11). Additionally there is a raster-based paint engine that is a fallback for when an engine does not support a certain capability. If one wants to use QPainter to draw to a different backend, one must subclass QPaintEngine and reimplement all its virtual functions. The QPaintEngine implementation is then made available by subclassing QPaintDevice and reimplementing the virtual function QPaintDevice::paintEngine(). QPaintEngine is created and owned by the QPaintDevice that created it. The big advantage of the QPaintEngine approach opposed to Qt 3's QPainter/QPaintDevice::cmd() approach is that it is now possible to adapt to multiple technologies on each platform and take advantage of each to the fullest. \sa QPainter, QPaintDevice::paintEngine(), {The Paint System}*//*! \enum QPaintEngine::PaintEngineFeature This enum is used to describe the features or capabilities that the paint engine has. If a feature is not supported by the engine, QPainter will do a best effort to emulate that feature through other means and pass on an alpha blended QImage to the engine with the emulated results. Some features cannot be emulated: AlphaBlend and PorterDuff. \value AlphaBlend The engine can alpha blend primitives. \value Antialiasing The engine can use antialising to improve the appearance of rendered primitives. \value BlendModes The engine supports blending modes. \value BrushStroke The engine supports drawing strokes that contain brushes as fills, not just solid colors (e.g. a dashed gradient line of width 2). \value ConicalGradientFill The engine supports conical gradient fills. \value ConstantOpacity The engine supports the feature provided by QPainter::setOpacity(). \value LinearGradientFill The engine supports linear gradient fills. \value MaskedBrush The engine is capable of rendering brushes that has a texture with an alpha channel or a mask. \value PainterPaths The engine has path support. \value PaintOutsidePaintEvent The engine is capable of painting outside of paint events. \value PatternBrush The engine is capable of rendering brushes with the brush patterns specified in Qt::BrushStyle. \value PatternTransform The engine has support for transforming brush patterns. \value PerspectiveTransform The engine has support for performing perspective transformations on primitives. \value PixmapTransform The engine can transform pixmaps, including rotation and shearing. \value PorterDuff The engine supports Porter-Duff operations \value PrimitiveTransform The engine has support for transforming drawing primitives. \value RadialGradientFill The engine supports radial gradient fills. \value AllFeatures All of the above features. This enum value is usually used as a bit mask.*//*! \enum QPaintEngine::PolygonDrawMode \value OddEvenMode The polygon should be drawn using OddEven fill rule. \value WindingMode The polygon should be drawn using Winding fill rule. \value ConvexMode The polygon is a convex polygon and can be drawn using specialized algorithms where available. \value PolylineMode Only the outline of the polygon should be drawn.*//*! \enum QPaintEngine::DirtyFlag \value DirtyPen The pen is dirty and needs to be updated. \value DirtyBrush The brush is dirty and needs to be updated. \value DirtyBrushOrigin The brush origin is dirty and needs to updated. \value DirtyFont The font is dirty and needs to be updated. \value DirtyBackground The background is dirty and needs to be updated. \value DirtyBackgroundMode The background mode is dirty and needs to be updated. \value DirtyTransform The transform is dirty and needs to be updated. \value DirtyClipRegion The clip region is dirty and needs to be updated. \value DirtyClipPath The clip path is dirty and needs to be updated. \value DirtyHints The render hints is dirty and needs to be updated. \value DirtyCompositionMode The composition mode is dirty and needs to be updated. \value DirtyClipEnabled Whether clipping is enabled or not is dirty and needs to be updated. \value DirtyOpacity The constant opacity has changed and needs to be updated as part of the state change in QPaintEngine::updateState(). \value AllDirty Convenience enum used internally. These types are used by QPainter to trigger lazy updates of the various states in the QPaintEngine using QPaintEngine::updateState(). A paint engine must update every dirty state.*//*! \fn void QPaintEngine::syncState() \internal Updates all dirty states in this engine. This function should ONLY be used when drawing with native handles directly and immediate sync from QPainters state to the native state is required.*/static QPaintEngine *qt_polygon_recursion = 0;struct QT_Point { int x; int y;};/*! \fn void QPaintEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode) Reimplement this virtual function to draw the polygon defined by the \a pointCount first points in \a points, using mode \a mode. The default implementation of this function will try to use drawPath if the engine supports the feature QPaintEngine::PainterPaths or try the float based drawPolygon() implementation if not.*/void QPaintEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode){ Q_ASSERT_X(qt_polygon_recursion != this, "QPaintEngine::drawPolygon", "At least one drawPolygon function must be implemented"); qt_polygon_recursion = this; Q_ASSERT(sizeof(QT_Point) == sizeof(QPoint)); QVarLengthArray<QT_Point> p(pointCount); for (int i = 0; i < pointCount; ++i) { p[i].x = qRound(points[i].x()); p[i].y = qRound(points[i].y()); } drawPolygon((QPoint *)p.data(), pointCount, mode); qt_polygon_recursion = 0;}struct QT_PointF { qreal x; qreal y;};/*! \overload Reimplement this virtual function to draw the polygon defined by the \a pointCount first points in \a points, using mode \a mode. The default implementation of this function will try to use drawPath() if the engine supports the feature QPaintEngine::PainterPaths or try the int based drawPolygon() implementation if not.*/void QPaintEngine::drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode){ Q_ASSERT_X(qt_polygon_recursion != this, "QPaintEngine::drawPolygon", "At least one drawPolygon function must be implemented"); qt_polygon_recursion = this; Q_ASSERT(sizeof(QT_PointF) == sizeof(QPointF)); QVarLengthArray<QT_PointF> p(pointCount); for (int i=0; i<pointCount; ++i) { p[i].x = points[i].x(); p[i].y = points[i].y(); } drawPolygon((QPointF *)p.data(), pointCount, mode); qt_polygon_recursion = 0;}/*! \enum QPaintEngine::Type \value X11 \value Windows \value MacPrinter \value CoreGraphics Mac OS X's Quartz2D (CoreGraphics) \value QuickDraw Mac OS X's QuickDraw \value QWindowSystem Qtopia Core \value PostScript \value OpenGL \value Picture QPicture format \value SVG Scalable Vector Graphics XML format \value Raster \value Direct3D Windows only, Direct3D based engine \value User First user type ID \value MaxUser Last user type ID*//*! \fn bool QPaintEngine::isActive() const Returns true if the paint engine is actively drawing; otherwise returns false. \sa setActive()*//*! \fn void QPaintEngine::setActive(bool state) Sets the active state of the paint engine to \a state. \sa isActive()*//*! \fn bool QPaintEngine::begin(QPaintDevice *pdev) Reimplement this function to initialise your paint engine when painting is to start on the paint device \a pdev. Return true if the initialization was successful; otherwise return false. \sa end() isActive()*//*! \fn bool QPaintEngine::end() Reimplement this function to finish painting on the current paint device. Return true if painting was finished successfully; otherwise return false. \sa begin() isActive()*//*! Draws the first \a pointCount points in the buffer \a points*/void QPaintEngine::drawPoints(const QPointF *points, int pointCount){ for (int i=0; i<pointCount; ++i) { QLineF line(points[i].x(), points[i].y(), points[i].x(), points[i].y() + 0.001); drawLines(&line, 1); }}/*! Draws the first \a pointCount points in the buffer \a points The default implementation converts the first \a pointCount QPoints in \a points to QPointFs and calls the floating point version of drawPoints.*/void QPaintEngine::drawPoints(const QPoint *points, int pointCount){ Q_ASSERT(sizeof(QT_PointF) == sizeof(QPointF)); QT_PointF fp[256]; while (pointCount) { int i = 0; while (i < pointCount && i < 256) { fp[i].x = points[i].x(); fp[i].y = points[i].y(); ++i; } drawPoints((QPointF *)(void *)fp, i); points += i; pointCount -= i; }}/*! \fn void QPaintEngine::drawEllipse(const QRectF &rect) Reimplement this function to draw the largest ellipse that can be contained within rectangle \a rect. The default implementation calls drawPolygon().*/void QPaintEngine::drawEllipse(const QRectF &rect){ QPainterPath path; path.addEllipse(rect); if (hasFeature(PainterPaths)) { drawPath(path); } else { QPolygonF polygon = path.toFillPolygon(); drawPolygon(polygon.data(), polygon.size(), ConvexMode); }}/*! The default implementation of this function calls the floating point version of this function*/void QPaintEngine::drawEllipse(const QRect &rect){ drawEllipse(QRectF(rect));}/*! \fn void QPaintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr) Reimplement this function to draw the part of the \a pm specified by the \a sr rectangle in the given \a r.*/void qt_fill_tile(QPixmap *tile, const QPixmap &pixmap){ QPainter p(tile); p.drawPixmap(0, 0, pixmap); int x = pixmap.width(); while (x < tile->width()) { p.drawPixmap(x, 0, *tile, 0, 0, x, pixmap.height()); x *= 2; } int y = pixmap.height(); while (y < tile->height()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -