QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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;
29 class QgsComposerMapGrid;
30 class QgsMapRenderer;
31 class QgsMapToPixel;
32 class QDomNode;
33 class QDomDocument;
34 class QGraphicsView;
35 class QPainter;
36 class QgsFillSymbolV2;
37 class QgsLineSymbolV2;
38 class QgsVectorLayer;
39 
45 class CORE_EXPORT QgsComposerMap : public QgsComposerItem
46 {
47  Q_OBJECT
48 
49  public:
51  QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height );
53  QgsComposerMap( QgsComposition *composition );
54  virtual ~QgsComposerMap();
55 
57  virtual int type() const { return ComposerMap; }
58 
61  {
62  Cache = 0, // Use raster cache
63  Render, // Render the map
64  Rectangle // Display only rectangle
65  };
66 
67  //grid enums are moved to QgsComposerMapGrid
68  //TODO - remove for QGIS 3.0
69  enum GridStyle
70  {
71  Solid = 0, //solid lines
72  Cross, //only draw line crossings
74  FrameAnnotationsOnly
75  };
76 
78  {
79  InsideMapFrame = 0,
81  Disabled
82  };
83 
85  {
86  Horizontal = 0,
89  BoundaryDirection
90  };
91 
93  {
94  Decimal = 0,
101  DegreeMinuteSecondPadded
102  };
103 
105  {
106  NoGridFrame = 0,
107  Zebra, // black/white pattern
111  LineBorder
112  };
113 
115  enum Border
116  {
120  Top
121  };
122 
126  {
127  Fixed, /*< The current scale of the map is used for each feature of the atlas */
128  Predefined, /*< A scale is chosen from the predefined scales. The smallest scale from
129  the list of scales where the atlas feature is fully visible is chosen.
130  @see QgsAtlasComposition::setPredefinedScales.
131  @note This mode is only valid for polygon or line atlas coverage layers
132  */
133  Auto /*< The extent is adjusted so that each feature is fully visible.
134  A margin is applied around the center @see setAtlasMargin
135  @note This mode is only valid for polygon or line atlas coverage layers*/
136  };
137 
145  void draw( QPainter *painter, const QgsRectangle& extent, const QSizeF& size, double dpi, double* forceWidthScale = 0 );
146 
148  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );
149 
151  void cache();
152 
154  int id() const {return mId;}
155 
157  bool isDrawing() const {return mDrawing;}
158 
160  void resize( double dx, double dy );
161 
165  void moveContent( double dx, double dy );
166 
171  void zoomContent( int delta, double x, double y );
172 
174  void setSceneRect( const QRectF& rectangle );
175 
177  double scale() const;
178 
180  void setNewScale( double scaleDenominator, bool forceUpdate = true );
181 
188  void setNewExtent( const QgsRectangle& extent );
189 
198  void zoomToExtent( const QgsRectangle& extent );
199 
203  void setNewAtlasFeatureExtent( const QgsRectangle& extent );
204 
206  void toggleAtlasPreview();
207 
214  QgsRectangle* currentMapExtent();
215  const QgsRectangle* currentMapExtent() const;
216 
217  PreviewMode previewMode() const {return mPreviewMode;}
218  void setPreviewMode( PreviewMode m );
219 
222  bool keepLayerSet() const {return mKeepLayerSet;}
225  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
226 
229  QStringList layerSet() const {return mLayerSet;}
232  void setLayerSet( const QStringList& layerSet ) {mLayerSet = layerSet;}
234  void storeCurrentLayerSet();
235 
236  // Set cache outdated
237  void setCacheUpdated( bool u = false );
238 
239  QgsRectangle extent() const {return mExtent;}
240 
242  Q_DECL_DEPRECATED const QgsMapRenderer* mapRenderer() const;
243 
245  void setOffset( double xOffset, double yOffset );
246 
248  bool containsWMSLayer() const;
249 
251  bool containsAdvancedEffects() const;
252 
257  bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
258 
263  bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
264 
269  Q_DECL_DEPRECATED void setGridEnabled( bool enabled );
270 
274  Q_DECL_DEPRECATED bool gridEnabled() const;
275 
280  Q_DECL_DEPRECATED void setGridStyle( GridStyle style );
281 
285  Q_DECL_DEPRECATED GridStyle gridStyle() const;
286 
291  Q_DECL_DEPRECATED void setGridIntervalX( double interval );
292 
296  Q_DECL_DEPRECATED double gridIntervalX() const;
297 
302  Q_DECL_DEPRECATED void setGridIntervalY( double interval );
303 
307  Q_DECL_DEPRECATED double gridIntervalY() const;
308 
313  Q_DECL_DEPRECATED void setGridOffsetX( double offset );
314 
318  Q_DECL_DEPRECATED double gridOffsetX() const;
319 
324  Q_DECL_DEPRECATED void setGridOffsetY( double offset );
325 
329  Q_DECL_DEPRECATED double gridOffsetY() const;
330 
335  Q_DECL_DEPRECATED void setGridPen( const QPen& p );
336 
340  Q_DECL_DEPRECATED QPen gridPen() const;
341 
346  Q_DECL_DEPRECATED void setGridPenWidth( double w );
347 
352  Q_DECL_DEPRECATED void setGridPenColor( const QColor& c );
353 
358  Q_DECL_DEPRECATED void setGridAnnotationFont( const QFont& f );
359 
363  Q_DECL_DEPRECATED QFont gridAnnotationFont() const;
364 
369  Q_DECL_DEPRECATED void setAnnotationFontColor( const QColor& c );
370 
375  Q_DECL_DEPRECATED QColor annotationFontColor() const;
376 
381  Q_DECL_DEPRECATED void setGridAnnotationPrecision( int p );
382 
386  Q_DECL_DEPRECATED int gridAnnotationPrecision() const;
387 
392  Q_DECL_DEPRECATED void setShowGridAnnotation( bool show );
393 
397  Q_DECL_DEPRECATED bool showGridAnnotation() const;
398 
402  Q_DECL_DEPRECATED void setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border );
403 
407  Q_DECL_DEPRECATED GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const;
408 
413  Q_DECL_DEPRECATED void setAnnotationFrameDistance( double d );
414 
418  Q_DECL_DEPRECATED double annotationFrameDistance() const;
419 
423  Q_DECL_DEPRECATED void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
424 
428  Q_DECL_DEPRECATED GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;
429 
433  Q_DECL_DEPRECATED void setGridAnnotationFormat( GridAnnotationFormat f );
434 
438  Q_DECL_DEPRECATED GridAnnotationFormat gridAnnotationFormat() const;
439 
444  Q_DECL_DEPRECATED void setGridFrameStyle( GridFrameStyle style );
445 
449  Q_DECL_DEPRECATED GridFrameStyle gridFrameStyle() const;
450 
455  Q_DECL_DEPRECATED void setGridFrameWidth( double w );
456 
460  Q_DECL_DEPRECATED double gridFrameWidth() const;
461 
466  Q_DECL_DEPRECATED void setGridFramePenSize( double w );
467 
471  Q_DECL_DEPRECATED double gridFramePenSize() const;
472 
477  Q_DECL_DEPRECATED void setGridFramePenColor( const QColor& c );
478 
483  Q_DECL_DEPRECATED QColor gridFramePenColor() const;
484 
489  Q_DECL_DEPRECATED void setGridFrameFillColor1( const QColor& c );
490 
495  Q_DECL_DEPRECATED QColor gridFrameFillColor1() const;
496 
501  Q_DECL_DEPRECATED void setGridFrameFillColor2( const QColor& c );
502 
507  Q_DECL_DEPRECATED QColor gridFrameFillColor2() const;
508 
513  Q_DECL_DEPRECATED void setCrossLength( double l );
514 
518  Q_DECL_DEPRECATED double crossLength();
519 
523  Q_DECL_DEPRECATED void setGridLineSymbol( QgsLineSymbolV2* symbol );
524 
528  Q_DECL_DEPRECATED QgsLineSymbolV2* gridLineSymbol();
529 
533  Q_DECL_DEPRECATED QPainter::CompositionMode gridBlendMode() const;
534 
538  Q_DECL_DEPRECATED void setGridBlendMode( QPainter::CompositionMode blendMode );
539 
546  QgsComposerMapGridStack* grids() { return mGridStack; }
547 
553  QgsComposerMapGrid* grid();
554 
557  QRectF boundingRect() const;
558 
559  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
560  virtual void setFrameOutlineWidth( const double outlineWidth );
561 
566  Q_DECL_DEPRECATED void setRotation( double r );
567 
571  Q_DECL_DEPRECATED double rotation() const { return mMapRotation;}
572 
576  void setMapRotation( double r );
577 
584  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
585 
586  void updateItem();
587 
589  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
590 
591  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
592  bool drawCanvasItems() const { return mDrawCanvasItems; }
593 
595  double mapUnitsToMM() const;
596 
599  void setOverviewFrameMap( int mapId );
602  int overviewFrameMapId() const;
603 
604  void setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol );
605  QgsFillSymbolV2* overviewFrameMapSymbol();
606 
608  QPainter::CompositionMode overviewBlendMode() const;
610  void setOverviewBlendMode( QPainter::CompositionMode blendMode );
611 
613  bool overviewInverted() const;
615  void setOverviewInverted( bool inverted );
616 
618  bool overviewCentered() const;
620  void setOverviewCentered( bool centered );
621 
624  void assignFreeId();
625 
630  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
635  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
640  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
641 
647  bool atlasDriven() const { return mAtlasDriven; }
648 
654  void setAtlasDriven( bool enabled );
655 
659  Q_DECL_DEPRECATED bool atlasFixedScale() const;
660 
664  Q_DECL_DEPRECATED void setAtlasFixedScale( bool fixed );
665 
674  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
675 
684  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
685 
692  double atlasMargin() const { return mAtlasMargin; }
693 
700  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
701 
703  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
704 
706  bool updatesEnabled() const { return mUpdatesEnabled; }
707 
713  int numberExportLayers() const;
714 
721  QPolygonF visibleExtentPolygon() const;
722 
723  //overriden to show "Map 1" type names
724  virtual QString displayName() const;
725 
727  void addOverview( QgsComposerMapOverview* overview );
728  void removeOverview( const QString& name );
729  void moveOverviewUp( const QString& name );
730  void moveOverviewDown( const QString& name );
731  const QgsComposerMapOverview* constMapOverview( const QString& id ) const;
732  QgsComposerMapOverview* mapOverview( const QString& id ) const;
733  QList<QgsComposerMapOverview *> mapOverviews() const;
734  int overviewCount() const { return mOverviews.size(); }
735 
737  QPolygonF transformedMapPolygon() const;
738 
740  QPointF mapToItemCoords( const QPointF& mapCoords ) const;
741 
742  void connectMapOverviewSignals();
743 
744  signals:
745  void extentChanged();
746 
748  void mapRotationChanged( double newRotation );
749 
751  void preparedForAtlas();
752 
753  public slots:
754 
756  void updateCachedImage( );
758  void renderModeUpdateCachedImage();
759 
762  void updateBoundingRect();
763 
766 
768 
769  private:
770 
772  int mId;
773 
774  QgsComposerMapGridStack* mGridStack;
775 
776  // Map region in map units realy used for rendering
777  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
778  // so that full rectangle in paper is used.
779  QgsRectangle mExtent;
780 
781  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
782  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
783  // to manually tweak each atlas preview page without affecting the actual original map extent.
784  QgsRectangle mAtlasFeatureExtent;
785 
786  // Cache used in composer preview
787  QImage mCacheImage;
788 
789  // Is cache up to date
790  bool mCacheUpdated;
791 
793  PreviewMode mPreviewMode;
794 
796  int mNumCachedLayers;
797 
799  bool mDrawing;
800 
802  double mXOffset;
804  double mYOffset;
805 
807  double mMapRotation;
810  double mEvaluatedMapRotation;
811 
813  bool mKeepLayerSet;
814 
816  QStringList mLayerSet;
817 
819  bool mUpdatesEnabled;
820 
822  void connectUpdateSlot();
823 
825  void syncLayerSet();
826 
828  const QgsComposerMapGrid* constFirstMapGrid() const;
829 
831  QgsComposerMapOverview* firstMapOverview();
832  const QgsComposerMapOverview* constFirstMapOverview() const;
833 
834  void removeOverviews();
835  void drawOverviews( QPainter* p );
836 
838  QRectF mCurrentRectangle;
839  QGraphicsView* mMapCanvas;
841  bool mDrawCanvasItems;
842 
843 
844  QList< QgsComposerMapOverview* > mOverviews;
845 
848  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
849 
851  bool mAtlasDriven;
853  AtlasScalingMode mAtlasScalingMode;
855  double mAtlasMargin;
856 
857  void init();
858 
860  QStringList layersToRender() const;
861 
863  QgsRectangle transformedExtent() const;
864 
866  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
867 
869  void requestedExtent( QgsRectangle& extent );
873  void transformShift( double& xShift, double& yShift ) const;
874 
875  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
876  void drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
877  QPointF composerMapPosForItem( const QGraphicsItem* item ) const;
878 
879  enum PartType
880  {
881  Background,
882  Layer,
883  Grid,
884  OverviewMapExtent,
885  Frame,
886  SelectionBoxes
887  };
888 
890  bool shouldDrawPart( PartType part ) const;
891 
895  void refreshMapExtents();
896 
897  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
898 };
899 
900 #endif
901 
GridStyle
Definition: qgscomposermap.h:69
AtlasScalingMode
Scaling modes used for the serial rendering (atlas)
Definition: qgscomposermap.h:125
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...
Definition: qgscomposeritem.cpp:1065
double outlineWidth
Definition: qgssvgcache.cpp:78
A rectangle specified with double values.
Definition: qgsrectangle.h:35
virtual bool writeXML(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
Definition: qgscomposerobject.cpp:61
bool updatesEnabled() const
Returns whether updates to the composer map are enabled.
Definition: qgscomposermap.h:706
void overviewExtentChanged()
Definition: qgscomposermap.h:765
virtual void setRotation(double r)
Sets the item rotation.
Definition: qgscomposeritem.cpp:990
Definition: qgscomposermap.h:72
double atlasMargin() const
Returns the margin size (percentage) used when the map is in atlas mode.
Definition: qgscomposermap.h:692
A collection of grids which is drawn above the map content in a QgsComposerMap.
Definition: qgscomposermapgrid.h:44
Definition: qgscomposermap.h:107
QStringList layerSet() const
Getter for stored layer set that is used if mKeepLayerSet is true.
Definition: qgscomposermap.h:229
Definition: qgscomposermap.h:119
A item that forms part of a map composition.
Definition: qgscomposeritem.h:38
GridAnnotationFormat
Definition: qgscomposermap.h:92
Border
Enum for different frame borders.
Definition: qgscomposermap.h:115
Definition: qgscomposermap.h:98
Definition: qgscomposermap.h:117
void setMapCanvas(QGraphicsView *canvas)
Sets canvas pointer (necessary to query and draw map canvas items)
Definition: qgscomposermap.h:589
Definition: qgscomposermap.h:127
DataDefinedProperty
Data defined properties for different item types.
Definition: qgscomposerobject.h:40
A non GUI class for rendering a map layer set onto a QPainter.
Definition: qgsmaprenderer.h:133
Definition: qgssymbolv2.h:275
Definition: qgscomposerobject.h:43
Definition: qgscomposerobject.h:79
bool isDrawing() const
True if a draw is already in progress.
Definition: qgscomposermap.h:157
void setAtlasMargin(double margin)
Sets the margin size (percentage) used when the map is in atlas mode.
Definition: qgscomposermap.h:700
virtual int type() const
return correct graphics item type.
Definition: qgscomposermap.h:57
Definition: qgscomposermap.h:100
Definition: qgscomposermap.h:87
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:33
virtual void moveContent(double dx, double dy)
Move Content of item.
Definition: qgscomposeritem.h:139
void setAtlasScalingMode(AtlasScalingMode mode)
Sets the current atlas scaling mode.
Definition: qgscomposermap.h:684
Definition: qgscomposermap.h:97
Definition: qgscomposermapoverview.h:30
Definition: qgscomposermap.h:109
virtual void refreshDataDefinedProperty(const QgsComposerObject::DataDefinedProperty property=QgsComposerObject::AllProperties)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
Definition: qgscomposeritem.cpp:1286
virtual void updateItem()
Updates item, with the possibility to do custom update for subclasses.
Definition: qgscomposeritem.h:460
Definition: qgssymbolv2.h:254
bool drawCanvasItems() const
Definition: qgscomposermap.h:592
Definition: qgscomposermap.h:95
PropertyValueType
Specifies whether the value returned by a function should be the original, user set value...
Definition: qgscomposerobject.h:77
virtual int numberExportLayers() const
Get the number of layers that this item requires for exporting as layers.
Definition: qgscomposeritem.h:523
An individual grid which is drawn above the map content in a QgsComposerMap.
Definition: qgscomposermapgrid.h:185
PreviewMode
Preview style.
Definition: qgscomposermap.h:60
GridAnnotationPosition
Definition: qgscomposermap.h:77
Graphics scene for map printing.
Definition: qgscomposition.h:70
Object representing map window.
Definition: qgscomposermap.h:45
Definition: qgscomposermap.h:88
void setUpdatesEnabled(bool enabled)
Sets whether updates to the composer map are enabled.
Definition: qgscomposermap.h:703
PreviewMode previewMode() const
Definition: qgscomposermap.h:217
Definition: qgscomposermap.h:108
GridAnnotationDirection
Definition: qgscomposermap.h:84
virtual QString displayName() const
Get item display name.
Definition: qgscomposeritem.cpp:1337
Definition: qgscomposermap.h:110
virtual void zoomContent(int delta, double x, double y)
Zoom content of item.
Definition: qgscomposeritem.h:145
GridFrameStyle
Definition: qgscomposermap.h:104
virtual void setFrameOutlineWidth(const double outlineWidth)
Sets frame outline width.
Definition: qgscomposeritem.cpp:430
int id() const
Get identification number.
Definition: qgscomposermap.h:154
Definition: qgscomposermap.h:118
Definition: qgscomposeritem.h:53
Definition: qgscomposermap.h:63
Definition: qgscomposermap.h:99
Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect(double &x, double &y, double width, double height, double rotation) const
Calculates corner point after rotation and scaling.
Definition: qgscomposeritem.cpp:1144
Q_DECL_DEPRECATED double rotation() const
Returns the rotation used for drawing the map within the composer item.
Definition: qgscomposermap.h:571
QgsRectangle extent() const
Definition: qgscomposermap.h:239
Definition: qgscomposermap.h:128
virtual bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
Sets item state from DOM element.
Definition: qgscomposerobject.cpp:74
void setLayerSet(const QStringList &layerSet)
Setter for stored layer set that is used if mKeepLayerSet is true.
Definition: qgscomposermap.h:232
int overviewCount() const
Definition: qgscomposermap.h:734
QgsComposerMapGridStack * grids()
Returns the map item's grid stack, which is used to control how grids are drawn over the map's conten...
Definition: qgscomposermap.h:546
Definition: qgscomposermap.h:96
AtlasScalingMode atlasScalingMode() const
Returns the current atlas scaling mode.
Definition: qgscomposermap.h:674
void setDrawCanvasItems(bool b)
Definition: qgscomposermap.h:591
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...
Definition: qgscomposeritem.cpp:676
Definition: qgscomposermap.h:80
void setKeepLayerSet(bool enabled)
Setter for flag that determines if the stored layer set should be used or the current layer set of th...
Definition: qgscomposermap.h:225
bool atlasDriven() const
Returns whether the map extent is set to follow the current atlas feature.
Definition: qgscomposermap.h:647
bool keepLayerSet() const
Getter for flag that determines if the stored layer set should be used or the current layer set of th...
Definition: qgscomposermap.h:222
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:470
double size
Definition: qgssvgcache.cpp:77
Definition: qgscomposermap.h:73
Q_DECL_DEPRECATED void sizeChangedByRotation(double &width, double &height, double rotation)
Calculates width / height of the bounding box of a rotated rectangle.
Definition: qgscomposeritem.cpp:1189