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();
82 QwtGraphic::RenderHints renderHints,
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 )
516 if ( sx == 1.0 && sy == 1.0 )
521 QTransform transform;
522 transform.scale( sx, sy );
555 const double w =
qwtMaxF( 0.0, size.width() );
556 const double h =
qwtMaxF( 0.0, size.height() );
598 return sz.height() * width / sz.width();
617 return sz.width() * height / sz.height();
637 const QTransform transform = painter->transform();
641 for (
int i = 0; i < numCommands; i++ )
661 Qt::AspectRatioMode aspectRatioMode )
const 663 const QRectF r( 0.0, 0.0, size.width(), size.height() );
664 render( painter, r, aspectRatioMode );
677 Qt::AspectRatioMode aspectRatioMode )
const 679 if (
isEmpty() || rect.isEmpty() )
710 if ( aspectRatioMode == Qt::KeepAspectRatio )
716 else if ( aspectRatioMode == Qt::KeepAspectRatioByExpanding )
724 tr.translate( rect.center().x() - 0.5 * sx *
m_data->
pointRect.width(),
729 const QTransform transform = painter->transform();
731 painter->setTransform( tr,
true );
733 if ( !scalePens && transform.isScaling() )
739 QTransform initialTransform;
740 initialTransform.scale( transform.m11(), transform.m22() );
749 painter->setTransform( transform );
763 const QPointF& pos, Qt::Alignment alignment )
const 767 if ( alignment & Qt::AlignLeft )
769 r.moveLeft( pos.x() );
771 else if ( alignment & Qt::AlignHCenter )
773 r.moveCenter( QPointF( pos.x(), r.center().y() ) );
775 else if ( alignment & Qt::AlignRight )
777 r.moveRight( pos.x() );
780 if ( alignment & Qt::AlignTop )
782 r.moveTop( pos.y() );
784 else if ( alignment & Qt::AlignVCenter )
786 r.moveCenter( QPointF( r.center().x(), pos.y() ) );
788 else if ( alignment & Qt::AlignBottom )
790 r.moveBottom( pos.y() );
819 const int w =
qwtCeil( sz.width() );
820 const int h =
qwtCeil( sz.height() );
822 QPixmap pixmap( w, h );
824 #if QT_VERSION >= 0x050000 825 if ( devicePixelRatio <= 0.0 )
826 devicePixelRatio = qwtDevicePixelRatio();
828 pixmap.setDevicePixelRatio( devicePixelRatio );
830 Q_UNUSED( devicePixelRatio )
833 pixmap.fill( Qt::transparent );
835 const QRectF r( 0.0, 0.0, sz.width(), sz.height() );
837 QPainter painter( &pixmap );
838 render( &painter, r, Qt::KeepAspectRatio );
860 Qt::AspectRatioMode aspectRatioMode, qreal devicePixelRatio )
const 862 QPixmap pixmap( size );
864 #if QT_VERSION >= 0x050000 865 if ( devicePixelRatio <= 0.0 )
866 devicePixelRatio = qwtDevicePixelRatio();
868 pixmap.setDevicePixelRatio( devicePixelRatio );
870 Q_UNUSED( devicePixelRatio )
872 pixmap.fill( Qt::transparent );
874 const QRect r( 0, 0, size.width(), size.height() );
876 QPainter painter( &pixmap );
877 render( &painter, r, aspectRatioMode );
901 Qt::AspectRatioMode aspectRatioMode, qreal devicePixelRatio )
const 903 #if QT_VERSION >= 0x050000 904 if ( devicePixelRatio <= 0.0 )
905 devicePixelRatio = qwtDevicePixelRatio();
907 QImage image( size* devicePixelRatio, QImage::Format_ARGB32_Premultiplied );
908 image.setDevicePixelRatio( devicePixelRatio );
910 Q_UNUSED( devicePixelRatio )
911 QImage image( size, QImage::Format_ARGB32_Premultiplied );
916 const QRect r( 0, 0, size.width(), size.height() );
918 QPainter painter( &image );
919 render( &painter, r, aspectRatioMode );
951 int h =
qwtCeil( sz.height() );
953 #if QT_VERSION >= 0x050000 954 if ( devicePixelRatio <= 0.0 )
955 devicePixelRatio = qwtDevicePixelRatio();
957 w *= devicePixelRatio;
958 h *= devicePixelRatio;
960 QImage image( w, h, QImage::Format_ARGB32 );
961 image.setDevicePixelRatio( devicePixelRatio );
963 Q_UNUSED( devicePixelRatio )
964 QImage image( w, h, QImage::Format_ARGB32 );
969 const QRect r( 0, 0, sz.width(), sz.height() );
971 QPainter painter( &image );
972 render( &painter, r, Qt::KeepAspectRatio );
986 const QPainter* painter =
paintEngine()->painter();
987 if ( painter == NULL )
993 if ( !path.isEmpty() )
995 const QPainterPath scaledPath = painter->transform().map( path );
997 QRectF pointRect = scaledPath.boundingRect();
1000 if ( painter->pen().style() != Qt::NoPen
1001 && painter->pen().brush().style() != Qt::NoBrush )
1024 const QPixmap& pixmap,
const QRectF& subRect )
1026 const QPainter* painter =
paintEngine()->painter();
1027 if ( painter == NULL )
1033 const QRectF r = painter->transform().mapRect( rect );
1049 const QRectF& subRect, Qt::ImageConversionFlags flags )
1051 const QPainter* painter =
paintEngine()->painter();
1052 if ( painter == NULL )
1058 const QRectF r = painter->transform().mapRect( rect );
1074 if ( state.state() & QPaintEngine::DirtyTransform )
1083 if ( state.transform().isScaling() )
1093 const QPainter* painter =
paintEngine()->painter();
1094 if ( painter && painter->hasClipping() )
1096 QRectF cr = painter->clipRegion().boundingRect();
1097 cr = painter->transform().mapRect( cr );
1135 const int numCommands = commands.size();
1136 if ( numCommands <= 0 )
1144 const QTransform noTransform;
1145 const RenderHints noRenderHints;
1147 QPainter painter(
this );
1148 for (
int i = 0; i < numCommands; i++ )
1149 qwtExecCommand( &painter, cmds[i], noRenderHints, noTransform, NULL );
QwtGraphic & operator=(const QwtGraphic &)
Assignment operator.
QRectF boundingRect() const
Qt::ImageConversionFlags flags
QRectF scaledBoundingRect(qreal sx, qreal sy) const
Calculate the target rectangle for scaling the graphic.
Attributes how to paint a QPixmap.
QWT_CONSTEXPR float qwtMaxF(float a, float b)
PathInfo(const QRectF &pointRect, const QRectF &boundingRect, bool scalablePen)
RenderHints renderHints() const
bool testRenderHint(RenderHint) const
QWT_CONSTEXPR float qwtMinF(float a, float b)
const QVector< QwtPainterCommand > & commands() const
QPixmap toPixmap(qreal devicePixelRatio=0.0) const
Convert the graphic to a QPixmap.
The graphic contains scalable vector data.
CommandTypes commandTypes() const
void setRenderHint(RenderHint, bool on=true)
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.
void renderGraphic(QPainter *, QTransform *) const
QPainter::CompositionMode compositionMode
QPainter::RenderHints renderHints
PixmapData * pixmapData()
qreal widthForHeight(qreal height) const
void reset()
Clear all stored commands.
double scaleFactorX(const QRectF &pathRect, const QRectF &targetRect, bool scalePens) const
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
QImage toImage(qreal devicePixelRatio=0.0) const
Convert the graphic to a QImage.
Attributes of a state change.
QPaintEngine::DirtyFlags flags
static QRectF qwtStrokedPathRect(const QPainter *painter, const QPainterPath &path)
The graphic contains raster data ( QPixmap or QImage )
Attributes how to paint a QImage.
void render(QPainter *) const
Replay all recorded painter commands.
virtual void updateState(const QPaintEngineState &) QWT_OVERRIDE
Store a state command in the command list.
static bool qwtHasScalablePen(const QPainter *painter)
QwtGraphic::RenderHints renderHints
QRectF scaledBoundingRect(qreal sx, qreal sy, bool scalePens) const
virtual QSize sizeMetrics() const QWT_OVERRIDE
virtual QPaintEngine * paintEngine() const QWT_OVERRIDE
See QPaintDevice::paintEngine()
span_constexpr std::size_t size(span< T, Extent > const &spn)
void updateBoundingRect(const QRectF &)
virtual void drawPath(const QPainterPath &) QWT_OVERRIDE
QVector< QwtGraphic::PathInfo > pathInfos
QSizeF defaultSize() const
Default size.
double scaleFactorY(const QRectF &pathRect, const QRectF &targetRect, bool scalePens) const
qreal heightForWidth(qreal width) const
QRectF controlPointRect() const
void setCommands(const QVector< QwtPainterCommand > &)
Append paint commands.
virtual void drawImage(const QRectF &, const QImage &, const QRectF &, Qt::ImageConversionFlags) QWT_OVERRIDE
Store a image command in the command list.