QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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;
28 
34 class CORE_EXPORT QgsLayoutItemPicture: public QgsLayoutItem
35 {
36  Q_OBJECT
37  public:
38 
43  {
44  Zoom,
46  Clip,
48  FrameToImageSize
49  };
50 
54  enum Format
55  {
59  };
60 
62  enum NorthMode
63  {
64  GridNorth = 0,
66  };
67 
72 
73  int type() const override;
74  QIcon icon() const override;
75 
81  static QgsLayoutItemPicture *create( QgsLayout *layout ) SIP_FACTORY;
82 
83 
90  void setPicturePath( const QString &path );
91 
99  QString picturePath() const;
100 
107  double pictureRotation() const { return mPictureRotation; }
108 
119  void setLinkedMap( QgsLayoutItemMap *map );
120 
127  QgsLayoutItemMap *linkedMap() const;
128 
134  NorthMode northMode() const { return mNorthMode; }
135 
141  void setNorthMode( NorthMode mode );
142 
148  double northOffset() const { return mNorthOffset; }
149 
155  void setNorthOffset( double offset );
156 
162  ResizeMode resizeMode() const { return mResizeMode; }
163 
169  void setPictureAnchor( QgsLayoutItem::ReferencePoint anchor );
170 
176  QgsLayoutItem::ReferencePoint pictureAnchor() const { return mPictureAnchor; }
177 
183  QColor svgFillColor() const { return mSvgFillColor; }
184 
192  void setSvgFillColor( const QColor &color );
193 
199  QColor svgStrokeColor() const { return mSvgStrokeColor; }
200 
209  void setSvgStrokeColor( const QColor &color );
210 
216  double svgStrokeWidth() const { return mSvgStrokeWidth; }
217 
225  void setSvgStrokeWidth( double width );
226 
230  Format mode() const { return mMode; }
231 
232  void finalizeRestoreFromXml() override;
233 
240  bool isMissingImage() const;
241 
249  QString evaluatedPath() const;
250 
251  public slots:
252 
258  void setPictureRotation( double rotation );
259 
265  void setResizeMode( QgsLayoutItemPicture::ResizeMode mode );
266 
272  void refreshPicture( const QgsExpressionContext *context = nullptr );
273 
277  void recalculateSize();
278 
280 
281  signals:
283  void pictureRotationChanged( double newRotation );
284 
285  protected:
286 
287  void draw( QgsLayoutItemRenderContext &context ) override;
288  QSizeF applyItemSizeConstraint( QSizeF targetSize ) override;
289  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
290  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
291 
292  private:
293 
294  QgsLayoutItemPicture() = delete;
295 
297  QRectF boundedSVGRect( double deviceWidth, double deviceHeight );
299  QRectF boundedImageRect( double deviceWidth, double deviceHeight );
300 
302  QSizeF pictureSize();
303 
304  QImage mImage;
305  QSvgRenderer mSVG;
307  QString mSourcePath;
308  Format mMode = FormatUnknown;
309 
310  QSize mDefaultSvgSize;
311 
313  double mPictureRotation = 0;
314 
315  QString mRotationMapUuid;
317  QPointer< QgsLayoutItemMap > mRotationMap;
318 
320  NorthMode mNorthMode = GridNorth;
322  double mNorthOffset = 0.0;
323 
325  double mPictureWidth = 0.0;
327  double mPictureHeight = 0.0;
328 
330  QgsLayoutItem::ReferencePoint mPictureAnchor = UpperLeft;
331 
332  QColor mSvgFillColor = QColor( 255, 255, 255 );
333  QColor mSvgStrokeColor = QColor( 0, 0, 0 );
334  double mSvgStrokeWidth = 0.2;
335 
336  bool mHasExpressionError = false;
337  bool mLoaded = false;
338  bool mLoadingSvg = false;
339  bool mIsMissingImage = false;
340  QString mEvaluatedPath;
341 
343  void loadPicture( const QVariant &data );
344 
349  QRect clippedImageRect( double &boundRectWidthMM, double &boundRectHeightMM, QSize imageRectPixels );
350 
354  void loadRemotePicture( const QString &url );
355 
359  void loadLocalPicture( const QString &path );
360 
361  void disconnectMap( QgsLayoutItemMap *map );
362 
363  private slots:
364 
365  void updateMapRotation();
366 
367  void shapeChanged();
368 
370  friend class TestQgsCompositionConverter;
371 
372 };
373 
374 #endif // QGSLAYOUTITEMPICTURE_H
Class for parsing and evaluation of expressions (formerly called "search strings").
The class is used as a container of context for various read/write operations on other objects...
virtual QIcon icon() const
Returns the item&#39;s icon.
Base class for graphical items within a QgsLayout.
int type() const override
Returns a unique graphics item type identifier.
double northOffset() const
Returns the offset added to the picture&#39;s rotation from a map&#39;s North.
Stretches image to fit frame, ignores aspect ratio.
QColor svgStrokeColor() const
Returns the stroke color used for parametrized SVG files.
A layout item subclass that displays SVG files or raster format images (jpg, png, ...
double pictureRotation() const
Returns the rotation used for drawing the picture within the item&#39;s frame, in degrees clockwise...
Format mode() const
Returns the current picture mode (image format).
virtual void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::AllProperties)
Refreshes a data defined property for the item by reevaluating the property&#39;s value and redrawing the...
NorthMode northMode() const
Returns the mode used to align the picture to a map&#39;s North.
ReferencePoint
Fixed position reference point.
Invalid or unknown image type.
QgsLayoutItem::ReferencePoint pictureAnchor() const
Returns the picture&#39;s current anchor, which controls how it is placed within the picture item&#39;s frame...
Layout graphical items for displaying a map.
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
double svgStrokeWidth() const
Returns the stroke width (in layout units) used for parametrized SVG files.
#define SIP_FACTORY
Definition: qgis_sip.h:76
Upper left corner of item.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
QColor svgFillColor() const
Returns the fill color used for parametrized SVG files.
ResizeMode resizeMode() const
Returns the resize mode used for drawing the picture within the composer item&#39;s frame.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:44
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
Enlarges image to fit frame, then resizes frame to fit resultant image.
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
NorthMode
Method for syncing rotation to a map&#39;s North direction.
Format
Format of source image.
ResizeMode
Controls how pictures are scaled within the item&#39;s frame.
virtual QSizeF applyItemSizeConstraint(QSizeF targetSize)
Applies any item-specific size constraint handling to a given targetSize in layout units...
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
Draws image at original size and clips any portion which falls outside frame.
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item&#39;s contents using the specified item render context.
Enlarges image to fit frame while maintaining aspect ratio of picture.
DataDefinedProperty
Data defined properties for different item types.
All properties for item.