QGIS API Documentation  2.99.0-Master (64819bc)
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 
212  bool keepLayerSet() const {return mKeepLayerSet;}
213 
220  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
221 
228  QList<QgsMapLayer*> layers() const;
229 
236  void setLayers( const QList<QgsMapLayer*> layers );
237 
239  bool keepLayerStyles() const { return mKeepLayerStyles; }
241  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
242 
244  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
246  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
248  void storeCurrentLayerStyles();
249 
259  bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
260 
263  void setFollowVisibilityPreset( bool follow ) { mFollowVisibilityPreset = follow; }
264 
268  QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
269 
272  void setFollowVisibilityPresetName( const QString& name ) { mFollowVisibilityPresetName = name; }
273 
274  // Set cache outdated
275  void setCacheUpdated( bool u = false );
276 
277  QgsRectangle extent() const {return mExtent;}
278 
280  void setOffset( double xOffset, double yOffset );
281 
283  bool containsWmsLayer() const;
284 
286  bool containsAdvancedEffects() const;
287 
292  bool writeXml( QDomElement& elem, QDomDocument & doc ) const override;
293 
298  bool readXml( const QDomElement& itemElem, const QDomDocument& doc ) override;
299 
306  QgsComposerMapGridStack* grids() { return mGridStack; }
307 
313  QgsComposerMapGrid* grid();
314 
321  QgsComposerMapOverviewStack* overviews() { return mOverviewStack; }
322 
328  QgsComposerMapOverview* overview();
329 
331  QRectF boundingRect() const override;
332 
333  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
334  virtual void setFrameOutlineWidth( const double outlineWidth ) override;
335 
340  void setMapRotation( double r );
341 
348  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
349 
350  void updateItem() override;
351 
356  void setDrawAnnotations( bool draw ) { mDrawAnnotations = draw; }
357 
362  bool drawAnnotations() const { return mDrawAnnotations; }
363 
365  double mapUnitsToMM() const;
366 
369  void assignFreeId();
370 
376  bool atlasDriven() const { return mAtlasDriven; }
377 
383  void setAtlasDriven( bool enabled );
384 
393  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
394 
403  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
404 
414  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
415 
422  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
423 
425  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
426 
428  bool updatesEnabled() const { return mUpdatesEnabled; }
429 
435  int numberExportLayers() const override;
436 
443  QPolygonF visibleExtentPolygon() const;
444 
445  //overridden to show "Map 1" type names
446  virtual QString displayName() const override;
447 
449  QPolygonF transformedMapPolygon() const;
450 
452  QPointF mapToItemCoords( QPointF mapCoords ) const;
453 
456  void requestedExtent( QgsRectangle& extent ) const;
457 
458  virtual QgsExpressionContext createExpressionContext() const override;
459 
460  signals:
461  void extentChanged();
462 
464  void mapRotationChanged( double newRotation );
465 
467  void preparedForAtlas();
468 
473  void layerStyleOverridesChanged();
474 
475  public slots:
476 
478  void updateCachedImage();
479 
483  void renderModeUpdateCachedImage();
484 
486  void updateBoundingRect();
487 
488  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
489 
490  private slots:
491  void layersAboutToBeRemoved( QList<QgsMapLayer*> layers );
492 
493  private:
494 
496  int mId = 0;
497 
498  QgsComposerMapGridStack* mGridStack = nullptr;
499 
500  QgsComposerMapOverviewStack* mOverviewStack = nullptr;
501 
502  // Map region in map units really used for rendering
503  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
504  // so that full rectangle in paper is used.
505  QgsRectangle mExtent;
506 
509 
510  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
511  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
512  // to manually tweak each atlas preview page without affecting the actual original map extent.
513  QgsRectangle mAtlasFeatureExtent;
514 
515  // Cache used in composer preview
516  QImage mCacheImage;
517 
518  // Is cache up to date
519  bool mCacheUpdated = false;
520 
522  PreviewMode mPreviewMode = QgsComposerMap::Rectangle;
523 
525  int mNumCachedLayers;
526 
528  bool mDrawing = false;
529 
531  double mXOffset = 0.0;
533  double mYOffset = 0.0;
534 
536  double mMapRotation = 0;
537 
540  double mEvaluatedMapRotation = 0;
541 
543  bool mKeepLayerSet = false;
544 
547 
548  bool mKeepLayerStyles = false;
550  QMap<QString, QString> mLayerStyleOverrides;
551 
555  bool mFollowVisibilityPreset = false;
556 
559  QString mFollowVisibilityPresetName;
560 
562  bool mUpdatesEnabled = true;
563 
565  void connectUpdateSlot();
566 
568  void syncLayerSet();
569 
571  const QgsComposerMapGrid* constFirstMapGrid() const;
572 
574  const QgsComposerMapOverview* constFirstMapOverview() const;
575 
577  QRectF mCurrentRectangle;
579  bool mDrawAnnotations = true;
580 
583  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
584 
586  bool mAtlasDriven = false;
588  AtlasScalingMode mAtlasScalingMode = Auto;
590  double mAtlasMargin = 0.10;
591 
592  void init();
593 
595  void updateToolTip();
596 
598  QList<QgsMapLayer*> layersToRender( const QgsExpressionContext* context = nullptr ) const;
599 
601  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext& context ) const;
602 
604  QgsRectangle transformedExtent() const;
605 
607  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
608 
612  void transformShift( double& xShift, double& yShift ) const;
613 
614  void drawAnnotations( QPainter* painter );
615  void drawAnnotation( const QgsAnnotation* item, QgsRenderContext& context );
616  QPointF composerMapPosForItem( const QgsAnnotation* item ) const;
617 
618  enum PartType
619  {
620  Background,
621  Layer,
622  Grid,
623  OverviewMapExtent,
624  Frame,
625  SelectionBoxes
626  };
627 
629  bool shouldDrawPart( PartType part ) const;
630 
635  void refreshMapExtents( const QgsExpressionContext* context = nullptr );
636 
637  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
638  friend class TestQgsComposerMap;
639 };
640 
641 #endif
642 
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;...
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.
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...
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...
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.
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.
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...
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.