QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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"
31 
32 #include <QDomDocument>
33 #include <QGraphicsView>
34 #include <QtCore>
35 
36 #include "qgsmapsettings.h" // TEMPORARY
37 #include "qgsprevieweffect.h" //for QgsPreviewEffect::PreviewMode
38 
39 #include <QGestureEvent>
40 #include "qgis_gui.h"
41 
42 class QWheelEvent;
43 class QPixmap;
44 class QPaintEvent;
45 class QKeyEvent;
46 class ResizeEvent;
47 
48 class QColor;
49 class QDomDocument;
50 class QPaintDevice;
51 class QMouseEvent;
52 class QRubberBand;
53 class QGraphicsScene;
54 
55 class QgsMapToPixel;
56 class QgsMapLayer;
57 class QgsHighlight;
58 class QgsVectorLayer;
59 
60 class QgsLabelingResults;
63 class QgsMapSettings;
64 class QgsMapCanvasMap;
66 class QgsMapTool;
67 class QgsSnappingUtils;
68 class QgsRubberBand;
71 
73 
74 class QMenu;
75 class QgsMapMouseEvent;
76 
77 
83 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
84 {
85 
86 #ifdef SIP_RUN
88  if ( qobject_cast<QgsMapCanvas *>( sipCpp ) != nullptr )
89  sipType = sipType_QgsMapCanvas;
90  else
91  sipType = nullptr;
92  SIP_END
93 #endif
94 
95  Q_OBJECT
96  Q_PROPERTY( QString theme READ theme WRITE setTheme NOTIFY themeChanged )
97  Q_PROPERTY( bool previewJobsEnabled READ previewJobsEnabled WRITE setPreviewJobsEnabled )
98 
99  public:
100 
102  QgsMapCanvas( QWidget *parent SIP_TRANSFERTHIS = nullptr );
103 
104  ~QgsMapCanvas() override;
105 
110  double magnificationFactor() const;
111 
123  void setLayers( const QList<QgsMapLayer *> &layers );
124 
125  void setCurrentLayer( QgsMapLayer *layer );
126 
131  const QgsMapSettings &mapSettings() const SIP_KEEPREFERENCE;
132 
139  void setTemporalController( QgsTemporalController *controller );
140 
147  const QgsTemporalController *temporalController() const;
148 
153  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
154 
159  void setMapSettingsFlags( QgsMapSettings::Flags flags );
160 
165  const QgsLabelingResults *labelingResults() const;
166 
171  void setCachingEnabled( bool enabled );
172 
177  bool isCachingEnabled() const;
178 
183  void clearCache();
184 
194  void waitWhileRendering();
195 
200  void setParallelRenderingEnabled( bool enabled );
201 
206  bool isParallelRenderingEnabled() const;
207 
212  void setMapUpdateInterval( int timeMilliseconds );
213 
218  int mapUpdateInterval() const;
219 
224  double scale() const;
225 
227  double mapUnitsPerPixel() const;
228 
230  QgsRectangle extent() const;
232  QgsRectangle fullExtent() const;
233 
245  void setExtent( const QgsRectangle &r, bool magnified = false );
246 
255  bool setReferencedExtent( const QgsReferencedRectangle &extent ) SIP_THROW( QgsCsException );
256 
261  double rotation() const;
262 
267  void setRotation( double degrees );
268 
273  void setCenter( const QgsPointXY &center );
274 
279  QgsPointXY center() const;
280 
282  void zoomToFullExtent();
283 
285  void zoomToPreviousExtent();
286 
288  void zoomToNextExtent();
289 
290  // ! Clears the list of extents and sets current extent as first item
291  void clearExtentHistory();
292 
293 
298  void zoomToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
299 
306  void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids, bool alwaysRecenter = true );
307 
309  void panToSelected( QgsVectorLayer *layer = nullptr );
310 
323  void flashFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids,
324  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
325  int flashes = 3, int duration = 500 );
326 
339  void flashGeometries( const QList< QgsGeometry > &geometries, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(),
340  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
341  int flashes = 3, int duration = 500 );
342 
344  void setMapTool( QgsMapTool *mapTool, bool clean = false );
345 
353  void unsetMapTool( QgsMapTool *mapTool );
354 
356  QgsMapTool *mapTool();
357 
359  void setCanvasColor( const QColor &_newVal );
361  QColor canvasColor() const;
362 
367  void setSelectionColor( const QColor &color );
368 
373  QColor selectionColor() const;
374 
376  void updateScale();
377 
379  QgsMapLayer *layer( int index );
380 
382  int layerCount() const;
383 
388  QList<QgsMapLayer *> layers() const;
389 
399  void freeze( bool frozen = true );
400 
408  bool isFrozen() const;
409 
418  bool renderFlag() const { return mRenderFlag; }
419 
424  QgsUnitTypes::DistanceUnit mapUnits() const;
425 
431  QMap<QString, QString> layerStyleOverrides() const;
432 
444  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
445 
464  void setTheme( const QString &theme );
465 
471  QString theme() const { return mTheme; }
472 
474  const QgsMapToPixel *getCoordinateTransform();
475 
477  bool isDrawing();
478 
480  QgsMapLayer *currentLayer();
481 
483  void setWheelFactor( double factor );
484 
492  void zoomScale( double scale, bool ignoreScaleLock = false );
493 
501  void zoomByFactor( double scaleFactor, const QgsPointXY *center = nullptr, bool ignoreScaleLock = false );
502 
504  void zoomWithCenter( int x, int y, bool zoomIn );
505 
510  void zoomToFeatureExtent( QgsRectangle &rect );
511 
517  bool scaleLocked() const { return mScaleLocked;}
518 
520  void enableAntiAliasing( bool flag );
521 
523  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
524 
526  void enableMapTileRendering( bool flag );
527 
528  // following 2 methods should be moved elsewhere or changed to private
529  // currently used by pan map tool
531  void panActionEnd( QPoint releasePoint );
532 
533 #ifndef SIP_RUN
534 
540  void panActionStart( QPoint releasePoint );
541 #endif
542 
544  void panAction( QMouseEvent *event );
545 
547  QPoint mouseLastXY();
548 
554  void setPreviewModeEnabled( bool previewEnabled );
555 
562  bool previewModeEnabled() const;
563 
572  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
573 
581  QgsPreviewEffect::PreviewMode previewMode() const;
582 
591  QgsSnappingUtils *snappingUtils() const;
592 
601  void setSnappingUtils( QgsSnappingUtils *utils );
602 
613  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
614 
623  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
624 
632  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP
633 
640  QgsExpressionContextScope *defaultExpressionContextScope() SIP_FACTORY;
641 
645  void setSegmentationTolerance( double tolerance );
646 
650  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
651 
656  QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
657 
663  bool annotationsVisible() const { return mAnnotationsVisible; }
664 
670  void setAnnotationsVisible( bool visible );
671 
676  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
677 
682  const QgsLabelingEngineSettings &labelingEngineSettings() const;
683 
692  bool previewJobsEnabled() const;
693 
702  void setPreviewJobsEnabled( bool enabled );
703 
709  void setCustomDropHandlers( const QVector<QPointer<QgsCustomDropHandler >> &handlers ) SIP_SKIP;
710 
721  void setTemporalRange( const QgsDateTimeRange &range );
722 
729  const QgsDateTimeRange &temporalRange() const;
730 
742  void installInteractionBlocker( QgsMapCanvasInteractionBlocker *blocker );
743 
751  void removeInteractionBlocker( QgsMapCanvasInteractionBlocker *blocker );
752 
758  bool allowInteraction( QgsMapCanvasInteractionBlocker::Interaction interaction ) const;
759 
760  public slots:
761 
763  void refresh();
764 
774  void refreshAllLayers();
775 
784  void redrawAllLayers();
785 
787  void selectionChangedSlot();
788 
790  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
791 
793  void layerStateChange();
794 
802  void setRenderFlag( bool flag );
803 
808  void stopRendering();
809 
811  void readProject( const QDomDocument & );
812 
814  void writeProject( QDomDocument & );
815 
824  void setMagnificationFactor( double factor, const QgsPointXY *center = nullptr );
825 
831  void setScaleLocked( bool isLocked );
832 
834  void zoomIn();
835 
837  void zoomOut();
838 
843  void zoomToSelected( QgsVectorLayer *layer = nullptr );
844 
850  void setZoomResolutions( const QList<double> &resolutions ) { mZoomResolutions = resolutions; }
851 
857  const QList<double> &zoomResolutions() const { return mZoomResolutions; }
858 
859  private slots:
861  void mapToolDestroyed();
862 
864  void rendererJobFinished();
865 
867  void previewJobFinished();
868 
869  void mapUpdateTimeout();
870 
871  void refreshMap();
872 
873  void mapThemeChanged( const QString &theme );
875  void mapThemeRenamed( const QString &theme, const QString &newTheme );
876 
877  signals:
878 
882  void xyCoordinates( const QgsPointXY &p );
883 
885  void scaleChanged( double );
886 
888  void extentsChanged();
889 
894  void rotationChanged( double );
895 
900  void magnificationChanged( double );
901 
906  void canvasColorChanged();
907 
917  // TODO: deprecate when decorations are reimplemented as map canvas items
918 
923  void renderComplete( QPainter * );
924 
925  // ### QGIS 3: renamte to mapRefreshFinished()
927  void mapCanvasRefreshed();
928 
929  // ### QGIS 3: rename to mapRefreshStarted()
931  void renderStarting();
932 
934  void layersChanged();
935 
937  void keyPressed( QKeyEvent *e );
938 
940  void keyReleased( QKeyEvent *e );
941 
946  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
947 
948 
950  void selectionChanged( QgsVectorLayer *layer );
951 
953  void zoomLastStatusChanged( bool );
954 
956  void zoomNextStatusChanged( bool );
957 
962  void destinationCrsChanged();
963 
968  void transformContextChanged();
969 
974  void currentLayerChanged( QgsMapLayer *layer );
975 
980  void layerStyleOverridesChanged();
981 
987  void themeChanged( const QString &theme );
988 
990  void messageEmitted( const QString &title, const QString &message, Qgis::MessageLevel = Qgis::Info );
991 
999  void renderErrorOccurred( const QString &error, QgsMapLayer *layer );
1000 
1011  void panDistanceBearingChanged( double distance, QgsUnitTypes::DistanceUnit unit, double bearing );
1012 
1017  void tapAndHoldGestureOccurred( const QgsPointXY &mapPoint, QTapAndHoldGesture *gesture );
1018 
1024  void temporalRangeChanged();
1025 
1026  protected:
1027 
1028  bool event( QEvent *e ) override;
1029  void keyPressEvent( QKeyEvent *e ) override;
1030  void keyReleaseEvent( QKeyEvent *e ) override;
1031  void mouseDoubleClickEvent( QMouseEvent *e ) override;
1032  void mouseMoveEvent( QMouseEvent *e ) override;
1033  void mousePressEvent( QMouseEvent *e ) override;
1034  void mouseReleaseEvent( QMouseEvent *e ) override;
1035  void wheelEvent( QWheelEvent *e ) override;
1036  void resizeEvent( QResizeEvent *e ) override;
1037  void paintEvent( QPaintEvent *e ) override;
1038  void dragEnterEvent( QDragEnterEvent *e ) override;
1039 
1041  void moveCanvasContents( bool reset = false );
1042 
1043  void dropEvent( QDropEvent *event ) override;
1044 
1045 
1048 
1050  std::unique_ptr<CanvasProperties> mCanvasProperties;
1051 
1052 #if 0
1053 
1058  void connectNotify( const char *signal ) override;
1059 #endif
1060 
1061  protected slots:
1063  void updateCanvasItemPositions();
1064 
1065  private slots:
1066 
1067  void layerRepaintRequested( bool deferred );
1068 
1069  void autoRefreshTriggered();
1070 
1071  void updateAutoRefreshTimer();
1072 
1073  void projectThemesChanged();
1074 
1075  void startPreviewJob( int number );
1076 
1077  private:
1078 
1080  QgsMapSettings mSettings;
1081 
1083  QgsMapCanvasMap *mMap = nullptr;
1084 
1089  QgsTemporalController *mController = nullptr;
1090 
1092  bool mFrozen = false;
1093 
1095  bool mRefreshScheduled = false;
1096 
1098  bool mRefreshAfterJob = false;
1099 
1101  bool mRenderFlag = true;
1102 
1104  QPointer< QgsMapLayer > mCurrentLayer;
1105 
1107  QGraphicsScene *mScene = nullptr;
1108 
1110  QgsMapTool *mMapTool = nullptr;
1111 
1113  QgsMapTool *mLastNonZoomMapTool = nullptr;
1114 
1116  QMenu *mMenu = nullptr;
1117 
1119  QList <QgsRectangle> mLastExtent;
1120  int mLastExtentIndex = -1;
1121 
1123  double mWheelZoomFactor = 2.0;
1124 
1126  QTimer mMapUpdateTimer;
1127 
1129  QgsMapRendererQImageJob *mJob = nullptr;
1130 
1132  bool mJobCanceled = false;
1133 
1135  QgsLabelingResults *mLabelingResults = nullptr;
1136 
1138  bool mUseParallelRendering = false;
1139 
1141  bool mDrawRenderingStats = false;
1142 
1144  QgsMapRendererCache *mCache = nullptr;
1145 
1146  QTimer *mResizeTimer = nullptr;
1147  QTimer *mRefreshTimer = nullptr;
1148 
1149  QgsPreviewEffect *mPreviewEffect = nullptr;
1150 
1151  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
1152 
1153  QgsSnappingUtils *mSnappingUtils = nullptr;
1154 
1155  QList< QgsMapRendererQImageJob * > mPreviewJobs;
1156 
1158  bool mScaleLocked = false;
1159 
1160  QgsExpressionContextScope mExpressionContextScope;
1161 
1163  QRect mZoomRect;
1164 
1166  bool mZoomDragging = false;
1167 
1169  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
1170 
1171  QCursor mZoomCursor;
1172 
1173  QTimer mAutoRefreshTimer;
1174 
1175  QTimer mPreviewTimer;
1176  QMetaObject::Connection mPreviewTimerConnection;
1177 
1178  QString mTheme;
1179 
1180  QgsPointXY mCursorPoint;
1181 
1182  bool mAnnotationsVisible = true;
1183 
1184  bool mUsePreviewJobs = false;
1185 
1186  QHash< QString, int > mLastLayerRenderTime;
1187 
1188  QVector<QPointer<QgsCustomDropHandler >> mDropHandlers;
1189 
1190  QgsDistanceArea mDa;
1191  QList<double> mZoomResolutions;
1192 
1193  QList< QgsMapCanvasInteractionBlocker * > mInteractionBlockers;
1194 
1199  QgsPointXY cursorPoint() const;
1200 
1205  void updateMapSize();
1206 
1212  void beginZoomRect( QPoint pos );
1213 
1219  void endZoomRect( QPoint pos );
1220 
1228  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
1229 
1230  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
1231 
1232  void startPreviewJobs();
1233  void stopPreviewJobs();
1234  void schedulePreviewJob( int number );
1235 
1239  bool panOperationInProgress();
1240 
1241  int nextZoomLevel( const QList<double> &resolutions, bool zoomIn = true ) const;
1242  double zoomInFactor() const;
1243  double zoomOutFactor() const;
1244 
1249  void clearTemporalCache();
1250 
1251  void showContextMenu( QgsMapMouseEvent *event );
1252 
1253  friend class TestQgsMapCanvas;
1254 
1255 }; // class QgsMapCanvas
1256 
1257 // clazy:excludeall=qstring-allocations
1258 
1259 #endif
QgsMapCanvasInteractionBlocker
Definition: qgsmapcanvasinteractionblocker.h:29
QgsMapCanvasAnnotationItem
Definition: qgsmapcanvasannotationitem.h:39
qgsrectangle.h
QgsReferencedRectangle
Definition: qgsreferencedgeometry.h:72
qgstemporalrangeobject.h
QgsRubberBand
Definition: qgsrubberband.h:49
QgsMapCanvas::antiAliasingEnabled
bool antiAliasingEnabled() const
true if antialiasing is enabled
Definition: qgsmapcanvas.h:523
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsMapCanvas
Definition: qgsmapcanvas.h:83
QgsMapCanvas::theme
QString theme() const
Returns the map's theme shown in the canvas, if set.
Definition: qgsmapcanvas.h:471
QgsLabelingResults
Definition: qgspallabeling.h:1224
qgsfeatureid.h
qgsmapsettings.h
QgsUnitTypes::DistanceUnit
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:67
QgsRectangle
Definition: qgsrectangle.h:41
QgsMapRendererCache
Definition: qgsmaprenderercache.h:42
SIP_KEEPREFERENCE
#define SIP_KEEPREFERENCE
Definition: qgis_sip.h:86
QgsHighlight
Definition: qgshighlight.h:57
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsMapTool
Definition: qgsmaptool.h:63
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
QgsPreviewEffect::PreviewMode
PreviewMode
Definition: qgsprevieweffect.h:49
qgsexpressioncontext.h
QgsCsException
Definition: qgsexception.h:65
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
SIP_THROW
#define SIP_THROW(name)
Definition: qgis_sip.h:184
Qgis::Info
@ Info
Definition: qgis.h:103
QgsLabelingEngineSettings
Definition: qgslabelingenginesettings.h:30
QgsMapCanvasInteractionBlocker::Interaction
Interaction
Available interactions to block.
Definition: qgsmapcanvasinteractionblocker.h:36
QgsMapCanvas::expressionContextScope
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:623
qgis_sip.h
QgsMapSettings::Antialiasing
@ Antialiasing
Enable anti-aliasing for map rendering.
Definition: qgsmapsettings.h:302
QgsTemporalController
Definition: qgstemporalcontroller.h:35
QgsMapCanvas::mCanvasProperties
std::unique_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:1047
QgsMapOverviewCanvas
Definition: qgsmapoverviewcanvas.h:38
QgsMapCanvas::zoomResolutions
const QList< double > & zoomResolutions() const
Definition: qgsmapcanvas.h:857
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsSnappingUtils
Definition: qgssnappingutils.h:49
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:71
QgsExpressionContextScope
Single scope for storing variables and functions for use within a QgsExpressionContext....
Definition: qgsexpressioncontext.h:111
QgsPointXY
Definition: qgspointxy.h:43
QgsMapMouseEvent
Definition: qgsmapmouseevent.h:35
QgsMapRendererQImageJob
Definition: qgsmaprendererjob.h:435
QgsMapCanvas::setZoomResolutions
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:850
qgsgeometry.h
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:101
QgsGeometry
Definition: qgsgeometry.h:122
QgsMapToPixel
Definition: qgsmaptopixel.h:37
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsMapLayer
Definition: qgsmaplayer.h:81
qgsprevieweffect.h
QgsMapCanvas::setExpressionContextScope
void setExpressionContextScope(const QgsExpressionContextScope &scope)
Sets an expression context scope for the map canvas.
Definition: qgsmapcanvas.h:613
QgsMapCanvas::CanvasProperties
Definition: qgsmapcanvas.cpp:93
qgscustomdrophandler.h
QgsDistanceArea
Definition: qgsdistancearea.h:49
QgsPreviewEffect
Definition: qgsprevieweffect.h:31
QgsMapCanvas::scaleLocked
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:517
QgsMapSettings
Definition: qgsmapsettings.h:86
SIP_END
#define SIP_END
Definition: qgis_sip.h:189
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
qgsmapcanvasinteractionblocker.h
QgsMapCanvas::expressionContextScope
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:632