QGIS API Documentation  2.12.0-Lyon
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, const QSizeF& size, double dpi, double* forceWidthScale = 0 );
148 
150  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) override;
151 
153  void cache();
154 
157  QgsMapSettings mapSettings( const QgsRectangle& extent, const 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 
171  void moveContent( double dx, double dy ) override;
172 
179  Q_DECL_DEPRECATED void zoomContent( int delta, double x, double y ) override;
180 
187  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) override;
188 
190  void setSceneRect( const QRectF& rectangle ) override;
191 
193  double scale() const;
194 
196  void setNewScale( double scaleDenominator, bool forceUpdate = true );
197 
204  void setNewExtent( const QgsRectangle& extent );
205 
214  void zoomToExtent( const QgsRectangle& extent );
215 
219  void setNewAtlasFeatureExtent( const QgsRectangle& extent );
220 
224  Q_DECL_DEPRECATED void toggleAtlasPreview() {}
225 
232  QgsRectangle* currentMapExtent();
233  const QgsRectangle* currentMapExtent() const;
234 
235  PreviewMode previewMode() const {return mPreviewMode;}
236  void setPreviewMode( PreviewMode m );
237 
239  bool keepLayerSet() const {return mKeepLayerSet;}
241  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
242 
244  QStringList layerSet() const {return mLayerSet;}
246  void setLayerSet( const QStringList& layerSet ) {mLayerSet = layerSet;}
248  void storeCurrentLayerSet();
249 
251  bool keepLayerStyles() const { return mKeepLayerStyles; }
253  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
254 
256  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
258  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
260  void storeCurrentLayerStyles();
261 
262  // Set cache outdated
263  void setCacheUpdated( bool u = false );
264 
265  QgsRectangle extent() const {return mExtent;}
266 
268  Q_DECL_DEPRECATED const QgsMapRenderer* mapRenderer() const;
269 
271  void setOffset( double xOffset, double yOffset );
272 
274  bool containsWMSLayer() const;
275 
277  bool containsAdvancedEffects() const;
278 
283  bool writeXML( QDomElement& elem, QDomDocument & doc ) const override;
284 
289  bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) override;
290 
294  Q_DECL_DEPRECATED void setGridEnabled( bool enabled );
295 
299  Q_DECL_DEPRECATED bool gridEnabled() const;
300 
304  Q_DECL_DEPRECATED void setGridStyle( GridStyle style );
305 
309  Q_DECL_DEPRECATED GridStyle gridStyle() const;
310 
314  Q_DECL_DEPRECATED void setGridIntervalX( double interval );
315 
319  Q_DECL_DEPRECATED double gridIntervalX() const;
320 
324  Q_DECL_DEPRECATED void setGridIntervalY( double interval );
325 
329  Q_DECL_DEPRECATED double gridIntervalY() const;
330 
334  Q_DECL_DEPRECATED void setGridOffsetX( double offset );
335 
339  Q_DECL_DEPRECATED double gridOffsetX() const;
340 
344  Q_DECL_DEPRECATED void setGridOffsetY( double offset );
345 
349  Q_DECL_DEPRECATED double gridOffsetY() const;
350 
354  Q_DECL_DEPRECATED void setGridPen( const QPen& p );
355 
359  Q_DECL_DEPRECATED QPen gridPen() const;
360 
364  Q_DECL_DEPRECATED void setGridPenWidth( double w );
365 
369  Q_DECL_DEPRECATED void setGridPenColor( const QColor& c );
370 
374  Q_DECL_DEPRECATED void setGridAnnotationFont( const QFont& f );
375 
379  Q_DECL_DEPRECATED QFont gridAnnotationFont() const;
380 
384  Q_DECL_DEPRECATED void setAnnotationFontColor( const QColor& c );
385 
389  Q_DECL_DEPRECATED QColor annotationFontColor() const;
390 
394  Q_DECL_DEPRECATED void setGridAnnotationPrecision( int p );
395 
399  Q_DECL_DEPRECATED int gridAnnotationPrecision() const;
400 
404  Q_DECL_DEPRECATED void setShowGridAnnotation( bool show );
405 
409  Q_DECL_DEPRECATED bool showGridAnnotation() const;
410 
414  Q_DECL_DEPRECATED void setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border );
415 
419  Q_DECL_DEPRECATED GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const;
420 
424  Q_DECL_DEPRECATED void setAnnotationFrameDistance( double d );
425 
429  Q_DECL_DEPRECATED double annotationFrameDistance() const;
430 
434  Q_DECL_DEPRECATED void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
435 
439  Q_DECL_DEPRECATED GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;
440 
444  Q_DECL_DEPRECATED void setGridAnnotationFormat( GridAnnotationFormat f );
445 
449  Q_DECL_DEPRECATED GridAnnotationFormat gridAnnotationFormat() const;
450 
454  Q_DECL_DEPRECATED void setGridFrameStyle( GridFrameStyle style );
455 
459  Q_DECL_DEPRECATED GridFrameStyle gridFrameStyle() const;
460 
464  Q_DECL_DEPRECATED void setGridFrameWidth( double w );
465 
469  Q_DECL_DEPRECATED double gridFrameWidth() const;
470 
475  Q_DECL_DEPRECATED void setGridFramePenSize( double w );
476 
480  Q_DECL_DEPRECATED double gridFramePenSize() const;
481 
486  Q_DECL_DEPRECATED void setGridFramePenColor( const QColor& c );
487 
492  Q_DECL_DEPRECATED QColor gridFramePenColor() const;
493 
498  Q_DECL_DEPRECATED void setGridFrameFillColor1( const QColor& c );
499 
504  Q_DECL_DEPRECATED QColor gridFrameFillColor1() const;
505 
510  Q_DECL_DEPRECATED void setGridFrameFillColor2( const QColor& c );
511 
516  Q_DECL_DEPRECATED QColor gridFrameFillColor2() const;
517 
521  Q_DECL_DEPRECATED void setCrossLength( double l );
522 
526  Q_DECL_DEPRECATED double crossLength();
527 
531  Q_DECL_DEPRECATED void setGridLineSymbol( QgsLineSymbolV2* symbol );
532 
536  Q_DECL_DEPRECATED QgsLineSymbolV2* gridLineSymbol();
537 
541  Q_DECL_DEPRECATED QPainter::CompositionMode gridBlendMode() const;
542 
546  Q_DECL_DEPRECATED void setGridBlendMode( QPainter::CompositionMode blendMode );
547 
554  QgsComposerMapGridStack* grids() { return mGridStack; }
555 
561  QgsComposerMapGrid* grid();
562 
569  QgsComposerMapOverviewStack* overviews() { return mOverviewStack; }
570 
576  QgsComposerMapOverview* overview();
577 
579  QRectF boundingRect() const override;
580 
581  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
582  virtual void setFrameOutlineWidth( const double outlineWidth ) override;
583 
588  Q_DECL_DEPRECATED void setRotation( double r ) override;
589 
593  Q_DECL_DEPRECATED double rotation() const { return mMapRotation;}
594 
598  void setMapRotation( double r );
599 
606  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
607 
608  void updateItem() override;
609 
611  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
612 
613  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
614  bool drawCanvasItems() const { return mDrawCanvasItems; }
615 
617  double mapUnitsToMM() const;
618 
622  Q_DECL_DEPRECATED void setOverviewFrameMap( int mapId );
623 
627  Q_DECL_DEPRECATED int overviewFrameMapId() const;
628 
632  Q_DECL_DEPRECATED void setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol );
633 
637  Q_DECL_DEPRECATED QgsFillSymbolV2* overviewFrameMapSymbol();
638 
642  Q_DECL_DEPRECATED QPainter::CompositionMode overviewBlendMode() const;
643 
647  Q_DECL_DEPRECATED void setOverviewBlendMode( QPainter::CompositionMode blendMode );
648 
652  Q_DECL_DEPRECATED bool overviewInverted() const;
653 
657  Q_DECL_DEPRECATED void setOverviewInverted( bool inverted );
658 
662  Q_DECL_DEPRECATED bool overviewCentered() const;
663 
667  Q_DECL_DEPRECATED void setOverviewCentered( bool centered );
668 
671  void assignFreeId();
672 
677  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
682  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
687  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
688 
694  bool atlasDriven() const { return mAtlasDriven; }
695 
701  void setAtlasDriven( bool enabled );
702 
706  Q_DECL_DEPRECATED bool atlasFixedScale() const;
707 
711  Q_DECL_DEPRECATED void setAtlasFixedScale( bool fixed );
712 
721  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
722 
731  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
732 
742  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
743 
750  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
751 
753  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
754 
756  bool updatesEnabled() const { return mUpdatesEnabled; }
757 
763  int numberExportLayers() const override;
764 
771  QPolygonF visibleExtentPolygon() const;
772 
773  //overriden to show "Map 1" type names
774  virtual QString displayName() const override;
775 
777  QPolygonF transformedMapPolygon() const;
778 
780  QPointF mapToItemCoords( const QPointF& mapCoords ) const;
781 
782  Q_DECL_DEPRECATED void connectMapOverviewSignals();
783 
786  void requestedExtent( QgsRectangle& extent ) const;
787 
788  virtual QgsExpressionContext* createExpressionContext() const override;
789 
790  signals:
791  void extentChanged();
792 
794  void mapRotationChanged( double newRotation );
795 
797  void preparedForAtlas();
798 
803  void layerStyleOverridesChanged();
804 
805  public slots:
806 
808  void updateCachedImage();
809 
813  void renderModeUpdateCachedImage();
814 
816  void updateBoundingRect();
817 
820 
822 
823  protected slots:
824 
829  void layersChanged();
830 
831  private:
832 
834  int mId;
835 
836  QgsComposerMapGridStack* mGridStack;
837 
838  QgsComposerMapOverviewStack* mOverviewStack;
839 
840  // Map region in map units realy used for rendering
841  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
842  // so that full rectangle in paper is used.
843  QgsRectangle mExtent;
844 
845  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
846  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
847  // to manually tweak each atlas preview page without affecting the actual original map extent.
848  QgsRectangle mAtlasFeatureExtent;
849 
850  // Cache used in composer preview
851  QImage mCacheImage;
852 
853  // Is cache up to date
854  bool mCacheUpdated;
855 
857  PreviewMode mPreviewMode;
858 
860  int mNumCachedLayers;
861 
863  bool mDrawing;
864 
866  double mXOffset;
868  double mYOffset;
869 
871  double mMapRotation;
874  double mEvaluatedMapRotation;
875 
877  bool mKeepLayerSet;
878 
880  QStringList mLayerSet;
881 
882  bool mKeepLayerStyles;
884  QMap<QString, QString> mLayerStyleOverrides;
885 
887  bool mUpdatesEnabled;
888 
890  void connectUpdateSlot();
891 
893  void syncLayerSet();
894 
896  const QgsComposerMapGrid* constFirstMapGrid() const;
897 
899  const QgsComposerMapOverview* constFirstMapOverview() const;
900 
902  QRectF mCurrentRectangle;
903  QGraphicsView* mMapCanvas;
905  bool mDrawCanvasItems;
906 
909  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
910 
912  bool mAtlasDriven;
914  AtlasScalingMode mAtlasScalingMode;
916  double mAtlasMargin;
917 
918  void init();
919 
921  void updateToolTip();
922 
924  QStringList layersToRender( const QgsExpressionContext* context = 0 ) const;
925 
927  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext& context ) const;
928 
930  QgsRectangle transformedExtent() const;
931 
933  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
934 
938  void transformShift( double& xShift, double& yShift ) const;
939 
940  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
941  void drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
942  QPointF composerMapPosForItem( const QGraphicsItem* item ) const;
943 
944  enum PartType
945  {
946  Background,
947  Layer,
948  Grid,
949  OverviewMapExtent,
950  Frame,
951  SelectionBoxes
952  };
953 
955  bool shouldDrawPart( PartType part ) const;
956 
961  void refreshMapExtents( const QgsExpressionContext* context = 0 );
962 
963  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
964  friend class TestQgsComposerMap;
965 };
967 
968 #endif
969 
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.
virtual void refreshDataDefinedProperty(const QgsComposerObject::DataDefinedProperty property=QgsComposerObject::AllProperties, const QgsExpressionContext *context=0) override
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
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's overview stack, which is used to control how overviews are drawn over the map'...
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.
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:390
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.
DataDefinedProperty
Data defined properties for different item types.
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 objects's current state.
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...
PropertyValueType
Specifies whether the value returned by a function should be the original, user set value...
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.
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:391
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's grid stack, which is used to control how grids are drawn over the map'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.