QGIS API Documentation  2.99.0-Master (d55fa22)
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 
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() SIP_SKIP;
168 
178  QgsCoordinateReferenceSystem crs() const;
179 
189  QgsCoordinateReferenceSystem presetCrs() const { return mCrs; }
190 
199  void setCrs( const QgsCoordinateReferenceSystem &crs );
200 
209  bool keepLayerSet() const {return mKeepLayerSet;}
210 
219  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
220 
227  QList<QgsMapLayer *> layers() const;
228 
235  void setLayers( const QList<QgsMapLayer *> &layers );
236 
238  bool keepLayerStyles() const { return mKeepLayerStyles; }
240  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
241 
243  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
245  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
247  void storeCurrentLayerStyles();
248 
258  bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
259 
262  void setFollowVisibilityPreset( bool follow ) { mFollowVisibilityPreset = follow; }
263 
267  QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
268 
271  void setFollowVisibilityPresetName( const QString &name ) { mFollowVisibilityPresetName = name; }
272 
273  QgsRectangle extent() const {return mExtent;}
274 
276  void setOffset( double xOffset, double yOffset );
277 
279  bool containsWmsLayer() const;
280 
282  bool containsAdvancedEffects() const;
283 
288  bool writeXml( QDomElement &elem, QDomDocument &doc ) const override;
289 
294  bool readXml( const QDomElement &itemElem, const QDomDocument &doc ) override;
295 
302  QgsComposerMapGridStack *grids() { return mGridStack; }
303 
309  QgsComposerMapGrid *grid();
310 
317  QgsComposerMapOverviewStack *overviews() { return mOverviewStack; }
318 
324  QgsComposerMapOverview *overview();
325 
327  QRectF boundingRect() const override;
328 
329  /* reimplement setFrameStrokeWidth, so that updateBoundingRect() is called after setting the frame width */
330  virtual void setFrameStrokeWidth( const double strokeWidth ) override;
331 
336  void setMapRotation( double r );
337 
344  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
345 
350  void setDrawAnnotations( bool draw ) { mDrawAnnotations = draw; }
351 
356  bool drawAnnotations() const { return mDrawAnnotations; }
357 
359  double mapUnitsToMM() const;
360 
363  void assignFreeId();
364 
370  bool atlasDriven() const { return mAtlasDriven; }
371 
377  void setAtlasDriven( bool enabled );
378 
387  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
388 
397  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
398 
408  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
409 
416  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
417 
423  int numberExportLayers() const override;
424 
431  QPolygonF visibleExtentPolygon() const;
432 
433  //overridden to show "Map 1" type names
434  virtual QString displayName() const override;
435 
437  QPolygonF transformedMapPolygon() const;
438 
440  QPointF mapToItemCoords( QPointF mapCoords ) const;
441 
444  void requestedExtent( QgsRectangle &extent ) const;
445 
446  virtual QgsExpressionContext createExpressionContext() const override;
447 
448  signals:
449  void extentChanged();
450 
452  void mapRotationChanged( double newRotation );
453 
455  void preparedForAtlas();
456 
461  void layerStyleOverridesChanged();
462 
463  public slots:
464 
469  void invalidateCache();
470 
472  void updateBoundingRect();
473 
474  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext *context = nullptr ) override;
475 
476  private slots:
477  void layersAboutToBeRemoved( QList<QgsMapLayer *> layers );
478 
479  void painterJobFinished();
480 
481  private:
482 
484  int mId = 0;
485 
486  QgsComposerMapGridStack *mGridStack = nullptr;
487 
488  QgsComposerMapOverviewStack *mOverviewStack = nullptr;
489 
490  // Map region in map units really used for rendering
491  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
492  // so that full rectangle in paper is used.
493  QgsRectangle mExtent;
494 
497 
498  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
499  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
500  // to manually tweak each atlas preview page without affecting the actual original map extent.
501  QgsRectangle mAtlasFeatureExtent;
502 
503  // We have two images used for rendering/storing cached map images.
504  // the first (mCacheFinalImage) is used ONLY for storing the most recent completed map render. It's always
505  // used when drawing map item previews. The second (mCacheRenderingImage) is used temporarily while
506  // rendering a new preview image in the background. If (and only if) the background render completes, then
507  // mCacheRenderingImage is pushed into mCacheFinalImage, and used from then on when drawing the item preview.
508  // This ensures that something is always shown in the map item, even while refreshing the preview image in the
509  // background
510  std::unique_ptr< QImage > mCacheFinalImage;
511  std::unique_ptr< QImage > mCacheRenderingImage;
512 
514  bool mCacheInvalidated = true;
515 
517  int mNumCachedLayers;
518 
520  bool mDrawing = false;
521 
523  double mXOffset = 0.0;
525  double mYOffset = 0.0;
526 
527  double mLastRenderedImageOffsetX = 0.0;
528  double mLastRenderedImageOffsetY = 0.0;
529 
531  double mMapRotation = 0;
532 
535  double mEvaluatedMapRotation = 0;
536 
538  bool mKeepLayerSet = false;
539 
541  QList< QgsMapLayerRef > mLayers;
542 
543  bool mKeepLayerStyles = false;
545  QMap<QString, QString> mLayerStyleOverrides;
546 
550  bool mFollowVisibilityPreset = false;
551 
554  QString mFollowVisibilityPresetName;
555 
557  void recreateCachedImageInBackground();
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  std::unique_ptr< QPainter > mPainter;
588  std::unique_ptr< QgsMapRendererCustomPainterJob > mPainterJob;
589  bool mPainterCancelWait = false;
590 
591  void init();
592 
594  void updateToolTip();
595 
597  QList<QgsMapLayer *> layersToRender( const QgsExpressionContext *context = nullptr ) const;
598 
600  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext &context ) const;
601 
603  QgsRectangle transformedExtent() const;
604 
606  void mapPolygon( const QgsRectangle &extent, QPolygonF &poly ) const;
607 
611  void transformShift( double &xShift, double &yShift ) const;
612 
613  void drawAnnotations( QPainter *painter );
614  void drawAnnotation( const QgsAnnotation *item, QgsRenderContext &context );
615  QPointF composerMapPosForItem( const QgsAnnotation *item ) const;
616 
617  enum PartType
618  {
619  Background,
620  Layer,
621  Grid,
622  OverviewMapExtent,
623  Frame,
624  SelectionBoxes
625  };
626 
628  bool shouldDrawPart( PartType part ) const;
629 
634  void refreshMapExtents( const QgsExpressionContext *context = nullptr );
635 
636  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
637  friend class TestQgsComposerMap;
638 };
639 
640 #endif
641 
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:34
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:87
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.