QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgslayoutitemmap.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutitemmap.h
3  -------------------
4  begin : July 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #ifndef QGSLAYOUTITEMMAP_H
18 #define QGSLAYOUTITEMMAP_H
19 
20 #include "qgis_core.h"
21 #include "qgslayoutitem.h"
22 #include "qgslayoutitemregistry.h"
23 #include "qgsmaplayerref.h"
25 #include "qgslayoutitemmapgrid.h"
27 
28 class QgsAnnotation;
29 
36 class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem
37 {
38 
39  Q_OBJECT
40 
41  public:
42 
47  {
49 
57 
63  Auto
64  };
65 
69  explicit QgsLayoutItemMap( QgsLayout *layout );
70  ~QgsLayoutItemMap() override;
71 
72  int type() const override;
73  QIcon icon() const override;
74  QgsLayoutItem::Flags itemFlags() const override;
75 
79  void assignFreeId();
80 
81  //overridden to show "Map 1" type names
82  QString displayName() const override;
83 
89  static QgsLayoutItemMap *create( QgsLayout *layout ) SIP_FACTORY;
90 
91  // for now, map items behave a bit differently and don't implement draw. TODO - see if we can avoid this
92  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
93  int numberExportLayers() const override;
94  void setFrameStrokeWidth( QgsLayoutMeasurement width ) override;
95 
101  double scale() const;
102 
110  void setScale( double scale, bool forceUpdate = true );
111 
119  void setExtent( const QgsRectangle &extent );
120 
128  void zoomToExtent( const QgsRectangle &extent );
129 
135  QgsRectangle extent() const;
136 
137 
145  QPolygonF visibleExtentPolygon() const;
146 
156 
165  QgsCoordinateReferenceSystem presetCrs() const { return mCrs; }
166 
174  void setCrs( const QgsCoordinateReferenceSystem &crs );
175 
184  bool keepLayerSet() const { return mKeepLayerSet; }
185 
194  void setKeepLayerSet( bool enabled ) { mKeepLayerSet = enabled; }
195 
202  QList<QgsMapLayer *> layers() const;
203 
210  void setLayers( const QList<QgsMapLayer *> &layers );
211 
216  bool keepLayerStyles() const { return mKeepLayerStyles; }
217 
222  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
223 
228  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
229 
234  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
235 
239  void storeCurrentLayerStyles();
240 
251  bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
252 
256  void setFollowVisibilityPreset( bool follow ) { mFollowVisibilityPreset = follow; }
257 
263  QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
264 
269  void setFollowVisibilityPresetName( const QString &name ) { mFollowVisibilityPresetName = name; }
270 
271  void moveContent( double dx, double dy ) override;
272  void setMoveContentPreviewOffset( double dx, double dy ) override;
273 
274  void zoomContent( double factor, QPointF point ) override;
275 
276 
278  bool containsWmsLayer() const;
279 
280  bool requiresRasterization() const override;
281  bool containsAdvancedEffects() const override;
282 
289  void setMapRotation( double rotation );
290 
299  double mapRotation( QgsLayoutObject::PropertyValueType valueType = QgsLayoutObject::EvaluatedValue ) const;
300 
305  void setDrawAnnotations( bool draw ) { mDrawAnnotations = draw; }
306 
311  bool drawAnnotations() const { return mDrawAnnotations; }
312 
313 
320  bool atlasDriven() const { return mAtlasDriven; }
321 
328  void setAtlasDriven( bool enabled );
329 
339  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
340 
350  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
351 
363 
371  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
372 
378  QgsLayoutItemMapGridStack *grids() { return mGridStack.get(); }
379 
384  QgsLayoutItemMapGrid *grid();
385 
392  QgsLayoutItemMapOverviewStack *overviews() { return mOverviewStack.get(); }
393 
399  QgsLayoutItemMapOverview *overview();
400 
402 
408  double mapUnitsToLayoutUnits() const;
409 
417  QgsMapSettings mapSettings( const QgsRectangle &extent, QSizeF size, double dpi, bool includeLayerSettings ) const;
418 
419  void finalizeRestoreFromXml() override;
420 
425  QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
426 
427  protected:
428 
429  void draw( QgsLayoutItemRenderContext &context ) override;
430  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
431  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
432 
434  bool isDrawing() const {return mDrawing;}
435 
436  // In case of annotations, the bounding rectangle can be larger than the map item rectangle
437  QRectF boundingRect() const override;
438 
440  QPolygonF transformedMapPolygon() const;
441 
443  QPointF mapToItemCoords( QPointF mapCoords ) const;
444 
448  QgsRectangle requestedExtent() const;
449 
450  signals:
451 
457  void extentChanged();
458 
464  void mapRotationChanged( double newRotation );
465 
467  void preparedForAtlas();
468 
473  void layerStyleOverridesChanged();
474 
475  public slots:
476 
477  void refresh() override;
478 
479  void invalidateCache() override;
480 
482  void updateBoundingRect();
483 
485 
486  private slots:
487  void layersAboutToBeRemoved( const QList<QgsMapLayer *> &layers );
488 
489  void painterJobFinished();
490 
491  void shapeChanged();
492 
493  void mapThemeChanged( const QString &theme );
494 
496  void recreateCachedImageInBackground();
497 
498  private:
499 
500 
502  int mMapId = 1;
503 
504  std::unique_ptr< QgsLayoutItemMapGridStack > mGridStack;
505  std::unique_ptr< QgsLayoutItemMapOverviewStack > mOverviewStack;
506 
507  // Map region in map units really used for rendering
508  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
509  // so that full rectangle in paper is used.
510  QgsRectangle mExtent;
511 
514 
515  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
516  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
517  // to manually tweak each atlas preview page without affecting the actual original map extent.
518  QgsRectangle mAtlasFeatureExtent;
519 
520  // We have two images used for rendering/storing cached map images.
521  // the first (mCacheFinalImage) is used ONLY for storing the most recent completed map render. It's always
522  // used when drawing map item previews. The second (mCacheRenderingImage) is used temporarily while
523  // rendering a new preview image in the background. If (and only if) the background render completes, then
524  // mCacheRenderingImage is pushed into mCacheFinalImage, and used from then on when drawing the item preview.
525  // This ensures that something is always shown in the map item, even while refreshing the preview image in the
526  // background
527  std::unique_ptr< QImage > mCacheFinalImage;
528  std::unique_ptr< QImage > mCacheRenderingImage;
529  bool mUpdatesEnabled = true;
530 
532  bool mCacheInvalidated = true;
533 
535  int mNumCachedLayers;
536 
537  // Set to true if in state of drawing. Concurrent requests to draw method are returned if set to true
538  bool mDrawing = false;
539 
540  QTimer *mBackgroundUpdateTimer = nullptr;
541  double mPreviewScaleFactor = 0;
542 
543  bool mDrawingPreview = false;
544 
546  double mXOffset = 0.0;
548  double mYOffset = 0.0;
549 
550  double mLastRenderedImageOffsetX = 0.0;
551  double mLastRenderedImageOffsetY = 0.0;
552 
554  double mMapRotation = 0;
555 
559  double mEvaluatedMapRotation = 0;
560 
562  bool mKeepLayerSet = false;
563 
565  QList< QgsMapLayerRef > mLayers;
566 
567  bool mKeepLayerStyles = false;
569  QMap<QString, QString> mLayerStyleOverrides;
570 
572  mutable QString mCachedLayerStyleOverridesPresetName;
574  mutable QMap<QString, QString> mCachedPresetLayerStyleOverrides;
575 
580  bool mFollowVisibilityPreset = false;
581 
585  QString mFollowVisibilityPresetName;
586 
594  void drawMap( QPainter *painter, const QgsRectangle &extent, QSizeF size, double dpi );
595 
597  void connectUpdateSlot();
598 
600  void syncLayerSet();
601 
603  const QgsLayoutItemMapGrid *constFirstMapGrid() const;
604 
606  const QgsLayoutItemMapOverview *constFirstMapOverview() const;
607 
609  QRectF mCurrentRectangle;
611  bool mDrawAnnotations = true;
612 
614  bool mAtlasDriven = false;
616  AtlasScalingMode mAtlasScalingMode = Auto;
618  double mAtlasMargin = 0.10;
619 
620  std::unique_ptr< QPainter > mPainter;
621  std::unique_ptr< QgsMapRendererCustomPainterJob > mPainterJob;
622  bool mPainterCancelWait = false;
623 
624  void init();
625 
627  void updateToolTip();
628 
630  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;
631 
633  QgsRectangle transformedExtent() const;
634 
636  void mapPolygon( const QgsRectangle &extent, QPolygonF &poly ) const;
637 
643  void transformShift( double &xShift, double &yShift ) const;
644 
645  void drawAnnotations( QPainter *painter );
646  void drawAnnotation( const QgsAnnotation *item, QgsRenderContext &context );
647  QPointF layoutMapPosForItem( const QgsAnnotation *item ) const;
648 
649  void drawMapFrame( QPainter *p );
650  void drawMapBackground( QPainter *p );
651 
652  enum PartType
653  {
654  Background,
655  Layer,
656  Grid,
657  OverviewMapExtent,
658  Frame,
659  SelectionBoxes
660  };
661 
663  bool shouldDrawPart( PartType part ) const;
664 
669  void refreshMapExtents( const QgsExpressionContext *context = nullptr );
670 
671  void updateAtlasFeature();
672 
673  QgsRectangle computeAtlasRectangle();
674 
675  friend class QgsLayoutItemMapGrid;
677  friend class QgsLayoutItemLegend;
678  friend class TestQgsLayoutMap;
680 
681 };
682 
683 #endif //QGSLAYOUTITEMMAP_H
The class is used as a container of context for various read/write operations on other objects...
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
void setFollowVisibilityPresetName(const QString &name)
Sets preset name for map rendering.
A rectangle specified with double values.
Definition: qgsrectangle.h:40
virtual QIcon icon() const
Returns the item&#39;s icon.
Base class for graphical items within a QgsLayout.
An individual overview which is drawn above the map content in a QgsLayoutItemMap, and shows the extent of another QgsLayoutItemMap.
int type() const override
Returns a unique graphics item type identifier.
void setKeepLayerStyles(bool enabled)
Sets whether current styles of layers should be overridden by previously stored styles.
bool keepLayerStyles() const
Returns whether current styles of layers should be overridden by previously stored styles...
QMap< QString, QString > layerStyleOverrides() const
Returns stored overrides of styles for layers.
virtual int numberExportLayers() const
Returns the number of layers that this item requires for exporting during layered exports (e...
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
bool drawAnnotations() const
Returns whether annotations are drawn within the map.
A collection of overviews which are drawn above the map content in a QgsLayoutItemMap.
virtual void setFrameStrokeWidth(QgsLayoutMeasurement width)
Sets the frame stroke width.
void setKeepLayerSet(bool enabled)
Sets whether the stored layer set should be used or the current layer set of the associated project...
const QgsCoordinateReferenceSystem & crs
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...
Abstract base class for annotation items which are drawn over a map.
Definition: qgsannotation.h:48
The QgsMapSettings class contains configuration for rendering of the map.
PropertyValueType
Specifies whether the value returned by a function should be the original, user set value...
virtual void invalidateCache()
Forces a deferred update of any cached image the item uses.
Layout graphical items for displaying a map.
A collection of grids which is drawn above the map content in a QgsLayoutItemMap. ...
QString followVisibilityPresetName() const
Preset name that decides which layers and layer styles are used for map rendering.
This class provides a method of storing measurements for use in QGIS layouts using a variety of diffe...
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
virtual Flags itemFlags() const
Returns the item&#39;s flags, which indicate how the item behaves.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Handles preparing a paint surface for the layout item and painting the item&#39;s content.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsCoordinateReferenceSystem presetCrs() const
Returns the map&#39;s preset coordinate reference system.
bool isDrawing() const
True if a draw is already in progress.
#define SIP_FACTORY
Definition: qgis_sip.h:69
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
void setAtlasScalingMode(AtlasScalingMode mode)
Sets the current atlas scaling mode.
An individual grid which is drawn above the map content in a QgsLayoutItemMap.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:43
bool followVisibilityPreset() const
Returns whether the map should follow a map theme.
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
void setAtlasMargin(double margin)
Sets the margin size (percentage) used when the map is in atlas mode.
void setFollowVisibilityPreset(bool follow)
Sets whether the map should follow a map theme.
QgsCompositionConverter class converts a QGIS 2.x composition to a QGIS 3.x layout.
Return the current evaluated value for the property.
Contains information about the context of a rendering operation.
void setDrawAnnotations(bool draw)
Sets whether annotations are drawn within the map.
virtual bool containsAdvancedEffects() const
Returns true if the item contains contents with blend modes or transparency effects which can only be...
virtual void setMoveContentPreviewOffset(double dx, double dy)
Sets temporary offset for the item, by a specified dx and dy in layout units.
The current scale of the map is used for each feature of the atlas.
virtual void moveContent(double dx, double dy)
Moves the content of the item, by a specified dx and dy in layout units.
virtual bool requiresRasterization() const
Returns true if the item is drawn in such a way that forces the whole layout to be rasterized when ex...
This class represents a coordinate reference system (CRS).
virtual void zoomContent(double factor, QPointF point)
Zooms content of item.
virtual QString displayName() const
Gets item display name.
AtlasScalingMode atlasScalingMode() const
Returns the current atlas scaling mode.
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item&#39;s contents using the specified item render context.
AtlasScalingMode
Scaling modes used for the serial rendering (atlas)
A layout item subclass for map legends.
bool atlasDriven() const
Returns whether the map extent is set to follow the current atlas feature.
bool keepLayerSet() const
Returns whether a stored layer set should be used or the current layer set from the project associate...
DataDefinedProperty
Data defined properties for different item types.
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
QgsLayoutItemMapOverviewStack * overviews()
Returns the map item&#39;s overview stack, which is used to control how overviews are drawn over the map&#39;...
QgsLayoutItemMapGridStack * grids()
Returns the map item&#39;s grid stack, which is used to control how grids are drawn over the map&#39;s conten...
A scale is chosen from the predefined scales.
All properties for item.