QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgslayoutitempicture.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutitempicture.h
3  -------------------
4 begin : October 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #ifndef QGSLAYOUTITEMPICTURE_H
18 #define QGSLAYOUTITEMPICTURE_H
19 
20 #include "qgis_core.h"
21 #include "qgslayoutitem.h"
22 #include <QFile>
23 #include <QImage>
24 #include <QSvgRenderer>
25 
26 class QgsLayoutItemMap;
27 class QgsExpression;
29 
35 class CORE_EXPORT QgsLayoutItemPicture: public QgsLayoutItem
36 {
37  Q_OBJECT
38  public:
39 
44  {
45  Zoom,
47  Clip,
49  FrameToImageSize
50  };
51 
55  enum Format
56  {
60  };
61 
63  enum NorthMode
64  {
65  GridNorth = 0,
67  };
68 
73 
74  int type() const override;
75  QIcon icon() const override;
76 
82  static QgsLayoutItemPicture *create( QgsLayout *layout ) SIP_FACTORY;
83 
84 
94  void setPicturePath( const QString &path, Format format = FormatUnknown );
95 
103  QString picturePath() const;
104 
111  double pictureRotation() const { return mPictureRotation; }
112 
123  void setLinkedMap( QgsLayoutItemMap *map );
124 
131  QgsLayoutItemMap *linkedMap() const;
132 
138  NorthMode northMode() const;
139 
145  void setNorthMode( NorthMode mode );
146 
152  double northOffset() const;
153 
159  void setNorthOffset( double offset );
160 
166  ResizeMode resizeMode() const { return mResizeMode; }
167 
173  void setPictureAnchor( QgsLayoutItem::ReferencePoint anchor );
174 
180  QgsLayoutItem::ReferencePoint pictureAnchor() const { return mPictureAnchor; }
181 
187  QColor svgFillColor() const { return mSvgFillColor; }
188 
196  void setSvgFillColor( const QColor &color );
197 
203  QColor svgStrokeColor() const { return mSvgStrokeColor; }
204 
213  void setSvgStrokeColor( const QColor &color );
214 
220  double svgStrokeWidth() const { return mSvgStrokeWidth; }
221 
229  void setSvgStrokeWidth( double width );
230 
235  Format mode() const { return mMode; }
236 
242  void setMode( Format mode );
243 
244  void finalizeRestoreFromXml() override;
245 
252  bool isMissingImage() const;
253 
261  QString evaluatedPath() const;
262 
267  QMap<QString, QgsProperty> svgDynamicParameters() const;
268 
273  void setSvgDynamicParameters( const QMap<QString, QgsProperty> &parameters );
274 
275  public slots:
276 
282  void setPictureRotation( double rotation );
283 
289  void setResizeMode( QgsLayoutItemPicture::ResizeMode mode );
290 
296  void refreshPicture( const QgsExpressionContext *context = nullptr );
297 
301  void recalculateSize();
302 
304 
305  signals:
307  void pictureRotationChanged( double newRotation );
308 
309  protected:
310 
311  void draw( QgsLayoutItemRenderContext &context ) override;
312  QSizeF applyItemSizeConstraint( QSizeF targetSize ) override;
313  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
314  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
315 
316  private:
317 
318  QgsLayoutItemPicture() = delete;
319 
321  QRectF boundedSVGRect( double deviceWidth, double deviceHeight );
323  QRectF boundedImageRect( double deviceWidth, double deviceHeight );
324 
326  QSizeF pictureSize();
327 
328  QImage mImage;
329  QSvgRenderer mSVG;
331  QString mSourcePath;
332  Format mMode = FormatUnknown;
333 
334  QSize mDefaultSvgSize;
335 
337  double mPictureRotation = 0;
338 
339  QString mRotationMapUuid;
340 
342  double mPictureWidth = 0.0;
344  double mPictureHeight = 0.0;
345 
346  ResizeMode mResizeMode = QgsLayoutItemPicture::Zoom;
347  QgsLayoutItem::ReferencePoint mPictureAnchor = UpperLeft;
348 
349  QColor mSvgFillColor = QColor( 255, 255, 255 );
350  QColor mSvgStrokeColor = QColor( 0, 0, 0 );
351  double mSvgStrokeWidth = 0.2;
352 
353  bool mHasExpressionError = false;
354  bool mLoaded = false;
355  bool mLoadingSvg = false;
356  bool mIsMissingImage = false;
357  QString mEvaluatedPath;
358 
359  QgsLayoutNorthArrowHandler *mNorthArrowHandler = nullptr;
360 
362  void loadPicture( const QVariant &data );
363 
368  QRect clippedImageRect( double &boundRectWidthMM, double &boundRectHeightMM, QSize imageRectPixels );
369 
373  void loadRemotePicture( const QString &url );
374 
378  void loadLocalPicture( const QString &path );
379 
380  void loadPictureUsingCache( const QString &path );
381 
382  QgsLayoutItemPicture( const QgsLayoutItemPicture & ) = delete;
383  QgsLayoutItemPicture &operator=( const QgsLayoutItemPicture & ) = delete;
384 
385  private slots:
386 
387  void updateNorthArrowRotation( double rotation );
388 
389  void shapeChanged();
390 
392  friend class TestQgsCompositionConverter;
393 
394 };
395 
396 #endif // QGSLAYOUTITEMPICTURE_H
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsLayoutItem::applyItemSizeConstraint
virtual QSizeF applyItemSizeConstraint(QSizeF targetSize)
Applies any item-specific size constraint handling to a given targetSize in layout units.
Definition: qgslayoutitem.cpp:1264
QgsLayoutItem::writePropertiesToElement
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
Definition: qgslayoutitem.cpp:1326
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsLayoutItem::icon
virtual QIcon icon() const
Returns the item's icon.
Definition: qgslayoutitem.h:336
QgsLayoutItem::finalizeRestoreFromXml
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
Definition: qgslayoutitem.cpp:825
QgsLayoutItemRenderContext
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:44
QgsLayoutItemPicture::NorthMode
NorthMode
Method for syncing rotation to a map's North direction.
Definition: qgslayoutitempicture.h:63
QgsLayoutItem::refreshDataDefinedProperty
virtual void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
Definition: qgslayoutitem.cpp:1082
QgsLayoutItemPicture::svgFillColor
QColor svgFillColor() const
Returns the fill color used for parametrized SVG files.
Definition: qgslayoutitempicture.h:187
QgsLayoutItemPicture::svgStrokeColor
QColor svgStrokeColor() const
Returns the stroke color used for parametrized SVG files.
Definition: qgslayoutitempicture.h:203
QgsLayoutItem::ReferencePoint
ReferencePoint
Fixed position reference point.
Definition: qgslayoutitem.h:201
QgsLayoutItem::readPropertiesFromElement
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
Definition: qgslayoutitem.cpp:1331
QgsLayoutItem::draw
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item's contents using the specified item render context.
QgsLayoutItemPicture::pictureAnchor
QgsLayoutItem::ReferencePoint pictureAnchor() const
Returns the picture's current anchor, which controls how it is placed within the picture item's frame...
Definition: qgslayoutitempicture.h:180
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsLayoutItem::type
int type() const override
Returns a unique graphics item type identifier.
Definition: qgslayoutitem.cpp:124
QgsLayoutItemPicture::pictureRotation
double pictureRotation() const
Returns the rotation used for drawing the picture within the item's frame, in degrees clockwise.
Definition: qgslayoutitempicture.h:111
QgsLayoutObject::AllProperties
@ AllProperties
All properties for item.
Definition: qgslayoutobject.h:135
QgsLayoutItemPicture::Stretch
@ Stretch
Stretches image to fit frame, ignores aspect ratio.
Definition: qgslayoutitempicture.h:46
qgslayoutitem.h
QgsCompositionConverter
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
Definition: qgscompositionconverter.h:56
QgsLayoutItemPicture::ZoomResizeFrame
@ ZoomResizeFrame
Enlarges image to fit frame, then resizes frame to fit resultant image.
Definition: qgslayoutitempicture.h:48
QgsLayoutItemPicture::Format
Format
Format of source image.
Definition: qgslayoutitempicture.h:55
QgsLayoutItemPicture::FormatUnknown
@ FormatUnknown
Invalid or unknown image type.
Definition: qgslayoutitempicture.h:59
QgsLayoutItemPicture::Clip
@ Clip
Draws image at original size and clips any portion which falls outside frame.
Definition: qgslayoutitempicture.h:47
QgsLayoutItemPicture::ResizeMode
ResizeMode
Controls how pictures are scaled within the item's frame.
Definition: qgslayoutitempicture.h:43
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
QgsLayoutItemPicture::mode
Format mode() const
Returns the current picture mode (image format).
Definition: qgslayoutitempicture.h:235
QgsLayoutItemPicture::FormatSVG
@ FormatSVG
SVG image.
Definition: qgslayoutitempicture.h:57
QgsLayoutNorthArrowHandler
An object which handles north-arrow type behavior for layout items.
Definition: qgslayoutnortharrowhandler.h:32
QgsLayoutItemPicture
A layout item subclass that displays SVG files or raster format images (jpg, png, ....
Definition: qgslayoutitempicture.h:35
QgsLayoutItemMap
Layout graphical items for displaying a map.
Definition: qgslayoutitemmap.h:317
QgsLayoutItemPicture::Zoom
@ Zoom
Enlarges image to fit frame while maintaining aspect ratio of picture.
Definition: qgslayoutitempicture.h:45
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsLayoutItemPicture::TrueNorth
@ TrueNorth
Align to true north.
Definition: qgslayoutitempicture.h:66
QgsLayoutItemPicture::resizeMode
ResizeMode resizeMode() const
Returns the resize mode used for drawing the picture within the layout item's frame.
Definition: qgslayoutitempicture.h:166
QgsLayoutItemPicture::FormatRaster
@ FormatRaster
Raster image.
Definition: qgslayoutitempicture.h:58
QgsLayoutItemPicture::svgStrokeWidth
double svgStrokeWidth() const
Returns the stroke width (in layout units) used for parametrized SVG files.
Definition: qgslayoutitempicture.h:220
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsLayoutItem::UpperLeft
@ UpperLeft
Upper left corner of item.
Definition: qgslayoutitem.h:203
QgsLayoutObject::DataDefinedProperty
DataDefinedProperty
Data defined properties for different item types.
Definition: qgslayoutobject.h:132