QGIS API Documentation  2.15.0-Master (af20121)
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 
274  bool followVisibilityPreset() const { return mFollowVisibilityPreset; }
277  void setFollowVisibilityPreset( bool follow ) { mFollowVisibilityPreset = follow; }
281  QString followVisibilityPresetName() const { return mFollowVisibilityPresetName; }
284  void setFollowVisibilityPresetName( const QString& name ) { mFollowVisibilityPresetName = name; }
285 
286  // Set cache outdated
287  void setCacheUpdated( bool u = false );
288 
289  QgsRectangle extent() const {return mExtent;}
290 
292  Q_DECL_DEPRECATED const QgsMapRenderer* mapRenderer() const;
293 
295  void setOffset( double xOffset, double yOffset );
296 
298  bool containsWMSLayer() const;
299 
301  bool containsAdvancedEffects() const;
302 
307  bool writeXML( QDomElement& elem, QDomDocument & doc ) const override;
308 
313  bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) override;
314 
318  Q_DECL_DEPRECATED void setGridEnabled( bool enabled );
319 
323  Q_DECL_DEPRECATED bool gridEnabled() const;
324 
328  Q_DECL_DEPRECATED void setGridStyle( GridStyle style );
329 
333  Q_DECL_DEPRECATED GridStyle gridStyle() const;
334 
338  Q_DECL_DEPRECATED void setGridIntervalX( double interval );
339 
343  Q_DECL_DEPRECATED double gridIntervalX() const;
344 
348  Q_DECL_DEPRECATED void setGridIntervalY( double interval );
349 
353  Q_DECL_DEPRECATED double gridIntervalY() const;
354 
358  Q_DECL_DEPRECATED void setGridOffsetX( double offset );
359 
363  Q_DECL_DEPRECATED double gridOffsetX() const;
364 
368  Q_DECL_DEPRECATED void setGridOffsetY( double offset );
369 
373  Q_DECL_DEPRECATED double gridOffsetY() const;
374 
378  Q_DECL_DEPRECATED void setGridPen( const QPen& p );
379 
383  Q_DECL_DEPRECATED QPen gridPen() const;
384 
388  Q_DECL_DEPRECATED void setGridPenWidth( double w );
389 
393  Q_DECL_DEPRECATED void setGridPenColor( const QColor& c );
394 
398  Q_DECL_DEPRECATED void setGridAnnotationFont( const QFont& f );
399 
403  Q_DECL_DEPRECATED QFont gridAnnotationFont() const;
404 
408  Q_DECL_DEPRECATED void setAnnotationFontColor( const QColor& c );
409 
413  Q_DECL_DEPRECATED QColor annotationFontColor() const;
414 
418  Q_DECL_DEPRECATED void setGridAnnotationPrecision( int p );
419 
423  Q_DECL_DEPRECATED int gridAnnotationPrecision() const;
424 
428  Q_DECL_DEPRECATED void setShowGridAnnotation( bool show );
429 
433  Q_DECL_DEPRECATED bool showGridAnnotation() const;
434 
438  Q_DECL_DEPRECATED void setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border );
439 
443  Q_DECL_DEPRECATED GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const;
444 
448  Q_DECL_DEPRECATED void setAnnotationFrameDistance( double d );
449 
453  Q_DECL_DEPRECATED double annotationFrameDistance() const;
454 
458  Q_DECL_DEPRECATED void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
459 
463  Q_DECL_DEPRECATED GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;
464 
468  Q_DECL_DEPRECATED void setGridAnnotationFormat( GridAnnotationFormat f );
469 
473  Q_DECL_DEPRECATED GridAnnotationFormat gridAnnotationFormat() const;
474 
478  Q_DECL_DEPRECATED void setGridFrameStyle( GridFrameStyle style );
479 
483  Q_DECL_DEPRECATED GridFrameStyle gridFrameStyle() const;
484 
488  Q_DECL_DEPRECATED void setGridFrameWidth( double w );
489 
493  Q_DECL_DEPRECATED double gridFrameWidth() const;
494 
499  Q_DECL_DEPRECATED void setGridFramePenSize( double w );
500 
504  Q_DECL_DEPRECATED double gridFramePenSize() const;
505 
510  Q_DECL_DEPRECATED void setGridFramePenColor( const QColor& c );
511 
516  Q_DECL_DEPRECATED QColor gridFramePenColor() const;
517 
522  Q_DECL_DEPRECATED void setGridFrameFillColor1( const QColor& c );
523 
528  Q_DECL_DEPRECATED QColor gridFrameFillColor1() const;
529 
534  Q_DECL_DEPRECATED void setGridFrameFillColor2( const QColor& c );
535 
540  Q_DECL_DEPRECATED QColor gridFrameFillColor2() const;
541 
545  Q_DECL_DEPRECATED void setCrossLength( double l );
546 
550  Q_DECL_DEPRECATED double crossLength();
551 
555  Q_DECL_DEPRECATED void setGridLineSymbol( QgsLineSymbolV2* symbol );
556 
560  Q_DECL_DEPRECATED QgsLineSymbolV2* gridLineSymbol();
561 
565  Q_DECL_DEPRECATED QPainter::CompositionMode gridBlendMode() const;
566 
570  Q_DECL_DEPRECATED void setGridBlendMode( QPainter::CompositionMode blendMode );
571 
578  QgsComposerMapGridStack* grids() { return mGridStack; }
579 
585  QgsComposerMapGrid* grid();
586 
593  QgsComposerMapOverviewStack* overviews() { return mOverviewStack; }
594 
600  QgsComposerMapOverview* overview();
601 
603  QRectF boundingRect() const override;
604 
605  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
606  virtual void setFrameOutlineWidth( const double outlineWidth ) override;
607 
612  Q_DECL_DEPRECATED void setRotation( double r ) override;
613 
617  Q_DECL_DEPRECATED double rotation() const { return mMapRotation;}
618 
623  void setMapRotation( double r );
624 
631  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
632 
633  void updateItem() override;
634 
636  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
637 
638  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
639  bool drawCanvasItems() const { return mDrawCanvasItems; }
640 
642  double mapUnitsToMM() const;
643 
647  Q_DECL_DEPRECATED void setOverviewFrameMap( int mapId );
648 
652  Q_DECL_DEPRECATED int overviewFrameMapId() const;
653 
657  Q_DECL_DEPRECATED void setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol );
658 
662  Q_DECL_DEPRECATED QgsFillSymbolV2* overviewFrameMapSymbol();
663 
667  Q_DECL_DEPRECATED QPainter::CompositionMode overviewBlendMode() const;
668 
672  Q_DECL_DEPRECATED void setOverviewBlendMode( QPainter::CompositionMode blendMode );
673 
677  Q_DECL_DEPRECATED bool overviewInverted() const;
678 
682  Q_DECL_DEPRECATED void setOverviewInverted( bool inverted );
683 
687  Q_DECL_DEPRECATED bool overviewCentered() const;
688 
692  Q_DECL_DEPRECATED void setOverviewCentered( bool centered );
693 
696  void assignFreeId();
697 
702  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
707  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
712  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
713 
719  bool atlasDriven() const { return mAtlasDriven; }
720 
726  void setAtlasDriven( bool enabled );
727 
731  Q_DECL_DEPRECATED bool atlasFixedScale() const;
732 
736  Q_DECL_DEPRECATED void setAtlasFixedScale( bool fixed );
737 
746  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
747 
756  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
757 
767  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
768 
775  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
776 
778  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
779 
781  bool updatesEnabled() const { return mUpdatesEnabled; }
782 
788  int numberExportLayers() const override;
789 
796  QPolygonF visibleExtentPolygon() const;
797 
798  //overridden to show "Map 1" type names
799  virtual QString displayName() const override;
800 
802  QPolygonF transformedMapPolygon() const;
803 
805  QPointF mapToItemCoords( QPointF mapCoords ) const;
806 
808  Q_DECL_DEPRECATED void connectMapOverviewSignals();
809 
812  void requestedExtent( QgsRectangle& extent ) const;
813 
814  virtual QgsExpressionContext* createExpressionContext() const override;
815 
816  signals:
817  void extentChanged();
818 
820  void mapRotationChanged( double newRotation );
821 
823  void preparedForAtlas();
824 
829  void layerStyleOverridesChanged();
830 
831  public slots:
832 
834  void updateCachedImage();
835 
839  void renderModeUpdateCachedImage();
840 
842  void updateBoundingRect();
843 
846 
847  virtual void refreshDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property = QgsComposerObject::AllProperties, const QgsExpressionContext* context = nullptr ) override;
848 
849  protected slots:
850 
855  void layersChanged();
856 
857  private:
858 
860  int mId;
861 
862  QgsComposerMapGridStack* mGridStack;
863 
864  QgsComposerMapOverviewStack* mOverviewStack;
865 
866  // Map region in map units realy used for rendering
867  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
868  // so that full rectangle in paper is used.
869  QgsRectangle mExtent;
870 
871  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
872  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
873  // to manually tweak each atlas preview page without affecting the actual original map extent.
874  QgsRectangle mAtlasFeatureExtent;
875 
876  // Cache used in composer preview
877  QImage mCacheImage;
878 
879  // Is cache up to date
880  bool mCacheUpdated;
881 
883  PreviewMode mPreviewMode;
884 
886  int mNumCachedLayers;
887 
889  bool mDrawing;
890 
892  double mXOffset;
894  double mYOffset;
895 
897  double mMapRotation;
900  double mEvaluatedMapRotation;
901 
903  bool mKeepLayerSet;
904 
906  QStringList mLayerSet;
907 
908  bool mKeepLayerStyles;
910  QMap<QString, QString> mLayerStyleOverrides;
911 
915  bool mFollowVisibilityPreset;
918  QString mFollowVisibilityPresetName;
919 
921  bool mUpdatesEnabled;
922 
924  void connectUpdateSlot();
925 
927  void syncLayerSet();
928 
930  const QgsComposerMapGrid* constFirstMapGrid() const;
931 
933  const QgsComposerMapOverview* constFirstMapOverview() const;
934 
936  QRectF mCurrentRectangle;
937  QGraphicsView* mMapCanvas;
939  bool mDrawCanvasItems;
940 
943  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
944 
946  bool mAtlasDriven;
948  AtlasScalingMode mAtlasScalingMode;
950  double mAtlasMargin;
951 
952  void init();
953 
955  void updateToolTip();
956 
958  QStringList layersToRender( const QgsExpressionContext* context = nullptr ) const;
959 
961  QMap<QString, QString> layerStyleOverridesToRender( const QgsExpressionContext& context ) const;
962 
964  QgsRectangle transformedExtent() const;
965 
967  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
968 
972  void transformShift( double& xShift, double& yShift ) const;
973 
974  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
975  void drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
976  QPointF composerMapPosForItem( const QGraphicsItem* item ) const;
977 
978  enum PartType
979  {
980  Background,
981  Layer,
982  Grid,
983  OverviewMapExtent,
984  Frame,
985  SelectionBoxes
986  };
987 
989  bool shouldDrawPart( PartType part ) const;
990 
995  void refreshMapExtents( const QgsExpressionContext* context = nullptr );
996 
997  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
998  friend class TestQgsComposerMap;
999 };
1001 
1002 #endif
1003 
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:504
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)
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:505
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.