QGIS API Documentation  2.99.0-Master (0a63d1f)
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"
24 #include <QFont>
25 #include <QGraphicsRectItem>
26 
27 class QgsComposition;
31 class QgsComposerMapGrid;
32 class QgsMapToPixel;
33 class QDomNode;
34 class QDomDocument;
35 class QGraphicsView;
36 class QPainter;
37 class QgsFillSymbol;
38 class QgsLineSymbol;
39 class QgsVectorLayer;
40 class QgsAnnotation;
41 
47 class CORE_EXPORT QgsComposerMap : public QgsComposerItem
48 {
49  Q_OBJECT
50 
51  public:
53  QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height );
55  QgsComposerMap( QgsComposition *composition );
56  virtual ~QgsComposerMap();
57 
59  virtual int type() const override { return ComposerMap; }
60 
63  {
64  Cache = 0, // Use raster cache
65  Render, // Render the map
66  Rectangle // Display only rectangle
67  };
68 
72  {
74  Predefined,
79  Auto
82  };
83 
91  void draw( QPainter *painter, const QgsRectangle& extent, QSizeF size, double dpi, double* forceWidthScale = nullptr );
92 
94  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) override;
95 
97  void cache();
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 
130  double scale() const;
131 
133  void setNewScale( double scaleDenominator, bool forceUpdate = true );
134 
141  void setNewExtent( const QgsRectangle& extent );
142 
151  void zoomToExtent( const QgsRectangle& extent );
152 
156  void setNewAtlasFeatureExtent( const QgsRectangle& extent );
157 
164  const QgsRectangle* currentMapExtent() const;
165 
167  QgsRectangle* currentMapExtent();
168 
169  PreviewMode previewMode() const {return mPreviewMode;}
170  void setPreviewMode( PreviewMode m );
171 
173  bool keepLayerSet() const {return mKeepLayerSet;}
175  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
176 
178  QList<QgsMapLayer*> layers() const;
180  void setLayers( const QList<QgsMapLayer*> layers );
181 
183  bool keepLayerStyles() const { return mKeepLayerStyles; }
185  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
186 
188  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
190  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
192  void storeCurrentLayerStyles();
193 
203  bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
204 
207  void setFollowVisibilityPreset( bool follow ) { mFollowVisibilityPreset = follow; }
208 
212  QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
213 
216  void setFollowVisibilityPresetName( const QString& name ) { mFollowVisibilityPresetName = name; }
217 
218  // Set cache outdated
219  void setCacheUpdated( bool u = false );
220 
221  QgsRectangle extent() const {return mExtent;}
222 
224  void setOffset( double xOffset, double yOffset );
225 
227  bool containsWmsLayer() const;
228 
230  bool containsAdvancedEffects() const;
231 
236  bool writeXml( QDomElement& elem, QDomDocument & doc ) const override;
237 
242  bool readXml( const QDomElement& itemElem, const QDomDocument& doc ) override;
243 
250  QgsComposerMapGridStack* grids() { return mGridStack; }
251 
257  QgsComposerMapGrid* grid();
258 
265  QgsComposerMapOverviewStack* overviews() { return mOverviewStack; }
266 
272  QgsComposerMapOverview* overview();
273 
275  QRectF boundingRect() const override;
276 
277  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
278  virtual void setFrameOutlineWidth( const double outlineWidth ) override;
279 
284  void setMapRotation( double r );
285 
292  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
293 
294  void updateItem() override;
295 
297  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
298 
299  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
300  bool drawCanvasItems() const { return mDrawCanvasItems; }
301 
303  double mapUnitsToMM() const;
304 
307  void assignFreeId();
308 
314  bool atlasDriven() const { return mAtlasDriven; }
315 
321  void setAtlasDriven( bool enabled );
322 
331  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
332 
341  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
342 
352  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
353 
360  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
361 
363  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
364 
366  bool updatesEnabled() const { return mUpdatesEnabled; }
367 
373  int numberExportLayers() const override;
374 
381  QPolygonF visibleExtentPolygon() const;
382 
383  //overridden to show "Map 1" type names
384  virtual QString displayName() const override;
385 
387  QPolygonF transformedMapPolygon() const;
388 
390  QPointF mapToItemCoords( QPointF mapCoords ) const;
391 
394  void requestedExtent( QgsRectangle& extent ) const;
395 
396  virtual QgsExpressionContext createExpressionContext() const override;
397 
398  signals:
399  void extentChanged();
400 
402  void mapRotationChanged( double newRotation );
403 
405  void preparedForAtlas();
406 
411  void layerStyleOverridesChanged();
412 
413  public slots:
414 
416  void updateCachedImage();
417 
421  void renderModeUpdateCachedImage();
422 
424  void updateBoundingRect();
425 
426  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
427 
428  protected slots:
429 
434  void layersChanged();
435 
436  private:
437 
439  int mId;
440 
441  QgsComposerMapGridStack* mGridStack;
442 
443  QgsComposerMapOverviewStack* mOverviewStack;
444 
445  // Map region in map units really used for rendering
446  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
447  // so that full rectangle in paper is used.
448  QgsRectangle mExtent;
449 
450  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
451  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
452  // to manually tweak each atlas preview page without affecting the actual original map extent.
453  QgsRectangle mAtlasFeatureExtent;
454 
455  // Cache used in composer preview
456  QImage mCacheImage;
457 
458  // Is cache up to date
459  bool mCacheUpdated;
460 
462  PreviewMode mPreviewMode;
463 
465  int mNumCachedLayers;
466 
468  bool mDrawing;
469 
471  double mXOffset;
473  double mYOffset;
474 
476  double mMapRotation;
477 
480  double mEvaluatedMapRotation;
481 
483  bool mKeepLayerSet;
484 
486  QList< QPointer<QgsMapLayer> > mLayers;
487 
488  bool mKeepLayerStyles;
490  QMap<QString, QString> mLayerStyleOverrides;
491 
495  bool mFollowVisibilityPreset;
496 
499  QString mFollowVisibilityPresetName;
500 
502  bool mUpdatesEnabled;
503 
505  void connectUpdateSlot();
506 
508  void syncLayerSet();
509 
511  const QgsComposerMapGrid* constFirstMapGrid() const;
512 
514  const QgsComposerMapOverview* constFirstMapOverview() const;
515 
517  QRectF mCurrentRectangle;
518  QGraphicsView* mMapCanvas;
520  bool mDrawCanvasItems;
521 
524  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
525 
527  bool mAtlasDriven;
529  AtlasScalingMode mAtlasScalingMode;
531  double mAtlasMargin;
532 
533  void init();
534 
536  void updateToolTip();
537 
539  QList<QgsMapLayer*> layersToRender( const QgsExpressionContext* context = nullptr ) const;
540 
542  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext& context ) const;
543 
545  QgsRectangle transformedExtent() const;
546 
548  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
549 
553  void transformShift( double& xShift, double& yShift ) const;
554 
555  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
556  void drawCanvasItem( const QgsAnnotation* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
557  QPointF composerMapPosForItem( const QgsAnnotation* item ) const;
558 
559  enum PartType
560  {
561  Background,
562  Layer,
563  Grid,
564  OverviewMapExtent,
565  Frame,
566  SelectionBoxes
567  };
568 
570  bool shouldDrawPart( PartType part ) const;
571 
576  void refreshMapExtents( const QgsExpressionContext* context = nullptr );
577 
578  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
579  friend class TestQgsComposerMap;
580 };
581 
582 #endif
583 
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.
bool drawCanvasItems() const
QgsComposerMapOverviewStack * overviews()
Returns the map item&#39;s overview stack, which is used to control how overviews are drawn over the map&#39;...
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.
bool updatesEnabled() const
Returns whether updates to the composer map are enabled.
virtual int numberExportLayers() const
Get the number of layers that this item requires for exporting as layers.
int id() const
Get identification number.
void setMapCanvas(QGraphicsView *canvas)
Sets canvas pointer (necessary to query and draw map canvas items)
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.
DataDefinedProperty
Data defined properties for different item types.
An interface for annotation items which are drawn over a map.
Definition: qgsannotation.h:41
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...
virtual void updateItem()
Updates item, with the possibility to do custom update for subclasses.
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...
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.
void setUpdatesEnabled(bool enabled)
Sets whether updates to the composer map are enabled.
virtual QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the item&#39;s current state.
virtual void setFrameOutlineWidth(const double outlineWidth)
Sets frame outline width.
AtlasScalingMode atlasScalingMode() const
Returns the current atlas scaling mode.
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 setDrawCanvasItems(bool b)
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 the stored layer set should be used or the current layer set of th...
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.