QGIS API Documentation  2.99.0-Master (7d4f81d)
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 
53 class CORE_EXPORT QgsComposerMap : public QgsComposerItem
54 {
55  Q_OBJECT
56 
57  public:
59  QgsComposerMap( QgsComposition *composition SIP_TRANSFERTHIS, int x, int y, int width, int height );
61  QgsComposerMap( QgsComposition *composition SIP_TRANSFERTHIS );
62  virtual ~QgsComposerMap();
63 
65  virtual int type() const override { return ComposerMap; }
66 
70  {
72 
80 
85  Auto
86  };
87 
95  void draw( QPainter *painter, const QgsRectangle &extent, QSizeF size, double dpi, double *forceWidthScale = nullptr );
96 
97  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
98 
101  QgsMapSettings mapSettings( const QgsRectangle &extent, QSizeF size, int dpi ) const;
102 
104  int id() const {return mId;}
105 
107  bool isDrawing() const {return mDrawing;}
108 
110  void resize( double dx, double dy );
111 
116  void moveContent( double dx, double dy ) override;
117 
124  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) override;
125 
127  void setSceneRect( const QRectF &rectangle ) override;
128 
134  double scale() const;
135 
141  void setNewScale( double scaleDenominator, bool forceUpdate = true );
142 
149  void setNewExtent( const QgsRectangle &extent );
150 
159  void zoomToExtent( const QgsRectangle &extent );
160 
164  void setNewAtlasFeatureExtent( const QgsRectangle &extent );
165 
172  const QgsRectangle *currentMapExtent() const;
173 
175  QgsRectangle *currentMapExtent() SIP_SKIP;
176 
186  QgsCoordinateReferenceSystem crs() const;
187 
197  QgsCoordinateReferenceSystem presetCrs() const { return mCrs; }
198 
207  void setCrs( const QgsCoordinateReferenceSystem &crs );
208 
217  bool keepLayerSet() const {return mKeepLayerSet;}
218 
227  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
228 
235  QList<QgsMapLayer *> layers() const;
236 
243  void setLayers( const QList<QgsMapLayer *> &layers );
244 
246  bool keepLayerStyles() const { return mKeepLayerStyles; }
248  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
249 
251  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
253  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
255  void storeCurrentLayerStyles();
256 
266  bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
267 
270  void setFollowVisibilityPreset( bool follow ) { mFollowVisibilityPreset = follow; }
271 
275  QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
276 
279  void setFollowVisibilityPresetName( const QString &name ) { mFollowVisibilityPresetName = name; }
280 
281  QgsRectangle extent() const {return mExtent;}
282 
284  void setOffset( double xOffset, double yOffset );
285 
287  bool containsWmsLayer() const;
288 
290  bool containsAdvancedEffects() const;
291 
296  bool writeXml( QDomElement &elem, QDomDocument &doc ) const override;
297 
302  bool readXml( const QDomElement &itemElem, const QDomDocument &doc ) override;
303 
310  QgsComposerMapGridStack *grids() { return mGridStack; }
311 
317  QgsComposerMapGrid *grid();
318 
325  QgsComposerMapOverviewStack *overviews() { return mOverviewStack; }
326 
332  QgsComposerMapOverview *overview();
333 
335  QRectF boundingRect() const override;
336 
337  /* reimplement setFrameStrokeWidth, so that updateBoundingRect() is called after setting the frame width */
338  virtual void setFrameStrokeWidth( const double strokeWidth ) override;
339 
346  void setMapRotation( double rotation );
347 
355  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
356 
361  void setDrawAnnotations( bool draw ) { mDrawAnnotations = draw; }
362 
367  bool drawAnnotations() const { return mDrawAnnotations; }
368 
370  double mapUnitsToMM() const;
371 
374  void assignFreeId();
375 
381  bool atlasDriven() const { return mAtlasDriven; }
382 
388  void setAtlasDriven( bool enabled );
389 
398  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
399 
408  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
409 
419  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
420 
427  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
428 
434  int numberExportLayers() const override;
435 
442  QPolygonF visibleExtentPolygon() const;
443 
444  //overridden to show "Map 1" type names
445  virtual QString displayName() const override;
446 
448  QPolygonF transformedMapPolygon() const;
449 
451  QPointF mapToItemCoords( QPointF mapCoords ) const;
452 
455  void requestedExtent( QgsRectangle &extent ) const;
456 
457  virtual QgsExpressionContext createExpressionContext() const override;
458 
459  signals:
460  void extentChanged();
461 
463  void mapRotationChanged( double newRotation );
464 
466  void preparedForAtlas();
467 
472  void layerStyleOverridesChanged();
473 
474  public slots:
475 
480  void invalidateCache();
481 
483  void updateBoundingRect();
484 
485  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
486 
487  private slots:
488  void layersAboutToBeRemoved( QList<QgsMapLayer *> layers );
489 
490  void painterJobFinished();
491 
492  private:
493 
495  int mId = 0;
496 
497  QgsComposerMapGridStack *mGridStack = nullptr;
498 
499  QgsComposerMapOverviewStack *mOverviewStack = nullptr;
500 
501  // Map region in map units really used for rendering
502  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
503  // so that full rectangle in paper is used.
504  QgsRectangle mExtent;
505 
508 
509  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
510  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
511  // to manually tweak each atlas preview page without affecting the actual original map extent.
512  QgsRectangle mAtlasFeatureExtent;
513 
514  // We have two images used for rendering/storing cached map images.
515  // the first (mCacheFinalImage) is used ONLY for storing the most recent completed map render. It's always
516  // used when drawing map item previews. The second (mCacheRenderingImage) is used temporarily while
517  // rendering a new preview image in the background. If (and only if) the background render completes, then
518  // mCacheRenderingImage is pushed into mCacheFinalImage, and used from then on when drawing the item preview.
519  // This ensures that something is always shown in the map item, even while refreshing the preview image in the
520  // background
521  std::unique_ptr< QImage > mCacheFinalImage;
522  std::unique_ptr< QImage > mCacheRenderingImage;
523 
525  bool mCacheInvalidated = true;
526 
528  int mNumCachedLayers;
529 
531  bool mDrawing = false;
532 
534  double mXOffset = 0.0;
536  double mYOffset = 0.0;
537 
538  double mLastRenderedImageOffsetX = 0.0;
539  double mLastRenderedImageOffsetY = 0.0;
540 
542  double mMapRotation = 0;
543 
546  double mEvaluatedMapRotation = 0;
547 
549  bool mKeepLayerSet = false;
550 
552  QList< QgsMapLayerRef > mLayers;
553 
554  bool mKeepLayerStyles = false;
556  QMap<QString, QString> mLayerStyleOverrides;
557 
561  bool mFollowVisibilityPreset = false;
562 
565  QString mFollowVisibilityPresetName;
566 
568  void recreateCachedImageInBackground();
569 
571  void connectUpdateSlot();
572 
574  void syncLayerSet();
575 
577  const QgsComposerMapGrid *constFirstMapGrid() const;
578 
580  const QgsComposerMapOverview *constFirstMapOverview() const;
581 
583  QRectF mCurrentRectangle;
585  bool mDrawAnnotations = true;
586 
589  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle &extent ) const;
590 
592  bool mAtlasDriven = false;
594  AtlasScalingMode mAtlasScalingMode = Auto;
596  double mAtlasMargin = 0.10;
597 
598  std::unique_ptr< QPainter > mPainter;
599  std::unique_ptr< QgsMapRendererCustomPainterJob > mPainterJob;
600  bool mPainterCancelWait = false;
601 
602  void init();
603 
605  void updateToolTip();
606 
608  QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
609 
611  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;
612 
614  QgsRectangle transformedExtent() const;
615 
617  void mapPolygon( const QgsRectangle &extent, QPolygonF &poly ) const;
618 
622  void transformShift( double &xShift, double &yShift ) const;
623 
624  void drawAnnotations( QPainter *painter );
625  void drawAnnotation( const QgsAnnotation *item, QgsRenderContext &context );
626  QPointF composerMapPosForItem( const QgsAnnotation *item ) const;
627 
628  enum PartType
629  {
630  Background,
631  Layer,
632  Grid,
633  OverviewMapExtent,
634  Frame,
635  SelectionBoxes
636  };
637 
639  bool shouldDrawPart( PartType part ) const;
640 
645  void refreshMapExtents( const QgsExpressionContext *context = nullptr );
646 
647  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
648  friend class TestQgsComposerMap;
649 };
650 
651 #endif
652 
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:38
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:46
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:35
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.