40 return QStringLiteral(
"picture" );
46 bool fitsInCache =
false;
52 double aspectRatio = painterBounds.height() / painterBounds.width();
53 double svgWidth = painterBounds.width();
56 aspectRatio = svgViewbox.height() / svgViewbox.width();
57 if ( ( painterBounds.height() / painterBounds.width() ) < aspectRatio )
59 svgWidth = painterBounds.height() / aspectRatio;
66 const double pictureWidth = picture.boundingRect().width();
67 const double pictureHeight = picture.boundingRect().height();
70 if (
lockAspectRatio &&
static_cast< int >( painterBounds.width() ) > pictureWidth )
72 xOffset = ( painterBounds.width() - pictureWidth ) * 0.5;
75 if (
lockAspectRatio &&
static_cast< int >( painterBounds.height() ) > pictureHeight )
77 yOffset = ( painterBounds.height() - pictureHeight ) * 0.5;
81 painterBounds.top() + pictureHeight / 2 + yOffset ), picture );
89 QSize(
static_cast< int >( std::round( painterBounds.width() ) ),
static_cast< int >( std::round( painterBounds.height() ) ) ),
92 if (
lockAspectRatio &&
static_cast< int >( painterBounds.width() ) > im.width() )
94 xOffset = ( painterBounds.width() - im.width() ) * 0.5;
97 if (
lockAspectRatio &&
static_cast< int >( painterBounds.height() ) > im.height() )
99 yOffset = ( painterBounds.height() - im.height() ) * 0.5;
101 context.
painter()->drawImage( QPointF( painterBounds.left() + xOffset, painterBounds.top() + yOffset ), im );
112 element.setAttribute( QStringLiteral(
"lockAspect" ), mLockAspectRatio ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
113 element.setAttribute( QStringLiteral(
"path" ), mPath );
114 element.setAttribute( QStringLiteral(
"format" ),
qgsEnumValueToKey( mFormat ) );
126 mLockAspectRatio = element.attribute( QStringLiteral(
"lockAspect" ), QStringLiteral(
"1" ) ).toInt();
129 setPath(
format, element.attribute( QStringLiteral(
"path" ) ) );
137 auto item = std::make_unique< QgsAnnotationPictureItem >( mFormat, mPath,
bounds() );
138 item->setLockAspectRatio( mLockAspectRatio );
140 item->copyCommonProperties(
this );
141 return item.release();
149 if (
path.isEmpty() )
158 return mLockAspectRatio;
163 mLockAspectRatio = locked;
@ Default
Allow raster-based rendering in situations where it is required for correct rendering or where it wil...
PictureFormat
Picture formats.
@ Unknown
Invalid or unknown image type.
An annotation item which renders a picture.
Qgis::PictureFormat format() const
Returns the picture format.
QString path() const
Returns the path of the image used to render the item.
void renderInBounds(QgsRenderContext &context, const QRectF &painterBounds, QgsFeedback *feedback) override
Renders the item to the specified render context.
bool lockAspectRatio() const
Returns true if the aspect ratio of the picture will be retained.
~QgsAnnotationPictureItem() override
bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes the item's state into an XML element.
QgsAnnotationPictureItem(Qgis::PictureFormat format, const QString &path, const QgsRectangle &bounds)
Constructor for QgsAnnotationPictureItem, rendering the specified image path within the specified bou...
QgsAnnotationPictureItem * clone() const override
Returns a clone of the item.
bool readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the item's state from the given DOM element.
void setLockAspectRatio(bool locked)
Sets whether the aspect ratio of the picture will be retained.
void setPath(Qgis::PictureFormat format, const QString &path)
Sets the format and path of the image used to render the item.
static QgsAnnotationPictureItem * create()
Creates a new polygon annotation item.
QString type() const override
Returns a unique (untranslated) string identifying the type of item.
Abstract base class for annotation items which render annotations in a rectangular shape.
QgsRectangle bounds() const
Returns the bounds of the item.
bool readCommonProperties(const QDomElement &element, const QgsReadWriteContext &context) override
Reads common properties from the base class from the given DOM element.
bool writeCommonProperties(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const override
Writes common properties from the base class into an XML element.
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images.
static QgsSvgCache * svgCache()
Returns the application's SVG cache, used for caching SVG images and handling parameter replacement w...
Base class for feedback objects to be used for cancellation of something running in a worker thread.
QImage pathAsImage(const QString &path, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache, bool blocking=false, double targetDpi=96, int frameNumber=-1, bool *isMissing=nullptr)
Returns the specified path rendered as an image.
static void drawPicture(QPainter *painter, const QPointF &point, const QPicture &picture)
Draws a picture onto a painter, correctly applying workarounds to avoid issues with incorrect scaling...
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
Contains information about the context of a rendering operation.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
QPainter * painter()
Returns the destination QPainter for the render operation.
Qgis::RasterizedRenderingPolicy rasterizedRenderingPolicy() const
Returns the policy controlling when rasterisation of content during renders is permitted.
QSizeF svgViewboxSize(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > ¶meters=QMap< QString, QString >())
Calculates the viewbox size of a (possibly cached) SVG file.
QPicture svgAsPicture(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, bool forceVectorOutput=false, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > ¶meters=QMap< QString, QString >())
Returns an SVG drawing as a QPicture.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.