QGIS API Documentation  2.99.0-Master (314842d)
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 "qgscomposeritem.h"
23 #include "qgsrectangle.h"
25 #include "qgsrendercontext.h"
26 #include "qgsmaplayer.h"
27 #include <QFont>
28 #include <QGraphicsRectItem>
29 
30 class QgsComposition;
34 class QgsComposerMapGrid;
35 class QgsMapToPixel;
36 class QDomNode;
37 class QDomDocument;
38 class QPainter;
39 class QgsFillSymbol;
40 class QgsLineSymbol;
41 class QgsVectorLayer;
42 class QgsAnnotation;
43 
49 class CORE_EXPORT QgsComposerMap : public QgsComposerItem
50 {
51  Q_OBJECT
52 
53  public:
55  QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height );
57  QgsComposerMap( QgsComposition *composition );
58  virtual ~QgsComposerMap();
59 
61  virtual int type() const override { return ComposerMap; }
62 
65  {
66  Cache = 0, // Use raster cache
67  Render, // Render the map
68  Rectangle // Display only rectangle
69  };
70 
74  {
76  Predefined,
81  Auto
84  };
85 
93  void draw( QPainter *painter, const QgsRectangle &extent, QSizeF size, double dpi, double *forceWidthScale = nullptr );
94 
96  void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
97 
99  void cache();
100 
103  QgsMapSettings mapSettings( const QgsRectangle &extent, QSizeF size, int dpi ) const;
104 
106  int id() const {return mId;}
107 
109  bool isDrawing() const {return mDrawing;}
110 
112  void resize( double dx, double dy );
113 
118  void moveContent( double dx, double dy ) override;
119 
126  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) override;
127 
129  void setSceneRect( const QRectF &rectangle ) override;
130 
132  double scale() const;
133 
135  void setNewScale( double scaleDenominator, bool forceUpdate = true );
136 
143  void setNewExtent( const QgsRectangle &extent );
144 
153  void zoomToExtent( const QgsRectangle &extent );
154 
158  void setNewAtlasFeatureExtent( const QgsRectangle &extent );
159 
166  const QgsRectangle *currentMapExtent() const;
167 
169  QgsRectangle *currentMapExtent();
170 
180  QgsCoordinateReferenceSystem crs() const;
181 
191  QgsCoordinateReferenceSystem presetCrs() const { return mCrs; }
192 
201  void setCrs( const QgsCoordinateReferenceSystem &crs );
202 
203  PreviewMode previewMode() const {return mPreviewMode;}
204  void setPreviewMode( PreviewMode m );
205 
214  bool keepLayerSet() const {return mKeepLayerSet;}
215 
224  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
225 
232  QList<QgsMapLayer *> layers() const;
233 
240  void setLayers( const QList<QgsMapLayer *> &layers );
241 
243  bool keepLayerStyles() const { return mKeepLayerStyles; }
245  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
246 
248  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
250  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
252  void storeCurrentLayerStyles();
253 
263  bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
264 
267  void setFollowVisibilityPreset( bool follow ) { mFollowVisibilityPreset = follow; }
268 
272  QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
273 
276  void setFollowVisibilityPresetName( const QString &name ) { mFollowVisibilityPresetName = name; }
277 
278  // Set cache outdated
279  void setCacheUpdated( bool u = false );
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 
344  void setMapRotation( double r );
345 
352  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
353 
354  void updateItem() override;
355 
360  void setDrawAnnotations( bool draw ) { mDrawAnnotations = draw; }
361 
366  bool drawAnnotations() const { return mDrawAnnotations; }
367 
369  double mapUnitsToMM() const;
370 
373  void assignFreeId();
374 
380  bool atlasDriven() const { return mAtlasDriven; }
381 
387  void setAtlasDriven( bool enabled );
388 
397  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
398 
407  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
408 
418  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
419 
426  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
427 
433  int numberExportLayers() const override;
434 
441  QPolygonF visibleExtentPolygon() const;
442 
443  //overridden to show "Map 1" type names
444  virtual QString displayName() const override;
445 
447  QPolygonF transformedMapPolygon() const;
448 
450  QPointF mapToItemCoords( QPointF mapCoords ) const;
451 
454  void requestedExtent( QgsRectangle &extent ) const;
455 
456  virtual QgsExpressionContext createExpressionContext() const override;
457 
458  signals:
459  void extentChanged();
460 
462  void mapRotationChanged( double newRotation );
463 
465  void preparedForAtlas();
466 
471  void layerStyleOverridesChanged();
472 
473  public slots:
474 
476  void updateCachedImage();
477 
481  void renderModeUpdateCachedImage();
482 
484  void updateBoundingRect();
485 
486  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
487 
488  private slots:
489  void layersAboutToBeRemoved( QList<QgsMapLayer *> layers );
490 
491  private:
492 
494  int mId = 0;
495 
496  QgsComposerMapGridStack *mGridStack = nullptr;
497 
498  QgsComposerMapOverviewStack *mOverviewStack = nullptr;
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  // Cache used in composer preview
514  QImage mCacheImage;
515 
516  // Is cache up to date
517  bool mCacheUpdated = false;
518 
520  PreviewMode mPreviewMode = QgsComposerMap::Cache;
521 
523  int mNumCachedLayers;
524 
526  bool mDrawing = false;
527 
529  double mXOffset = 0.0;
531  double mYOffset = 0.0;
532 
534  double mMapRotation = 0;
535 
538  double mEvaluatedMapRotation = 0;
539 
541  bool mKeepLayerSet = false;
542 
545 
546  bool mKeepLayerStyles = false;
548  QMap<QString, QString> mLayerStyleOverrides;
549 
553  bool mFollowVisibilityPreset = false;
554 
557  QString mFollowVisibilityPresetName;
558 
560  void connectUpdateSlot();
561 
563  void syncLayerSet();
564 
566  const QgsComposerMapGrid *constFirstMapGrid() const;
567 
569  const QgsComposerMapOverview *constFirstMapOverview() const;
570 
572  QRectF mCurrentRectangle;
574  bool mDrawAnnotations = true;
575 
578  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle &extent ) const;
579 
581  bool mAtlasDriven = false;
583  AtlasScalingMode mAtlasScalingMode = Auto;
585  double mAtlasMargin = 0.10;
586 
587  void init();
588 
590  void updateToolTip();
591 
593  QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
594 
596  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;
597 
599  QgsRectangle transformedExtent() const;
600 
602  void mapPolygon( const QgsRectangle &extent, QPolygonF &poly ) const;
603 
607  void transformShift( double &xShift, double &yShift ) const;
608 
609  void drawAnnotations( QPainter *painter );
610  void drawAnnotation( const QgsAnnotation *item, QgsRenderContext &context );
611  QPointF composerMapPosForItem( const QgsAnnotation *item ) const;
612 
613  enum PartType
614  {
615  Background,
616  Layer,
617  Grid,
618  OverviewMapExtent,
619  Frame,
620  SelectionBoxes
621  };
622 
624  bool shouldDrawPart( PartType part ) const;
625 
630  void refreshMapExtents( const QgsExpressionContext *context = nullptr );
631 
632  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
633  friend class TestQgsComposerMap;
634 };
635 
636 #endif
637 
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:36
virtual QString displayName() const
Get item display name.
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:45
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:34
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...
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.
PreviewMode
Preview style.
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.
QList< QgsWeakMapLayerPointer > QgsWeakMapLayerPointerList
A list of weak pointers to QgsMapLayers.
Definition: qgsmaplayer.h:983
PreviewMode previewMode() const
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...
virtual void updateItem()
Updates (redraws) the item, with the possibility to do custom update for subclasses.
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.