QGIS API Documentation
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 
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 
69  //grid enums are moved to QgsComposerMapGrid
70  //TODO - remove for QGIS 3.0
71  enum GridStyle
72  {
73  Solid = 0, //solid lines
74  Cross, //only draw line crossings
76  FrameAnnotationsOnly
77  };
78 
80  {
81  InsideMapFrame = 0,
83  Disabled
84  };
85 
87  {
88  Horizontal = 0,
91  BoundaryDirection
92  };
93 
95  {
96  Decimal = 0,
103  DegreeMinuteSecondPadded
104  };
105 
107  {
108  NoGridFrame = 0,
109  Zebra, // black/white pattern
113  LineBorder
114  };
115 
117  enum Border
118  {
122  Top
123  };
124 
128  {
130  Predefined,
135  Auto
138  };
139 
147  void draw( QPainter *painter, const QgsRectangle& extent, QSizeF size, double dpi, double* forceWidthScale = nullptr );
148 
150  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) override;
151 
153  void cache();
154 
157  QgsMapSettings mapSettings( const QgsRectangle& extent, QSizeF size, int dpi ) const;
158 
160  int id() const {return mId;}
161 
163  bool isDrawing() const {return mDrawing;}
164 
166  void resize( double dx, double dy );
167 
172  void moveContent( double dx, double dy ) override;
173 
180  Q_DECL_DEPRECATED void zoomContent( int delta, double x, double y ) override;
181 
188  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) override;
189 
191  void setSceneRect( const QRectF& rectangle ) override;
192 
194  double scale() const;
195 
197  void setNewScale( double scaleDenominator, bool forceUpdate = true );
198 
205  void setNewExtent( const QgsRectangle& extent );
206 
215  void zoomToExtent( const QgsRectangle& extent );
216 
220  void setNewAtlasFeatureExtent( const QgsRectangle& extent );
221 
225  Q_DECL_DEPRECATED void toggleAtlasPreview() {}
226 
233  const QgsRectangle* currentMapExtent() const;
234 
236  QgsRectangle* currentMapExtent();
237 
238  PreviewMode previewMode() const {return mPreviewMode;}
239  void setPreviewMode( PreviewMode m );
240 
242  bool keepLayerSet() const {return mKeepLayerSet;}
244  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
245 
247  QStringList layerSet() const {return mLayerSet;}
249  void setLayerSet( const QStringList& layerSet ) {mLayerSet = layerSet;}
251  void storeCurrentLayerSet();
252 
254  bool keepLayerStyles() const { return mKeepLayerStyles; }
256  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
257 
259  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
261  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
263  void storeCurrentLayerStyles();
264 
265  // Set cache outdated
266  void setCacheUpdated( bool u = false );
267 
268  QgsRectangle extent() const {return mExtent;}
269 
271  Q_DECL_DEPRECATED const QgsMapRenderer* mapRenderer() const;
272 
274  void setOffset( double xOffset, double yOffset );
275 
277  bool containsWMSLayer() const;
278 
280  bool containsAdvancedEffects() const;
281 
286  bool writeXML( QDomElement& elem, QDomDocument & doc ) const override;
287 
292  bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) override;
293 
297  Q_DECL_DEPRECATED void setGridEnabled( bool enabled );
298 
302  Q_DECL_DEPRECATED bool gridEnabled() const;
303 
307  Q_DECL_DEPRECATED void setGridStyle( GridStyle style );
308 
312  Q_DECL_DEPRECATED GridStyle gridStyle() const;
313 
317  Q_DECL_DEPRECATED void setGridIntervalX( double interval );
318 
322  Q_DECL_DEPRECATED double gridIntervalX() const;
323 
327  Q_DECL_DEPRECATED void setGridIntervalY( double interval );
328 
332  Q_DECL_DEPRECATED double gridIntervalY() const;
333 
337  Q_DECL_DEPRECATED void setGridOffsetX( double offset );
338 
342  Q_DECL_DEPRECATED double gridOffsetX() const;
343 
347  Q_DECL_DEPRECATED void setGridOffsetY( double offset );
348 
352  Q_DECL_DEPRECATED double gridOffsetY() const;
353 
357  Q_DECL_DEPRECATED void setGridPen( const QPen& p );
358 
362  Q_DECL_DEPRECATED QPen gridPen() const;
363 
367  Q_DECL_DEPRECATED void setGridPenWidth( double w );
368 
372  Q_DECL_DEPRECATED void setGridPenColor( const QColor& c );
373 
377  Q_DECL_DEPRECATED void setGridAnnotationFont( const QFont& f );
378 
382  Q_DECL_DEPRECATED QFont gridAnnotationFont() const;
383 
387  Q_DECL_DEPRECATED void setAnnotationFontColor( const QColor& c );
388 
392  Q_DECL_DEPRECATED QColor annotationFontColor() const;
393 
397  Q_DECL_DEPRECATED void setGridAnnotationPrecision( int p );
398 
402  Q_DECL_DEPRECATED int gridAnnotationPrecision() const;
403 
407  Q_DECL_DEPRECATED void setShowGridAnnotation( bool show );
408 
412  Q_DECL_DEPRECATED bool showGridAnnotation() const;
413 
417  Q_DECL_DEPRECATED void setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border );
418 
422  Q_DECL_DEPRECATED GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const;
423 
427  Q_DECL_DEPRECATED void setAnnotationFrameDistance( double d );
428 
432  Q_DECL_DEPRECATED double annotationFrameDistance() const;
433 
437  Q_DECL_DEPRECATED void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
438 
442  Q_DECL_DEPRECATED GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;
443 
447  Q_DECL_DEPRECATED void setGridAnnotationFormat( GridAnnotationFormat f );
448 
452  Q_DECL_DEPRECATED GridAnnotationFormat gridAnnotationFormat() const;
453 
457  Q_DECL_DEPRECATED void setGridFrameStyle( GridFrameStyle style );
458 
462  Q_DECL_DEPRECATED GridFrameStyle gridFrameStyle() const;
463 
467  Q_DECL_DEPRECATED void setGridFrameWidth( double w );
468 
472  Q_DECL_DEPRECATED double gridFrameWidth() const;
473 
478  Q_DECL_DEPRECATED void setGridFramePenSize( double w );
479 
483  Q_DECL_DEPRECATED double gridFramePenSize() const;
484 
489  Q_DECL_DEPRECATED void setGridFramePenColor( const QColor& c );
490 
495  Q_DECL_DEPRECATED QColor gridFramePenColor() const;
496 
501  Q_DECL_DEPRECATED void setGridFrameFillColor1( const QColor& c );
502 
507  Q_DECL_DEPRECATED QColor gridFrameFillColor1() const;
508 
513  Q_DECL_DEPRECATED void setGridFrameFillColor2( const QColor& c );
514 
519  Q_DECL_DEPRECATED QColor gridFrameFillColor2() const;
520 
524  Q_DECL_DEPRECATED void setCrossLength( double l );
525 
529  Q_DECL_DEPRECATED double crossLength();
530 
534  Q_DECL_DEPRECATED void setGridLineSymbol( QgsLineSymbolV2* symbol );
535 
539  Q_DECL_DEPRECATED QgsLineSymbolV2* gridLineSymbol();
540 
544  Q_DECL_DEPRECATED QPainter::CompositionMode gridBlendMode() const;
545 
549  Q_DECL_DEPRECATED void setGridBlendMode( QPainter::CompositionMode blendMode );
550 
557  QgsComposerMapGridStack* grids() { return mGridStack; }
558 
564  QgsComposerMapGrid* grid();
565 
572  QgsComposerMapOverviewStack* overviews() { return mOverviewStack; }
573 
579  QgsComposerMapOverview* overview();
580 
582  QRectF boundingRect() const override;
583 
584  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
585  virtual void setFrameOutlineWidth( const double outlineWidth ) override;
586 
591  Q_DECL_DEPRECATED void setRotation( double r ) override;
592 
596  Q_DECL_DEPRECATED double rotation() const { return mMapRotation;}
597 
602  void setMapRotation( double r );
603 
610  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
611 
612  void updateItem() override;
613 
615  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
616 
617  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
618  bool drawCanvasItems() const { return mDrawCanvasItems; }
619 
621  double mapUnitsToMM() const;
622 
626  Q_DECL_DEPRECATED void setOverviewFrameMap( int mapId );
627 
631  Q_DECL_DEPRECATED int overviewFrameMapId() const;
632 
636  Q_DECL_DEPRECATED void setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol );
637 
641  Q_DECL_DEPRECATED QgsFillSymbolV2* overviewFrameMapSymbol();
642 
646  Q_DECL_DEPRECATED QPainter::CompositionMode overviewBlendMode() const;
647 
651  Q_DECL_DEPRECATED void setOverviewBlendMode( QPainter::CompositionMode blendMode );
652 
656  Q_DECL_DEPRECATED bool overviewInverted() const;
657 
661  Q_DECL_DEPRECATED void setOverviewInverted( bool inverted );
662 
666  Q_DECL_DEPRECATED bool overviewCentered() const;
667 
671  Q_DECL_DEPRECATED void setOverviewCentered( bool centered );
672 
675  void assignFreeId();
676 
681  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
686  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
691  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
692 
698  bool atlasDriven() const { return mAtlasDriven; }
699 
705  void setAtlasDriven( bool enabled );
706 
710  Q_DECL_DEPRECATED bool atlasFixedScale() const;
711 
715  Q_DECL_DEPRECATED void setAtlasFixedScale( bool fixed );
716 
725  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
726 
735  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
736 
746  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
747 
754  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
755 
757  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
758 
760  bool updatesEnabled() const { return mUpdatesEnabled; }
761 
767  int numberExportLayers() const override;
768 
775  QPolygonF visibleExtentPolygon() const;
776 
777  //overridden to show "Map 1" type names
778  virtual QString displayName() const override;
779 
781  QPolygonF transformedMapPolygon() const;
782 
784  QPointF mapToItemCoords( QPointF mapCoords ) const;
785 
787  Q_DECL_DEPRECATED void connectMapOverviewSignals();
788 
791  void requestedExtent( QgsRectangle& extent ) const;
792 
793  virtual QgsExpressionContext* createExpressionContext() const override;
794 
795  signals:
796  void extentChanged();
797 
799  void mapRotationChanged( double newRotation );
800 
802  void preparedForAtlas();
803 
808  void layerStyleOverridesChanged();
809 
810  public slots:
811 
813  void updateCachedImage();
814 
818  void renderModeUpdateCachedImage();
819 
821  void updateBoundingRect();
822 
825 
826  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
827 
828  protected slots:
829 
834  void layersChanged();
835 
836  private:
837 
839  int mId;
840 
841  QgsComposerMapGridStack* mGridStack;
842 
843  QgsComposerMapOverviewStack* mOverviewStack;
844 
845  // Map region in map units realy used for rendering
846  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
847  // so that full rectangle in paper is used.
848  QgsRectangle mExtent;
849 
850  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
851  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
852  // to manually tweak each atlas preview page without affecting the actual original map extent.
853  QgsRectangle mAtlasFeatureExtent;
854 
855  // Cache used in composer preview
856  QImage mCacheImage;
857 
858  // Is cache up to date
859  bool mCacheUpdated;
860 
862  PreviewMode mPreviewMode;
863 
865  int mNumCachedLayers;
866 
868  bool mDrawing;
869 
871  double mXOffset;
873  double mYOffset;
874 
876  double mMapRotation;
879  double mEvaluatedMapRotation;
880 
882  bool mKeepLayerSet;
883 
885  QStringList mLayerSet;
886 
887  bool mKeepLayerStyles;
889  QMap<QString, QString> mLayerStyleOverrides;
890 
892  bool mUpdatesEnabled;
893 
895  void connectUpdateSlot();
896 
898  void syncLayerSet();
899 
901  const QgsComposerMapGrid* constFirstMapGrid() const;
902 
904  const QgsComposerMapOverview* constFirstMapOverview() const;
905 
907  QRectF mCurrentRectangle;
908  QGraphicsView* mMapCanvas;
910  bool mDrawCanvasItems;
911 
914  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
915 
917  bool mAtlasDriven;
919  AtlasScalingMode mAtlasScalingMode;
921  double mAtlasMargin;
922 
923  void init();
924 
926  void updateToolTip();
927 
929  QStringList layersToRender( const QgsExpressionContext* context = nullptr ) const;
930 
932  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext& context ) const;
933 
935  QgsRectangle transformedExtent() const;
936 
938  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
939 
943  void transformShift( double& xShift, double& yShift ) const;
944 
945  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
946  void drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
947  QPointF composerMapPosForItem( const QGraphicsItem* item ) const;
948 
949  enum PartType
950  {
951  Background,
952  Layer,
953  Grid,
954  OverviewMapExtent,
955  Frame,
956  SelectionBoxes
957  };
958 
960  bool shouldDrawPart( PartType part ) const;
961 
966  void refreshMapExtents( const QgsExpressionContext* context = nullptr );
967 
968  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
969  friend class TestQgsComposerMap;
970 };
972 
973 #endif
974 
AtlasScalingMode
Scaling modes used for the serial rendering (atlas)
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...
A rectangle specified with double values.
Definition: qgsrectangle.h:35
virtual bool writeXML(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
bool updatesEnabled() const
Returns whether updates to the composer map are enabled.
Q_DECL_DEPRECATED void toggleAtlasPreview()
Called when atlas preview is toggled, to force map item to update its extent and redraw.
void overviewExtentChanged()
QgsComposerMapOverviewStack * overviews()
Returns the map item&#39;s overview stack, which is used to control how overviews are drawn over the map&#39;...
bool keepLayerStyles() const
Getter for flag that determines if current styles of layers should be overridden by previously stored...
virtual void setRotation(double r)
Sets the item rotation.
ZoomMode
Modes for zooming item content.
A collection of grids which is drawn above the map content in a QgsComposerMap.
QStringList layerSet() const
Getter for stored layer set that is used if mKeepLayerSet is true.
A item that forms part of a map composition.
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:474
Border
Enum for different frame borders.
void setMapCanvas(QGraphicsView *canvas)
Sets canvas pointer (necessary to query and draw map canvas items)
QMap< QString, QString > layerStyleOverrides() const
Getter for stored overrides of styles for layers.
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.
void setKeepLayerStyles(bool enabled)
Setter for flag that determines if current styles of layers should be overridden by previously stored...
bool isDrawing() const
True if a draw is already in progress.
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)
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.
bool drawCanvasItems() const
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...
virtual int numberExportLayers() const
Get the number of layers that this item requires for exporting as 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...
PreviewMode
Preview style.
Graphics scene for map printing.
Object representing map window.
void setUpdatesEnabled(bool enabled)
Sets whether updates to the composer map are enabled.
PreviewMode previewMode() const
virtual QString displayName() const
Get item display name.
virtual void setFrameOutlineWidth(const double outlineWidth)
Sets frame outline width.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:475
int id() const
Get identification number.
Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect(double &x, double &y, double width, double height, double rotation) const
Calculates corner point after rotation and scaling.
virtual Q_DECL_DEPRECATED void zoomContent(int delta, double x, double y)
Zoom content of item.
Q_DECL_DEPRECATED double rotation() const
Returns the rotation used for drawing the map within the composer item.
QgsRectangle extent() 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...
AtlasScalingMode atlasScalingMode() const
Returns the current atlas scaling mode.
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...
bool atlasDriven() const
Returns whether the map extent is set to follow the current atlas feature.
bool keepLayerSet() const
Getter 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.
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
A collection of overviews which are drawn above the map content in a QgsComposerMap.