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;
64 const double pictureWidth = picture.boundingRect().width();
65 const double pictureHeight = picture.boundingRect().height();
68 if (
lockAspectRatio &&
static_cast< int >( painterBounds.width() ) > pictureWidth )
70 xOffset = ( painterBounds.width() - pictureWidth ) * 0.5;
73 if (
lockAspectRatio &&
static_cast< int >( painterBounds.height() ) > pictureHeight )
75 yOffset = ( painterBounds.height() - pictureHeight ) * 0.5;
79 painterBounds.top() + pictureHeight / 2 + yOffset ), picture );
87 QSize(
static_cast< int >( std::round( painterBounds.width() ) ),
static_cast< int >( std::round( painterBounds.height() ) ) ),
90 if (
lockAspectRatio &&
static_cast< int >( painterBounds.width() ) > im.width() )
92 xOffset = ( painterBounds.width() - im.width() ) * 0.5;
95 if (
lockAspectRatio &&
static_cast< int >( painterBounds.height() ) > im.height() )
97 yOffset = ( painterBounds.height() - im.height() ) * 0.5;
99 context.
painter()->drawImage( QPointF( painterBounds.left() + xOffset, painterBounds.top() + yOffset ), im );
110 element.setAttribute( QStringLiteral(
"lockAspect" ), mLockAspectRatio ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
111 element.setAttribute( QStringLiteral(
"path" ), mPath );
112 element.setAttribute( QStringLiteral(
"format" ),
qgsEnumValueToKey( mFormat ) );
124 mLockAspectRatio = element.attribute( QStringLiteral(
"lockAspect" ), QStringLiteral(
"1" ) ).toInt();
127 setPath(
format, element.attribute( QStringLiteral(
"path" ) ) );
135 std::unique_ptr< QgsAnnotationPictureItem > item = std::make_unique< QgsAnnotationPictureItem >( mFormat, mPath,
bounds() );
136 item->setLockAspectRatio( mLockAspectRatio );
138 item->copyCommonProperties(
this );
139 return item.release();
147 if (
path.isEmpty() )
156 return mLockAspectRatio;
161 mLockAspectRatio = locked;
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...
The class is used as a container of context for various read/write operations on other 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.
bool forceVectorOutput() const
Returns true if rendering operations should use vector operations instead of any faster raster shortc...
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.