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;
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 
176  void setNewExtent( const QgsRectangle& extent );
177 
181  void setNewAtlasFeatureExtent( const QgsRectangle& extent );
182 
184  void toggleAtlasPreview();
185 
192  QgsRectangle* currentMapExtent();
193  const QgsRectangle* currentMapExtent() const;
194 
195  PreviewMode previewMode() const {return mPreviewMode;}
196  void setPreviewMode( PreviewMode m );
197 
200  bool keepLayerSet() const {return mKeepLayerSet;}
203  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
204 
207  QStringList layerSet() const {return mLayerSet;}
210  void setLayerSet( const QStringList& layerSet ) {mLayerSet = layerSet;}
212  void storeCurrentLayerSet();
213 
214  // Set cache outdated
215  void setCacheUpdated( bool u = false );
216 
217  QgsRectangle extent() const {return mExtent;}
218 
220  Q_DECL_DEPRECATED const QgsMapRenderer* mapRenderer() const;
221 
223  void setOffset( double xOffset, double yOffset );
224 
226  bool containsWMSLayer() const;
227 
229  bool containsAdvancedEffects() const;
230 
235  bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
236 
241  bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
242 
245  void setGridEnabled( bool enabled );
246  bool gridEnabled() const;
247 
250  void setGridStyle( GridStyle style );
251  GridStyle gridStyle() const;
252 
255  void setGridIntervalX( double interval );
256  double gridIntervalX() const;
257 
260  void setGridIntervalY( double interval );
261  double gridIntervalY() const;
262 
265  void setGridOffsetX( double offset );
266  double gridOffsetX() const;
267 
270  void setGridOffsetY( double offset );
271  double gridOffsetY() const;
272 
275  void setGridPen( const QPen& p );
276  QPen gridPen() const;
277 
280  void setGridPenWidth( double w );
281 
284  void setGridPenColor( const QColor& c );
285 
288  void setGridAnnotationFont( const QFont& f );
289  QFont gridAnnotationFont() const;
290 
293  void setAnnotationFontColor( const QColor& c );
296  QColor annotationFontColor() const;
297 
300  void setGridAnnotationPrecision( int p );
301  int gridAnnotationPrecision() const;
302 
305  void setShowGridAnnotation( bool show );
306  bool showGridAnnotation() const;
307 
308  void setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border );
309  GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const;
310 
313  void setAnnotationFrameDistance( double d );
314  double annotationFrameDistance() const;
315 
316  void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
317  GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;
318 
319  void setGridAnnotationFormat( GridAnnotationFormat f );
320  GridAnnotationFormat gridAnnotationFormat() const;
321 
324  void setGridFrameStyle( GridFrameStyle style );
325  GridFrameStyle gridFrameStyle() const;
326 
329  void setGridFrameWidth( double w );
330  double gridFrameWidth() const;
331 
334  void setGridFramePenSize( double w );
335  double gridFramePenSize() const;
336 
339  void setGridFramePenColor( const QColor& c );
342  QColor gridFramePenColor() const;
343 
346  void setGridFrameFillColor1( const QColor& c );
349  QColor gridFrameFillColor1() const;
350 
353  void setGridFrameFillColor2( const QColor& c );
356  QColor gridFrameFillColor2() const;
357 
360  void setCrossLength( double l );
361  double crossLength();
362 
365  QRectF boundingRect() const;
368  void updateBoundingRect();
369 
370  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
371  virtual void setFrameOutlineWidth( const double outlineWidth );
372 
377  Q_DECL_DEPRECATED void setRotation( double r );
378 
382  Q_DECL_DEPRECATED double rotation() const { return mMapRotation;};
383 
387  void setMapRotation( double r );
388 
395  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
396 
397  void updateItem();
398 
400  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
401 
402  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
403  bool drawCanvasItems() const { return mDrawCanvasItems; }
404 
406  double mapUnitsToMM() const;
407 
410  void setOverviewFrameMap( int mapId );
413  int overviewFrameMapId() const;
414 
415  void setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol );
416  QgsFillSymbolV2* overviewFrameMapSymbol();
417 
419  QPainter::CompositionMode overviewBlendMode() const;
421  void setOverviewBlendMode( QPainter::CompositionMode blendMode );
422 
424  bool overviewInverted() const;
426  void setOverviewInverted( bool inverted );
427 
429  bool overviewCentered() const;
431  void setOverviewCentered( bool centered );
432 
433  void setGridLineSymbol( QgsLineSymbolV2* symbol );
434  QgsLineSymbolV2* gridLineSymbol();
435 
437  QPainter::CompositionMode gridBlendMode() const;
439  void setGridBlendMode( QPainter::CompositionMode blendMode );
440 
443  void assignFreeId();
444 
449  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
454  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
459  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
460 
466  bool atlasDriven() const { return mAtlasDriven; }
467 
473  void setAtlasDriven( bool enabled );
474 
478  Q_DECL_DEPRECATED bool atlasFixedScale() const;
479 
483  Q_DECL_DEPRECATED void setAtlasFixedScale( bool fixed );
484 
493  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
494 
503  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
504 
511  double atlasMargin() const { return mAtlasMargin; }
512 
519  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
520 
522  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
523 
525  bool updatesEnabled() const { return mUpdatesEnabled; }
526 
532  int numberExportLayers() const;
533 
540  QPolygonF visibleExtentPolygon() const;
541 
543  void addGrid( QgsComposerMapGrid* grid );
544  void removeGrid( const QString& name );
545  void moveGridUp( const QString& name );
546  void moveGridDown( const QString& name );
547  const QgsComposerMapGrid* constMapGrid( const QString& id ) const;
548  QgsComposerMapGrid* mapGrid( const QString& id ) const;
549  QList< const QgsComposerMapGrid* > mapGrids() const;
550 
551  int gridCount() const { return mGrids.size(); }
552 
554  void addOverview( QgsComposerMapOverview* overview );
555  void removeOverview( const QString& name );
556  void moveOverviewUp( const QString& name );
557  void moveOverviewDown( const QString& name );
558  const QgsComposerMapOverview* constMapOverview( const QString& id ) const;
559  QgsComposerMapOverview* mapOverview( const QString& id ) const;
560  QList<QgsComposerMapOverview *> mapOverviews() const;
561  int overviewCount() const { return mOverviews.size(); }
562 
564  QPolygonF transformedMapPolygon() const;
565 
567  QPointF mapToItemCoords( const QPointF& mapCoords ) const;
568 
569  void connectMapOverviewSignals();
570 
571 signals:
572  void extentChanged();
573 
575  void mapRotationChanged( double newRotation );
576 
578  void preparedForAtlas();
579 
580  public slots:
581 
583  void updateCachedImage( );
585  void renderModeUpdateCachedImage();
586 
589 
591 
592  private:
593 
595  int mId;
596 
597  // Map region in map units realy used for rendering
598  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
599  // so that full rectangle in paper is used.
601 
602  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
603  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
604  // to manually tweak each atlas preview page without affecting the actual original map extent.
606 
607  // Cache used in composer preview
608  QImage mCacheImage;
609 
610  // Is cache up to date
612 
615 
618 
620  bool mDrawing;
621 
623  double mXOffset;
625  double mYOffset;
626 
628  double mMapRotation;
632 
635 
637  QStringList mLayerSet;
638 
641 
643  void connectUpdateSlot();
644 
646  void syncLayerSet();
647 
649  QgsComposerMapGrid* firstMapGrid();
650  const QgsComposerMapGrid* constFirstMapGrid() const;
651 
652  void removeGrids();
653  void drawGrids( QPainter* p );
654 
656  QgsComposerMapOverview* firstMapOverview();
657  const QgsComposerMapOverview* constFirstMapOverview() const;
658 
659  void removeOverviews();
660  void drawOverviews( QPainter* p );
661 
662  //QPainter::CompositionMode mGridBlendMode;
663  /*double mGridFrameWidth;
664  double mGridFramePenThickness;
665  QColor mGridFramePenColor;
666  QColor mGridFrameFillColor1;
667  QColor mGridFrameFillColor2;*/
668 
671  QGraphicsView* mMapCanvas;
674  QList< QgsComposerMapGrid* > mGrids;
675 
676  QList< QgsComposerMapOverview* > mOverviews;
677 
680  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
681 
687  double mAtlasMargin;
688 
689  void init();
690 
692  QStringList layersToRender() const;
693 
695  QgsRectangle transformedExtent() const;
696 
698  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
699 
701  void requestedExtent( QgsRectangle& extent );
705  void transformShift( double& xShift, double& yShift ) const;
707  Border borderForLineCoord( const QPointF& p ) const;
708 
709  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
710  void drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
711  QPointF composerMapPosForItem( const QGraphicsItem* item ) const;
712  //void initGridAnnotationFormatFromProject();
713 
714  enum PartType
715  {
721  SelectionBoxes
722  };
723 
725  bool shouldDrawPart( PartType part ) const;
726 
730  void refreshMapExtents();
731 
732  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
733 };
734 
735 #endif
736 
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)
A non GUI class for rendering a map layer set onto a QPainter.
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.
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.
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.
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.
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.