QGIS API Documentation  2.8.2-Wien
 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 
47 class CORE_EXPORT QgsComposerMap : public QgsComposerItem
48 {
49  Q_OBJECT
50 
51  public:
53  QgsComposerMap( QgsComposition *composition, int x, int y, int width, int height );
55  QgsComposerMap( QgsComposition *composition );
56  virtual ~QgsComposerMap();
57 
59  virtual int type() const override { return ComposerMap; }
60 
63  {
64  Cache = 0, // Use raster cache
65  Render, // Render the map
66  Rectangle // Display only rectangle
67  };
68 
69  //grid enums are moved to QgsComposerMapGrid
70  //TODO - remove for QGIS 3.0
71  enum GridStyle
72  {
73  Solid = 0, //solid lines
74  Cross, //only draw line crossings
76  FrameAnnotationsOnly
77  };
78 
80  {
81  InsideMapFrame = 0,
83  Disabled
84  };
85 
87  {
88  Horizontal = 0,
91  BoundaryDirection
92  };
93 
95  {
96  Decimal = 0,
103  DegreeMinuteSecondPadded
104  };
105 
107  {
108  NoGridFrame = 0,
109  Zebra, // black/white pattern
113  LineBorder
114  };
115 
117  enum Border
118  {
122  Top
123  };
124 
128  {
129  Fixed, /*< The current scale of the map is used for each feature of the atlas */
130  Predefined, /*< A scale is chosen from the predefined scales. The smallest scale from
131  the list of scales where the atlas feature is fully visible is chosen.
132  @see QgsAtlasComposition::setPredefinedScales.
133  @note This mode is only valid for polygon or line atlas coverage layers
134  */
135  Auto /*< The extent is adjusted so that each feature is fully visible.
136  A margin is applied around the center @see setAtlasMargin
137  @note This mode is only valid for polygon or line atlas coverage layers*/
138  };
139 
147  void draw( QPainter *painter, const QgsRectangle& extent, const QSizeF& size, double dpi, double* forceWidthScale = 0 );
148 
150  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) override;
151 
153  void cache();
154 
157  QgsMapSettings mapSettings( const QgsRectangle& extent, const QSizeF& size, int dpi ) const;
158 
160  int id() const {return mId;}
161 
163  bool isDrawing() const {return mDrawing;}
164 
166  void resize( double dx, double dy );
167 
171  void moveContent( double dx, double dy ) override;
172 
179  Q_DECL_DEPRECATED void zoomContent( int delta, double x, double y ) override;
180 
187  virtual void zoomContent( const double factor, const QPointF point, const ZoomMode mode = QgsComposerItem::Zoom ) override;
188 
190  void setSceneRect( const QRectF& rectangle ) override;
191 
193  double scale() const;
194 
196  void setNewScale( double scaleDenominator, bool forceUpdate = true );
197 
204  void setNewExtent( const QgsRectangle& extent );
205 
214  void zoomToExtent( const QgsRectangle& extent );
215 
219  void setNewAtlasFeatureExtent( const QgsRectangle& extent );
220 
224  Q_DECL_DEPRECATED void toggleAtlasPreview() {}
225 
232  QgsRectangle* currentMapExtent();
233  const QgsRectangle* currentMapExtent() const;
234 
235  PreviewMode previewMode() const {return mPreviewMode;}
236  void setPreviewMode( PreviewMode m );
237 
239  bool keepLayerSet() const {return mKeepLayerSet;}
241  void setKeepLayerSet( bool enabled ) {mKeepLayerSet = enabled;}
242 
244  QStringList layerSet() const {return mLayerSet;}
246  void setLayerSet( const QStringList& layerSet ) {mLayerSet = layerSet;}
248  void storeCurrentLayerSet();
249 
251  bool keepLayerStyles() const { return mKeepLayerStyles; }
253  void setKeepLayerStyles( bool enabled ) { mKeepLayerStyles = enabled; }
254 
256  QMap<QString, QString> layerStyleOverrides() const { return mLayerStyleOverrides; }
258  void setLayerStyleOverrides( const QMap<QString, QString>& overrides ) { mLayerStyleOverrides = overrides; }
260  void storeCurrentLayerStyles();
261 
262  // Set cache outdated
263  void setCacheUpdated( bool u = false );
264 
265  QgsRectangle extent() const {return mExtent;}
266 
268  Q_DECL_DEPRECATED const QgsMapRenderer* mapRenderer() const;
269 
271  void setOffset( double xOffset, double yOffset );
272 
274  bool containsWMSLayer() const;
275 
277  bool containsAdvancedEffects() const;
278 
283  bool writeXML( QDomElement& elem, QDomDocument & doc ) const override;
284 
289  bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) override;
290 
294  Q_DECL_DEPRECATED void setGridEnabled( bool enabled );
295 
299  Q_DECL_DEPRECATED bool gridEnabled() const;
300 
304  Q_DECL_DEPRECATED void setGridStyle( GridStyle style );
305 
309  Q_DECL_DEPRECATED GridStyle gridStyle() const;
310 
314  Q_DECL_DEPRECATED void setGridIntervalX( double interval );
315 
319  Q_DECL_DEPRECATED double gridIntervalX() const;
320 
324  Q_DECL_DEPRECATED void setGridIntervalY( double interval );
325 
329  Q_DECL_DEPRECATED double gridIntervalY() const;
330 
334  Q_DECL_DEPRECATED void setGridOffsetX( double offset );
335 
339  Q_DECL_DEPRECATED double gridOffsetX() const;
340 
344  Q_DECL_DEPRECATED void setGridOffsetY( double offset );
345 
349  Q_DECL_DEPRECATED double gridOffsetY() const;
350 
354  Q_DECL_DEPRECATED void setGridPen( const QPen& p );
355 
359  Q_DECL_DEPRECATED QPen gridPen() const;
360 
364  Q_DECL_DEPRECATED void setGridPenWidth( double w );
365 
369  Q_DECL_DEPRECATED void setGridPenColor( const QColor& c );
370 
374  Q_DECL_DEPRECATED void setGridAnnotationFont( const QFont& f );
375 
379  Q_DECL_DEPRECATED QFont gridAnnotationFont() const;
380 
384  Q_DECL_DEPRECATED void setAnnotationFontColor( const QColor& c );
385 
389  Q_DECL_DEPRECATED QColor annotationFontColor() const;
390 
394  Q_DECL_DEPRECATED void setGridAnnotationPrecision( int p );
395 
399  Q_DECL_DEPRECATED int gridAnnotationPrecision() const;
400 
404  Q_DECL_DEPRECATED void setShowGridAnnotation( bool show );
405 
409  Q_DECL_DEPRECATED bool showGridAnnotation() const;
410 
414  Q_DECL_DEPRECATED void setGridAnnotationPosition( GridAnnotationPosition p, QgsComposerMap::Border border );
415 
419  Q_DECL_DEPRECATED GridAnnotationPosition gridAnnotationPosition( QgsComposerMap::Border border ) const;
420 
424  Q_DECL_DEPRECATED void setAnnotationFrameDistance( double d );
425 
429  Q_DECL_DEPRECATED double annotationFrameDistance() const;
430 
434  Q_DECL_DEPRECATED void setGridAnnotationDirection( GridAnnotationDirection d, QgsComposerMap::Border border );
435 
439  Q_DECL_DEPRECATED GridAnnotationDirection gridAnnotationDirection( QgsComposerMap::Border border ) const;
440 
444  Q_DECL_DEPRECATED void setGridAnnotationFormat( GridAnnotationFormat f );
445 
449  Q_DECL_DEPRECATED GridAnnotationFormat gridAnnotationFormat() const;
450 
454  Q_DECL_DEPRECATED void setGridFrameStyle( GridFrameStyle style );
455 
459  Q_DECL_DEPRECATED GridFrameStyle gridFrameStyle() const;
460 
464  Q_DECL_DEPRECATED void setGridFrameWidth( double w );
465 
469  Q_DECL_DEPRECATED double gridFrameWidth() const;
470 
475  Q_DECL_DEPRECATED void setGridFramePenSize( double w );
476 
480  Q_DECL_DEPRECATED double gridFramePenSize() const;
481 
486  Q_DECL_DEPRECATED void setGridFramePenColor( const QColor& c );
487 
492  Q_DECL_DEPRECATED QColor gridFramePenColor() const;
493 
498  Q_DECL_DEPRECATED void setGridFrameFillColor1( const QColor& c );
499 
504  Q_DECL_DEPRECATED QColor gridFrameFillColor1() const;
505 
510  Q_DECL_DEPRECATED void setGridFrameFillColor2( const QColor& c );
511 
516  Q_DECL_DEPRECATED QColor gridFrameFillColor2() const;
517 
521  Q_DECL_DEPRECATED void setCrossLength( double l );
522 
526  Q_DECL_DEPRECATED double crossLength();
527 
531  Q_DECL_DEPRECATED void setGridLineSymbol( QgsLineSymbolV2* symbol );
532 
536  Q_DECL_DEPRECATED QgsLineSymbolV2* gridLineSymbol();
537 
541  Q_DECL_DEPRECATED QPainter::CompositionMode gridBlendMode() const;
542 
546  Q_DECL_DEPRECATED void setGridBlendMode( QPainter::CompositionMode blendMode );
547 
554  QgsComposerMapGridStack* grids() { return mGridStack; }
555 
561  QgsComposerMapGrid* grid();
562 
569  QgsComposerMapOverviewStack* overviews() { return mOverviewStack; }
570 
576  QgsComposerMapOverview* overview();
577 
579  QRectF boundingRect() const override;
580 
581  /* reimplement setFrameOutlineWidth, so that updateBoundingRect() is called after setting the frame width */
582  virtual void setFrameOutlineWidth( const double outlineWidth ) override;
583 
588  Q_DECL_DEPRECATED void setRotation( double r ) override;
589 
593  Q_DECL_DEPRECATED double rotation() const { return mMapRotation;}
594 
598  void setMapRotation( double r );
599 
606  double mapRotation( QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue ) const;
607 
608  void updateItem() override;
609 
611  void setMapCanvas( QGraphicsView* canvas ) { mMapCanvas = canvas; }
612 
613  void setDrawCanvasItems( bool b ) { mDrawCanvasItems = b; }
614  bool drawCanvasItems() const { return mDrawCanvasItems; }
615 
617  double mapUnitsToMM() const;
618 
622  Q_DECL_DEPRECATED void setOverviewFrameMap( int mapId );
623 
627  Q_DECL_DEPRECATED int overviewFrameMapId() const;
628 
632  Q_DECL_DEPRECATED void setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol );
633 
637  Q_DECL_DEPRECATED QgsFillSymbolV2* overviewFrameMapSymbol();
638 
642  Q_DECL_DEPRECATED QPainter::CompositionMode overviewBlendMode() const;
643 
647  Q_DECL_DEPRECATED void setOverviewBlendMode( QPainter::CompositionMode blendMode );
648 
652  Q_DECL_DEPRECATED bool overviewInverted() const;
653 
657  Q_DECL_DEPRECATED void setOverviewInverted( bool inverted );
658 
662  Q_DECL_DEPRECATED bool overviewCentered() const;
663 
667  Q_DECL_DEPRECATED void setOverviewCentered( bool centered );
668 
671  void assignFreeId();
672 
677  Q_DECL_DEPRECATED bool imageSizeConsideringRotation( double& width, double& height ) const;
682  Q_DECL_DEPRECATED bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
687  Q_DECL_DEPRECATED void sizeChangedByRotation( double& width, double& height );
688 
694  bool atlasDriven() const { return mAtlasDriven; }
695 
701  void setAtlasDriven( bool enabled );
702 
706  Q_DECL_DEPRECATED bool atlasFixedScale() const;
707 
711  Q_DECL_DEPRECATED void setAtlasFixedScale( bool fixed );
712 
721  AtlasScalingMode atlasScalingMode() const { return mAtlasScalingMode; }
722 
731  void setAtlasScalingMode( AtlasScalingMode mode ) { mAtlasScalingMode = mode; }
732 
742  double atlasMargin( const QgsComposerObject::PropertyValueType valueType = QgsComposerObject::EvaluatedValue );
743 
750  void setAtlasMargin( double margin ) { mAtlasMargin = margin; }
751 
753  void setUpdatesEnabled( bool enabled ) { mUpdatesEnabled = enabled; }
754 
756  bool updatesEnabled() const { return mUpdatesEnabled; }
757 
763  int numberExportLayers() const override;
764 
771  QPolygonF visibleExtentPolygon() const;
772 
773  //overriden to show "Map 1" type names
774  virtual QString displayName() const override;
775 
777  QPolygonF transformedMapPolygon() const;
778 
780  QPointF mapToItemCoords( const QPointF& mapCoords ) const;
781 
782  Q_DECL_DEPRECATED void connectMapOverviewSignals();
783 
786  void requestedExtent( QgsRectangle& extent ) const;
787 
788  signals:
789  void extentChanged();
790 
792  void mapRotationChanged( double newRotation );
793 
795  void preparedForAtlas();
796 
797  public slots:
798 
800  void updateCachedImage();
802  void renderModeUpdateCachedImage();
803 
805  void updateBoundingRect();
806 
809 
811 
812  private:
813 
815  int mId;
816 
817  QgsComposerMapGridStack* mGridStack;
818 
819  QgsComposerMapOverviewStack* mOverviewStack;
820 
821  // Map region in map units realy used for rendering
822  // It can be the same as mUserExtent, but it can be bigger in on dimension if mCalculate==Scale,
823  // so that full rectangle in paper is used.
824  QgsRectangle mExtent;
825 
826  // Current temporary map region in map units. This is overwritten when atlas feature changes. It's also
827  // used when the user changes the map extent and an atlas preview is enabled. This allows the user
828  // to manually tweak each atlas preview page without affecting the actual original map extent.
829  QgsRectangle mAtlasFeatureExtent;
830 
831  // Cache used in composer preview
832  QImage mCacheImage;
833 
834  // Is cache up to date
835  bool mCacheUpdated;
836 
838  PreviewMode mPreviewMode;
839 
841  int mNumCachedLayers;
842 
844  bool mDrawing;
845 
847  double mXOffset;
849  double mYOffset;
850 
852  double mMapRotation;
855  double mEvaluatedMapRotation;
856 
858  bool mKeepLayerSet;
859 
861  QStringList mLayerSet;
862 
863  bool mKeepLayerStyles;
865  QMap<QString, QString> mLayerStyleOverrides;
866 
868  bool mUpdatesEnabled;
869 
871  void connectUpdateSlot();
872 
874  void syncLayerSet();
875 
877  const QgsComposerMapGrid* constFirstMapGrid() const;
878 
880  const QgsComposerMapOverview* constFirstMapOverview() const;
881 
883  QRectF mCurrentRectangle;
884  QGraphicsView* mMapCanvas;
886  bool mDrawCanvasItems;
887 
890  void adjustExtentToItemShape( double itemWidth, double itemHeight, QgsRectangle& extent ) const;
891 
893  bool mAtlasDriven;
895  AtlasScalingMode mAtlasScalingMode;
897  double mAtlasMargin;
898 
899  void init();
900 
902  void updateToolTip();
903 
905  QStringList layersToRender() const;
906 
908  QgsRectangle transformedExtent() const;
909 
911  void mapPolygon( const QgsRectangle& extent, QPolygonF& poly ) const;
912 
916  void transformShift( double& xShift, double& yShift ) const;
917 
918  void drawCanvasItems( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
919  void drawCanvasItem( QGraphicsItem* item, QPainter* painter, const QStyleOptionGraphicsItem* itemStyle );
920  QPointF composerMapPosForItem( const QGraphicsItem* item ) const;
921 
922  enum PartType
923  {
924  Background,
925  Layer,
926  Grid,
927  OverviewMapExtent,
928  Frame,
929  SelectionBoxes
930  };
931 
933  bool shouldDrawPart( PartType part ) const;
934 
938  void refreshMapExtents();
939 
940  friend class QgsComposerMapOverview; //to access mXOffset, mYOffset
941 };
943 
944 #endif
945