QGIS API Documentation  2.99.0-Master (5d67572)
qgscomposermap.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposermap.h
3  -------------------
4  begin : January 2005
5  copyright : (C) 2005 by Radim Blazek
6  email : [email protected]
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 QGSCOMPOSERMAP_H
18 #define QGSCOMPOSERMAP_H
19 
20 //#include "ui_qgscomposermapbase.h"
21 #include "qgis_core.h"
22 #include "qgis_sip.h"
23 #include "qgis.h"
24 #include "qgscomposeritem.h"
25 #include "qgsrectangle.h"
27 #include "qgsrendercontext.h"
28 #include "qgsmaplayer.h"
29 #include "qgsmaplayerref.h"
30 #include <QFont>
31 #include <QGraphicsRectItem>
32 
33 class QgsComposition;
37 class QgsComposerMapGrid;
38 class QgsMapToPixel;
39 class QDomNode;
40 class QDomDocument;
41 class QPainter;
42 class QgsFillSymbol;
43 class QgsLineSymbol;
44 class QgsVectorLayer;
45 class QgsAnnotation;
47 
54 class CORE_EXPORT QgsComposerMap : public QgsComposerItem
55 {
56  Q_OBJECT
57 
58  public:
60  QgsComposerMap( QgsComposition *composition SIP_TRANSFERTHIS, int x, int y, int width, int height );
62  QgsComposerMap( QgsComposition *composition SIP_TRANSFERTHIS );
63  virtual ~QgsComposerMap();
64 
66  virtual int type() const override { return ComposerMap; }
67 
72  {
74 
82 
88  Auto
89  };
90 
99  void draw( QPainter *painter, const QgsRectangle &extent, QSizeF size, double dpi, double *forceWidthScale = nullptr );
100 
101  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
102 
106  QgsMapSettings mapSettings( const QgsRectangle &extent, QSizeF size, int dpi ) const;
107 
109  int id() const {return mId;}
110 
112  bool isDrawing() const {return mDrawing;}
113 
115  void resize( double dx, double dy );
116 
122  void moveContent( double dx, double dy ) override;
123 
131  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) override;
132 
134  void setSceneRect( const QRectF &rectangle ) override;
135 
141  double scale() const;
142 
148  void setNewScale( double scaleDenominator, bool forceUpdate = true );
149 
157  void setNewExtent( const QgsRectangle &extent );
158 
168  void zoomToExtent( const QgsRectangle &extent );
169 
174  void setNewAtlasFeatureExtent( const QgsRectangle &extent );
175 
183  const QgsRectangle *currentMapExtent() const;
184 
186  QgsRectangle *currentMapExtent() SIP_SKIP;
187 
197  QgsCoordinateReferenceSystem crs() const;
198 
208  QgsCoordinateReferenceSystem presetCrs() const { return mCrs; }
209 
218  void setCrs( const QgsCoordinateReferenceSystem &crs );
219 
228  bool keepLayerSet() const {return mKeepLayerSet;}
229 
238  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
239 
246  QList<QgsMapLayer *> layers() const;
247 
254  void setLayers( const QList<QgsMapLayer *> &layers );
255 
257  bool keepLayerStyles() const { return mKeepLayerStyles; }
259  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
260 
262  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
264  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
266  void storeCurrentLayerStyles();
267 
278  bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
279 
283  void setFollowVisibilityPreset( bool follow ) { mFollowVisibilityPreset = follow; }
284 
289  QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
290 
294  void setFollowVisibilityPresetName( const QString &name ) { mFollowVisibilityPresetName = name; }
295 
296  QgsRectangle extent() const {return mExtent;}
297 
299  void setOffset( double xOffset, double yOffset );
300 
302  bool containsWmsLayer() const;
303 
305  bool containsAdvancedEffects() const;
306 
312  bool writeXml( QDomElement &elem, QDomDocument &doc ) const override;
313 
319  bool readXml( const QDomElement &itemElem, const QDomDocument &doc ) override;
320 
328  QgsComposerMapGridStack *grids() { return mGridStack; }
329 
336  QgsComposerMapGrid *grid();
337 
345  QgsComposerMapOverviewStack *overviews() { return mOverviewStack; }
346 
353  QgsComposerMapOverview *overview();
354 
356  QRectF boundingRect() const override;
357 
358  /* reimplement setFrameStrokeWidth, so that updateBoundingRect() is called after setting the frame width */
359  virtual void setFrameStrokeWidth( const double strokeWidth ) override;
360 
367  void setMapRotation( double rotation );
368 
376  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
377 
382  void setDrawAnnotations( bool draw ) { mDrawAnnotations = draw; }
383 
388  bool drawAnnotations() const { return mDrawAnnotations; }
389 
391  double mapUnitsToMM() const;
392 
396  void assignFreeId();
397 
404  bool atlasDriven() const { return mAtlasDriven; }
405 
412  void setAtlasDriven( bool enabled );
413 
423  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
424 
434  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
435 
446  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
447 
455  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
456 
463  int numberExportLayers() const override;
464 
472  QPolygonF visibleExtentPolygon() const;
473 
474  //overridden to show "Map 1" type names
475  virtual QString displayName() const override;
476 
478  QPolygonF transformedMapPolygon() const;
479 
481  QPointF mapToItemCoords( QPointF mapCoords ) const;
482 
486  void requestedExtent( QgsRectangle &extent ) const;
487 
488  virtual QgsExpressionContext createExpressionContext() const override;
489 
490  signals:
491  void extentChanged();
492 
494  void mapRotationChanged( double newRotation );
495 
497  void preparedForAtlas();
498 
504  void layerStyleOverridesChanged();
505 
506  public slots:
507 
512  void invalidateCache();
513 
515  void updateBoundingRect();
516 
517  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
518 
519  private slots:
520  void layersAboutToBeRemoved( QList<QgsMapLayer *> layers );
521 
522  void painterJobFinished();
523 
524  private:
525 
527  int mId = 0;
528 
529  QgsComposerMapGridStack *mGridStack = nullptr;
530 
531  QgsComposerMapOverviewStack *mOverviewStack = nullptr;
532 
533  // Map region in map units really used for rendering
534  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
535  // so that full rectangle in paper is used.
536  QgsRectangle mExtent;
537 
540 
541  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
542  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
543  // to manually tweak each atlas preview page without affecting the actual original map extent.
544  QgsRectangle mAtlasFeatureExtent;
545 
546  // We have two images used for rendering/storing cached map images.
547  // the first (mCacheFinalImage) is used ONLY for storing the most recent completed map render. It's always
548  // used when drawing map item previews. The second (mCacheRenderingImage) is used temporarily while
549  // rendering a new preview image in the background. If (and only if) the background render completes, then
550  // mCacheRenderingImage is pushed into mCacheFinalImage, and used from then on when drawing the item preview.
551  // This ensures that something is always shown in the map item, even while refreshing the preview image in the
552  // background
553  std::unique_ptr< QImage > mCacheFinalImage;
554  std::unique_ptr< QImage > mCacheRenderingImage;
555 
557  bool mCacheInvalidated = true;
558 
560  int mNumCachedLayers;
561 
563  bool mDrawing = false;
564 
566  double mXOffset = 0.0;
568  double mYOffset = 0.0;
569 
570  double mLastRenderedImageOffsetX = 0.0;
571  double mLastRenderedImageOffsetY = 0.0;
572 
574  double mMapRotation = 0;
575 
579  double mEvaluatedMapRotation = 0;
580 
582  bool mKeepLayerSet = false;
583 
585  QList< QgsMapLayerRef > mLayers;
586 
587  bool mKeepLayerStyles = false;
589  QMap<QString, QString> mLayerStyleOverrides;
590 
595  bool mFollowVisibilityPreset = false;
596 
600  QString mFollowVisibilityPresetName;
601 
603  void recreateCachedImageInBackground();
604 
606  void connectUpdateSlot();
607 
609  void syncLayerSet();
610 
612  const QgsComposerMapGrid *constFirstMapGrid() const;
613 
615  const QgsComposerMapOverview *constFirstMapOverview() const;
616 
618  QRectF mCurrentRectangle;
620  bool mDrawAnnotations = true;
621 
625  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle &extent ) const;
626 
628  bool mAtlasDriven = false;
630  AtlasScalingMode mAtlasScalingMode = Auto;
632  double mAtlasMargin = 0.10;
633 
634  std::unique_ptr< QPainter > mPainter;
635  std::unique_ptr< QgsMapRendererCustomPainterJob > mPainterJob;
636  bool mPainterCancelWait = false;
637 
638  void init();
639 
641  void updateToolTip();
642 
644  QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
645 
647  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;
648 
650  QgsRectangle transformedExtent() const;
651 
653  void mapPolygon( const QgsRectangle &extent, QPolygonF &poly ) const;
654 
659  void transformShift( double &xShift, double &yShift ) const;
660 
661  void drawAnnotations( QPainter *painter );
662  void drawAnnotation( const QgsAnnotation *item, QgsRenderContext &context );
663  QPointF composerMapPosForItem( const QgsAnnotation *item ) const;
664 
665  enum PartType
666  {
667  Background,
668  Layer,
669  Grid,
670  OverviewMapExtent,
671  Frame,
672  SelectionBoxes
673  };
674 
676  bool shouldDrawPart( PartType part ) const;
677 
683  void refreshMapExtents( const QgsExpressionContext *context = nullptr );
684 
685  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
686  friend class TestQgsComposerMap;
687 };
688 
689 #endif
690 
AtlasScalingMode
Scaling modes used for the serial rendering (atlas)
bool atlasDriven() const
Returns whether the map extent is set to follow the current atlas feature.
A rectangle specified with double values.
Definition: qgsrectangle.h:39
Job implementation that renders everything sequentially using a custom painter.
virtual QString displayName() const
Get item display name.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
QgsComposerMapOverviewStack * overviews()
Returns the map item&#39;s overview stack, which is used to control how overviews are drawn over the map&#39;...
virtual void setFrameStrokeWidth(const double strokeWidth)
Sets frame stroke width.
bool followVisibilityPreset() const
Whether the map should follow a map theme.
QgsRectangle extent() const
ZoomMode
Modes for zooming item content.
A collection of grids which is drawn above the map content in a QgsComposerMap.
A item that forms part of a map composition.
Zoom to center of content.
virtual int numberExportLayers() const
Get the number of layers that this item requires for exporting as layers.
int id() const
Get identification number.
The current scale of the map is used for each feature of the atlas.
virtual void zoomContent(const double factor, const QPointF point, const ZoomMode mode=QgsComposerItem::Zoom)
Zoom content of item.
QgsCoordinateReferenceSystem presetCrs() const
Returns the map&#39;s preset coordinate reference system.
DataDefinedProperty
Data defined properties for different item types.
Abstract base class for annotation items which are drawn over a map.
Definition: qgsannotation.h:47
void setKeepLayerStyles(bool enabled)
Setter for flag that determines if current styles of layers should be overridden by previously stored...
The QgsMapSettings class contains configuration for rendering of the map.
void setAtlasMargin(double margin)
Sets the margin size (percentage) used when the map is in atlas mode.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:36
virtual void moveContent(double dx, double dy)
Move Content of item.
void setAtlasScalingMode(AtlasScalingMode mode)
Sets the current atlas scaling mode.
An individual overview which is drawn above the map content in a QgsComposerMap, and shows the extent...
#define SIP_SKIP
Definition: qgis_sip.h:119
void setFollowVisibilityPreset(bool follow)
Sets whether the map should follow a map theme.
virtual bool readXml(const QDomElement &itemElem, const QDomDocument &doc)
Sets item state from DOM element.
virtual int type() const override
Return correct graphics item type.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
virtual void refreshDataDefinedProperty(const QgsComposerObject::DataDefinedProperty property=QgsComposerObject::AllProperties, const QgsExpressionContext *context=nullptr) override
Refreshes a data defined property for the item by reevaluating the property&#39;s value and redrawing the...
bool drawAnnotations() const
Returns whether annotations are drawn within the composer map.
QMap< QString, QString > layerStyleOverrides() const
Getter for stored overrides of styles for layers.
An individual grid which is drawn above the map content in a QgsComposerMap.
PropertyValueType
Specifies whether the value returned by a function should be the original, user set value...
bool keepLayerStyles() const
Getter for flag that determines if current styles of layers should be overridden by previously stored...
QString followVisibilityPresetName() const
Preset name that decides which layers and layer styles are used for map rendering.
bool isDrawing() const
True if a draw is already in progress.
Graphics scene for map printing.
Object representing map window.
Return the current evaluated value for the property.
virtual QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the item&#39;s current state.
Contains information about the context of a rendering operation.
AtlasScalingMode atlasScalingMode() const
Returns the current atlas scaling mode.
A scale is chosen from the predefined scales.
QgsComposerMapGridStack * grids()
Returns the map item&#39;s grid stack, which is used to control how grids are drawn over the map&#39;s conten...
void setDrawAnnotations(bool draw)
Sets whether annotations are drawn within the composer map.
This class represents a coordinate reference system (CRS).
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
void setKeepLayerSet(bool enabled)
Setter for flag that determines if the stored layer set should be used or the current layer set of th...
virtual bool writeXml(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
Represents a vector layer which manages a vector based data sets.
bool keepLayerSet() const
Getter for flag that determines if a stored layer set should be used or the current layer set of the ...
All properties for item.
void setFollowVisibilityPresetName(const QString &name)
Sets preset name for map rendering.
A collection of overviews which are drawn above the map content in a QgsComposerMap.