QGIS API Documentation  2.2.0-Valmiera
 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 : [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;
27 class QgsMapRenderer;
28 class QgsMapToPixel;
29 class QDomNode;
30 class QDomDocument;
31 class QGraphicsView;
32 class QPainter;
33 class QgsFillSymbolV2;
34 class QgsLineSymbolV2;
35 class QgsVectorLayer;
36 
41 // NOTE: QgsComposerMapBase must be first, otherwise does not compile
42 class CORE_EXPORT QgsComposerMap : public QgsComposerItem
43 {
44  Q_OBJECT
45 
46  public:
48  QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height );
50  QgsComposerMap( QgsComposition *composition );
51  virtual ~QgsComposerMap();
52 
54  virtual int type() const { return ComposerMap; }
55 
58  {
59  Cache = 0, // Use raster cache
60  Render, // Render the map
61  Rectangle // Display only rectangle
62  };
63 
64  enum GridStyle
65  {
66  Solid = 0, //solid lines
67  Cross //only draw line crossings
68  };
69 
71  {
72  InsideMapFrame = 0,
74  Disabled
75  };
76 
78  {
79  Horizontal = 0,
82  BoundaryDirection
83  };
84 
86  {
87  Decimal = 0,
89  DegreeMinuteSecond
90  };
91 
93  {
94  NoGridFrame = 0,
95  Zebra //black / white pattern
96  };
97 
99  enum Border
100  {
104  Top
105  };
106 
114  void draw( QPainter *painter, const QgsRectangle& extent, const QSizeF& size, double dpi, double* forceWidthScale = 0 );
115 
117  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );
118 
120  void cache();
121 
123  int id() const {return mId;}
124 
126  bool isDrawing() const {return mDrawing;}
127 
129  void resize( double dx, double dy );
130 
134  void moveContent( double dx, double dy );
135 
140  void zoomContent( int delta, double x, double y );
141 
143  void setSceneRect( const QRectF& rectangle );
144 
146  double scale() const;
147 
149  void setNewScale( double scaleDenominator );
150 
152  void setNewExtent( const QgsRectangle& extent );
153 
157  void setNewAtlasFeatureExtent( const QgsRectangle& extent );
158 
160  void toggleAtlasPreview();
161 
165  QgsRectangle* currentMapExtent();
166  const QgsRectangle* currentMapExtent() const;
167 
168  PreviewMode previewMode() const {return mPreviewMode;}
169  void setPreviewMode( PreviewMode m );
170 
173  bool keepLayerSet() const {return mKeepLayerSet;}
176  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
177 
180  QStringList layerSet() const {return mLayerSet;}
183  void setLayerSet( const QStringList& layerSet ) {mLayerSet = layerSet;}
185  void storeCurrentLayerSet();
186 
187  // Set cache outdated
188  void setCacheUpdated( bool u = false );
189 
190  QgsRectangle extent() const {return mExtent;}
191 
192  const QgsMapRenderer* mapRenderer() const {return mMapRenderer;}
193 
195  void setOffset( double xOffset, double yOffset );
196 
198  bool containsWMSLayer() const;
199 
201  bool containsAdvancedEffects() const;
202 
207  bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
208 
213  bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
214 
217  void setGridEnabled( bool enabled ) {mGridEnabled = enabled;}
218  bool gridEnabled() const { return mGridEnabled; }
219 
222  void setGridStyle( GridStyle style ) {mGridStyle = style;}
223  GridStyle gridStyle() const { return mGridStyle; }
224 
227  void setGridIntervalX( double interval ) { mGridIntervalX = interval;}
228  double gridIntervalX() const { return mGridIntervalX; }
229 
232  void setGridIntervalY( double interval ) { mGridIntervalY = interval;}
233  double gridIntervalY() const { return mGridIntervalY; }
234 
237  void setGridOffsetX( double offset ) { mGridOffsetX = offset; }
238  double gridOffsetX() const { return mGridOffsetX; }
239 
242  void setGridOffsetY( double offset ) { mGridOffsetY = offset; }
243  double gridOffsetY() const { return mGridOffsetY; }
244 
247  void setGridPen( const QPen& p );
248  QPen gridPen() const;
249 
252  void setGridPenWidth( double w );
253 
256  void setGridPenColor( const QColor& c );
257 
260  void setGridAnnotationFont( const QFont& f ) { mGridAnnotationFont = f; }
261  QFont gridAnnotationFont() const { return mGridAnnotationFont; }
262 
265  void setAnnotationFontColor( const QColor& c ) {mGridAnnotationFontColor = c;}
268  QColor annotationFontColor() const {return mGridAnnotationFontColor;}
269 
272  void setGridAnnotationPrecision( int p ) {mGridAnnotationPrecision = p;}
273  int gridAnnotationPrecision() const {return mGridAnnotationPrecision;}
274 
277  void setShowGridAnnotation( bool show ) {mShowGridAnnotation = show;}
278  bool showGridAnnotation() const {return mShowGridAnnotation;}
279 
280  void setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border );
281  GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const;
282 
285  void setAnnotationFrameDistance( double d ) {mAnnotationFrameDistance = d;}
286  double annotationFrameDistance() const {return mAnnotationFrameDistance;}
287 
288  void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
289  GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;
290 
291  void setGridAnnotationFormat( GridAnnotationFormat f ) { mGridAnnotationFormat = f; }
292  GridAnnotationFormat gridAnnotationFormat() const { return mGridAnnotationFormat; }
293 
296  void setGridFrameStyle( GridFrameStyle style ) { mGridFrameStyle = style; }
297  GridFrameStyle gridFrameStyle() const { return mGridFrameStyle; }
298 
301  void setGridFrameWidth( double w ) { mGridFrameWidth = w; }
302  double gridFrameWidth() const { return mGridFrameWidth; }
303 
306  void setGridFramePenSize( double w ) { mGridFramePenThickness = w; }
307  double gridFramePenSize() const { return mGridFramePenThickness; }
308 
311  void setGridFramePenColor( const QColor& c ) { mGridFramePenColor = c;}
314  QColor gridFramePenColor() const {return mGridFramePenColor;}
315 
318  void setGridFrameFillColor1( const QColor& c ) { mGridFrameFillColor1 = c;}
321  QColor gridFrameFillColor1() const {return mGridFrameFillColor1;}
322 
325  void setGridFrameFillColor2( const QColor& c ) { mGridFrameFillColor2 = c;}
328  QColor gridFrameFillColor2() const {return mGridFrameFillColor2;}
329 
332  QRectF boundingRect() const;
335  void updateBoundingRect();
336 
337  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
338  virtual void setFrameOutlineWidth( double outlineWidth );
339 
342  void setCrossLength( double l ) {mCrossLength = l;}
343  double crossLength() {return mCrossLength;}
344 
349  void setRotation( double r );
353  double rotation() const { return mMapRotation;};
354 
358  void setMapRotation( double r );
360  double mapRotation() const { return mMapRotation;};
361 
362  void updateItem();
363 
365  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
366 
367  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
368  bool drawCanvasItems() const { return mDrawCanvasItems; }
369 
371  double mapUnitsToMM() const;
372 
375  void setOverviewFrameMap( int mapId );
378  int overviewFrameMapId() const { return mOverviewFrameMapId; }
379 
380  void setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol );
381  QgsFillSymbolV2* overviewFrameMapSymbol() { return mOverviewFrameMapSymbol; }
382 
384  QPainter::CompositionMode overviewBlendMode() const {return mOverviewBlendMode;}
386  void setOverviewBlendMode( QPainter::CompositionMode blendMode );
387 
389  bool overviewInverted() const {return mOverviewInverted;}
391  void setOverviewInverted( bool inverted );
392 
394  bool overviewCentered() const { return mOverviewCentered; }
396  void setOverviewCentered( bool centered );
397 
398  void setGridLineSymbol( QgsLineSymbolV2* symbol );
399  QgsLineSymbolV2* gridLineSymbol() { return mGridLineSymbol; }
400 
402  QPainter::CompositionMode gridBlendMode() const {return mGridBlendMode;}
404  void setGridBlendMode( QPainter::CompositionMode blendMode );
405 
408  void assignFreeId();
409 
414  bool imageSizeConsideringRotation( double& width, double& height ) const;
419  bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
424  void sizeChangedByRotation( double& width, double& height );
425 
427  bool atlasDriven() const { return mAtlasDriven; }
429  void setAtlasDriven( bool enabled ) { mAtlasDriven = enabled; }
430 
432  bool atlasFixedScale() const { return mAtlasFixedScale; }
434  void setAtlasFixedScale( bool fixed ) { mAtlasFixedScale = fixed; }
435 
437  double atlasMargin() const { return mAtlasMargin; }
439  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
440 
441  signals:
442  void extentChanged();
443 
445  void mapRotationChanged( double newRotation );
446 
447  public slots:
448 
450  void updateCachedImage( );
452  void renderModeUpdateCachedImage();
453 
454  void overviewExtentChanged();
455 
456  private:
457 
459  {
460  Longitude = 0,
461  Latitude
462  };
463 
464  // Pointer to map renderer of the QGIS main map. Note that QgsComposerMap uses a different map renderer,
465  //it just copies some properties from the main map renderer.
467 
469  int mId;
470 
471  // Map region in map units realy used for rendering
472  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
473  // so that full rectangle in paper is used.
475 
476  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
477  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
478  // to manually tweak each atlas preview page without affecting the actual original map extent.
480 
481  // Cache used in composer preview
482  QImage mCacheImage;
483 
484  // Is cache up to date
486 
489 
492 
494  bool mDrawing;
495 
497  double mXOffset;
499  double mYOffset;
500 
502  double mMapRotation;
503 
506 
508  QStringList mLayerSet;
509 
516  QPainter::CompositionMode mOverviewBlendMode;
520 
522  void connectUpdateSlot();
523 
525  void syncLayerSet();
526 
536  double mGridOffsetX;
538  double mGridOffsetY;
548  QPainter::CompositionMode mGridBlendMode;
549 
558 
561 
570 
572 
579 
583  double mCrossLength;
584  QGraphicsView* mMapCanvas;
587 
590  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
591 
597  double mAtlasMargin;
598 
600  QStringList layersToRender();
601 
603  void drawGrid( QPainter* p );
604  void drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines );
609  void drawCoordinateAnnotations( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines );
610  void drawCoordinateAnnotation( QPainter* p, const QPointF& pos, QString annotationString );
616  void drawAnnotation( QPainter* p, const QPointF& pos, int rotation, const QString& annotationText );
617  QString gridAnnotationString( double value, AnnotationCoordinate coord ) const;
620  int xGridLines( QList< QPair< double, QLineF > >& lines ) const;
623  int yGridLines( QList< QPair< double, QLineF > >& lines ) const;
625  QgsRectangle transformedExtent() const;
627  QPolygonF transformedMapPolygon() const;
628  double maxExtension() const;
632  void mapPolygon( QPolygonF& poly ) const;
634  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
635 
637  void requestedExtent( QgsRectangle& extent ) const;
641  void transformShift( double& xShift, double& yShift ) const;
643  QPointF mapToItemCoords( const QPointF& mapCoords ) const;
645  Border borderForLineCoord( const QPointF& p ) const;
646 
647  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
648  void drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
649  QPointF composerMapPosForItem( const QGraphicsItem* item ) const;
650  void sortGridLinesOnBorders( const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines, QMap< double, double >& leftFrameEntries,
651  QMap< double, double >& rightFrameEntries, QMap< double, double >& topFrameEntries, QMap< double, double >& bottomFrameEntries ) const;
652  void drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, Border border );
653  void drawGridLine( const QLineF& line, QPainter* p );
654  void drawOverviewMapExtent( QPainter* p );
655  void createDefaultOverviewFrameSymbol();
656  void createDefaultGridLineSymbol();
657  void initGridAnnotationFormatFromProject();
658 
659 };
660 
661 #endif