QGIS API Documentation  2.99.0-Master (b8fd1fd)
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 "qgis.h"
28 
29 #include <QDomDocument>
30 #include <QGraphicsView>
31 #include <QtCore>
32 
33 #include "qgsmapsettings.h" // TEMPORARY
34 #include "qgsprevieweffect.h" //for QgsPreviewEffect::PreviewMode
35 
36 #include <QGestureEvent>
37 #include "qgis_gui.h"
38 
39 class QWheelEvent;
40 class QPixmap;
41 class QPaintEvent;
42 class QKeyEvent;
43 class ResizeEvent;
44 
45 class QColor;
46 class QDomDocument;
47 class QPaintDevice;
48 class QMouseEvent;
49 class QRubberBand;
50 class QGraphicsScene;
51 
52 class QgsMapToPixel;
53 class QgsMapLayer;
54 class QgsHighlight;
55 class QgsVectorLayer;
56 
57 class QgsLabelingResults;
60 class QgsMapSettings;
61 class QgsMapCanvasMap;
63 class QgsMapTool;
64 class QgsSnappingUtils;
65 class QgsRubberBand;
67 
72 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
73 {
74  Q_OBJECT
75  Q_PROPERTY( QString theme READ theme WRITE setTheme NOTIFY themeChanged )
76 
77  public:
78 
80  QgsMapCanvas( QWidget *parent = nullptr );
81 
82  ~QgsMapCanvas();
83 
86  double magnificationFactor() const;
87 
99  void setLayers( const QList<QgsMapLayer *> &layers );
100 
101  void setCurrentLayer( QgsMapLayer *layer );
102 
105  const QgsMapSettings &mapSettings() const;
106 
109  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
110 
113  const QgsLabelingResults *labelingResults() const;
114 
117  void setCachingEnabled( bool enabled );
118 
121  bool isCachingEnabled() const;
122 
125  void clearCache();
126 
129  void refreshAllLayers();
130 
140  void waitWhileRendering();
141 
144  void setParallelRenderingEnabled( bool enabled );
145 
148  bool isParallelRenderingEnabled() const;
149 
152  void setMapUpdateInterval( int timeMilliseconds );
153 
156  int mapUpdateInterval() const;
157 
159  double scale();
160 
162  double mapUnitsPerPixel() const;
163 
165  QgsRectangle extent() const;
167  QgsRectangle fullExtent() const;
168 
170  void setExtent( const QgsRectangle &r, bool magnified = false );
171 
174  double rotation() const;
175 
178  void setRotation( double degrees );
179 
182  void setCenter( const QgsPoint &center );
183 
186  QgsPoint center() const;
187 
189  void zoomToFullExtent();
190 
192  void zoomToPreviousExtent();
193 
195  void zoomToNextExtent();
196 
197  // ! Clears the list of extents and sets current extent as first item
198  void clearExtentHistory();
199 
203  void zoomToSelected( QgsVectorLayer *layer = nullptr );
204 
208  void zoomToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
209 
213  void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
214 
216  void panToSelected( QgsVectorLayer *layer = nullptr );
217 
219  void setMapTool( QgsMapTool *mapTool );
220 
227  void unsetMapTool( QgsMapTool *mapTool );
228 
230  QgsMapTool *mapTool();
231 
233  void setCanvasColor( const QColor &_newVal );
235  QColor canvasColor() const;
236 
239  void setSelectionColor( const QColor &color );
240 
242  void updateScale();
243 
245  QgsMapLayer *layer( int index );
246 
248  int layerCount() const;
249 
254  QList<QgsMapLayer *> layers() const;
255 
265  void freeze( bool frozen = true );
266 
274  bool isFrozen() const;
275 
284  bool renderFlag() const { return mRenderFlag; }
285 
290  QgsUnitTypes::DistanceUnit mapUnits() const;
291 
297  QMap<QString, QString> layerStyleOverrides() const;
298 
310  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
311 
330  void setTheme( const QString &theme );
331 
337  QString theme() const { return mTheme; }
338 
340  const QgsMapToPixel *getCoordinateTransform();
341 
343  bool isDrawing();
344 
346  QgsMapLayer *currentLayer();
347 
349  void setWheelFactor( double factor );
350 
352  void zoomScale( double scale );
353 
356  void zoomByFactor( double scaleFactor, const QgsPoint *center = nullptr );
357 
359  void zoomWithCenter( int x, int y, bool zoomIn );
360 
363  void zoomToFeatureExtent( QgsRectangle &rect );
364 
368  bool scaleLocked() const { return mScaleLocked;}
369 
371  void enableAntiAliasing( bool flag );
372 
374  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
375 
377  void enableMapTileRendering( bool flag );
378 
379  // following 2 methods should be moved elsewhere or changed to private
380  // currently used by pan map tool
382  void panActionEnd( QPoint releasePoint );
383 
385  void panAction( QMouseEvent *event );
386 
388  QPoint mouseLastXY();
389 
394  void setPreviewModeEnabled( bool previewEnabled );
395 
401  bool previewModeEnabled() const;
402 
410  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
411 
418  QgsPreviewEffect::PreviewMode previewMode() const;
419 
427  QgsSnappingUtils *snappingUtils() const;
428 
436  void setSnappingUtils( QgsSnappingUtils *utils );
437 
446  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
447 
454  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
455 
461  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; }
462 
465  void setSegmentationTolerance( double tolerance );
466 
469  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
470 
475  QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
476 
482  bool annotationsVisible() const { return mAnnotationsVisible; }
483 
489  void setAnnotationsVisible( bool visible );
490 
491  public slots:
492 
494  void refresh();
495 
497  void selectionChangedSlot();
498 
500  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
501 
503  void layerStateChange();
504 
506  void layerCrsChange();
507 
515  void setRenderFlag( bool flag );
516 
519  void stopRendering();
520 
522  void readProject( const QDomDocument & );
523 
525  void writeProject( QDomDocument & );
526 
528  void getDatumTransformInfo( const QgsMapLayer *ml, const QString &srcAuthId, const QString &destAuthId );
529 
534  void setMagnificationFactor( double factor );
535 
539  void setScaleLocked( bool isLocked );
540 
542  void zoomIn();
543 
545  void zoomOut();
546 
547  private slots:
549  void mapToolDestroyed();
550 
552  void rendererJobFinished();
553 
554  void mapUpdateTimeout();
555 
556  void refreshMap();
557 
558  void mapThemeChanged( const QString &theme );
559 
560  signals:
561 
564  void xyCoordinates( const QgsPoint &p );
565 
567  void scaleChanged( double );
568 
570  void extentsChanged();
571 
574  void rotationChanged( double );
575 
578  void magnificationChanged( double );
579 
587  void renderComplete( QPainter * );
591 
592  // ### QGIS 3: renamte to mapRefreshFinished()
594  void mapCanvasRefreshed();
595 
596  // ### QGIS 3: rename to mapRefreshStarted()
598  void renderStarting();
599 
601  void layersChanged();
602 
604  void keyPressed( QKeyEvent *e );
605 
607  void keyReleased( QKeyEvent *e );
608 
612  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
613 
614  // ### QGIS 3: remove the signal
616  void selectionChanged( QgsMapLayer *layer );
617 
619  void zoomLastStatusChanged( bool );
620 
622  void zoomNextStatusChanged( bool );
623 
626  void destinationCrsChanged();
627 
630  void currentLayerChanged( QgsMapLayer *layer );
631 
634  void layerStyleOverridesChanged();
635 
641  void themeChanged( const QString &theme );
642 
644  void messageEmitted( const QString &title, const QString &message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
645 
646  protected:
647 
649  bool event( QEvent *e ) override;
650 
652  void keyPressEvent( QKeyEvent *e ) override;
653 
655  void keyReleaseEvent( QKeyEvent *e ) override;
656 
658  void mouseDoubleClickEvent( QMouseEvent *e ) override;
659 
661  void mouseMoveEvent( QMouseEvent *e ) override;
662 
664  void mousePressEvent( QMouseEvent *e ) override;
665 
667  void mouseReleaseEvent( QMouseEvent *e ) override;
668 
670  void wheelEvent( QWheelEvent *e ) override;
671 
673  void resizeEvent( QResizeEvent *e ) override;
674 
676  void paintEvent( QPaintEvent *e ) override;
677 
679  void dragEnterEvent( QDragEnterEvent *e ) override;
680 
682  void moveCanvasContents( bool reset = false );
683 
685  void updateCanvasItemPositions();
686 
689 
691  std::unique_ptr<CanvasProperties> mCanvasProperties;
692 
693 #if 0
694 
698  void connectNotify( const char *signal ) override;
699 #endif
700  void updateDatumTransformEntries();
702 
703  private slots:
704 
705  void layerRepaintRequested( bool deferred );
706 
707  void autoRefreshTriggered();
708 
709  void updateAutoRefreshTimer();
710 
711  void projectThemesChanged();
712 
713  private:
715 
722  QgsMapCanvas( QgsMapCanvas const & );
723 
725  QgsMapSettings mSettings;
726 
728  QgsMapCanvasMap *mMap = nullptr;
729 
731  bool mFrozen;
732 
734  bool mRefreshScheduled;
735 
737  bool mRenderFlag;
738 
740  QgsMapLayer *mCurrentLayer = nullptr;
741 
743  QGraphicsScene *mScene = nullptr;
744 
746  QgsMapTool *mMapTool = nullptr;
747 
749  QgsMapTool *mLastNonZoomMapTool = nullptr;
750 
752  QList <QgsRectangle> mLastExtent;
753  int mLastExtentIndex;
754 
756  double mWheelZoomFactor;
757 
759  QTimer mMapUpdateTimer;
760 
762  QgsMapRendererQImageJob *mJob = nullptr;
763 
765  bool mJobCanceled;
766 
768  QgsLabelingResults *mLabelingResults = nullptr;
769 
771  bool mUseParallelRendering;
772 
774  bool mDrawRenderingStats;
775 
777  QgsMapRendererCache *mCache = nullptr;
778 
779  QTimer *mResizeTimer = nullptr;
780 
781  QgsPreviewEffect *mPreviewEffect = nullptr;
782 
783  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
784 
785  QgsSnappingUtils *mSnappingUtils = nullptr;
786 
788  bool mScaleLocked;
789 
790  QgsExpressionContextScope mExpressionContextScope;
791 
793  QRect mZoomRect;
794 
796  bool mZoomDragging;
797 
799  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
800 
801  QCursor mZoomCursor;
802 
803  QTimer mAutoRefreshTimer;
804 
805  QString mTheme;
806 
807  bool mAnnotationsVisible = true;
808 
811  void updateMapSize();
812 
817  void beginZoomRect( QPoint pos );
818 
823  void endZoomRect( QPoint pos );
824 
831  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
832 
833  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
834 
835  friend class TestQgsMapCanvas;
836 
837 }; // class QgsMapCanvas
838 
839 
840 
841 #endif
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:36
Base class for all map layer types.
Definition: qgsmaplayer.h:52
std::unique_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:688
A widget that displays an overview map.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:358
bool annotationsVisible() const
Returns true if annotations are visible within the map canvas.
Definition: qgsmapcanvas.h:482
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:72
A graphics effect which can be applied to a widget to simulate various printing and color blindness m...
The QgsMapSettings class contains configuration for rendering of the map.
Deprecated to be deleted, stuff from here should be moved elsewhere.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:33
bool renderFlag() const
Returns true if canvas render is disabled as a result of user disabling renders via the GUI...
Definition: qgsmapcanvas.h:284
Enable anti-aliasing for map rendering.
A class for highlight features on the map.
Definition: qgshighlight.h:36
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:446
A class to represent a point.
Definition: qgspoint.h:37
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:42
Abstract base class for all map tools.
Definition: qgsmaptool.h:49
QString theme() const
Returns the map&#39;s theme shown in the canvas, if set.
Definition: qgsmapcanvas.h:337
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:454
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:374
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:368
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:461
Intermediate base class adding functionality that allows client to query the rendered image...
This class represents a coordinate reference system (CRS).
This class has all the configuration of snapping and can return answers to snapping queries...
Class that stores computed placement from labeling engine.
This class is responsible for keeping cache of rendered images resulting from a map rendering job...
Represents a vector layer which manages a vector based data sets.
An interactive map canvas item which displays a QgsAnnotation.