QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgscomposeritem.h"
22 #include "qgsrectangle.h"
23 #include <QFont>
24 #include <QGraphicsRectItem>
25 
26 class QgsComposition;
30 class QgsComposerMapGrid;
31 class QgsMapRenderer;
32 class QgsMapToPixel;
33 class QDomNode;
34 class QDomDocument;
35 class QGraphicsView;
36 class QPainter;
37 class QgsFillSymbolV2;
38 class QgsLineSymbolV2;
39 class QgsVectorLayer;
40 class QgsAnnotation;
41 
48 class CORE_EXPORT QgsComposerMap : public QgsComposerItem
49 {
50  Q_OBJECT
51 
52  public:
54  QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height );
56  QgsComposerMap( QgsComposition *composition );
57  virtual ~QgsComposerMap();
58 
60  virtual int type() const override { return ComposerMap; }
61 
64  {
65  Cache = 0, // Use raster cache
66  Render, // Render the map
67  Rectangle // Display only rectangle
68  };
69 
70  //grid enums are moved to QgsComposerMapGrid
71  //TODO - remove for QGIS 3.0
72  enum GridStyle
73  {
74  Solid = 0, //solid lines
75  Cross, //only draw line crossings
77  FrameAnnotationsOnly
78  };
79 
81  {
82  InsideMapFrame = 0,
84  Disabled
85  };
86 
88  {
89  Horizontal = 0,
92  BoundaryDirection
93  };
94 
96  {
97  Decimal = 0,
104  DegreeMinuteSecondPadded
105  };
106 
108  {
109  NoGridFrame = 0,
110  Zebra, // black/white pattern
114  LineBorder
115  };
116 
118  enum Border
119  {
123  Top
124  };
125 
129  {
131  Predefined,
136  Auto
139  };
140 
148  void draw( QPainter *painter, const QgsRectangle& extent, QSizeF size, double dpi, double* forceWidthScale = nullptr );
149 
151  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) override;
152 
154  void cache();
155 
158  QgsMapSettings mapSettings( const QgsRectangle& extent, QSizeF size, int dpi ) const;
159 
161  int id() const {return mId;}
162 
164  bool isDrawing() const {return mDrawing;}
165 
167  void resize( double dx, double dy );
168 
173  void moveContent( double dx, double dy ) override;
174 
181  Q_DECL_DEPRECATED void zoomContent( int delta, double x, double y ) override;
182 
189  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) override;
190 
192  void setSceneRect( const QRectF& rectangle ) override;
193 
195  double scale() const;
196 
198  void setNewScale( double scaleDenominator, bool forceUpdate = true );
199 
206  void setNewExtent( const QgsRectangle& extent );
207 
216  void zoomToExtent( const QgsRectangle& extent );
217 
221  void setNewAtlasFeatureExtent( const QgsRectangle& extent );
222 
226  Q_DECL_DEPRECATED void toggleAtlasPreview() {}
227 
234  const QgsRectangle* currentMapExtent() const;
235 
237  QgsRectangle* currentMapExtent();
238 
239  PreviewMode previewMode() const {return mPreviewMode;}
240  void setPreviewMode( PreviewMode m );
241 
243  bool keepLayerSet() const {return mKeepLayerSet;}
245  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
246 
248  QStringList layerSet() const {return mLayerSet;}
250  void setLayerSet( const QStringList& layerSet ) {mLayerSet = layerSet;}
252  void storeCurrentLayerSet();
253 
255  bool keepLayerStyles() const { return mKeepLayerStyles; }
257  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
258 
260  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
262  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
264  void storeCurrentLayerStyles();
265 
275  bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
278  void setFollowVisibilityPreset( bool follow ) { mFollowVisibilityPreset = follow; }
282  QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
285  void setFollowVisibilityPresetName( const QString& name ) { mFollowVisibilityPresetName = name; }
286 
287  // Set cache outdated
288  void setCacheUpdated( bool u = false );
289 
290  QgsRectangle extent() const {return mExtent;}
291 
293  Q_DECL_DEPRECATED const QgsMapRenderer* mapRenderer() const;
294 
296  void setOffset( double xOffset, double yOffset );
297 
299  bool containsWMSLayer() const;
300 
302  bool containsAdvancedEffects() const;
303 
308  bool writeXML( QDomElement& elem, QDomDocument & doc ) const override;
309 
314  bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) override;
315 
319  Q_DECL_DEPRECATED void setGridEnabled( bool enabled );
320 
324  Q_DECL_DEPRECATED bool gridEnabled() const;
325 
329  Q_DECL_DEPRECATED void setGridStyle( GridStyle style );
330 
334  Q_DECL_DEPRECATED GridStyle gridStyle() const;
335 
339  Q_DECL_DEPRECATED void setGridIntervalX( double interval );
340 
344  Q_DECL_DEPRECATED double gridIntervalX() const;
345 
349  Q_DECL_DEPRECATED void setGridIntervalY( double interval );
350 
354  Q_DECL_DEPRECATED double gridIntervalY() const;
355 
359  Q_DECL_DEPRECATED void setGridOffsetX( double offset );
360 
364  Q_DECL_DEPRECATED double gridOffsetX() const;
365 
369  Q_DECL_DEPRECATED void setGridOffsetY( double offset );
370 
374  Q_DECL_DEPRECATED double gridOffsetY() const;
375 
379  Q_DECL_DEPRECATED void setGridPen( const QPen& p );
380 
384  Q_DECL_DEPRECATED QPen gridPen() const;
385 
389  Q_DECL_DEPRECATED void setGridPenWidth( double w );
390 
394  Q_DECL_DEPRECATED void setGridPenColor( const QColor& c );
395 
399  Q_DECL_DEPRECATED void setGridAnnotationFont( const QFont& f );
400 
404  Q_DECL_DEPRECATED QFont gridAnnotationFont() const;
405 
409  Q_DECL_DEPRECATED void setAnnotationFontColor( const QColor& c );
410 
414  Q_DECL_DEPRECATED QColor annotationFontColor() const;
415 
419  Q_DECL_DEPRECATED void setGridAnnotationPrecision( int p );
420 
424  Q_DECL_DEPRECATED int gridAnnotationPrecision() const;
425 
429  Q_DECL_DEPRECATED void setShowGridAnnotation( bool show );
430 
434  Q_DECL_DEPRECATED bool showGridAnnotation() const;
435 
439  Q_DECL_DEPRECATED void setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border );
440 
444  Q_DECL_DEPRECATED GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const;
445 
449  Q_DECL_DEPRECATED void setAnnotationFrameDistance( double d );
450 
454  Q_DECL_DEPRECATED double annotationFrameDistance() const;
455 
459  Q_DECL_DEPRECATED void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
460 
464  Q_DECL_DEPRECATED GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;
465 
469  Q_DECL_DEPRECATED void setGridAnnotationFormat( GridAnnotationFormat f );
470 
474  Q_DECL_DEPRECATED GridAnnotationFormat gridAnnotationFormat() const;
475 
479  Q_DECL_DEPRECATED void setGridFrameStyle( GridFrameStyle style );
480 
484  Q_DECL_DEPRECATED GridFrameStyle gridFrameStyle() const;
485 
489  Q_DECL_DEPRECATED void setGridFrameWidth( double w );
490 
494  Q_DECL_DEPRECATED double gridFrameWidth() const;
495 
500  Q_DECL_DEPRECATED void setGridFramePenSize( double w );
501 
505  Q_DECL_DEPRECATED double gridFramePenSize() const;
506 
511  Q_DECL_DEPRECATED void setGridFramePenColor( const QColor& c );
512 
517  Q_DECL_DEPRECATED QColor gridFramePenColor() const;
518 
523  Q_DECL_DEPRECATED void setGridFrameFillColor1( const QColor& c );
524 
529  Q_DECL_DEPRECATED QColor gridFrameFillColor1() const;
530 
535  Q_DECL_DEPRECATED void setGridFrameFillColor2( const QColor& c );
536 
541  Q_DECL_DEPRECATED QColor gridFrameFillColor2() const;
542 
546  Q_DECL_DEPRECATED void setCrossLength( double l );
547 
551  Q_DECL_DEPRECATED double crossLength();
552 
556  Q_DECL_DEPRECATED void setGridLineSymbol( QgsLineSymbolV2* symbol );
557 
561  Q_DECL_DEPRECATED QgsLineSymbolV2* gridLineSymbol();
562 
566  Q_DECL_DEPRECATED QPainter::CompositionMode gridBlendMode() const;
567 
571  Q_DECL_DEPRECATED void setGridBlendMode( QPainter::CompositionMode blendMode );
572 
579  QgsComposerMapGridStack* grids() { return mGridStack; }
580 
586  QgsComposerMapGrid* grid();
587 
594  QgsComposerMapOverviewStack* overviews() { return mOverviewStack; }
595 
601  QgsComposerMapOverview* overview();
602 
604  QRectF boundingRect() const override;
605 
606  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
607  virtual void setFrameOutlineWidth( const double outlineWidth ) override;
608 
613  Q_DECL_DEPRECATED void setRotation( double r ) override;
614 
618  Q_DECL_DEPRECATED double rotation() const { return mMapRotation;}
619 
624  void setMapRotation( double r );
625 
632  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
633 
634  void updateItem() override;
635 
637  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
638 
639  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
640  bool drawCanvasItems() const { return mDrawCanvasItems; }
641 
646  void setDrawSelection( bool b ) { mDrawSelection = b; }
651  bool drawSelection() const { return mDrawSelection; }
652 
654  double mapUnitsToMM() const;
655 
659  Q_DECL_DEPRECATED void setOverviewFrameMap( int mapId );
660 
664  Q_DECL_DEPRECATED int overviewFrameMapId() const;
665 
669  Q_DECL_DEPRECATED void setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol );
670 
674  Q_DECL_DEPRECATED QgsFillSymbolV2* overviewFrameMapSymbol();
675 
679  Q_DECL_DEPRECATED QPainter::CompositionMode overviewBlendMode() const;
680 
684  Q_DECL_DEPRECATED void setOverviewBlendMode( QPainter::CompositionMode blendMode );
685 
689  Q_DECL_DEPRECATED bool overviewInverted() const;
690 
694  Q_DECL_DEPRECATED void setOverviewInverted( bool inverted );
695 
699  Q_DECL_DEPRECATED bool overviewCentered() const;
700 
704  Q_DECL_DEPRECATED void setOverviewCentered( bool centered );
705 
708  void assignFreeId();
709 
714  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
719  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
724  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
725 
731  bool atlasDriven() const { return mAtlasDriven; }
732 
738  void setAtlasDriven( bool enabled );
739 
743  Q_DECL_DEPRECATED bool atlasFixedScale() const;
744 
748  Q_DECL_DEPRECATED void setAtlasFixedScale( bool fixed );
749 
758  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
759 
768  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
769 
779  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
780 
787  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
788 
790  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
791 
793  bool updatesEnabled() const { return mUpdatesEnabled; }
794 
800  int numberExportLayers() const override;
801 
808  QPolygonF visibleExtentPolygon() const;
809 
810  //overridden to show "Map 1" type names
811  virtual QString displayName() const override;
812 
814  QPolygonF transformedMapPolygon() const;
815 
817  QPointF mapToItemCoords( QPointF mapCoords ) const;
818 
820  Q_DECL_DEPRECATED void connectMapOverviewSignals();
821 
824  void requestedExtent( QgsRectangle& extent ) const;
825 
826  virtual QgsExpressionContext* createExpressionContext() const override;
827 
828  signals:
829  void extentChanged();
830 
832  void mapRotationChanged( double newRotation );
833 
835  void preparedForAtlas();
836 
841  void layerStyleOverridesChanged();
842 
843  public slots:
844 
846  void updateCachedImage();
847 
851  void renderModeUpdateCachedImage();
852 
854  void updateBoundingRect();
855 
858 
859  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
860 
861  protected slots:
862 
867  void layersChanged();
868 
869  private:
870 
872  int mId;
873 
874  QgsComposerMapGridStack* mGridStack;
875 
876  QgsComposerMapOverviewStack* mOverviewStack;
877 
878  // Map region in map units realy used for rendering
879  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
880  // so that full rectangle in paper is used.
881  QgsRectangle mExtent;
882 
883  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
884  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
885  // to manually tweak each atlas preview page without affecting the actual original map extent.
886  QgsRectangle mAtlasFeatureExtent;
887 
888  // Cache used in composer preview
889  QImage mCacheImage;
890 
891  // Is cache up to date
892  bool mCacheUpdated;
893 
895  PreviewMode mPreviewMode;
896 
898  int mNumCachedLayers;
899 
901  bool mDrawing;
902 
904  double mXOffset;
906  double mYOffset;
907 
909  double mMapRotation;
912  double mEvaluatedMapRotation;
913 
915  bool mKeepLayerSet;
916 
918  QStringList mLayerSet;
919 
920  bool mKeepLayerStyles;
922  QMap<QString, QString> mLayerStyleOverrides;
923 
927  bool mFollowVisibilityPreset;
930  QString mFollowVisibilityPresetName;
931 
933  bool mUpdatesEnabled;
934 
936  void connectUpdateSlot();
937 
939  void syncLayerSet();
940 
942  const QgsComposerMapGrid* constFirstMapGrid() const;
943 
945  const QgsComposerMapOverview* constFirstMapOverview() const;
946 
948  QRectF mCurrentRectangle;
949  QGraphicsView* mMapCanvas;
951  bool mDrawCanvasItems;
952 
954  bool mDrawSelection;
955 
958  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
959 
961  bool mAtlasDriven;
963  AtlasScalingMode mAtlasScalingMode;
965  double mAtlasMargin;
966 
967  void init();
968 
970  void updateToolTip();
971 
973  QStringList layersToRender( const QgsExpressionContext* context = nullptr ) const;
974 
976  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext& context ) const;
977 
979  QgsRectangle transformedExtent() const;
980 
982  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
983 
987  void transformShift( double& xShift, double& yShift ) const;
988 
989  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
990  void drawCanvasItem( const QgsAnnotation* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
991  QPointF composerMapPosForItem( const QgsAnnotation* item ) const;
992 
993  enum PartType
994  {
995  Background,
996  Layer,
997  Grid,
998  OverviewMapExtent,
999  Frame,
1000  SelectionBoxes
1001  };
1002 
1004  bool shouldDrawPart( PartType part ) const;
1005 
1010  void refreshMapExtents( const QgsExpressionContext* context = nullptr );
1011 
1012  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
1013  friend class TestQgsComposerMap;
1014 };
1016 
1017 #endif
1018 
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:35
virtual QString displayName() const
Get item display name.
Q_DECL_DEPRECATED void toggleAtlasPreview()
Called when atlas preview is toggled, to force map item to update its extent and redraw.
void overviewExtentChanged()
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;...
virtual void setRotation(double r)
Sets the item rotation.
QStringList layerSet() const
Getter for stored layer set that is used if mKeepLayerSet is true.
bool followVisibilityPreset() const
Whether the map should follow a visibility preset.
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.
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:515
bool updatesEnabled() const
Returns whether updates to the composer map are enabled.
Border
Enum for different frame borders.
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)
Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect(double &x, double &y, double width, double height, double rotation) const
Calculates corner point after rotation and scaling.
virtual bool writeXML(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
A non GUI class for rendering a map layer set onto a QPainter.
virtual QgsExpressionContext * createExpressionContext() const override
Creates an expression context relating to the item&#39;s current state.
DataDefinedProperty
Data defined properties for different item types.
An interface for annotation items which are drawn over a map.
Definition: qgsannotation.h:40
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 QRectF boundingRect() const
virtual void moveContent(double dx, double dy)
Move Content of item.
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
const char * name() const
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 visibility preset.
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.
bool drawSelection() const
Get the flag to draw selection in map.
Graphics scene for map printing.
Object representing map window.
void setUpdatesEnabled(bool enabled)
Sets whether updates to the composer map are enabled.
virtual void setFrameOutlineWidth(const double outlineWidth)
Sets frame outline width.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:516
virtual Q_DECL_DEPRECATED void zoomContent(int delta, double x, double y)
Zoom content of item.
AtlasScalingMode atlasScalingMode() const
Returns the current atlas scaling mode.
PreviewMode previewMode() const
virtual bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
Sets item state from DOM element.
void setLayerSet(const QStringList &layerSet)
Setter for stored layer set that is used if mKeepLayerSet is true.
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 setDrawSelection(bool b)
Set the flag to draw selection in map.
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...
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...
Q_DECL_DEPRECATED double rotation() const
Returns the rotation used for drawing the map within the composer item.
Q_DECL_DEPRECATED bool imageSizeConsideringRotation(double &width, double &height, double rotation) const
Calculates width and hight of the picture (in mm) such that it fits into the item frame with the give...
Q_DECL_DEPRECATED void sizeChangedByRotation(double &width, double &height, double rotation)
Calculates width / height of the bounding box of a rotated rectangle.
qreal scale() const
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.