QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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;
28 class QgsMapRenderer;
29 class QgsMapToPixel;
30 class QDomNode;
31 class QDomDocument;
32 class QGraphicsView;
33 class QPainter;
34 class QgsFillSymbolV2;
35 class QgsLineSymbolV2;
36 class QgsVectorLayer;
37 
42 // NOTE: QgsComposerMapBase must be first, otherwise does not compile
43 class CORE_EXPORT QgsComposerMap : public QgsComposerItem
44 {
45  Q_OBJECT
46 
47  public:
49  QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height );
51  QgsComposerMap( QgsComposition *composition );
52  virtual ~QgsComposerMap();
53 
55  virtual int type() const { return ComposerMap; }
56 
59  {
60  Cache = 0, // Use raster cache
61  Render, // Render the map
62  Rectangle // Display only rectangle
63  };
64 
65  enum GridStyle
66  {
67  Solid = 0, //solid lines
68  Cross, //only draw line crossings
69  Markers
70  };
71 
73  {
74  InsideMapFrame = 0,
76  Disabled
77  };
78 
80  {
81  Horizontal = 0,
84  BoundaryDirection
85  };
86 
88  {
89  Decimal = 0,
91  DegreeMinuteSecond
92  };
93 
95  {
96  NoGridFrame = 0,
97  Zebra // black/white pattern
98  };
99 
101  enum Border
102  {
106  Top
107  };
108 
110  {
111  Longitude = 0,
112  Latitude
113  };
114 
118  {
119  Fixed, /*< The current scale of the map is used for each feature of the atlas */
120  Predefined, /*< A scale is chosen from the predefined scales. The smallest scale from
121  the list of scales where the atlas feature is fully visible is chosen.
122  @see QgsAtlasComposition::setPredefinedScales.
123  @note This mode is only valid for polygon or line atlas coverage layers
124  */
125  Auto /*< The extent is adjusted so that each feature is fully visible.
126  A margin is applied around the center @see setAtlasMargin
127  @note This mode is only valid for polygon or line atlas coverage layers*/
128  };
129 
137  void draw( QPainter *painter, const QgsRectangle& extent, const QSizeF& size, double dpi, double* forceWidthScale = 0 );
138 
140  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );
141 
143  void cache();
144 
146  int id() const {return mId;}
147 
149  bool isDrawing() const {return mDrawing;}
150 
152  void resize( double dx, double dy );
153 
157  void moveContent( double dx, double dy );
158 
163  void zoomContent( int delta, double x, double y );
164 
166  void setSceneRect( const QRectF& rectangle );
167 
169  double scale() const;
170 
172  void setNewScale( double scaleDenominator, bool forceUpdate = true );
173 
175  void setNewExtent( const QgsRectangle& extent );
176 
180  void setNewAtlasFeatureExtent( const QgsRectangle& extent );
181 
183  void toggleAtlasPreview();
184 
191  QgsRectangle* currentMapExtent();
192  const QgsRectangle* currentMapExtent() const;
193 
194  PreviewMode previewMode() const {return mPreviewMode;}
195  void setPreviewMode( PreviewMode m );
196 
199  bool keepLayerSet() const {return mKeepLayerSet;}
202  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
203 
206  QStringList layerSet() const {return mLayerSet;}
209  void setLayerSet( const QStringList& layerSet ) {mLayerSet = layerSet;}
211  void storeCurrentLayerSet();
212 
213  // Set cache outdated
214  void setCacheUpdated( bool u = false );
215 
216  QgsRectangle extent() const {return mExtent;}
217 
219  Q_DECL_DEPRECATED const QgsMapRenderer* mapRenderer() const;
220 
222  void setOffset( double xOffset, double yOffset );
223 
225  bool containsWMSLayer() const;
226 
228  bool containsAdvancedEffects() const;
229 
234  bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
235 
240  bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
241 
244  void setGridEnabled( bool enabled );
245  bool gridEnabled() const;
246 
249  void setGridStyle( GridStyle style );
250  GridStyle gridStyle() const;
251 
254  void setGridIntervalX( double interval );
255  double gridIntervalX() const;
256 
259  void setGridIntervalY( double interval );
260  double gridIntervalY() const;
261 
264  void setGridOffsetX( double offset );
265  double gridOffsetX() const;
266 
269  void setGridOffsetY( double offset );
270  double gridOffsetY() const;
271 
274  void setGridPen( const QPen& p );
275  QPen gridPen() const;
276 
279  void setGridPenWidth( double w );
280 
283  void setGridPenColor( const QColor& c );
284 
287  void setGridAnnotationFont( const QFont& f );
288  QFont gridAnnotationFont() const;
289 
292  void setAnnotationFontColor( const QColor& c );
295  QColor annotationFontColor() const;
296 
299  void setGridAnnotationPrecision( int p );
300  int gridAnnotationPrecision() const;
301 
304  void setShowGridAnnotation( bool show );
305  bool showGridAnnotation() const;
306 
307  void setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border );
308  GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const;
309 
312  void setAnnotationFrameDistance( double d );
313  double annotationFrameDistance() const;
314 
315  void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
316  GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;
317 
318  void setGridAnnotationFormat( GridAnnotationFormat f );
319  GridAnnotationFormat gridAnnotationFormat() const;
320 
323  void setGridFrameStyle( GridFrameStyle style );
324  GridFrameStyle gridFrameStyle() const;
325 
328  void setGridFrameWidth( double w );
329  double gridFrameWidth() const;
330 
333  void setGridFramePenSize( double w );
334  double gridFramePenSize() const;
335 
338  void setGridFramePenColor( const QColor& c );
341  QColor gridFramePenColor() const;
342 
345  void setGridFrameFillColor1( const QColor& c );
348  QColor gridFrameFillColor1() const;
349 
352  void setGridFrameFillColor2( const QColor& c );
355  QColor gridFrameFillColor2() const;
356 
359  void setCrossLength( double l );
360  double crossLength();
361 
364  QRectF boundingRect() const;
367  void updateBoundingRect();
368 
369  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
370  virtual void setFrameOutlineWidth( const double outlineWidth );
371 
376  Q_DECL_DEPRECATED void setRotation( double r );
377 
381  Q_DECL_DEPRECATED double rotation() const { return mMapRotation;};
382 
386  void setMapRotation( double r );
387 
394  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
395 
396  void updateItem();
397 
399  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
400 
401  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
402  bool drawCanvasItems() const { return mDrawCanvasItems; }
403 
405  double mapUnitsToMM() const;
406 
409  void setOverviewFrameMap( int mapId );
412  int overviewFrameMapId() const { return mOverviewFrameMapId; }
413 
414  void setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol );
415  QgsFillSymbolV2* overviewFrameMapSymbol() { return mOverviewFrameMapSymbol; }
416 
418  QPainter::CompositionMode overviewBlendMode() const {return mOverviewBlendMode;}
420  void setOverviewBlendMode( QPainter::CompositionMode blendMode );
421 
423  bool overviewInverted() const {return mOverviewInverted;}
425  void setOverviewInverted( bool inverted );
426 
428  bool overviewCentered() const { return mOverviewCentered; }
430  void setOverviewCentered( bool centered );
431 
432  void setGridLineSymbol( QgsLineSymbolV2* symbol );
433  QgsLineSymbolV2* gridLineSymbol();
434 
436  QPainter::CompositionMode gridBlendMode() const;
438  void setGridBlendMode( QPainter::CompositionMode blendMode );
439 
442  void assignFreeId();
443 
448  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
453  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
458  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
459 
465  bool atlasDriven() const { return mAtlasDriven; }
466 
472  void setAtlasDriven( bool enabled );
473 
477  Q_DECL_DEPRECATED bool atlasFixedScale() const;
478 
482  Q_DECL_DEPRECATED void setAtlasFixedScale( bool fixed );
483 
492  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
493 
502  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
503 
510  double atlasMargin() const { return mAtlasMargin; }
511 
518  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
519 
521  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
522 
524  bool updatesEnabled() const { return mUpdatesEnabled; }
525 
531  int numberExportLayers() const;
532 
539  QPolygonF visibleExtentPolygon() const;
540 
542  void addGrid( QgsComposerMapGrid* grid );
543  void removeGrid( const QString& name );
544  void moveGridUp( const QString& name );
545  void moveGridDown( const QString& name );
546  const QgsComposerMapGrid* constMapGrid( const QString& id ) const;
547  QgsComposerMapGrid* mapGrid( const QString& id ) const;
548  QList< const QgsComposerMapGrid* > mapGrids() const;
549 
550  int gridCount() const { return mGrids.size(); }
551 
553  QPolygonF transformedMapPolygon() const;
554 
556  QPointF mapToItemCoords( const QPointF& mapCoords ) const;
557 
558  signals:
559  void extentChanged();
560 
562  void mapRotationChanged( double newRotation );
563 
565  void preparedForAtlas();
566 
567  public slots:
568 
570  void updateCachedImage( );
572  void renderModeUpdateCachedImage();
573 
574  void overviewExtentChanged();
575 
577 
578  private:
579 
581  int mId;
582 
583  // Map region in map units realy used for rendering
584  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
585  // so that full rectangle in paper is used.
587 
588  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
589  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
590  // to manually tweak each atlas preview page without affecting the actual original map extent.
592 
593  // Cache used in composer preview
594  QImage mCacheImage;
595 
596  // Is cache up to date
598 
601 
604 
606  bool mDrawing;
607 
609  double mXOffset;
611  double mYOffset;
612 
614  double mMapRotation;
618 
621 
623  QStringList mLayerSet;
624 
629 
631  QPainter::CompositionMode mOverviewBlendMode;
635 
638 
640  void connectUpdateSlot();
641 
643  void syncLayerSet();
644 
646  QgsComposerMapGrid* firstMapGrid();
647  const QgsComposerMapGrid* constFirstMapGrid() const;
648 
649  void removeGrids();
650  void drawGrids( QPainter* p );
651 
652  //QPainter::CompositionMode mGridBlendMode;
653  /*double mGridFrameWidth;
654  double mGridFramePenThickness;
655  QColor mGridFramePenColor;
656  QColor mGridFrameFillColor1;
657  QColor mGridFrameFillColor2;*/
658 
661  QGraphicsView* mMapCanvas;
664  QList< QgsComposerMapGrid* > mGrids;
665 
668  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
669 
675  double mAtlasMargin;
676 
677  void init();
678 
680  QStringList layersToRender() const;
681 
683  QgsRectangle transformedExtent() const;
684 
686  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
687 
689  void requestedExtent( QgsRectangle& extent );
693  void transformShift( double& xShift, double& yShift ) const;
695  Border borderForLineCoord( const QPointF& p ) const;
696 
697  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
698  void drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
699  QPointF composerMapPosForItem( const QGraphicsItem* item ) const;
700  void drawOverviewMapExtent( QPainter* p );
701  void createDefaultOverviewFrameSymbol();
702  //void initGridAnnotationFormatFromProject();
703 
704  enum PartType
705  {
711  SelectionBoxes
712  };
713 
715  bool shouldDrawPart( PartType part ) const;
716 
720  void refreshMapExtents();
721 };
722 
723 #endif
724 
int mOverviewFrameMapId
Id of map which displays its extent rectangle into this composer map (overview map functionality)...
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.
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.
QPainter::CompositionMode mOverviewBlendMode
Blend mode for overview.
AtlasScalingMode mAtlasScalingMode
Current atlas scaling mode.
QGraphicsView * mMapCanvas
void setMapCanvas(QGraphicsView *canvas)
Sets canvas pointer (necessary to query and draw map canvas items)
A non GUI class for rendering a map layer set onto a QPainter.
bool mOverviewCentered
Centering mode for overview.
DataDefinedProperty
Data defined properties for different item types.
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.
bool overviewInverted() const
Returns true if the overview frame is inverted.
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.
int overviewFrameMapId() const
Returns id of overview frame (or -1 if no overfiew frame)
QRectF mCurrentRectangle
Current bounding rectangle.
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.
QPainter::CompositionMode overviewBlendMode() const
Returns the overview's blending mode.
bool drawCanvasItems() const
double mYOffset
Offset in y direction for showing map cache image.
virtual int numberExportLayers() const
Get the number of layers that this item requires for exporting as layers.
PropertyValueType
Specifies whether the value returned by a function should be the original, user set value...
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.
QgsFillSymbolV2 * mOverviewFrameMapSymbol
Drawing style for overview farme.
void setUpdatesEnabled(bool enabled)
Sets whether updates to the composer map are enabled.
PreviewMode previewMode() const
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.
QgsFillSymbolV2 * overviewFrameMapSymbol()
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.
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.
bool overviewCentered() const
Returns true if the extent is forced to center on the overview.