QGIS API Documentation  3.9.0-Master (224899f119)
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 
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 
73 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
74 {
75 
76 #ifdef SIP_RUN
78  if ( qobject_cast<QgsMapCanvas *>( sipCpp ) != nullptr )
79  sipType = sipType_QgsMapCanvas;
80  else
81  sipType = nullptr;
82  SIP_END
83 #endif
84 
85  Q_OBJECT
86  Q_PROPERTY( QString theme READ theme WRITE setTheme NOTIFY themeChanged )
87  Q_PROPERTY( bool previewJobsEnabled READ previewJobsEnabled WRITE setPreviewJobsEnabled )
88 
89  public:
90 
92  QgsMapCanvas( QWidget *parent SIP_TRANSFERTHIS = nullptr );
93 
94  ~QgsMapCanvas() override;
95 
100  double magnificationFactor() const;
101 
113  void setLayers( const QList<QgsMapLayer *> &layers );
114 
115  void setCurrentLayer( QgsMapLayer *layer );
116 
121  const QgsMapSettings &mapSettings() const SIP_KEEPREFERENCE;
122 
127  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
128 
133  void setMapSettingsFlags( QgsMapSettings::Flags flags );
134 
139  const QgsLabelingResults *labelingResults() const;
140 
145  void setCachingEnabled( bool enabled );
146 
151  bool isCachingEnabled() const;
152 
157  void clearCache();
158 
168  void waitWhileRendering();
169 
174  void setParallelRenderingEnabled( bool enabled );
175 
180  bool isParallelRenderingEnabled() const;
181 
186  void setMapUpdateInterval( int timeMilliseconds );
187 
192  int mapUpdateInterval() const;
193 
198  double scale() const;
199 
201  double mapUnitsPerPixel() const;
202 
204  QgsRectangle extent() const;
206  QgsRectangle fullExtent() const;
207 
209  void setExtent( const QgsRectangle &r, bool magnified = false );
210 
215  double rotation() const;
216 
221  void setRotation( double degrees );
222 
227  void setCenter( const QgsPointXY &center );
228 
233  QgsPointXY center() const;
234 
236  void zoomToFullExtent();
237 
239  void zoomToPreviousExtent();
240 
242  void zoomToNextExtent();
243 
244  // ! Clears the list of extents and sets current extent as first item
245  void clearExtentHistory();
246 
247 
252  void zoomToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
253 
260  void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids, bool alwaysRecenter = true );
261 
263  void panToSelected( QgsVectorLayer *layer = nullptr );
264 
277  void flashFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids,
278  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
279  int flashes = 3, int duration = 500 );
280 
293  void flashGeometries( const QList< QgsGeometry > &geometries, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(),
294  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
295  int flashes = 3, int duration = 500 );
296 
298  void setMapTool( QgsMapTool *mapTool, bool clean = false );
299 
307  void unsetMapTool( QgsMapTool *mapTool );
308 
310  QgsMapTool *mapTool();
311 
313  void setCanvasColor( const QColor &_newVal );
315  QColor canvasColor() const;
316 
321  void setSelectionColor( const QColor &color );
322 
327  QColor selectionColor() const;
328 
330  void updateScale();
331 
333  QgsMapLayer *layer( int index );
334 
336  int layerCount() const;
337 
342  QList<QgsMapLayer *> layers() const;
343 
353  void freeze( bool frozen = true );
354 
362  bool isFrozen() const;
363 
372  bool renderFlag() const { return mRenderFlag; }
373 
378  QgsUnitTypes::DistanceUnit mapUnits() const;
379 
385  QMap<QString, QString> layerStyleOverrides() const;
386 
398  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
399 
418  void setTheme( const QString &theme );
419 
425  QString theme() const { return mTheme; }
426 
428  const QgsMapToPixel *getCoordinateTransform();
429 
431  bool isDrawing();
432 
434  QgsMapLayer *currentLayer();
435 
437  void setWheelFactor( double factor );
438 
443  void zoomScale( double scale );
444 
449  void zoomByFactor( double scaleFactor, const QgsPointXY *center = nullptr );
450 
452  void zoomWithCenter( int x, int y, bool zoomIn );
453 
458  void zoomToFeatureExtent( QgsRectangle &rect );
459 
465  bool scaleLocked() const { return mScaleLocked;}
466 
468  void enableAntiAliasing( bool flag );
469 
471  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
472 
474  void enableMapTileRendering( bool flag );
475 
476  // following 2 methods should be moved elsewhere or changed to private
477  // currently used by pan map tool
479  void panActionEnd( QPoint releasePoint );
480 
482  void panAction( QMouseEvent *event );
483 
485  QPoint mouseLastXY();
486 
492  void setPreviewModeEnabled( bool previewEnabled );
493 
500  bool previewModeEnabled() const;
501 
510  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
511 
519  QgsPreviewEffect::PreviewMode previewMode() const;
520 
529  QgsSnappingUtils *snappingUtils() const;
530 
539  void setSnappingUtils( QgsSnappingUtils *utils );
540 
551  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
552 
561  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
562 
570  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP
571 
578  QgsExpressionContextScope *defaultExpressionContextScope() SIP_FACTORY;
579 
583  void setSegmentationTolerance( double tolerance );
584 
588  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
589 
594  QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
595 
601  bool annotationsVisible() const { return mAnnotationsVisible; }
602 
608  void setAnnotationsVisible( bool visible );
609 
614  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
615 
620  const QgsLabelingEngineSettings &labelingEngineSettings() const;
621 
630  bool previewJobsEnabled() const;
631 
640  void setPreviewJobsEnabled( bool enabled );
641 
642  public slots:
643 
645  void refresh();
646 
656  void refreshAllLayers();
657 
666  void redrawAllLayers();
667 
669  void selectionChangedSlot();
670 
672  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
673 
675  void layerStateChange();
676 
684  void setRenderFlag( bool flag );
685 
690  void stopRendering();
691 
693  void readProject( const QDomDocument & );
694 
696  void writeProject( QDomDocument & );
697 
704  void setMagnificationFactor( double factor );
705 
711  void setScaleLocked( bool isLocked );
712 
714  void zoomIn();
715 
717  void zoomOut();
718 
723  void zoomToSelected( QgsVectorLayer *layer = nullptr );
724 
725  private slots:
727  void mapToolDestroyed();
728 
730  void rendererJobFinished();
731 
733  void previewJobFinished();
734 
735  void mapUpdateTimeout();
736 
737  void refreshMap();
738 
739  void mapThemeChanged( const QString &theme );
740 
741  signals:
742 
746  void xyCoordinates( const QgsPointXY &p );
747 
749  void scaleChanged( double );
750 
752  void extentsChanged();
753 
758  void rotationChanged( double );
759 
764  void magnificationChanged( double );
765 
770  void canvasColorChanged();
771 
786  void renderComplete( QPainter * );
787 
788  // ### QGIS 3: renamte to mapRefreshFinished()
790  void mapCanvasRefreshed();
791 
792  // ### QGIS 3: rename to mapRefreshStarted()
794  void renderStarting();
795 
797  void layersChanged();
798 
800  void keyPressed( QKeyEvent *e );
801 
803  void keyReleased( QKeyEvent *e );
804 
809  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
810 
811 
813  void selectionChanged( QgsVectorLayer *layer );
814 
816  void zoomLastStatusChanged( bool );
817 
819  void zoomNextStatusChanged( bool );
820 
825  void destinationCrsChanged();
826 
831  void transformContextChanged();
832 
837  void currentLayerChanged( QgsMapLayer *layer );
838 
843  void layerStyleOverridesChanged();
844 
850  void themeChanged( const QString &theme );
851 
853  void messageEmitted( const QString &title, const QString &message, Qgis::MessageLevel = Qgis::Info );
854 
862  void renderErrorOccurred( const QString &error, QgsMapLayer *layer );
863 
864  protected:
865 
867  bool event( QEvent *e ) override;
868 
870  void keyPressEvent( QKeyEvent *e ) override;
871 
873  void keyReleaseEvent( QKeyEvent *e ) override;
874 
876  void mouseDoubleClickEvent( QMouseEvent *e ) override;
877 
879  void mouseMoveEvent( QMouseEvent *e ) override;
880 
882  void mousePressEvent( QMouseEvent *e ) override;
883 
885  void mouseReleaseEvent( QMouseEvent *e ) override;
886 
888  void wheelEvent( QWheelEvent *e ) override;
889 
891  void resizeEvent( QResizeEvent *e ) override;
892 
894  void paintEvent( QPaintEvent *e ) override;
895 
897  void dragEnterEvent( QDragEnterEvent *e ) override;
898 
900  void moveCanvasContents( bool reset = false );
901 
904 
906  std::unique_ptr<CanvasProperties> mCanvasProperties;
907 
908 #if 0
909 
914  void connectNotify( const char *signal ) override;
915 #endif
916 
917  protected slots:
919  void updateCanvasItemPositions();
920 
921  private slots:
922 
923  void layerRepaintRequested( bool deferred );
924 
925  void autoRefreshTriggered();
926 
927  void updateAutoRefreshTimer();
928 
929  void projectThemesChanged();
930 
931  void startPreviewJob( int number );
932 
933  private:
934 
936  QgsMapSettings mSettings;
937 
939  QgsMapCanvasMap *mMap = nullptr;
940 
942  bool mFrozen = false;
943 
945  bool mRefreshScheduled = false;
946 
948  bool mRenderFlag = true;
949 
951  QPointer< QgsMapLayer > mCurrentLayer;
952 
954  QGraphicsScene *mScene = nullptr;
955 
957  QgsMapTool *mMapTool = nullptr;
958 
960  QgsMapTool *mLastNonZoomMapTool = nullptr;
961 
963  QList <QgsRectangle> mLastExtent;
964  int mLastExtentIndex = -1;
965 
967  double mWheelZoomFactor = 2.0;
968 
970  QTimer mMapUpdateTimer;
971 
973  QgsMapRendererQImageJob *mJob = nullptr;
974 
976  bool mJobCanceled = false;
977 
979  QgsLabelingResults *mLabelingResults = nullptr;
980 
982  bool mUseParallelRendering = false;
983 
985  bool mDrawRenderingStats = false;
986 
988  QgsMapRendererCache *mCache = nullptr;
989 
990  QTimer *mResizeTimer = nullptr;
991  QTimer *mRefreshTimer = nullptr;
992 
993  QgsPreviewEffect *mPreviewEffect = nullptr;
994 
995  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
996 
997  QgsSnappingUtils *mSnappingUtils = nullptr;
998 
999  QList< QgsMapRendererQImageJob * > mPreviewJobs;
1000 
1002  bool mScaleLocked = false;
1003 
1004  QgsExpressionContextScope mExpressionContextScope;
1005 
1007  QRect mZoomRect;
1008 
1010  bool mZoomDragging = false;
1011 
1013  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
1014 
1015  QCursor mZoomCursor;
1016 
1017  QTimer mAutoRefreshTimer;
1018 
1019  QTimer mPreviewTimer;
1020  QMetaObject::Connection mPreviewTimerConnection;
1021 
1022  QString mTheme;
1023 
1024  QgsPointXY mCursorPoint;
1025 
1026  bool mAnnotationsVisible = true;
1027 
1028  bool mUsePreviewJobs = false;
1029 
1030  QHash< QString, int > mLastLayerRenderTime;
1031 
1036  QgsPointXY cursorPoint() const;
1037 
1042  void updateMapSize();
1043 
1049  void beginZoomRect( QPoint pos );
1050 
1056  void endZoomRect( QPoint pos );
1057 
1065  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
1066 
1067  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
1068 
1069  void startPreviewJobs();
1070  void stopPreviewJobs();
1071  void schedulePreviewJob( int number );
1072 
1073  friend class TestQgsMapCanvas;
1074 
1075 }; // class QgsMapCanvas
1076 
1077 // clazy:excludeall=qstring-allocations
1078 
1079 #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:903
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
#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:601
A class to represent a 2D point.
Definition: qgspointxy.h:43
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:121
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:66
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:73
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:41
Enable anti-aliasing for map rendering.
A class for highlight features on the map.
Definition: qgshighlight.h:49
#define SIP_END
Definition: qgis_sip.h:189
#define SIP_KEEPREFERENCE
Definition: qgis_sip.h:86
#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:551
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:54
Abstract base class for all map tools.
Definition: qgsmaptool.h:62
QString theme() const
Returns the map&#39;s theme shown in the canvas, if set.
Definition: qgsmapcanvas.h:425
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:561
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:471
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:465
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:570
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
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.