QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 
30 #include <QDomDocument>
31 #include <QGraphicsView>
32 #include <QtCore>
33 
34 #include "qgsmapsettings.h" // TEMPORARY
35 #include "qgsprevieweffect.h" //for QgsPreviewEffect::PreviewMode
36 
37 #include <QGestureEvent>
38 #include "qgis_gui.h"
39 
40 class QWheelEvent;
41 class QPixmap;
42 class QPaintEvent;
43 class QKeyEvent;
44 class ResizeEvent;
45 
46 class QColor;
47 class QDomDocument;
48 class QPaintDevice;
49 class QMouseEvent;
50 class QRubberBand;
51 class QGraphicsScene;
52 
53 class QgsMapToPixel;
54 class QgsMapLayer;
55 class QgsHighlight;
56 class QgsVectorLayer;
57 
58 class QgsLabelingResults;
61 class QgsMapSettings;
62 class QgsMapCanvasMap;
64 class QgsMapTool;
65 class QgsSnappingUtils;
66 class QgsRubberBand;
69 
75 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
76 {
77 
78 #ifdef SIP_RUN
80  if ( qobject_cast<QgsMapCanvas *>( sipCpp ) != nullptr )
81  sipType = sipType_QgsMapCanvas;
82  else
83  sipType = nullptr;
84  SIP_END
85 #endif
86 
87  Q_OBJECT
88  Q_PROPERTY( QString theme READ theme WRITE setTheme NOTIFY themeChanged )
89  Q_PROPERTY( bool previewJobsEnabled READ previewJobsEnabled WRITE setPreviewJobsEnabled )
90 
91  public:
92 
94  QgsMapCanvas( QWidget *parent SIP_TRANSFERTHIS = nullptr );
95 
96  ~QgsMapCanvas() override;
97 
102  double magnificationFactor() const;
103 
115  void setLayers( const QList<QgsMapLayer *> &layers );
116 
117  void setCurrentLayer( QgsMapLayer *layer );
118 
123  const QgsMapSettings &mapSettings() const SIP_KEEPREFERENCE;
124 
129  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
130 
135  void setMapSettingsFlags( QgsMapSettings::Flags flags );
136 
141  const QgsLabelingResults *labelingResults() const;
142 
147  void setCachingEnabled( bool enabled );
148 
153  bool isCachingEnabled() const;
154 
159  void clearCache();
160 
170  void waitWhileRendering();
171 
176  void setParallelRenderingEnabled( bool enabled );
177 
182  bool isParallelRenderingEnabled() const;
183 
188  void setMapUpdateInterval( int timeMilliseconds );
189 
194  int mapUpdateInterval() const;
195 
200  double scale() const;
201 
203  double mapUnitsPerPixel() const;
204 
206  QgsRectangle extent() const;
208  QgsRectangle fullExtent() const;
209 
211  void setExtent( const QgsRectangle &r, bool magnified = false );
212 
221  bool setReferencedExtent( const QgsReferencedRectangle &extent ) SIP_THROW( QgsCsException );
222 
227  double rotation() const;
228 
233  void setRotation( double degrees );
234 
239  void setCenter( const QgsPointXY &center );
240 
245  QgsPointXY center() const;
246 
248  void zoomToFullExtent();
249 
251  void zoomToPreviousExtent();
252 
254  void zoomToNextExtent();
255 
256  // ! Clears the list of extents and sets current extent as first item
257  void clearExtentHistory();
258 
259 
264  void zoomToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
265 
272  void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids, bool alwaysRecenter = true );
273 
275  void panToSelected( QgsVectorLayer *layer = nullptr );
276 
289  void flashFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids,
290  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
291  int flashes = 3, int duration = 500 );
292 
305  void flashGeometries( const QList< QgsGeometry > &geometries, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(),
306  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
307  int flashes = 3, int duration = 500 );
308 
310  void setMapTool( QgsMapTool *mapTool, bool clean = false );
311 
319  void unsetMapTool( QgsMapTool *mapTool );
320 
322  QgsMapTool *mapTool();
323 
325  void setCanvasColor( const QColor &_newVal );
327  QColor canvasColor() const;
328 
333  void setSelectionColor( const QColor &color );
334 
339  QColor selectionColor() const;
340 
342  void updateScale();
343 
345  QgsMapLayer *layer( int index );
346 
348  int layerCount() const;
349 
354  QList<QgsMapLayer *> layers() const;
355 
365  void freeze( bool frozen = true );
366 
374  bool isFrozen() const;
375 
384  bool renderFlag() const { return mRenderFlag; }
385 
390  QgsUnitTypes::DistanceUnit mapUnits() const;
391 
397  QMap<QString, QString> layerStyleOverrides() const;
398 
410  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
411 
430  void setTheme( const QString &theme );
431 
437  QString theme() const { return mTheme; }
438 
440  const QgsMapToPixel *getCoordinateTransform();
441 
443  bool isDrawing();
444 
446  QgsMapLayer *currentLayer();
447 
449  void setWheelFactor( double factor );
450 
455  void zoomScale( double scale );
456 
461  void zoomByFactor( double scaleFactor, const QgsPointXY *center = nullptr );
462 
464  void zoomWithCenter( int x, int y, bool zoomIn );
465 
470  void zoomToFeatureExtent( QgsRectangle &rect );
471 
477  bool scaleLocked() const { return mScaleLocked;}
478 
480  void enableAntiAliasing( bool flag );
481 
483  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
484 
486  void enableMapTileRendering( bool flag );
487 
488  // following 2 methods should be moved elsewhere or changed to private
489  // currently used by pan map tool
491  void panActionEnd( QPoint releasePoint );
492 
493 #ifndef SIP_RUN
494 
500  void panActionStart( QPoint releasePoint );
501 #endif
502 
504  void panAction( QMouseEvent *event );
505 
507  QPoint mouseLastXY();
508 
514  void setPreviewModeEnabled( bool previewEnabled );
515 
522  bool previewModeEnabled() const;
523 
532  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
533 
541  QgsPreviewEffect::PreviewMode previewMode() const;
542 
551  QgsSnappingUtils *snappingUtils() const;
552 
561  void setSnappingUtils( QgsSnappingUtils *utils );
562 
573  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
574 
583  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
584 
592  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP
593 
600  QgsExpressionContextScope *defaultExpressionContextScope() SIP_FACTORY;
601 
605  void setSegmentationTolerance( double tolerance );
606 
610  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
611 
616  QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
617 
623  bool annotationsVisible() const { return mAnnotationsVisible; }
624 
630  void setAnnotationsVisible( bool visible );
631 
636  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
637 
642  const QgsLabelingEngineSettings &labelingEngineSettings() const;
643 
652  bool previewJobsEnabled() const;
653 
662  void setPreviewJobsEnabled( bool enabled );
663 
669  void setCustomDropHandlers( const QVector<QPointer<QgsCustomDropHandler >> &handlers ) SIP_SKIP;
670 
671  public slots:
672 
674  void refresh();
675 
685  void refreshAllLayers();
686 
695  void redrawAllLayers();
696 
698  void selectionChangedSlot();
699 
701  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
702 
704  void layerStateChange();
705 
713  void setRenderFlag( bool flag );
714 
719  void stopRendering();
720 
722  void readProject( const QDomDocument & );
723 
725  void writeProject( QDomDocument & );
726 
733  void setMagnificationFactor( double factor );
734 
740  void setScaleLocked( bool isLocked );
741 
743  void zoomIn();
744 
746  void zoomOut();
747 
752  void zoomToSelected( QgsVectorLayer *layer = nullptr );
753 
759  void setZoomResolutions( const QList<double> &resolutions ) { mZoomResolutions = resolutions; }
760 
766  const QList<double> &zoomResolutions() const { return mZoomResolutions; }
767 
768  private slots:
770  void mapToolDestroyed();
771 
773  void rendererJobFinished();
774 
776  void previewJobFinished();
777 
778  void mapUpdateTimeout();
779 
780  void refreshMap();
781 
782  void mapThemeChanged( const QString &theme );
783 
784  signals:
785 
789  void xyCoordinates( const QgsPointXY &p );
790 
792  void scaleChanged( double );
793 
795  void extentsChanged();
796 
801  void rotationChanged( double );
802 
807  void magnificationChanged( double );
808 
813  void canvasColorChanged();
814 
829  void renderComplete( QPainter * );
830 
831  // ### QGIS 3: renamte to mapRefreshFinished()
833  void mapCanvasRefreshed();
834 
835  // ### QGIS 3: rename to mapRefreshStarted()
837  void renderStarting();
838 
840  void layersChanged();
841 
843  void keyPressed( QKeyEvent *e );
844 
846  void keyReleased( QKeyEvent *e );
847 
852  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
853 
854 
856  void selectionChanged( QgsVectorLayer *layer );
857 
859  void zoomLastStatusChanged( bool );
860 
862  void zoomNextStatusChanged( bool );
863 
868  void destinationCrsChanged();
869 
874  void transformContextChanged();
875 
880  void currentLayerChanged( QgsMapLayer *layer );
881 
886  void layerStyleOverridesChanged();
887 
893  void themeChanged( const QString &theme );
894 
896  void messageEmitted( const QString &title, const QString &message, Qgis::MessageLevel = Qgis::Info );
897 
905  void renderErrorOccurred( const QString &error, QgsMapLayer *layer );
906 
917  void panDistanceBearingChanged( double distance, QgsUnitTypes::DistanceUnit unit, double bearing );
918 
923  void tapAndHoldGestureOccurred( const QgsPointXY &mapPoint, QTapAndHoldGesture *gesture );
924 
925  protected:
926 
927  bool event( QEvent *e ) override;
928  void keyPressEvent( QKeyEvent *e ) override;
929  void keyReleaseEvent( QKeyEvent *e ) override;
930  void mouseDoubleClickEvent( QMouseEvent *e ) override;
931  void mouseMoveEvent( QMouseEvent *e ) override;
932  void mousePressEvent( QMouseEvent *e ) override;
933  void mouseReleaseEvent( QMouseEvent *e ) override;
934  void wheelEvent( QWheelEvent *e ) override;
935  void resizeEvent( QResizeEvent *e ) override;
936  void paintEvent( QPaintEvent *e ) override;
937  void dragEnterEvent( QDragEnterEvent *e ) override;
938 
940  void moveCanvasContents( bool reset = false );
941 
942  void dropEvent( QDropEvent *event ) override;
943 
944 
947 
949  std::unique_ptr<CanvasProperties> mCanvasProperties;
950 
951 #if 0
952 
957  void connectNotify( const char *signal ) override;
958 #endif
959 
960  protected slots:
962  void updateCanvasItemPositions();
963 
964  private slots:
965 
966  void layerRepaintRequested( bool deferred );
967 
968  void autoRefreshTriggered();
969 
970  void updateAutoRefreshTimer();
971 
972  void projectThemesChanged();
973 
974  void startPreviewJob( int number );
975 
976  private:
977 
979  QgsMapSettings mSettings;
980 
982  QgsMapCanvasMap *mMap = nullptr;
983 
985  bool mFrozen = false;
986 
988  bool mRefreshScheduled = false;
989 
991  bool mRenderFlag = true;
992 
994  QPointer< QgsMapLayer > mCurrentLayer;
995 
997  QGraphicsScene *mScene = nullptr;
998 
1000  QgsMapTool *mMapTool = nullptr;
1001 
1003  QgsMapTool *mLastNonZoomMapTool = nullptr;
1004 
1006  QList <QgsRectangle> mLastExtent;
1007  int mLastExtentIndex = -1;
1008 
1010  double mWheelZoomFactor = 2.0;
1011 
1013  QTimer mMapUpdateTimer;
1014 
1016  QgsMapRendererQImageJob *mJob = nullptr;
1017 
1019  bool mJobCanceled = false;
1020 
1022  QgsLabelingResults *mLabelingResults = nullptr;
1023 
1025  bool mUseParallelRendering = false;
1026 
1028  bool mDrawRenderingStats = false;
1029 
1031  QgsMapRendererCache *mCache = nullptr;
1032 
1033  QTimer *mResizeTimer = nullptr;
1034  QTimer *mRefreshTimer = nullptr;
1035 
1036  QgsPreviewEffect *mPreviewEffect = nullptr;
1037 
1038  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
1039 
1040  QgsSnappingUtils *mSnappingUtils = nullptr;
1041 
1042  QList< QgsMapRendererQImageJob * > mPreviewJobs;
1043 
1045  bool mScaleLocked = false;
1046 
1047  QgsExpressionContextScope mExpressionContextScope;
1048 
1050  QRect mZoomRect;
1051 
1053  bool mZoomDragging = false;
1054 
1056  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
1057 
1058  QCursor mZoomCursor;
1059 
1060  QTimer mAutoRefreshTimer;
1061 
1062  QTimer mPreviewTimer;
1063  QMetaObject::Connection mPreviewTimerConnection;
1064 
1065  QString mTheme;
1066 
1067  QgsPointXY mCursorPoint;
1068 
1069  bool mAnnotationsVisible = true;
1070 
1071  bool mUsePreviewJobs = false;
1072 
1073  QHash< QString, int > mLastLayerRenderTime;
1074 
1075  QVector<QPointer<QgsCustomDropHandler >> mDropHandlers;
1076 
1077  QgsDistanceArea mDa;
1078  QList<double> mZoomResolutions;
1079 
1084  QgsPointXY cursorPoint() const;
1085 
1090  void updateMapSize();
1091 
1097  void beginZoomRect( QPoint pos );
1098 
1104  void endZoomRect( QPoint pos );
1105 
1113  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
1114 
1115  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
1116 
1117  void startPreviewJobs();
1118  void stopPreviewJobs();
1119  void schedulePreviewJob( int number );
1120 
1124  bool panOperationInProgress();
1125 
1126  int nextZoomLevel( const QList<double> &resolutions, bool zoomIn = true ) const;
1127  double zoomInFactor() const;
1128  double zoomOutFactor() const;
1129 
1130  friend class TestQgsMapCanvas;
1131 
1132 }; // class QgsMapCanvas
1133 
1134 // clazy:excludeall=qstring-allocations
1135 
1136 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Base class for all map layer types.
Definition: qgsmaplayer.h:79
std::unique_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:946
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
const QList< double > & zoomResolutions() const
Definition: qgsmapcanvas.h:766
#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:623
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:75
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:759
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:573
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:66
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:437
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:583
bool antiAliasingEnabled() const
true if antialiasing is enabled
Definition: qgsmapcanvas.h:483
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:477
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:592
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.