QGIS API Documentation  2.17.0-Master (bf77d09)
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 
643  double mapUnitsToMM() const;
644 
648  Q_DECL_DEPRECATED void setOverviewFrameMap( int mapId );
649 
653  Q_DECL_DEPRECATED int overviewFrameMapId() const;
654 
658  Q_DECL_DEPRECATED void setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol );
659 
663  Q_DECL_DEPRECATED QgsFillSymbolV2* overviewFrameMapSymbol();
664 
668  Q_DECL_DEPRECATED QPainter::CompositionMode overviewBlendMode() const;
669 
673  Q_DECL_DEPRECATED void setOverviewBlendMode( QPainter::CompositionMode blendMode );
674 
678  Q_DECL_DEPRECATED bool overviewInverted() const;
679 
683  Q_DECL_DEPRECATED void setOverviewInverted( bool inverted );
684 
688  Q_DECL_DEPRECATED bool overviewCentered() const;
689 
693  Q_DECL_DEPRECATED void setOverviewCentered( bool centered );
694 
697  void assignFreeId();
698 
703  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
708  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
713  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
714 
720  bool atlasDriven() const { return mAtlasDriven; }
721 
727  void setAtlasDriven( bool enabled );
728 
732  Q_DECL_DEPRECATED bool atlasFixedScale() const;
733 
737  Q_DECL_DEPRECATED void setAtlasFixedScale( bool fixed );
738 
747  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
748 
757  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
758 
768  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
769 
776  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
777 
779  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
780 
782  bool updatesEnabled() const { return mUpdatesEnabled; }
783 
789  int numberExportLayers() const override;
790 
797  QPolygonF visibleExtentPolygon() const;
798 
799  //overridden to show "Map 1" type names
800  virtual QString displayName() const override;
801 
803  QPolygonF transformedMapPolygon() const;
804 
806  QPointF mapToItemCoords( QPointF mapCoords ) const;
807 
809  Q_DECL_DEPRECATED void connectMapOverviewSignals();
810 
813  void requestedExtent( QgsRectangle& extent ) const;
814 
815  virtual QgsExpressionContext* createExpressionContext() const override;
816 
817  signals:
818  void extentChanged();
819 
821  void mapRotationChanged( double newRotation );
822 
824  void preparedForAtlas();
825 
830  void layerStyleOverridesChanged();
831 
832  public slots:
833 
835  void updateCachedImage();
836 
840  void renderModeUpdateCachedImage();
841 
843  void updateBoundingRect();
844 
847 
848  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
849 
850  protected slots:
851 
856  void layersChanged();
857 
858  private:
859 
861  int mId;
862 
863  QgsComposerMapGridStack* mGridStack;
864 
865  QgsComposerMapOverviewStack* mOverviewStack;
866 
867  // Map region in map units realy used for rendering
868  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
869  // so that full rectangle in paper is used.
870  QgsRectangle mExtent;
871 
872  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
873  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
874  // to manually tweak each atlas preview page without affecting the actual original map extent.
875  QgsRectangle mAtlasFeatureExtent;
876 
877  // Cache used in composer preview
878  QImage mCacheImage;
879 
880  // Is cache up to date
881  bool mCacheUpdated;
882 
884  PreviewMode mPreviewMode;
885 
887  int mNumCachedLayers;
888 
890  bool mDrawing;
891 
893  double mXOffset;
895  double mYOffset;
896 
898  double mMapRotation;
901  double mEvaluatedMapRotation;
902 
904  bool mKeepLayerSet;
905 
907  QStringList mLayerSet;
908 
909  bool mKeepLayerStyles;
911  QMap<QString, QString> mLayerStyleOverrides;
912 
916  bool mFollowVisibilityPreset;
919  QString mFollowVisibilityPresetName;
920 
922  bool mUpdatesEnabled;
923 
925  void connectUpdateSlot();
926 
928  void syncLayerSet();
929 
931  const QgsComposerMapGrid* constFirstMapGrid() const;
932 
934  const QgsComposerMapOverview* constFirstMapOverview() const;
935 
937  QRectF mCurrentRectangle;
938  QGraphicsView* mMapCanvas;
940  bool mDrawCanvasItems;
941 
944  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
945 
947  bool mAtlasDriven;
949  AtlasScalingMode mAtlasScalingMode;
951  double mAtlasMargin;
952 
953  void init();
954 
956  void updateToolTip();
957 
959  QStringList layersToRender( const QgsExpressionContext* context = nullptr ) const;
960 
962  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext& context ) const;
963 
965  QgsRectangle transformedExtent() const;
966 
968  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
969 
973  void transformShift( double& xShift, double& yShift ) const;
974 
975  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
976  void drawCanvasItem( const QgsAnnotation* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
977  QPointF composerMapPosForItem( const QgsAnnotation* item ) const;
978 
979  enum PartType
980  {
981  Background,
982  Layer,
983  Grid,
984  OverviewMapExtent,
985  Frame,
986  SelectionBoxes
987  };
988 
990  bool shouldDrawPart( PartType part ) const;
991 
996  void refreshMapExtents( const QgsExpressionContext* context = nullptr );
997 
998  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
999  friend class TestQgsComposerMap;
1000 };
1002 
1003 #endif
1004 
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:515
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.
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...
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)
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.
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.
QString followVisibilityPresetName() const
Preset name that decides which layers and layer styles are used for map rendering.
PropertyValueType
Specifies whether the value returned by a function should be the original, user set value...
PreviewMode
Preview style.
Graphics scene for map printing.
bool followVisibilityPreset() const
Whether the map should follow a visibility preset.
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:516
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
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.