QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsmapcanvas.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmapcanvas.h - description
3  -------------------
4  begin : Sun Jun 30 2002
5  copyright : (C) 2002 by Gary E.Sherman
6  email : sherman at mrcc.com
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 
18 #ifndef QGSMAPCANVAS_H
19 #define QGSMAPCANVAS_H
20 
21 #include "qgsconfig.h"
22 
23 #include "qgsexpressioncontext.h"
24 #include "qgsfeature.h"
25 #include "qgsmessagebar.h"
26 #include "qgsrectangle.h"
27 #include "qgspoint.h"
28 #include "qgis.h"
29 
30 #include <QDomDocument>
31 #include <QGraphicsView>
32 #include <QtCore>
33 
34 #include "qgsmapsettings.h" // TEMPORARY
35 #include "qgsprevieweffect.h" //for QgsPreviewEffect::PreviewMode
36 
37 #ifdef HAVE_TOUCH
38 #include <QGestureEvent>
39 #endif
40 
41 class QWheelEvent;
42 class QPixmap;
43 class QPaintEvent;
44 class QKeyEvent;
45 class ResizeEvent;
46 
47 class QColor;
48 class QDomDocument;
49 class QPaintDevice;
50 class QMouseEvent;
51 class QRubberBand;
52 class QGraphicsScene;
53 
54 class QgsMapToPixel;
55 class QgsMapLayer;
56 class QgsHighlight;
57 class QgsVectorLayer;
58 
59 class QgsLabelingResults;
60 class QgsMapRenderer;
63 class QgsMapSettings;
64 class QgsMapCanvasMap;
66 class QgsMapTool;
67 class QgsSnappingUtils;
68 class QgsRubberBand;
69 
75 class GUI_EXPORT QgsMapCanvasLayer
76 {
77  public:
78  QgsMapCanvasLayer( QgsMapLayer* layer, bool visible = true, bool isInOverview = false )
79  : mLayer( layer )
80  , mVisible( visible )
81  , mInOverview( isInOverview )
82  {}
83 
84  void setVisible( bool visible ) { mVisible = visible; }
85  void setInOverview( bool isInOverview ) { mInOverview = isInOverview; }
86 
87  bool isVisible() const { return mVisible; }
88  bool isInOverview() const { return mInOverview; }
89 
90  QgsMapLayer* layer() { return mLayer; }
91  const QgsMapLayer* layer() const { return mLayer; }
92 
93  private:
94  QgsMapLayer* mLayer;
95 
97  bool mVisible;
98 
100  bool mInOverview;
101 };
102 
103 
109 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
110 {
111  Q_OBJECT
112 
113  public:
114 
115  //TODO QGIS 3 remove this
116  enum WheelAction { WheelZoom, WheelZoomAndRecenter, WheelZoomToMouseCursor, WheelNothing };
117 
119  QgsMapCanvas( QWidget * parent = nullptr, const char *name = nullptr );
120 
122  ~QgsMapCanvas();
123 
126  double magnificationFactor() const;
127 
128  void setLayerSet( QList<QgsMapCanvasLayer>& layers );
129 
130  void setCurrentLayer( QgsMapLayer* layer );
131 
132  // ### QGIS 3: make QgsMapCanvas independent from overview
133  void updateOverview();
134 
135  // ### QGIS 3: make QgsMapCanvas independent from overview
136  void enableOverviewMode( QgsMapOverviewCanvas* overview );
137 
140  const QgsMapSettings& mapSettings() const;
141 
144  void setCrsTransformEnabled( bool enabled );
145 
148  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );
149 
152  const QgsLabelingResults* labelingResults() const;
153 
156  void setCachingEnabled( bool enabled );
157 
160  bool isCachingEnabled() const;
161 
164  void clearCache();
165 
168  void refreshAllLayers();
169 
172  void setParallelRenderingEnabled( bool enabled );
173 
176  bool isParallelRenderingEnabled() const;
177 
180  void setMapUpdateInterval( int timeMilliseconds );
181 
184  int mapUpdateInterval() const;
185 
187  Q_DECL_DEPRECATED QgsMapCanvasMap *map();
188 
194  Q_DECL_DEPRECATED QgsMapRenderer *mapRenderer();
195 
198  Q_DECL_DEPRECATED QPaintDevice &canvasPaintDevice();
199 
201  double scale();
202 
205  Q_DECL_DEPRECATED void clear();
206 
208  double mapUnitsPerPixel() const;
209 
211  QgsRectangle extent() const;
213  QgsRectangle fullExtent() const;
214 
216  void setExtent( const QgsRectangle &r, bool magnified = false );
217 
220  double rotation() const;
221 
224  void setRotation( double degrees );
225 
228  void setCenter( const QgsPoint& center );
229 
232  QgsPoint center() const;
233 
235  void zoomToFullExtent();
236 
238  void zoomToPreviousExtent();
239 
241  void zoomToNextExtent();
242 
243  // ! Clears the list of extents and sets current extent as first item
244  void clearExtentHistory();
245 
249  void zoomToSelected( QgsVectorLayer* layer = nullptr );
250 
254  void zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
255 
257  void panToSelected( QgsVectorLayer* layer = nullptr );
258 
260  void setMapTool( QgsMapTool* mapTool );
261 
268  void unsetMapTool( QgsMapTool* mapTool );
269 
271  QgsMapTool* mapTool();
272 
274  virtual void setCanvasColor( const QColor & _newVal );
276  virtual QColor canvasColor() const;
277 
279  void setSelectionColor( const QColor& color );
281 
283  void updateScale();
284 
286  Q_DECL_DEPRECATED void updateFullExtent() {}
288 
290  QgsMapLayer *layer( int index );
291 
293  int layerCount() const;
294 
296  QList<QgsMapLayer*> layers() const;
297 
306  void freeze( bool frz = true );
307 
313  bool isFrozen();
314 
317  Q_DECL_DEPRECATED void setDirty( bool _dirty );
318 
321  Q_DECL_DEPRECATED bool isDirty() const;
322 
324  void setMapUnits( QGis::UnitType mapUnits );
325 
327  QGis::UnitType mapUnits() const;
328 
331  QMap<QString, QString> layerStyleOverrides() const;
332 
335  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
336 
338  const QgsMapToPixel* getCoordinateTransform();
339 
341  bool isDrawing();
342 
344  QgsMapLayer* currentLayer();
345 
348  Q_DECL_DEPRECATED void setWheelAction( WheelAction action, double factor = 2 );
349 
351  void setWheelFactor( double factor );
352 
354  void zoomScale( double scale );
355 
358  void zoomByFactor( double scaleFactor, const QgsPoint *center = nullptr );
359 
361  void zoomWithCenter( int x, int y, bool zoomIn );
362 
366  bool scaleLocked() const { return mScaleLocked;}
367 
369  void enableAntiAliasing( bool theFlag );
370 
372  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
373 
375  void enableMapTileRendering( bool theFlag );
376 
379  Q_DECL_DEPRECATED void useImageToRender( bool theFlag );
380 
381  // following 2 methods should be moved elsewhere or changed to private
382  // currently used by pan map tool
384  void panActionEnd( QPoint releasePoint );
385 
387  void panAction( QMouseEvent * event );
388 
390  QPoint mouseLastXY();
391 
396  void setPreviewModeEnabled( bool previewEnabled );
397 
403  bool previewModeEnabled() const;
404 
412  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
413 
420  QgsPreviewEffect::PreviewMode previewMode() const;
421 
429  QgsSnappingUtils* snappingUtils() const;
437  void setSnappingUtils( QgsSnappingUtils* utils );
438 
447  void setExpressionContextScope( const QgsExpressionContextScope& scope ) { mExpressionContextScope = scope; }
448 
455  QgsExpressionContextScope& expressionContextScope() { return mExpressionContextScope; }
456 
462  const QgsExpressionContextScope& expressionContextScope() const { return mExpressionContextScope; }
463 
466  void setSegmentationTolerance( double tolerance );
469  void setSegmentationToleranceType( QgsAbstractGeometryV2::SegmentationToleranceType type );
470 
471  public slots:
472 
474  void refresh();
475 
477  void selectionChangedSlot();
478 
480  void saveAsImage( const QString& theFileName, QPixmap * QPixmap = nullptr, const QString& = "PNG" );
481 
483  void layerStateChange();
484 
486  void layerCrsChange();
487 
489  void setRenderFlag( bool theFlag );
491  bool renderFlag() {return mRenderFlag;}
492 
494  bool hasCrsTransformEnabled();
495 
497  Q_DECL_DEPRECATED void updateMap();
498 
501  void stopRendering();
502 
504  Q_DECL_DEPRECATED void showError( QgsMapLayer * mapLayer );
505 
507  void readProject( const QDomDocument & );
508 
510  void writeProject( QDomDocument & );
511 
513  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
514 
517  static bool rotationEnabled();
518 
521  static void enableRotation( bool enabled );
522 
527  void setMagnificationFactor( double factor );
528 
532  void setScaleLocked( bool isLocked );
533 
535  void zoomIn();
536 
538  void zoomOut();
539 
540  private slots:
542  void mapToolDestroyed();
543 
545  void rendererJobFinished();
546 
547  void mapUpdateTimeout();
548 
549  void refreshMap();
550 
551  signals:
553  Q_DECL_DEPRECATED void setProgress( int, int );
555 
558  void xyCoordinates( const QgsPoint &p );
559 
561  void scaleChanged( double );
562 
564  void extentsChanged();
565 
568  void rotationChanged( double );
569 
572  void magnificationChanged( double );
573 
581  void renderComplete( QPainter * );
585 
586  // ### QGIS 3: renamte to mapRefreshFinished()
588  void mapCanvasRefreshed();
589 
590  // ### QGIS 3: rename to mapRefreshStarted()
592  void renderStarting();
593 
595  void layersChanged();
596 
598  void keyPressed( QKeyEvent * e );
599 
601  void keyReleased( QKeyEvent * e );
602 
604  void mapToolSet( QgsMapTool *tool );
605 
609  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
610 
611  // ### QGIS 3: remove the signal
613  void selectionChanged( QgsMapLayer * layer );
614 
616  void zoomLastStatusChanged( bool );
617 
619  void zoomNextStatusChanged( bool );
620 
623  void hasCrsTransformEnabledChanged( bool flag );
624 
627  void destinationCrsChanged();
628 
631  void mapUnitsChanged();
632 
635  void currentLayerChanged( QgsMapLayer* layer );
636 
639  void layerStyleOverridesChanged();
640 
642  void messageEmitted( const QString& title, const QString& message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
643 
644  protected:
645 #ifdef HAVE_TOUCH
646  bool event( QEvent * e ) override;
648 #endif
649 
651  void keyPressEvent( QKeyEvent * e ) override;
652 
654  void keyReleaseEvent( QKeyEvent * e ) override;
655 
657  void mouseDoubleClickEvent( QMouseEvent * e ) override;
658 
660  void mouseMoveEvent( QMouseEvent * e ) override;
661 
663  void mousePressEvent( QMouseEvent * e ) override;
664 
666  void mouseReleaseEvent( QMouseEvent * e ) override;
667 
669  void wheelEvent( QWheelEvent * e ) override;
670 
672  void resizeEvent( QResizeEvent * e ) override;
673 
675  void paintEvent( QPaintEvent * e ) override;
676 
678  void dragEnterEvent( QDragEnterEvent * e ) override;
679 
681  void moveCanvasContents( bool reset = false );
682 
685  void zoomToFeatureExtent( QgsRectangle& rect );
686 
688  void updateCanvasItemPositions();
689 
692 
695 
696 #if 0
697 
700  void connectNotify( const char * signal ) override;
701 #endif
702  void updateDatumTransformEntries();
704 
705  private:
707 
713  QgsMapCanvas( QgsMapCanvas const & );
714 
716  QgsMapSettings mSettings;
717 
719  QgsMapRenderer* mMapRenderer;
720 
722  QgsMapCanvasMap* mMap;
723 
725  QgsMapOverviewCanvas* mMapOverview;
726 
728  bool mFrozen;
729 
731  bool mRefreshScheduled;
732 
734  bool mRenderFlag;
735 
737  QgsMapLayer* mCurrentLayer;
738 
740  QGraphicsScene* mScene;
741 
743  QgsMapTool* mMapTool;
744 
746  QgsMapTool* mLastNonZoomMapTool;
747 
749  QList <QgsRectangle> mLastExtent;
750  int mLastExtentIndex;
751 
753  double mWheelZoomFactor;
754 
756  QTimer mMapUpdateTimer;
757 
760 
762  bool mJobCancelled;
763 
765  QgsLabelingResults* mLabelingResults;
766 
768  bool mUseParallelRendering;
769 
771  bool mDrawRenderingStats;
772 
774  QgsMapRendererCache* mCache;
775 
776  QTimer *mResizeTimer;
777 
778  QgsPreviewEffect* mPreviewEffect;
779 
780  QgsRectangle imageRect( const QImage& img, const QgsMapSettings& mapSettings );
781 
782  QgsSnappingUtils* mSnappingUtils;
783 
785  bool mScaleLocked;
786 
787  QgsExpressionContextScope mExpressionContextScope;
788 
790  QRect mZoomRect;
791 
793  bool mZoomDragging;
794 
796  QScopedPointer< QgsRubberBand > mZoomRubberBand;
797 
798  QCursor mZoomCursor;
799 
802  void updateMapSize();
803 
808  void beginZoomRect( QPoint pos );
809 
814  void endZoomRect( QPoint pos );
815 
816  friend class TestQgsMapCanvas;
817 
818 }; // class QgsMapCanvas
820 
821 
822 
823 
834 {
835  Q_OBJECT
836  public:
838 
839  protected slots:
840  void onExtentC2R();
841  void onExtentR2C();
842 
843  void onMapUnitsC2R();
844  void onMapUnitsR2C();
845 
847  void onMapRotationC2R();
849  void onMapRotationR2C();
850 
851  void onCrsTransformC2R();
852  void onCrsTransformR2C();
853 
854  void onDestCrsC2R();
855  void onDestCrsR2C();
856 
857  void onLayersC2R();
858 
859  protected:
862 
864 };
865 
866 
867 #endif
virtual void mouseMoveEvent(QMouseEvent *event)
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Base class for all map layer types.
Definition: qgsmaplayer.h:49
void setInOverview(bool isInOverview)
Definition: qgsmapcanvas.h:85
virtual void keyReleaseEvent(QKeyEvent *event)
A widget that displays an overview map.
virtual void dragEnterEvent(QDragEnterEvent *event)
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:515
A class that stores visibility and presence in overview flags together with pointer to the layer...
Definition: qgsmapcanvas.h:75
virtual void mouseReleaseEvent(QMouseEvent *event)
A non GUI class for rendering a map layer set onto a QPainter.
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:109
void setVisible(bool visible)
Definition: qgsmapcanvas.h:84
A graphics effect which can be applied to a widget to simulate various printing and color blindness m...
bool renderFlag()
State of render suppression flag.
Definition: qgsmapcanvas.h:491
The QgsMapSettings class contains configuration for rendering of the map.
Deprecated to be deleted, stuff from here should be moved elsewhere.
QgsMapRenderer * mRenderer
Definition: qgsmapcanvas.h:861
void scale(qreal sx, qreal sy)
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
A rectangular graphics item representing the map on the canvas.
virtual bool event(QEvent *event)
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:32
QgsMapCanvas * mCanvas
Definition: qgsmapcanvas.h:860
Enable anti-aliasing for map rendering.
A class for highlight features on the map.
Definition: qgshighlight.h:37
virtual void keyPressEvent(QKeyEvent *event)
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setExpressionContextScope(const QgsExpressionContextScope &scope)
Sets an expression context scope for the map canvas.
Definition: qgsmapcanvas.h:447
A class to represent a point.
Definition: qgspoint.h:117
virtual void connectNotify(const char *signal)
QScopedPointer< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:691
virtual void mousePressEvent(QMouseEvent *event)
Abstract base class for all map tools.
Definition: qgsmaptool.h:50
virtual void paintEvent(QPaintEvent *event)
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:516
bool isVisible() const
Definition: qgsmapcanvas.h:87
bool isInOverview() const
Definition: qgsmapcanvas.h:88
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:455
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:372
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:366
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:462
Intermediate base class adding functionality that allows client to query the rendered image...
Class for storing a coordinate reference system (CRS)
This class has all the configuration of snapping and can return answers to snapping queries...
QgsMapCanvasLayer(QgsMapLayer *layer, bool visible=true, bool isInOverview=false)
Definition: qgsmapcanvas.h:78
UnitType
Map units that qgis supports.
Definition: qgis.h:159
Class that stores computed placement from labeling engine.
This class is responsible for keeping cache of rendered images of individual layers.
virtual void mouseDoubleClickEvent(QMouseEvent *event)
const QgsMapLayer * layer() const
Definition: qgsmapcanvas.h:91
Class that does synchronization between QgsMapCanvas and its associated QgsMapRenderer: ...
Definition: qgsmapcanvas.h:833
Represents a vector layer which manages a vector based data sets.
QgsMapLayer * layer()
Definition: qgsmapcanvas.h:90
virtual void wheelEvent(QWheelEvent *event)
virtual void resizeEvent(QResizeEvent *event)