QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
78  void assignFreeId();
79 
80  //overridden to show "Map 1" type names
81  QString displayName() const override;
82 
88  static QgsLayoutItemMap *create( QgsLayout *layout ) SIP_FACTORY;
89 
90  // for now, map items behave a bit differently and don't implement draw. TODO - see if we can avoid this
91  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
92  int numberExportLayers() const override;
93  void setFrameStrokeWidth( const QgsLayoutMeasurement &width ) override;
94 
100  double scale() const;
101 
109  void setScale( double scale, bool forceUpdate = true );
110 
118  void setExtent( const QgsRectangle &extent );
119 
127  void zoomToExtent( const QgsRectangle &extent );
128 
134  QgsRectangle extent() const;
135 
136 
144  QPolygonF visibleExtentPolygon() const;
145 
154  QgsCoordinateReferenceSystem crs() const;
155 
164  QgsCoordinateReferenceSystem presetCrs() const { return mCrs; }
165 
173  void setCrs( const QgsCoordinateReferenceSystem &crs );
174 
183  bool keepLayerSet() const { return mKeepLayerSet; }
184 
193  void setKeepLayerSet( bool enabled ) { mKeepLayerSet = enabled; }
194 
201  QList<QgsMapLayer *> layers() const;
202 
209  void setLayers( const QList<QgsMapLayer *> &layers );
210 
215  bool keepLayerStyles() const { return mKeepLayerStyles; }
216 
221  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
222 
227  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
228 
233  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
234 
238  void storeCurrentLayerStyles();
239 
250  bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
251 
255  void setFollowVisibilityPreset( bool follow ) { mFollowVisibilityPreset = follow; }
256 
262  QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
263 
268  void setFollowVisibilityPresetName( const QString &name ) { mFollowVisibilityPresetName = name; }
269 
270  void moveContent( double dx, double dy ) override;
271  void setMoveContentPreviewOffset( double dx, double dy ) override;
272 
273  void zoomContent( double factor, QPointF point ) override;
274 
275 
277  bool containsWmsLayer() const;
278 
279  bool requiresRasterization() const override;
280  bool containsAdvancedEffects() const override;
281 
288  void setMapRotation( double rotation );
289 
298  double mapRotation( QgsLayoutObject::PropertyValueType valueType = QgsLayoutObject::EvaluatedValue ) const;
299 
304  void setDrawAnnotations( bool draw ) { mDrawAnnotations = draw; }
305 
310  bool drawAnnotations() const { return mDrawAnnotations; }
311 
312 
319  bool atlasDriven() const { return mAtlasDriven; }
320 
327  void setAtlasDriven( bool enabled );
328 
338  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
339 
349  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
350 
362 
370  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
371 
377  QgsLayoutItemMapGridStack *grids() { return mGridStack.get(); }
378 
383  QgsLayoutItemMapGrid *grid();
384 
391  QgsLayoutItemMapOverviewStack *overviews() { return mOverviewStack.get(); }
392 
398  QgsLayoutItemMapOverview *overview();
399 
401 
407  double mapUnitsToLayoutUnits() const;
408 
416  QgsMapSettings mapSettings( const QgsRectangle &extent, QSizeF size, double dpi, bool includeLayerSettings ) const;
417 
418  void finalizeRestoreFromXml() override;
419 
420  protected:
421 
422  void draw( QgsLayoutItemRenderContext &context ) override;
423  bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
424  bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
425 
427  bool isDrawing() const {return mDrawing;}
428 
429  // In case of annotations, the bounding rectangle can be larger than the map item rectangle
430  QRectF boundingRect() const override;
431 
433  QPolygonF transformedMapPolygon() const;
434 
436  QPointF mapToItemCoords( QPointF mapCoords ) const;
437 
441  QgsRectangle requestedExtent() const;
442 
443  signals:
444 
450  void extentChanged();
451 
457  void mapRotationChanged( double newRotation );
458 
460  void preparedForAtlas();
461 
466  void layerStyleOverridesChanged();
467 
468  public slots:
469 
470  void refresh() override;
471 
472  void invalidateCache() override;
473 
475  void updateBoundingRect();
476 
478 
479  private slots:
480  void layersAboutToBeRemoved( const QList<QgsMapLayer *> &layers );
481 
482  void painterJobFinished();
483 
484  void shapeChanged();
485 
486  void mapThemeChanged( const QString &theme );
487 
489  void recreateCachedImageInBackground();
490 
491  private:
492 
493 
495  int mMapId = 1;
496 
497  std::unique_ptr< QgsLayoutItemMapGridStack > mGridStack;
498  std::unique_ptr< QgsLayoutItemMapOverviewStack > mOverviewStack;
499 
500  // Map region in map units really used for rendering
501  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
502  // so that full rectangle in paper is used.
503  QgsRectangle mExtent;
504 
507 
508  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
509  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
510  // to manually tweak each atlas preview page without affecting the actual original map extent.
511  QgsRectangle mAtlasFeatureExtent;
512 
513  // We have two images used for rendering/storing cached map images.
514  // the first (mCacheFinalImage) is used ONLY for storing the most recent completed map render. It's always
515  // used when drawing map item previews. The second (mCacheRenderingImage) is used temporarily while
516  // rendering a new preview image in the background. If (and only if) the background render completes, then
517  // mCacheRenderingImage is pushed into mCacheFinalImage, and used from then on when drawing the item preview.
518  // This ensures that something is always shown in the map item, even while refreshing the preview image in the
519  // background
520  std::unique_ptr< QImage > mCacheFinalImage;
521  std::unique_ptr< QImage > mCacheRenderingImage;
522  bool mUpdatesEnabled = true;
523 
525  bool mCacheInvalidated = true;
526 
528  int mNumCachedLayers;
529 
530  // Set to true if in state of drawing. Concurrent requests to draw method are returned if set to true
531  bool mDrawing = false;
532 
533  QTimer *mBackgroundUpdateTimer = nullptr;
534  double mPreviewScaleFactor = 0;
535 
536  bool mDrawingPreview = false;
537 
539  double mXOffset = 0.0;
541  double mYOffset = 0.0;
542 
543  double mLastRenderedImageOffsetX = 0.0;
544  double mLastRenderedImageOffsetY = 0.0;
545 
547  double mMapRotation = 0;
548 
552  double mEvaluatedMapRotation = 0;
553 
555  bool mKeepLayerSet = false;
556 
558  QList< QgsMapLayerRef > mLayers;
559 
560  bool mKeepLayerStyles = false;
562  QMap<QString, QString> mLayerStyleOverrides;
563 
565  mutable QString mCachedLayerStyleOverridesPresetName;
567  mutable QMap<QString, QString> mCachedPresetLayerStyleOverrides;
568 
573  bool mFollowVisibilityPreset = false;
574 
578  QString mFollowVisibilityPresetName;
579 
587  void drawMap( QPainter *painter, const QgsRectangle &extent, QSizeF size, double dpi );
588 
590  void connectUpdateSlot();
591 
593  void syncLayerSet();
594 
596  const QgsLayoutItemMapGrid *constFirstMapGrid() const;
597 
599  const QgsLayoutItemMapOverview *constFirstMapOverview() const;
600 
602  QRectF mCurrentRectangle;
604  bool mDrawAnnotations = true;
605 
607  bool mAtlasDriven = false;
609  AtlasScalingMode mAtlasScalingMode = Auto;
611  double mAtlasMargin = 0.10;
612 
613  std::unique_ptr< QPainter > mPainter;
614  std::unique_ptr< QgsMapRendererCustomPainterJob > mPainterJob;
615  bool mPainterCancelWait = false;
616 
617  void init();
618 
620  void updateToolTip();
621 
623  QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
624 
626  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;
627 
629  QgsRectangle transformedExtent() const;
630 
632  void mapPolygon( const QgsRectangle &extent, QPolygonF &poly ) const;
633 
639  void transformShift( double &xShift, double &yShift ) const;
640 
641  void drawAnnotations( QPainter *painter );
642  void drawAnnotation( const QgsAnnotation *item, QgsRenderContext &context );
643  QPointF layoutMapPosForItem( const QgsAnnotation *item ) const;
644 
645  void drawMapFrame( QPainter *p );
646  void drawMapBackground( QPainter *p );
647 
648  enum PartType
649  {
650  Background,
651  Layer,
652  Grid,
653  OverviewMapExtent,
654  Frame,
655  SelectionBoxes
656  };
657 
659  bool shouldDrawPart( PartType part ) const;
660 
665  void refreshMapExtents( const QgsExpressionContext *context = nullptr );
666 
667  void updateAtlasFeature();
668 
669  QgsRectangle computeAtlasRectangle();
670 
671  friend class QgsLayoutItemMapGrid;
673  friend class QgsLayoutItemLegend;
674  friend class TestQgsLayoutMap;
676 
677 };
678 
679 #endif //QGSLAYOUTITEMMAP_H
The class is used as a container of context for various read/write operations on other objects...
bool isDrawing() const
True if a draw is already in progress.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
bool followVisibilityPreset() const
Returns whether the map should follow a map theme.
virtual QIcon icon() const
Returns the item&#39;s icon.
void setFollowVisibilityPresetName(const QString &name)
Sets preset name for map rendering.
A rectangle specified with double values.
Definition: qgsrectangle.h:40
AtlasScalingMode atlasScalingMode() const
Returns the current atlas scaling mode.
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.
virtual bool containsAdvancedEffects() const
Returns true if the item contains contents with blend modes or transparency effects which can only be...
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...
A collection of overviews which are drawn above the map content in a QgsLayoutItemMap.
bool drawAnnotations() const
Returns whether annotations are drawn within the map.
void setKeepLayerSet(bool enabled)
Sets whether the stored layer set should be used or the current layer set of the associated project...
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. ...
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.
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...
virtual int numberExportLayers() const
Returns the number of layers that this item requires for exporting during layered exports (e...
virtual void setFrameStrokeWidth(const QgsLayoutMeasurement &width)
Sets the frame stroke width.
#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.
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...
An individual grid which is drawn above the map content in a QgsLayoutItemMap.
bool keepLayerSet() const
Returns whether a stored layer set should be used or the current layer set from the project associate...
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:43
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.
virtual QString displayName() const
Gets item display name.
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.
QgsCoordinateReferenceSystem presetCrs() const
Returns the map&#39;s preset coordinate reference system.
void setDrawAnnotations(bool draw)
Sets whether annotations are drawn within the map.
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.
This class represents a coordinate reference system (CRS).
virtual void zoomContent(double factor, QPointF point)
Zooms content of item.
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item&#39;s contents using the specified item render context.
bool atlasDriven() const
Returns whether the map extent is set to follow the current atlas feature.
AtlasScalingMode
Scaling modes used for the serial rendering (atlas)
A layout item subclass for map legends.
QString followVisibilityPresetName() const
Preset name that decides which layers and layer styles are used for map rendering.
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.
QMap< QString, QString > layerStyleOverrides() const
Returns stored overrides of styles for layers.