QGIS API Documentation  3.13.0-Master (740be229cb)
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 #include "qgis_sip.h"
23 
24 #include "qgsexpressioncontext.h"
25 #include "qgsrectangle.h"
26 #include "qgsfeatureid.h"
27 #include "qgsgeometry.h"
28 #include "qgscustomdrophandler.h"
29 #include "qgstemporalrangeobject.h"
30 
31 #include <QDomDocument>
32 #include <QGraphicsView>
33 #include <QtCore>
34 
35 #include "qgsmapsettings.h" // TEMPORARY
36 #include "qgsprevieweffect.h" //for QgsPreviewEffect::PreviewMode
37 
38 #include <QGestureEvent>
39 #include "qgis_gui.h"
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;
62 class QgsMapSettings;
63 class QgsMapCanvasMap;
65 class QgsMapTool;
66 class QgsSnappingUtils;
67 class QgsRubberBand;
70 
72 
78 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
79 {
80 
81 #ifdef SIP_RUN
83  if ( qobject_cast<QgsMapCanvas *>( sipCpp ) != nullptr )
84  sipType = sipType_QgsMapCanvas;
85  else
86  sipType = nullptr;
87  SIP_END
88 #endif
89 
90  Q_OBJECT
91  Q_PROPERTY( QString theme READ theme WRITE setTheme NOTIFY themeChanged )
92  Q_PROPERTY( bool previewJobsEnabled READ previewJobsEnabled WRITE setPreviewJobsEnabled )
93 
94  public:
95 
97  QgsMapCanvas( QWidget *parent SIP_TRANSFERTHIS = nullptr );
98 
99  ~QgsMapCanvas() override;
100 
105  double magnificationFactor() const;
106 
118  void setLayers( const QList<QgsMapLayer *> &layers );
119 
120  void setCurrentLayer( QgsMapLayer *layer );
121 
126  const QgsMapSettings &mapSettings() const SIP_KEEPREFERENCE;
127 
134  void setTemporalController( QgsTemporalController *controller );
135 
140  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
141 
146  void setMapSettingsFlags( QgsMapSettings::Flags flags );
147 
152  const QgsLabelingResults *labelingResults() const;
153 
158  void setCachingEnabled( bool enabled );
159 
164  bool isCachingEnabled() const;
165 
170  void clearCache();
171 
181  void waitWhileRendering();
182 
187  void setParallelRenderingEnabled( bool enabled );
188 
193  bool isParallelRenderingEnabled() const;
194 
199  void setMapUpdateInterval( int timeMilliseconds );
200 
205  int mapUpdateInterval() const;
206 
211  double scale() const;
212 
214  double mapUnitsPerPixel() const;
215 
217  QgsRectangle extent() const;
219  QgsRectangle fullExtent() const;
220 
232  void setExtent( const QgsRectangle &r, bool magnified = false );
233 
242  bool setReferencedExtent( const QgsReferencedRectangle &extent ) SIP_THROW( QgsCsException );
243 
248  double rotation() const;
249 
254  void setRotation( double degrees );
255 
260  void setCenter( const QgsPointXY &center );
261 
266  QgsPointXY center() const;
267 
269  void zoomToFullExtent();
270 
272  void zoomToPreviousExtent();
273 
275  void zoomToNextExtent();
276 
277  // ! Clears the list of extents and sets current extent as first item
278  void clearExtentHistory();
279 
280 
285  void zoomToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
286 
293  void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids, bool alwaysRecenter = true );
294 
296  void panToSelected( QgsVectorLayer *layer = nullptr );
297 
310  void flashFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids,
311  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
312  int flashes = 3, int duration = 500 );
313 
326  void flashGeometries( const QList< QgsGeometry > &geometries, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(),
327  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
328  int flashes = 3, int duration = 500 );
329 
331  void setMapTool( QgsMapTool *mapTool, bool clean = false );
332 
340  void unsetMapTool( QgsMapTool *mapTool );
341 
343  QgsMapTool *mapTool();
344 
346  void setCanvasColor( const QColor &_newVal );
348  QColor canvasColor() const;
349 
354  void setSelectionColor( const QColor &color );
355 
360  QColor selectionColor() const;
361 
363  void updateScale();
364 
366  QgsMapLayer *layer( int index );
367 
369  int layerCount() const;
370 
375  QList<QgsMapLayer *> layers() const;
376 
386  void freeze( bool frozen = true );
387 
395  bool isFrozen() const;
396 
405  bool renderFlag() const { return mRenderFlag; }
406 
411  QgsUnitTypes::DistanceUnit mapUnits() const;
412 
418  QMap<QString, QString> layerStyleOverrides() const;
419 
431  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
432 
451  void setTheme( const QString &theme );
452 
458  QString theme() const { return mTheme; }
459 
461  const QgsMapToPixel *getCoordinateTransform();
462 
464  bool isDrawing();
465 
467  QgsMapLayer *currentLayer();
468 
470  void setWheelFactor( double factor );
471 
479  void zoomScale( double scale, bool ignoreScaleLock = false );
480 
488  void zoomByFactor( double scaleFactor, const QgsPointXY *center = nullptr, bool ignoreScaleLock = false );
489 
491  void zoomWithCenter( int x, int y, bool zoomIn );
492 
497  void zoomToFeatureExtent( QgsRectangle &rect );
498 
504  bool scaleLocked() const { return mScaleLocked;}
505 
507  void enableAntiAliasing( bool flag );
508 
510  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
511 
513  void enableMapTileRendering( bool flag );
514 
515  // following 2 methods should be moved elsewhere or changed to private
516  // currently used by pan map tool
518  void panActionEnd( QPoint releasePoint );
519 
520 #ifndef SIP_RUN
521 
527  void panActionStart( QPoint releasePoint );
528 #endif
529 
531  void panAction( QMouseEvent *event );
532 
534  QPoint mouseLastXY();
535 
541  void setPreviewModeEnabled( bool previewEnabled );
542 
549  bool previewModeEnabled() const;
550 
559  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
560 
568  QgsPreviewEffect::PreviewMode previewMode() const;
569 
578  QgsSnappingUtils *snappingUtils() const;
579 
588  void setSnappingUtils( QgsSnappingUtils *utils );
589 
600  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
601 
610  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
611 
619  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP
620 
627  QgsExpressionContextScope *defaultExpressionContextScope() SIP_FACTORY;
628 
632  void setSegmentationTolerance( double tolerance );
633 
637  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
638 
643  QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
644 
650  bool annotationsVisible() const { return mAnnotationsVisible; }
651 
657  void setAnnotationsVisible( bool visible );
658 
663  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
664 
669  const QgsLabelingEngineSettings &labelingEngineSettings() const;
670 
679  bool previewJobsEnabled() const;
680 
689  void setPreviewJobsEnabled( bool enabled );
690 
696  void setCustomDropHandlers( const QVector<QPointer<QgsCustomDropHandler >> &handlers ) SIP_SKIP;
697 
708  void setTemporalRange( const QgsDateTimeRange &range );
709 
716  const QgsDateTimeRange &temporalRange() const;
717 
718  public slots:
719 
721  void refresh();
722 
732  void refreshAllLayers();
733 
742  void redrawAllLayers();
743 
745  void selectionChangedSlot();
746 
748  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
749 
751  void layerStateChange();
752 
760  void setRenderFlag( bool flag );
761 
766  void stopRendering();
767 
769  void readProject( const QDomDocument & );
770 
772  void writeProject( QDomDocument & );
773 
780  void setMagnificationFactor( double factor );
781 
787  void setScaleLocked( bool isLocked );
788 
790  void zoomIn();
791 
793  void zoomOut();
794 
799  void zoomToSelected( QgsVectorLayer *layer = nullptr );
800 
806  void setZoomResolutions( const QList<double> &resolutions ) { mZoomResolutions = resolutions; }
807 
813  const QList<double> &zoomResolutions() const { return mZoomResolutions; }
814 
815  private slots:
817  void mapToolDestroyed();
818 
820  void rendererJobFinished();
821 
823  void previewJobFinished();
824 
825  void mapUpdateTimeout();
826 
827  void refreshMap();
828 
829  void mapThemeChanged( const QString &theme );
830 
831  signals:
832 
836  void xyCoordinates( const QgsPointXY &p );
837 
839  void scaleChanged( double );
840 
842  void extentsChanged();
843 
848  void rotationChanged( double );
849 
854  void magnificationChanged( double );
855 
860  void canvasColorChanged();
861 
876  void renderComplete( QPainter * );
877 
878  // ### QGIS 3: renamte to mapRefreshFinished()
880  void mapCanvasRefreshed();
881 
882  // ### QGIS 3: rename to mapRefreshStarted()
884  void renderStarting();
885 
887  void layersChanged();
888 
890  void keyPressed( QKeyEvent *e );
891 
893  void keyReleased( QKeyEvent *e );
894 
899  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
900 
901 
903  void selectionChanged( QgsVectorLayer *layer );
904 
906  void zoomLastStatusChanged( bool );
907 
909  void zoomNextStatusChanged( bool );
910 
915  void destinationCrsChanged();
916 
921  void transformContextChanged();
922 
927  void currentLayerChanged( QgsMapLayer *layer );
928 
933  void layerStyleOverridesChanged();
934 
940  void themeChanged( const QString &theme );
941 
943  void messageEmitted( const QString &title, const QString &message, Qgis::MessageLevel = Qgis::Info );
944 
952  void renderErrorOccurred( const QString &error, QgsMapLayer *layer );
953 
964  void panDistanceBearingChanged( double distance, QgsUnitTypes::DistanceUnit unit, double bearing );
965 
970  void tapAndHoldGestureOccurred( const QgsPointXY &mapPoint, QTapAndHoldGesture *gesture );
971 
977  void temporalRangeChanged();
978 
979  protected:
980 
981  bool event( QEvent *e ) override;
982  void keyPressEvent( QKeyEvent *e ) override;
983  void keyReleaseEvent( QKeyEvent *e ) override;
984  void mouseDoubleClickEvent( QMouseEvent *e ) override;
985  void mouseMoveEvent( QMouseEvent *e ) override;
986  void mousePressEvent( QMouseEvent *e ) override;
987  void mouseReleaseEvent( QMouseEvent *e ) override;
988  void wheelEvent( QWheelEvent *e ) override;
989  void resizeEvent( QResizeEvent *e ) override;
990  void paintEvent( QPaintEvent *e ) override;
991  void dragEnterEvent( QDragEnterEvent *e ) override;
992 
994  void moveCanvasContents( bool reset = false );
995 
996  void dropEvent( QDropEvent *event ) override;
997 
998 
1001 
1003  std::unique_ptr<CanvasProperties> mCanvasProperties;
1004 
1005 #if 0
1006 
1011  void connectNotify( const char *signal ) override;
1012 #endif
1013 
1014  protected slots:
1016  void updateCanvasItemPositions();
1017 
1018  private slots:
1019 
1020  void layerRepaintRequested( bool deferred );
1021 
1022  void autoRefreshTriggered();
1023 
1024  void updateAutoRefreshTimer();
1025 
1026  void projectThemesChanged();
1027 
1028  void startPreviewJob( int number );
1029 
1030  private:
1031 
1033  QgsMapSettings mSettings;
1034 
1036  QgsMapCanvasMap *mMap = nullptr;
1037 
1042  QgsTemporalController *mController = nullptr;
1043 
1045  bool mFrozen = false;
1046 
1048  bool mRefreshScheduled = false;
1049 
1051  bool mRenderFlag = true;
1052 
1054  QPointer< QgsMapLayer > mCurrentLayer;
1055 
1057  QGraphicsScene *mScene = nullptr;
1058 
1060  QgsMapTool *mMapTool = nullptr;
1061 
1063  QgsMapTool *mLastNonZoomMapTool = nullptr;
1064 
1066  QList <QgsRectangle> mLastExtent;
1067  int mLastExtentIndex = -1;
1068 
1070  double mWheelZoomFactor = 2.0;
1071 
1073  QTimer mMapUpdateTimer;
1074 
1076  QgsMapRendererQImageJob *mJob = nullptr;
1077 
1079  bool mJobCanceled = false;
1080 
1082  QgsLabelingResults *mLabelingResults = nullptr;
1083 
1085  bool mUseParallelRendering = false;
1086 
1088  bool mDrawRenderingStats = false;
1089 
1091  QgsMapRendererCache *mCache = nullptr;
1092 
1093  QTimer *mResizeTimer = nullptr;
1094  QTimer *mRefreshTimer = nullptr;
1095 
1096  QgsPreviewEffect *mPreviewEffect = nullptr;
1097 
1098  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
1099 
1100  QgsSnappingUtils *mSnappingUtils = nullptr;
1101 
1102  QList< QgsMapRendererQImageJob * > mPreviewJobs;
1103 
1105  bool mScaleLocked = false;
1106 
1107  QgsExpressionContextScope mExpressionContextScope;
1108 
1110  QRect mZoomRect;
1111 
1113  bool mZoomDragging = false;
1114 
1116  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
1117 
1118  QCursor mZoomCursor;
1119 
1120  QTimer mAutoRefreshTimer;
1121 
1122  QTimer mPreviewTimer;
1123  QMetaObject::Connection mPreviewTimerConnection;
1124 
1125  QString mTheme;
1126 
1127  QgsPointXY mCursorPoint;
1128 
1129  bool mAnnotationsVisible = true;
1130 
1131  bool mUsePreviewJobs = false;
1132 
1133  QHash< QString, int > mLastLayerRenderTime;
1134 
1135  QVector<QPointer<QgsCustomDropHandler >> mDropHandlers;
1136 
1137  QgsDistanceArea mDa;
1138  QList<double> mZoomResolutions;
1139 
1144  QgsPointXY cursorPoint() const;
1145 
1150  void updateMapSize();
1151 
1157  void beginZoomRect( QPoint pos );
1158 
1164  void endZoomRect( QPoint pos );
1165 
1173  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
1174 
1175  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
1176 
1177  void startPreviewJobs();
1178  void stopPreviewJobs();
1179  void schedulePreviewJob( int number );
1180 
1184  bool panOperationInProgress();
1185 
1186  int nextZoomLevel( const QList<double> &resolutions, bool zoomIn = true ) const;
1187  double zoomInFactor() const;
1188  double zoomOutFactor() const;
1189 
1190  friend class TestQgsMapCanvas;
1191 
1192 }; // class QgsMapCanvas
1193 
1194 // clazy:excludeall=qstring-allocations
1195 
1196 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Base class for all map layer types.
Definition: qgsmaplayer.h:80
std::unique_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
const QList< double > & zoomResolutions() const
Definition: qgsmapcanvas.h:813
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
A widget that displays an overview map.
bool annotationsVisible() const
Returns true if annotations are visible within the map canvas.
Definition: qgsmapcanvas.h:650
A class to represent a 2D point.
Definition: qgspointxy.h:43
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:122
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
const QgsCoordinateReferenceSystem & crs
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:88
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:78
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:37
#define SIP_SKIP
Definition: qgis_sip.h:126
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:49
Enable anti-aliasing for map rendering.
A class for highlight features on the map.
Definition: qgshighlight.h:56
#define SIP_END
Definition: qgis_sip.h:189
#define SIP_KEEPREFERENCE
Definition: qgis_sip.h:86
void setZoomResolutions(const QList< double > &resolutions)
Set a list of resolutions (map units per pixel) to which to "snap to" when zooming the map...
Definition: qgsmapcanvas.h:806
A QgsRectangle with associated coordinate reference system.
#define SIP_FACTORY
Definition: qgis_sip.h:76
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:600
A controller base class for temporal objects, contains a signal for notifying updates of the objects ...
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:67
Abstract base class for all map tools.
Definition: qgsmaptool.h:62
A general purpose distance and area calculator, capable of performing ellipsoid based calculations...
QString theme() const
Returns the map&#39;s theme shown in the canvas, if set.
Definition: qgsmapcanvas.h:458
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:610
bool antiAliasingEnabled() const
true if antialiasing is enabled
Definition: qgsmapcanvas.h:510
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:504
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:619
Intermediate base class adding functionality that allows client to query the rendered image...
Stores global configuration for labeling engine.
This class represents a coordinate reference system (CRS).
This class has all the configuration of snapping and can return answers to snapping queries...
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
#define SIP_THROW(name)
Definition: qgis_sip.h:184
Class that stores computed placement from labeling engine.
This class is responsible for keeping cache of rendered images resulting from a map rendering job...
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
Represents a vector layer which manages a vector based data sets.
An interactive map canvas item which displays a QgsAnnotation.