QGIS API Documentation  2.14.11-Essen
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 : blazek@itc.it
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 
266  // Set cache outdated
267  void setCacheUpdated( bool u = false );
268 
269  QgsRectangle extent() const {return mExtent;}
270 
272  Q_DECL_DEPRECATED const QgsMapRenderer* mapRenderer() const;
273 
275  void setOffset( double xOffset, double yOffset );
276 
278  bool containsWMSLayer() const;
279 
281  bool containsAdvancedEffects() const;
282 
287  bool writeXML( QDomElement& elem, QDomDocument & doc ) const override;
288 
293  bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) override;
294 
298  Q_DECL_DEPRECATED void setGridEnabled( bool enabled );
299 
303  Q_DECL_DEPRECATED bool gridEnabled() const;
304 
308  Q_DECL_DEPRECATED void setGridStyle( GridStyle style );
309 
313  Q_DECL_DEPRECATED GridStyle gridStyle() const;
314 
318  Q_DECL_DEPRECATED void setGridIntervalX( double interval );
319 
323  Q_DECL_DEPRECATED double gridIntervalX() const;
324 
328  Q_DECL_DEPRECATED void setGridIntervalY( double interval );
329 
333  Q_DECL_DEPRECATED double gridIntervalY() const;
334 
338  Q_DECL_DEPRECATED void setGridOffsetX( double offset );
339 
343  Q_DECL_DEPRECATED double gridOffsetX() const;
344 
348  Q_DECL_DEPRECATED void setGridOffsetY( double offset );
349 
353  Q_DECL_DEPRECATED double gridOffsetY() const;
354 
358  Q_DECL_DEPRECATED void setGridPen( const QPen& p );
359 
363  Q_DECL_DEPRECATED QPen gridPen() const;
364 
368  Q_DECL_DEPRECATED void setGridPenWidth( double w );
369 
373  Q_DECL_DEPRECATED void setGridPenColor( const QColor& c );
374 
378  Q_DECL_DEPRECATED void setGridAnnotationFont( const QFont& f );
379 
383  Q_DECL_DEPRECATED QFont gridAnnotationFont() const;
384 
388  Q_DECL_DEPRECATED void setAnnotationFontColor( const QColor& c );
389 
393  Q_DECL_DEPRECATED QColor annotationFontColor() const;
394 
398  Q_DECL_DEPRECATED void setGridAnnotationPrecision( int p );
399 
403  Q_DECL_DEPRECATED int gridAnnotationPrecision() const;
404 
408  Q_DECL_DEPRECATED void setShowGridAnnotation( bool show );
409 
413  Q_DECL_DEPRECATED bool showGridAnnotation() const;
414 
418  Q_DECL_DEPRECATED void setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border );
419 
423  Q_DECL_DEPRECATED GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const;
424 
428  Q_DECL_DEPRECATED void setAnnotationFrameDistance( double d );
429 
433  Q_DECL_DEPRECATED double annotationFrameDistance() const;
434 
438  Q_DECL_DEPRECATED void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
439 
443  Q_DECL_DEPRECATED GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;
444 
448  Q_DECL_DEPRECATED void setGridAnnotationFormat( GridAnnotationFormat f );
449 
453  Q_DECL_DEPRECATED GridAnnotationFormat gridAnnotationFormat() const;
454 
458  Q_DECL_DEPRECATED void setGridFrameStyle( GridFrameStyle style );
459 
463  Q_DECL_DEPRECATED GridFrameStyle gridFrameStyle() const;
464 
468  Q_DECL_DEPRECATED void setGridFrameWidth( double w );
469 
473  Q_DECL_DEPRECATED double gridFrameWidth() const;
474 
479  Q_DECL_DEPRECATED void setGridFramePenSize( double w );
480 
484  Q_DECL_DEPRECATED double gridFramePenSize() const;
485 
490  Q_DECL_DEPRECATED void setGridFramePenColor( const QColor& c );
491 
496  Q_DECL_DEPRECATED QColor gridFramePenColor() const;
497 
502  Q_DECL_DEPRECATED void setGridFrameFillColor1( const QColor& c );
503 
508  Q_DECL_DEPRECATED QColor gridFrameFillColor1() const;
509 
514  Q_DECL_DEPRECATED void setGridFrameFillColor2( const QColor& c );
515 
520  Q_DECL_DEPRECATED QColor gridFrameFillColor2() const;
521 
525  Q_DECL_DEPRECATED void setCrossLength( double l );
526 
530  Q_DECL_DEPRECATED double crossLength();
531 
535  Q_DECL_DEPRECATED void setGridLineSymbol( QgsLineSymbolV2* symbol );
536 
540  Q_DECL_DEPRECATED QgsLineSymbolV2* gridLineSymbol();
541 
545  Q_DECL_DEPRECATED QPainter::CompositionMode gridBlendMode() const;
546 
550  Q_DECL_DEPRECATED void setGridBlendMode( QPainter::CompositionMode blendMode );
551 
558  QgsComposerMapGridStack* grids() { return mGridStack; }
559 
565  QgsComposerMapGrid* grid();
566 
573  QgsComposerMapOverviewStack* overviews() { return mOverviewStack; }
574 
580  QgsComposerMapOverview* overview();
581 
583  QRectF boundingRect() const override;
584 
585  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
586  virtual void setFrameOutlineWidth( const double outlineWidth ) override;
587 
592  Q_DECL_DEPRECATED void setRotation( double r ) override;
593 
597  Q_DECL_DEPRECATED double rotation() const { return mMapRotation;}
598 
603  void setMapRotation( double r );
604 
611  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
612 
613  void updateItem() override;
614 
616  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
617 
618  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
619  bool drawCanvasItems() const { return mDrawCanvasItems; }
620 
622  double mapUnitsToMM() const;
623 
627  Q_DECL_DEPRECATED void setOverviewFrameMap( int mapId );
628 
632  Q_DECL_DEPRECATED int overviewFrameMapId() const;
633 
637  Q_DECL_DEPRECATED void setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol );
638 
642  Q_DECL_DEPRECATED QgsFillSymbolV2* overviewFrameMapSymbol();
643 
647  Q_DECL_DEPRECATED QPainter::CompositionMode overviewBlendMode() const;
648 
652  Q_DECL_DEPRECATED void setOverviewBlendMode( QPainter::CompositionMode blendMode );
653 
657  Q_DECL_DEPRECATED bool overviewInverted() const;
658 
662  Q_DECL_DEPRECATED void setOverviewInverted( bool inverted );
663 
667  Q_DECL_DEPRECATED bool overviewCentered() const;
668 
672  Q_DECL_DEPRECATED void setOverviewCentered( bool centered );
673 
676  void assignFreeId();
677 
682  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
687  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
692  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
693 
699  bool atlasDriven() const { return mAtlasDriven; }
700 
706  void setAtlasDriven( bool enabled );
707 
711  Q_DECL_DEPRECATED bool atlasFixedScale() const;
712 
716  Q_DECL_DEPRECATED void setAtlasFixedScale( bool fixed );
717 
726  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
727 
736  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
737 
747  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
748 
755  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
756 
758  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
759 
761  bool updatesEnabled() const { return mUpdatesEnabled; }
762 
768  int numberExportLayers() const override;
769 
776  QPolygonF visibleExtentPolygon() const;
777 
778  //overridden to show "Map 1" type names
779  virtual QString displayName() const override;
780 
782  QPolygonF transformedMapPolygon() const;
783 
785  QPointF mapToItemCoords( QPointF mapCoords ) const;
786 
788  Q_DECL_DEPRECATED void connectMapOverviewSignals();
789 
792  void requestedExtent( QgsRectangle& extent ) const;
793 
794  virtual QgsExpressionContext* createExpressionContext() const override;
795 
796  signals:
797  void extentChanged();
798 
800  void mapRotationChanged( double newRotation );
801 
803  void preparedForAtlas();
804 
809  void layerStyleOverridesChanged();
810 
811  public slots:
812 
814  void updateCachedImage();
815 
819  void renderModeUpdateCachedImage();
820 
822  void updateBoundingRect();
823 
826 
827  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
828 
829  protected slots:
830 
835  void layersChanged();
836 
837  private:
838 
840  int mId;
841 
842  QgsComposerMapGridStack* mGridStack;
843 
844  QgsComposerMapOverviewStack* mOverviewStack;
845 
846  // Map region in map units realy used for rendering
847  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
848  // so that full rectangle in paper is used.
849  QgsRectangle mExtent;
850 
851  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
852  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
853  // to manually tweak each atlas preview page without affecting the actual original map extent.
854  QgsRectangle mAtlasFeatureExtent;
855 
856  // Cache used in composer preview
857  QImage mCacheImage;
858 
859  // Is cache up to date
860  bool mCacheUpdated;
861 
863  PreviewMode mPreviewMode;
864 
866  int mNumCachedLayers;
867 
869  bool mDrawing;
870 
872  double mXOffset;
874  double mYOffset;
875 
877  double mMapRotation;
880  double mEvaluatedMapRotation;
881 
883  bool mKeepLayerSet;
884 
886  QStringList mLayerSet;
887 
888  bool mKeepLayerStyles;
890  QMap<QString, QString> mLayerStyleOverrides;
891 
893  bool mUpdatesEnabled;
894 
896  void connectUpdateSlot();
897 
899  void syncLayerSet();
900 
902  const QgsComposerMapGrid* constFirstMapGrid() const;
903 
905  const QgsComposerMapOverview* constFirstMapOverview() const;
906 
908  QRectF mCurrentRectangle;
909  QGraphicsView* mMapCanvas;
911  bool mDrawCanvasItems;
912 
915  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
916 
918  bool mAtlasDriven;
920  AtlasScalingMode mAtlasScalingMode;
922  double mAtlasMargin;
923 
924  void init();
925 
927  void updateToolTip();
928 
930  QStringList layersToRender( const QgsExpressionContext* context = nullptr ) const;
931 
933  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext& context ) const;
934 
936  QgsRectangle transformedExtent() const;
937 
939  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
940 
944  void transformShift( double& xShift, double& yShift ) const;
945 
946  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
947  void drawCanvasItem( const QgsAnnotation* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
948  QPointF composerMapPosForItem( const QgsAnnotation* item ) const;
949 
950  enum PartType
951  {
952  Background,
953  Layer,
954  Grid,
955  OverviewMapExtent,
956  Frame,
957  SelectionBoxes
958  };
959 
961  bool shouldDrawPart( PartType part ) const;
962 
967  void refreshMapExtents( const QgsExpressionContext* context = nullptr );
968 
969  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
970  friend class TestQgsComposerMap;
971 };
973 
974 #endif
975 
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.
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:415
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)
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.
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...
bool isDrawing() const
True if a draw is already in progress.
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.
virtual void setFrameOutlineWidth(const double outlineWidth)
Sets frame outline width.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:416
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 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
A collection of overviews which are drawn above the map content in a QgsComposerMap.