QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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;
27 class QgsComposerMapGrid;
29 class QgsMapRenderer;
30 class QgsMapToPixel;
31 class QDomNode;
32 class QDomDocument;
33 class QGraphicsView;
34 class QPainter;
35 class QgsFillSymbolV2;
36 class QgsLineSymbolV2;
37 class QgsVectorLayer;
38 
43 // NOTE: QgsComposerMapBase must be first, otherwise does not compile
44 class CORE_EXPORT QgsComposerMap : public QgsComposerItem
45 {
46  Q_OBJECT
47 
48  public:
50  QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height );
52  QgsComposerMap( QgsComposition *composition );
53  virtual ~QgsComposerMap();
54 
56  virtual int type() const { return ComposerMap; }
57 
60  {
61  Cache = 0, // Use raster cache
62  Render, // Render the map
63  Rectangle // Display only rectangle
64  };
65 
66  enum GridStyle
67  {
68  Solid = 0, //solid lines
69  Cross, //only draw line crossings
70  Markers
71  };
72 
74  {
75  InsideMapFrame = 0,
77  Disabled
78  };
79 
81  {
82  Horizontal = 0,
85  BoundaryDirection
86  };
87 
89  {
90  Decimal = 0,
92  DegreeMinuteSecond
93  };
94 
96  {
97  NoGridFrame = 0,
98  Zebra // black/white pattern
99  };
100 
102  enum Border
103  {
107  Top
108  };
109 
111  {
112  Longitude = 0,
113  Latitude
114  };
115 
119  {
120  Fixed, /*< The current scale of the map is used for each feature of the atlas */
121  Predefined, /*< A scale is chosen from the predefined scales. The smallest scale from
122  the list of scales where the atlas feature is fully visible is chosen.
123  @see QgsAtlasComposition::setPredefinedScales.
124  @note This mode is only valid for polygon or line atlas coverage layers
125  */
126  Auto /*< The extent is adjusted so that each feature is fully visible.
127  A margin is applied around the center @see setAtlasMargin
128  @note This mode is only valid for polygon or line atlas coverage layers*/
129  };
130 
138  void draw( QPainter *painter, const QgsRectangle& extent, const QSizeF& size, double dpi, double* forceWidthScale = 0 );
139 
141  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );
142 
144  void cache();
145 
147  int id() const {return mId;}
148 
150  bool isDrawing() const {return mDrawing;}
151 
153  void resize( double dx, double dy );
154 
158  void moveContent( double dx, double dy );
159 
164  void zoomContent( int delta, double x, double y );
165 
167  void setSceneRect( const QRectF& rectangle );
168 
170  double scale() const;
171 
173  void setNewScale( double scaleDenominator, bool forceUpdate = true );
174 
181  void setNewExtent( const QgsRectangle& extent );
182 
191  void zoomToExtent( const QgsRectangle& extent );
192 
196  void setNewAtlasFeatureExtent( const QgsRectangle& extent );
197 
199  void toggleAtlasPreview();
200 
207  QgsRectangle* currentMapExtent();
208  const QgsRectangle* currentMapExtent() const;
209 
210  PreviewMode previewMode() const {return mPreviewMode;}
211  void setPreviewMode( PreviewMode m );
212 
215  bool keepLayerSet() const {return mKeepLayerSet;}
218  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
219 
222  QStringList layerSet() const {return mLayerSet;}
225  void setLayerSet( const QStringList& layerSet ) {mLayerSet = layerSet;}
227  void storeCurrentLayerSet();
228 
229  // Set cache outdated
230  void setCacheUpdated( bool u = false );
231 
232  QgsRectangle extent() const {return mExtent;}
233 
235  Q_DECL_DEPRECATED const QgsMapRenderer* mapRenderer() const;
236 
238  void setOffset( double xOffset, double yOffset );
239 
241  bool containsWMSLayer() const;
242 
244  bool containsAdvancedEffects() const;
245 
250  bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
251 
256  bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
257 
260  void setGridEnabled( bool enabled );
261  bool gridEnabled() const;
262 
265  void setGridStyle( GridStyle style );
266  GridStyle gridStyle() const;
267 
270  void setGridIntervalX( double interval );
271  double gridIntervalX() const;
272 
275  void setGridIntervalY( double interval );
276  double gridIntervalY() const;
277 
280  void setGridOffsetX( double offset );
281  double gridOffsetX() const;
282 
285  void setGridOffsetY( double offset );
286  double gridOffsetY() const;
287 
290  void setGridPen( const QPen& p );
291  QPen gridPen() const;
292 
295  void setGridPenWidth( double w );
296 
299  void setGridPenColor( const QColor& c );
300 
303  void setGridAnnotationFont( const QFont& f );
304  QFont gridAnnotationFont() const;
305 
308  void setAnnotationFontColor( const QColor& c );
311  QColor annotationFontColor() const;
312 
315  void setGridAnnotationPrecision( int p );
316  int gridAnnotationPrecision() const;
317 
320  void setShowGridAnnotation( bool show );
321  bool showGridAnnotation() const;
322 
323  void setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border );
324  GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const;
325 
328  void setAnnotationFrameDistance( double d );
329  double annotationFrameDistance() const;
330 
331  void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
332  GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;
333 
334  void setGridAnnotationFormat( GridAnnotationFormat f );
335  GridAnnotationFormat gridAnnotationFormat() const;
336 
339  void setGridFrameStyle( GridFrameStyle style );
340  GridFrameStyle gridFrameStyle() const;
341 
344  void setGridFrameWidth( double w );
345  double gridFrameWidth() const;
346 
349  void setGridFramePenSize( double w );
350  double gridFramePenSize() const;
351 
354  void setGridFramePenColor( const QColor& c );
357  QColor gridFramePenColor() const;
358 
361  void setGridFrameFillColor1( const QColor& c );
364  QColor gridFrameFillColor1() const;
365 
368  void setGridFrameFillColor2( const QColor& c );
371  QColor gridFrameFillColor2() const;
372 
375  void setCrossLength( double l );
376  double crossLength();
377 
380  QRectF boundingRect() const;
383  void updateBoundingRect();
384 
385  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
386  virtual void setFrameOutlineWidth( const double outlineWidth );
387 
392  Q_DECL_DEPRECATED void setRotation( double r );
393 
397  Q_DECL_DEPRECATED double rotation() const { return mMapRotation;};
398 
402  void setMapRotation( double r );
403 
410  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
411 
412  void updateItem();
413 
415  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
416 
417  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
418  bool drawCanvasItems() const { return mDrawCanvasItems; }
419 
421  double mapUnitsToMM() const;
422 
425  void setOverviewFrameMap( int mapId );
428  int overviewFrameMapId() const;
429 
430  void setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol );
431  QgsFillSymbolV2* overviewFrameMapSymbol();
432 
434  QPainter::CompositionMode overviewBlendMode() const;
436  void setOverviewBlendMode( QPainter::CompositionMode blendMode );
437 
439  bool overviewInverted() const;
441  void setOverviewInverted( bool inverted );
442 
444  bool overviewCentered() const;
446  void setOverviewCentered( bool centered );
447 
448  void setGridLineSymbol( QgsLineSymbolV2* symbol );
449  QgsLineSymbolV2* gridLineSymbol();
450 
452  QPainter::CompositionMode gridBlendMode() const;
454  void setGridBlendMode( QPainter::CompositionMode blendMode );
455 
458  void assignFreeId();
459 
464  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
469  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
474  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
475 
481  bool atlasDriven() const { return mAtlasDriven; }
482 
488  void setAtlasDriven( bool enabled );
489 
493  Q_DECL_DEPRECATED bool atlasFixedScale() const;
494 
498  Q_DECL_DEPRECATED void setAtlasFixedScale( bool fixed );
499 
508  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
509 
518  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
519 
526  double atlasMargin() const { return mAtlasMargin; }
527 
534  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
535 
537  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
538 
540  bool updatesEnabled() const { return mUpdatesEnabled; }
541 
547  int numberExportLayers() const;
548 
555  QPolygonF visibleExtentPolygon() const;
556 
557  //overriden to show "Map 1" type names
558  virtual QString displayName() const;
559 
561  void addGrid( QgsComposerMapGrid* grid );
562  void removeGrid( const QString& name );
563  void moveGridUp( const QString& name );
564  void moveGridDown( const QString& name );
565  const QgsComposerMapGrid* constMapGrid( const QString& id ) const;
566  QgsComposerMapGrid* mapGrid( const QString& id ) const;
567  QList< const QgsComposerMapGrid* > mapGrids() const;
568 
569  int gridCount() const { return mGrids.size(); }
570 
572  void addOverview( QgsComposerMapOverview* overview );
573  void removeOverview( const QString& name );
574  void moveOverviewUp( const QString& name );
575  void moveOverviewDown( const QString& name );
576  const QgsComposerMapOverview* constMapOverview( const QString& id ) const;
577  QgsComposerMapOverview* mapOverview( const QString& id ) const;
578  QList<QgsComposerMapOverview *> mapOverviews() const;
579  int overviewCount() const { return mOverviews.size(); }
580 
582  QPolygonF transformedMapPolygon() const;
583 
585  QPointF mapToItemCoords( const QPointF& mapCoords ) const;
586 
587  void connectMapOverviewSignals();
588 
589  signals:
590  void extentChanged();
591 
593  void mapRotationChanged( double newRotation );
594 
596  void preparedForAtlas();
597 
598  public slots:
599 
601  void updateCachedImage( );
603  void renderModeUpdateCachedImage();
604 
607 
609 
610  private:
611 
613  int mId;
614 
615  // Map region in map units realy used for rendering
616  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
617  // so that full rectangle in paper is used.
619 
620  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
621  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
622  // to manually tweak each atlas preview page without affecting the actual original map extent.
624 
625  // Cache used in composer preview
626  QImage mCacheImage;
627 
628  // Is cache up to date
630 
633 
636 
638  bool mDrawing;
639 
641  double mXOffset;
643  double mYOffset;
644 
646  double mMapRotation;
650 
653 
655  QStringList mLayerSet;
656 
659 
661  void connectUpdateSlot();
662 
664  void syncLayerSet();
665 
667  QgsComposerMapGrid* firstMapGrid();
668  const QgsComposerMapGrid* constFirstMapGrid() const;
669 
670  void removeGrids();
671  void drawGrids( QPainter* p );
672 
674  QgsComposerMapOverview* firstMapOverview();
675  const QgsComposerMapOverview* constFirstMapOverview() const;
676 
677  void removeOverviews();
678  void drawOverviews( QPainter* p );
679 
680  //QPainter::CompositionMode mGridBlendMode;
681  /*double mGridFrameWidth;
682  double mGridFramePenThickness;
683  QColor mGridFramePenColor;
684  QColor mGridFrameFillColor1;
685  QColor mGridFrameFillColor2;*/
686 
689  QGraphicsView* mMapCanvas;
692  QList< QgsComposerMapGrid* > mGrids;
693 
694  QList< QgsComposerMapOverview* > mOverviews;
695 
698  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
699 
705  double mAtlasMargin;
706 
707  void init();
708 
710  QStringList layersToRender() const;
711 
713  QgsRectangle transformedExtent() const;
714 
716  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
717 
719  void requestedExtent( QgsRectangle& extent );
723  void transformShift( double& xShift, double& yShift ) const;
725  Border borderForLineCoord( const QPointF& p ) const;
726 
727  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
728  void drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
729  QPointF composerMapPosForItem( const QGraphicsItem* item ) const;
730  //void initGridAnnotationFormatFromProject();
731 
732  enum PartType
733  {
739  SelectionBoxes
740  };
741 
743  bool shouldDrawPart( PartType part ) const;
744 
748  void refreshMapExtents();
749 
750  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
751 };
752 
753 #endif
754 
double mXOffset
Offset in x direction for showing map cache image.
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...
double outlineWidth
Definition: qgssvgcache.cpp:78
A rectangle specified with double values.
Definition: qgsrectangle.h:35
bool mDrawing
set to true if in state of drawing.
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.
double mMapRotation
Map rotation.
void overviewExtentChanged()
virtual void setRotation(double r)
Sets the item rotation.
QStringList mLayerSet
Stored layer list (used if layer live-link mKeepLayerSet is disabled)
double atlasMargin() const
Returns the margin size (percentage) used when the map is in atlas mode.
bool mDrawCanvasItems
True if annotation items, rubber band, etc.
QStringList layerSet() const
Getter for stored layer set that is used if mKeepLayerSet is true.
A item that forms part of a map composition.
Border
Enum for different frame borders.
AtlasScalingMode mAtlasScalingMode
Current atlas scaling mode.
QGraphicsView * mMapCanvas
void setMapCanvas(QGraphicsView *canvas)
Sets canvas pointer (necessary to query and draw map canvas items)
DataDefinedProperty
Data defined properties for different item types.
A non GUI class for rendering a map layer set onto a QPainter.
bool isDrawing() const
True if a draw is already in progress.
void setAtlasMargin(double margin)
Sets the margin size (percentage) used when the map is in atlas mode.
virtual int type() const
return correct graphics item type.
QgsRectangle mExtent
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:33
virtual void moveContent(double dx, double dy)
Move Content of item.
void setAtlasScalingMode(AtlasScalingMode mode)
Sets the current atlas scaling mode.
QRectF mCurrentRectangle
Current bounding rectangle.
QList< QgsComposerMapOverview * > mOverviews
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...
virtual void updateItem()
Updates item, with the possibility to do custom update for subclasses.
bool drawCanvasItems() const
double mYOffset
Offset in y direction for showing map cache image.
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.
bool mAtlasDriven
True if map is being controlled by an atlas.
PreviewMode
Preview style.
QList< QgsComposerMapGrid * > mGrids
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 zoomContent(int delta, double x, double y)
Zoom content of item.
virtual void setFrameOutlineWidth(const double outlineWidth)
Sets frame outline width.
int id() const
Get identification number.
double mAtlasMargin
Margin size for atlas driven extents (percentage of feature size) - when in auto scaling mode...
void init(const bool manageZValue)
bool mKeepLayerSet
Flag if layers to be displayed should be read from qgis canvas (true) or from stored list in mLayerSe...
Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect(double &x, double &y, double width, double height, double rotation) const
Calculates corner point after rotation and scaling.
Q_DECL_DEPRECATED double rotation() const
Returns the rotation used for drawing the map within the composer item.
QgsRectangle extent() const
int mId
Unique identifier.
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.
bool mUpdatesEnabled
Whether updates to the map are enabled.
int overviewCount() const
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...
QgsRectangle mAtlasFeatureExtent
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.
double size
Definition: qgssvgcache.cpp:77
int gridCount() const
PreviewMode mPreviewMode
Preview style.
double mEvaluatedMapRotation
Temporary evaluated map rotation.
int mNumCachedLayers
Number of layers when cache was created.
Q_DECL_DEPRECATED void sizeChangedByRotation(double &width, double &height, double rotation)
Calculates width / height of the bounding box of a rotated rectangle.