16 #include <qpaintengine.h> 19 #include <qpainterpath.h> 21 #if QT_VERSION >= 0x050000 23 #include <qguiapplication.h> 25 static inline qreal qwtDevicePixelRatio()
27 return qGuiApp ? qGuiApp->devicePixelRatio() : 1.0;
34 const QPen pen = painter->pen();
36 bool scalablePen =
false;
38 if ( pen.style() != Qt::NoPen && pen.brush().style() != Qt::NoBrush )
40 scalablePen = !pen.isCosmetic();
41 #if QT_VERSION < 0x050000 42 if ( !scalablePen && pen.widthF() == 0.0 )
44 const QPainter::RenderHints hints = painter->renderHints();
45 if ( hints.testFlag( QPainter::NonCosmeticDefaultPen ) )
55 const QPainter *painter,
const QPainterPath &path )
57 QPainterPathStroker stroker;
58 stroker.setWidth( painter->pen().widthF() );
59 stroker.setCapStyle( painter->pen().capStyle() );
60 stroker.setJoinStyle( painter->pen().joinStyle() );
61 stroker.setMiterLimit( painter->pen().miterLimit() );
66 QPainterPath stroke = stroker.createStroke( path );
67 rect = painter->transform().map( stroke ).boundingRect();
71 QPainterPath mappedPath = painter->transform().map( path );
72 mappedPath = stroker.createStroke( mappedPath );
74 rect = mappedPath.boundingRect();
83 const QTransform &transform,
84 const QTransform *initialTransform )
92 if ( painter->transform().isScaling() )
94 bool isCosmetic = painter->pen().isCosmetic();
95 #if QT_VERSION < 0x050000 96 if ( isCosmetic && painter->pen().widthF() == 0.0 )
98 QPainter::RenderHints hints = painter->renderHints();
99 if ( hints.testFlag( QPainter::NonCosmeticDefaultPen ) )
109 doMap = painter->paintEngine()->type() == QPaintEngine::OpenGL2;
119 const QTransform tr = painter->transform();
121 painter->resetTransform();
123 QPainterPath path = tr.map( *cmd.
path() );
124 if ( initialTransform )
126 painter->setTransform( *initialTransform );
127 path = initialTransform->inverted().map( path );
130 painter->drawPath( path );
132 painter->setTransform( tr );
136 painter->drawPath( *cmd.
path() );
149 painter->drawImage( data->
rect, data->
image,
157 if ( data->
flags & QPaintEngine::DirtyPen )
158 painter->setPen( data->
pen );
160 if ( data->
flags & QPaintEngine::DirtyBrush )
161 painter->setBrush( data->
brush );
163 if ( data->
flags & QPaintEngine::DirtyBrushOrigin )
166 if ( data->
flags & QPaintEngine::DirtyFont )
167 painter->setFont( data->
font );
169 if ( data->
flags & QPaintEngine::DirtyBackground )
175 if ( data->
flags & QPaintEngine::DirtyTransform )
177 painter->setTransform( data->
transform * transform );
180 if ( data->
flags & QPaintEngine::DirtyClipEnabled )
183 if ( data->
flags & QPaintEngine::DirtyClipRegion )
189 if ( data->
flags & QPaintEngine::DirtyClipPath )
194 if ( data->
flags & QPaintEngine::DirtyHints )
196 for (
int i = 0; i < 8; i++ )
198 const QPainter::RenderHint hint =
static_cast< QPainter::RenderHint
>( 1 << i );
199 painter->setRenderHint( hint, data->
renderHints.testFlag( hint ) );
203 if ( data->
flags & QPaintEngine::DirtyCompositionMode )
206 if ( data->
flags & QPaintEngine::DirtyOpacity )
207 painter->setOpacity( data->
opacity );
235 if ( sx == 1.0 && sy == 1.0 )
238 QTransform transform;
239 transform.scale( sx, sy );
255 rect.adjust( -l, -t, r, b );
262 const QRectF &targetRect,
bool scalePens )
const 264 if ( pathRect.width() <= 0.0 )
269 const qreal l = qAbs( pathRect.left() - p0.x() );
270 const qreal r = qAbs( pathRect.right() - p0.x() );
272 const double w = 2.0 *
qwtMinF( l, r )
273 * targetRect.width() / pathRect.width();
293 const QRectF &targetRect,
bool scalePens )
const 295 if ( pathRect.height() <= 0.0 )
300 const qreal t = qAbs( pathRect.top() - p0.y() );
301 const qreal b = qAbs( pathRect.bottom() - p0.y() );
303 const qreal h = 2.0 *
qwtMinF( t, b )
304 * targetRect.height() / pathRect.height();
334 pointRect( 0.0, 0.0, -1.0, -1.0 )
518 if ( sx == 1.0 && sy == 1.0 )
523 QTransform transform;
524 transform.scale( sx, sy );
557 const double w =
qwtMaxF( 0.0, size.width() );
558 const double h =
qwtMaxF( 0.0, size.height() );
600 return sz.height() * width / sz.width();
619 return sz.width() * height / sz.height();
639 const QTransform transform = painter->transform();
643 for (
int i = 0; i < numCommands; i++ )
663 Qt::AspectRatioMode aspectRatioMode )
const 665 const QRectF r( 0.0, 0.0, size.width(), size.height() );
666 render( painter, r, aspectRatioMode );
679 Qt::AspectRatioMode aspectRatioMode )
const 681 if (
isEmpty() || rect.isEmpty() )
712 if ( aspectRatioMode == Qt::KeepAspectRatio )
714 const qreal s =
qwtMinF( sx, sy );
718 else if ( aspectRatioMode == Qt::KeepAspectRatioByExpanding )
720 const qreal s =
qwtMaxF( sx, sy );
726 tr.translate( rect.center().x() - 0.5 * sx *
d_data->
pointRect.width(),
731 const QTransform transform = painter->transform();
733 painter->setTransform( tr,
true );
735 if ( !scalePens && transform.isScaling() )
741 QTransform initialTransform;
742 initialTransform.scale( transform.m11(), transform.m22() );
751 painter->setTransform( transform );
765 const QPointF &pos, Qt::Alignment alignment )
const 769 if ( alignment & Qt::AlignLeft )
771 r.moveLeft( pos.x() );
773 else if ( alignment & Qt::AlignHCenter )
775 r.moveCenter( QPointF( pos.x(), r.center().y() ) );
777 else if ( alignment & Qt::AlignRight )
779 r.moveRight( pos.x() );
782 if ( alignment & Qt::AlignTop )
784 r.moveTop( pos.y() );
786 else if ( alignment & Qt::AlignVCenter )
788 r.moveCenter( QPointF( r.center().x(), pos.y() ) );
790 else if ( alignment & Qt::AlignBottom )
792 r.moveBottom( pos.y() );
821 const int w =
qwtCeil( sz.width() );
822 const int h =
qwtCeil( sz.height() );
824 QPixmap pixmap( w, h );
826 #if QT_VERSION >= 0x050000 827 if ( devicePixelRatio <= 0.0 )
828 devicePixelRatio = qwtDevicePixelRatio();
830 pixmap.setDevicePixelRatio( devicePixelRatio );
832 Q_UNUSED( devicePixelRatio )
835 pixmap.fill( Qt::transparent );
837 const QRectF r( 0.0, 0.0, sz.width(), sz.height() );
839 QPainter painter( &pixmap );
840 render( &painter, r, Qt::KeepAspectRatio );
862 Qt::AspectRatioMode aspectRatioMode, qreal devicePixelRatio )
const 864 QPixmap pixmap( size );
866 #if QT_VERSION >= 0x050000 867 if ( devicePixelRatio <= 0.0 )
868 devicePixelRatio = qwtDevicePixelRatio();
870 pixmap.setDevicePixelRatio( devicePixelRatio );
872 Q_UNUSED( devicePixelRatio )
874 pixmap.fill( Qt::transparent );
876 const QRect r( 0, 0, size.width(), size.height() );
878 QPainter painter( &pixmap );
879 render( &painter, r, aspectRatioMode );
903 Qt::AspectRatioMode aspectRatioMode, qreal devicePixelRatio )
const 905 #if QT_VERSION >= 0x050000 906 if ( devicePixelRatio <= 0.0 )
907 devicePixelRatio = qwtDevicePixelRatio();
909 QImage image( size * devicePixelRatio, QImage::Format_ARGB32_Premultiplied );
910 image.setDevicePixelRatio( devicePixelRatio );
912 Q_UNUSED( devicePixelRatio )
913 QImage image( size, QImage::Format_ARGB32_Premultiplied );
918 const QRect r( 0, 0, size.width(), size.height() );
920 QPainter painter( &image );
921 render( &painter, r, aspectRatioMode );
953 int h =
qwtCeil( sz.height() );
955 #if QT_VERSION >= 0x050000 956 if ( devicePixelRatio <= 0.0 )
957 devicePixelRatio = qwtDevicePixelRatio();
959 w *= devicePixelRatio;
960 h *= devicePixelRatio;
962 QImage image( w, h, QImage::Format_ARGB32 );
963 image.setDevicePixelRatio( devicePixelRatio );
965 Q_UNUSED( devicePixelRatio )
966 QImage image( w, h, QImage::Format_ARGB32 );
971 const QRect r( 0, 0, sz.width(), sz.height() );
973 QPainter painter( &image );
974 render( &painter, r, Qt::KeepAspectRatio );
988 const QPainter *painter =
paintEngine()->painter();
989 if ( painter == NULL )
995 if ( !path.isEmpty() )
997 const QPainterPath scaledPath = painter->transform().map( path );
999 QRectF pointRect = scaledPath.boundingRect();
1002 if ( painter->pen().style() != Qt::NoPen
1003 && painter->pen().brush().style() != Qt::NoBrush )
1026 const QPixmap &pixmap,
const QRectF &subRect )
1028 const QPainter *painter =
paintEngine()->painter();
1029 if ( painter == NULL )
1035 const QRectF r = painter->transform().mapRect( rect );
1051 const QRectF &subRect, Qt::ImageConversionFlags flags )
1053 const QPainter *painter =
paintEngine()->painter();
1054 if ( painter == NULL )
1060 const QRectF r = painter->transform().mapRect( rect );
1076 if ( state.state() & QPaintEngine::DirtyTransform )
1085 if ( state.transform().isScaling() )
1095 const QPainter *painter =
paintEngine()->painter();
1096 if ( painter && painter->hasClipping() )
1098 QRectF cr = painter->clipRegion().boundingRect();
1099 cr = painter->transform().mapRect( cr );
1137 const int numCommands = commands.size();
1138 if ( numCommands <= 0 )
1149 QPainter painter(
this );
1150 for (
int i = 0; i < numCommands; i++ )
1151 qwtExecCommand( &painter, cmds[i], noRenderHints, noTransform, NULL );
QImage toImage(qreal devicePixelRatio=0.0) const
Convert the graphic to a QImage.
QwtGraphic & operator=(const QwtGraphic &)
Assignment operator.
QRectF scaledBoundingRect(qreal sx, qreal sy, bool scalePens) const
void render(QPainter *) const
Replay all recorded painter commands.
Qt::ImageConversionFlags flags
double scaleFactorX(const QRectF &pathRect, const QRectF &targetRect, bool scalePens) const
CommandTypes commandTypes() const
qreal heightForWidth(qreal width) const
Attributes how to paint a QPixmap.
QWT_CONSTEXPR float qwtMaxF(float a, float b)
PathInfo(const QRectF &pointRect, const QRectF &boundingRect, bool scalablePen)
QRectF scaledBoundingRect(qreal sx, qreal sy) const
Calculate the target rectangle for scaling the graphic.
A null paint device doing nothing.
QSizeF defaultSize() const
Default size.
QRectF boundingRect() const
QWT_CONSTEXPR float qwtMinF(float a, float b)
The graphic contains scalable vector data.
void setRenderHint(RenderHint, bool on=true)
QRectF controlPointRect() const
static void qwtExecCommand(QPainter *painter, const QwtPainterCommand &cmd, QwtGraphic::RenderHints renderHints, const QTransform &transform, const QTransform *initialTransform)
void setDefaultSize(const QSizeF &)
Set a default size.
Qt::BGMode backgroundMode
virtual ~QwtGraphic()
Destructor.
virtual void drawPixmap(const QRectF &, const QPixmap &, const QRectF &) QWT_OVERRIDE
Store a pixmap command in the command list.
QFlags< CommandType > CommandTypes
QPainter::CompositionMode compositionMode
RenderHints renderHints() const
QPainter::RenderHints renderHints
PixmapData * pixmapData()
void renderGraphic(QPainter *, QTransform *) const
void reset()
Clear all stored commands.
A paint device for scalable graphics.
The graphic contains transformations beyond simple translations.
Qt::ClipOperation clipOperation
QwtGraphic::CommandTypes commandTypes
void updateControlPointRect(const QRectF &)
QVector< QwtPainterCommand > commands
Attributes of a state change.
QPaintEngine::DirtyFlags flags
double scaleFactorY(const QRectF &pathRect, const QRectF &targetRect, bool scalePens) const
static QRectF qwtStrokedPathRect(const QPainter *painter, const QPainterPath &path)
The graphic contains raster data ( QPixmap or QImage )
QPixmap toPixmap(qreal devicePixelRatio=0.0) const
Convert the graphic to a QPixmap.
Attributes how to paint a QImage.
bool testRenderHint(RenderHint) const
virtual void updateState(const QPaintEngineState &) QWT_OVERRIDE
Store a state command in the command list.
qreal widthForHeight(qreal height) const
static bool qwtHasScalablePen(const QPainter *painter)
const QVector< QwtPainterCommand > & commands() const
QwtGraphic::RenderHints renderHints
virtual QSize sizeMetrics() const QWT_OVERRIDE
virtual QPaintEngine * paintEngine() const QWT_OVERRIDE
See QPaintDevice::paintEngine()
void updateBoundingRect(const QRectF &)
virtual void drawPath(const QPainterPath &) QWT_OVERRIDE
QVector< QwtGraphic::PathInfo > pathInfos
void setCommands(const QVector< QwtPainterCommand > &)
Append paint commands.
QFlags< RenderHint > RenderHints
Render hints.
virtual void drawImage(const QRectF &, const QImage &, const QRectF &, Qt::ImageConversionFlags) QWT_OVERRIDE
Store a image command in the command list.