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;
30 class QgsComposerMapGrid;
31 class QgsMapRenderer;
32 class QgsMapToPixel;
33 class QDomNode;
34 class QDomDocument;
35 class QGraphicsView;
36 class QPainter;
37 class QgsFillSymbolV2;
38 class QgsLineSymbolV2;
39 class QgsVectorLayer;
40 
46 class CORE_EXPORT QgsComposerMap : public QgsComposerItem
47 {
48  Q_OBJECT
49 
50  public:
52  QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height );
54  QgsComposerMap( QgsComposition *composition );
55  virtual ~QgsComposerMap();
56 
58  virtual int type() const { return ComposerMap; }
59 
62  {
63  Cache = 0, // Use raster cache
64  Render, // Render the map
65  Rectangle // Display only rectangle
66  };
67 
68  //grid enums are moved to QgsComposerMapGrid
69  //TODO - remove for QGIS 3.0
70  enum GridStyle
71  {
72  Solid = 0, //solid lines
73  Cross, //only draw line crossings
75  FrameAnnotationsOnly
76  };
77 
79  {
80  InsideMapFrame = 0,
82  Disabled
83  };
84 
86  {
87  Horizontal = 0,
90  BoundaryDirection
91  };
92 
94  {
95  Decimal = 0,
102  DegreeMinuteSecondPadded
103  };
104 
106  {
107  NoGridFrame = 0,
108  Zebra, // black/white pattern
112  LineBorder
113  };
114 
116  enum Border
117  {
121  Top
122  };
123 
127  {
128  Fixed, /*< The current scale of the map is used for each feature of the atlas */
129  Predefined, /*< A scale is chosen from the predefined scales. The smallest scale from
130  the list of scales where the atlas feature is fully visible is chosen.
131  @see QgsAtlasComposition::setPredefinedScales.
132  @note This mode is only valid for polygon or line atlas coverage layers
133  */
134  Auto /*< The extent is adjusted so that each feature is fully visible.
135  A margin is applied around the center @see setAtlasMargin
136  @note This mode is only valid for polygon or line atlas coverage layers*/
137  };
138 
146  void draw( QPainter *painter, const QgsRectangle& extent, const QSizeF& size, double dpi, double* forceWidthScale = 0 );
147 
149  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );
150 
152  void cache();
153 
155  int id() const {return mId;}
156 
158  bool isDrawing() const {return mDrawing;}
159 
161  void resize( double dx, double dy );
162 
166  void moveContent( double dx, double dy );
167 
172  void zoomContent( int delta, double x, double y );
173 
175  void setSceneRect( const QRectF& rectangle );
176 
178  double scale() const;
179 
181  void setNewScale( double scaleDenominator, bool forceUpdate = true );
182 
189  void setNewExtent( const QgsRectangle& extent );
190 
199  void zoomToExtent( const QgsRectangle& extent );
200 
204  void setNewAtlasFeatureExtent( const QgsRectangle& extent );
205 
207  void toggleAtlasPreview();
208 
215  QgsRectangle* currentMapExtent();
216  const QgsRectangle* currentMapExtent() const;
217 
218  PreviewMode previewMode() const {return mPreviewMode;}
219  void setPreviewMode( PreviewMode m );
220 
223  bool keepLayerSet() const {return mKeepLayerSet;}
226  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
227 
230  QStringList layerSet() const {return mLayerSet;}
233  void setLayerSet( const QStringList& layerSet ) {mLayerSet = layerSet;}
235  void storeCurrentLayerSet();
236 
237  // Set cache outdated
238  void setCacheUpdated( bool u = false );
239 
240  QgsRectangle extent() const {return mExtent;}
241 
243  Q_DECL_DEPRECATED const QgsMapRenderer* mapRenderer() const;
244 
246  void setOffset( double xOffset, double yOffset );
247 
249  bool containsWMSLayer() const;
250 
252  bool containsAdvancedEffects() const;
253 
258  bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
259 
264  bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
265 
270  Q_DECL_DEPRECATED void setGridEnabled( bool enabled );
271 
275  Q_DECL_DEPRECATED bool gridEnabled() const;
276 
281  Q_DECL_DEPRECATED void setGridStyle( GridStyle style );
282 
286  Q_DECL_DEPRECATED GridStyle gridStyle() const;
287 
292  Q_DECL_DEPRECATED void setGridIntervalX( double interval );
293 
297  Q_DECL_DEPRECATED double gridIntervalX() const;
298 
303  Q_DECL_DEPRECATED void setGridIntervalY( double interval );
304 
308  Q_DECL_DEPRECATED double gridIntervalY() const;
309 
314  Q_DECL_DEPRECATED void setGridOffsetX( double offset );
315 
319  Q_DECL_DEPRECATED double gridOffsetX() const;
320 
325  Q_DECL_DEPRECATED void setGridOffsetY( double offset );
326 
330  Q_DECL_DEPRECATED double gridOffsetY() const;
331 
336  Q_DECL_DEPRECATED void setGridPen( const QPen& p );
337 
341  Q_DECL_DEPRECATED QPen gridPen() const;
342 
347  Q_DECL_DEPRECATED void setGridPenWidth( double w );
348 
353  Q_DECL_DEPRECATED void setGridPenColor( const QColor& c );
354 
359  Q_DECL_DEPRECATED void setGridAnnotationFont( const QFont& f );
360 
364  Q_DECL_DEPRECATED QFont gridAnnotationFont() const;
365 
370  Q_DECL_DEPRECATED void setAnnotationFontColor( const QColor& c );
371 
376  Q_DECL_DEPRECATED QColor annotationFontColor() const;
377 
382  Q_DECL_DEPRECATED void setGridAnnotationPrecision( int p );
383 
387  Q_DECL_DEPRECATED int gridAnnotationPrecision() const;
388 
393  Q_DECL_DEPRECATED void setShowGridAnnotation( bool show );
394 
398  Q_DECL_DEPRECATED bool showGridAnnotation() const;
399 
403  Q_DECL_DEPRECATED void setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border );
404 
408  Q_DECL_DEPRECATED GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const;
409 
414  Q_DECL_DEPRECATED void setAnnotationFrameDistance( double d );
415 
419  Q_DECL_DEPRECATED double annotationFrameDistance() const;
420 
424  Q_DECL_DEPRECATED void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
425 
429  Q_DECL_DEPRECATED GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;
430 
434  Q_DECL_DEPRECATED void setGridAnnotationFormat( GridAnnotationFormat f );
435 
439  Q_DECL_DEPRECATED GridAnnotationFormat gridAnnotationFormat() const;
440 
445  Q_DECL_DEPRECATED void setGridFrameStyle( GridFrameStyle style );
446 
450  Q_DECL_DEPRECATED GridFrameStyle gridFrameStyle() const;
451 
456  Q_DECL_DEPRECATED void setGridFrameWidth( double w );
457 
461  Q_DECL_DEPRECATED double gridFrameWidth() const;
462 
467  Q_DECL_DEPRECATED void setGridFramePenSize( double w );
468 
472  Q_DECL_DEPRECATED double gridFramePenSize() const;
473 
478  Q_DECL_DEPRECATED void setGridFramePenColor( const QColor& c );
479 
484  Q_DECL_DEPRECATED QColor gridFramePenColor() const;
485 
490  Q_DECL_DEPRECATED void setGridFrameFillColor1( const QColor& c );
491 
496  Q_DECL_DEPRECATED QColor gridFrameFillColor1() const;
497 
502  Q_DECL_DEPRECATED void setGridFrameFillColor2( const QColor& c );
503 
508  Q_DECL_DEPRECATED QColor gridFrameFillColor2() const;
509 
514  Q_DECL_DEPRECATED void setCrossLength( double l );
515 
519  Q_DECL_DEPRECATED double crossLength();
520 
524  Q_DECL_DEPRECATED void setGridLineSymbol( QgsLineSymbolV2* symbol );
525 
529  Q_DECL_DEPRECATED QgsLineSymbolV2* gridLineSymbol();
530 
534  Q_DECL_DEPRECATED QPainter::CompositionMode gridBlendMode() const;
535 
539  Q_DECL_DEPRECATED void setGridBlendMode( QPainter::CompositionMode blendMode );
540 
547  QgsComposerMapGridStack* grids() { return mGridStack; }
548 
554  QgsComposerMapGrid* grid();
555 
562  QgsComposerMapOverviewStack* overviews() { return mOverviewStack; }
563 
569  QgsComposerMapOverview* overview();
570 
573  QRectF boundingRect() const;
574 
575  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
576  virtual void setFrameOutlineWidth( const double outlineWidth );
577 
582  Q_DECL_DEPRECATED void setRotation( double r );
583 
587  Q_DECL_DEPRECATED double rotation() const { return mMapRotation;}
588 
592  void setMapRotation( double r );
593 
600  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
601 
602  void updateItem();
603 
605  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
606 
607  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
608  bool drawCanvasItems() const { return mDrawCanvasItems; }
609 
611  double mapUnitsToMM() const;
612 
617  Q_DECL_DEPRECATED void setOverviewFrameMap( int mapId );
618 
623  Q_DECL_DEPRECATED int overviewFrameMapId() const;
624 
628  Q_DECL_DEPRECATED void setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol );
629 
633  Q_DECL_DEPRECATED QgsFillSymbolV2* overviewFrameMapSymbol();
634 
638  Q_DECL_DEPRECATED QPainter::CompositionMode overviewBlendMode() const;
639 
643  Q_DECL_DEPRECATED void setOverviewBlendMode( QPainter::CompositionMode blendMode );
644 
648  Q_DECL_DEPRECATED bool overviewInverted() const;
649 
653  Q_DECL_DEPRECATED void setOverviewInverted( bool inverted );
654 
658  Q_DECL_DEPRECATED bool overviewCentered() const;
659 
663  Q_DECL_DEPRECATED void setOverviewCentered( bool centered );
664 
667  void assignFreeId();
668 
673  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
678  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
683  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
684 
690  bool atlasDriven() const { return mAtlasDriven; }
691 
697  void setAtlasDriven( bool enabled );
698 
702  Q_DECL_DEPRECATED bool atlasFixedScale() const;
703 
707  Q_DECL_DEPRECATED void setAtlasFixedScale( bool fixed );
708 
717  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
718 
727  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
728 
735  double atlasMargin() const { return mAtlasMargin; }
736 
743  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
744 
746  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
747 
749  bool updatesEnabled() const { return mUpdatesEnabled; }
750 
756  int numberExportLayers() const;
757 
764  QPolygonF visibleExtentPolygon() const;
765 
766  //overriden to show "Map 1" type names
767  virtual QString displayName() const;
768 
770  QPolygonF transformedMapPolygon() const;
771 
773  QPointF mapToItemCoords( const QPointF& mapCoords ) const;
774 
775  Q_DECL_DEPRECATED void connectMapOverviewSignals();
776 
777  signals:
778  void extentChanged();
779 
781  void mapRotationChanged( double newRotation );
782 
784  void preparedForAtlas();
785 
786  public slots:
787 
789  void updateCachedImage( );
791  void renderModeUpdateCachedImage();
792 
795  void updateBoundingRect();
796 
799 
801 
802  private:
803 
805  int mId;
806 
807  QgsComposerMapGridStack* mGridStack;
808 
809  QgsComposerMapOverviewStack* mOverviewStack;
810 
811  // Map region in map units realy used for rendering
812  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
813  // so that full rectangle in paper is used.
814  QgsRectangle mExtent;
815 
816  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
817  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
818  // to manually tweak each atlas preview page without affecting the actual original map extent.
819  QgsRectangle mAtlasFeatureExtent;
820 
821  // Cache used in composer preview
822  QImage mCacheImage;
823 
824  // Is cache up to date
825  bool mCacheUpdated;
826 
828  PreviewMode mPreviewMode;
829 
831  int mNumCachedLayers;
832 
834  bool mDrawing;
835 
837  double mXOffset;
839  double mYOffset;
840 
842  double mMapRotation;
845  double mEvaluatedMapRotation;
846 
848  bool mKeepLayerSet;
849 
851  QStringList mLayerSet;
852 
854  bool mUpdatesEnabled;
855 
857  void connectUpdateSlot();
858 
860  void syncLayerSet();
861 
863  const QgsComposerMapGrid* constFirstMapGrid() const;
864 
866  const QgsComposerMapOverview* constFirstMapOverview() const;
867 
869  QRectF mCurrentRectangle;
870  QGraphicsView* mMapCanvas;
872  bool mDrawCanvasItems;
873 
876  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
877 
879  bool mAtlasDriven;
881  AtlasScalingMode mAtlasScalingMode;
883  double mAtlasMargin;
884 
885  void init();
886 
888  QStringList layersToRender() const;
889 
891  QgsRectangle transformedExtent() const;
892 
894  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
895 
897  void requestedExtent( QgsRectangle& extent );
901  void transformShift( double& xShift, double& yShift ) const;
902 
903  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
904  void drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
905  QPointF composerMapPosForItem( const QGraphicsItem* item ) const;
906 
907  enum PartType
908  {
909  Background,
910  Layer,
911  Grid,
912  OverviewMapExtent,
913  Frame,
914  SelectionBoxes
915  };
916 
918  bool shouldDrawPart( PartType part ) const;
919 
923  void refreshMapExtents();
924 
925  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
926 };
927 
928 #endif
929