QGIS API Documentation  3.9.0-Master (635e245143)
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 
494  void panAction( QMouseEvent *event );
495 
497  QPoint mouseLastXY();
498 
504  void setPreviewModeEnabled( bool previewEnabled );
505 
512  bool previewModeEnabled() const;
513 
522  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
523 
531  QgsPreviewEffect::PreviewMode previewMode() const;
532 
541  QgsSnappingUtils *snappingUtils() const;
542 
551  void setSnappingUtils( QgsSnappingUtils *utils );
552 
563  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
564 
573  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
574 
582  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP
583 
590  QgsExpressionContextScope *defaultExpressionContextScope() SIP_FACTORY;
591 
595  void setSegmentationTolerance( double tolerance );
596 
600  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
601 
606  QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
607 
613  bool annotationsVisible() const { return mAnnotationsVisible; }
614 
620  void setAnnotationsVisible( bool visible );
621 
626  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
627 
632  const QgsLabelingEngineSettings &labelingEngineSettings() const;
633 
642  bool previewJobsEnabled() const;
643 
652  void setPreviewJobsEnabled( bool enabled );
653 
659  void setCustomDropHandlers( const QVector<QPointer<QgsCustomDropHandler >> &handlers ) SIP_SKIP;
660 
661  public slots:
662 
664  void refresh();
665 
675  void refreshAllLayers();
676 
685  void redrawAllLayers();
686 
688  void selectionChangedSlot();
689 
691  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
692 
694  void layerStateChange();
695 
703  void setRenderFlag( bool flag );
704 
709  void stopRendering();
710 
712  void readProject( const QDomDocument & );
713 
715  void writeProject( QDomDocument & );
716 
723  void setMagnificationFactor( double factor );
724 
730  void setScaleLocked( bool isLocked );
731 
733  void zoomIn();
734 
736  void zoomOut();
737 
742  void zoomToSelected( QgsVectorLayer *layer = nullptr );
743 
744  private slots:
746  void mapToolDestroyed();
747 
749  void rendererJobFinished();
750 
752  void previewJobFinished();
753 
754  void mapUpdateTimeout();
755 
756  void refreshMap();
757 
758  void mapThemeChanged( const QString &theme );
759 
760  signals:
761 
765  void xyCoordinates( const QgsPointXY &p );
766 
768  void scaleChanged( double );
769 
771  void extentsChanged();
772 
777  void rotationChanged( double );
778 
783  void magnificationChanged( double );
784 
789  void canvasColorChanged();
790 
805  void renderComplete( QPainter * );
806 
807  // ### QGIS 3: renamte to mapRefreshFinished()
809  void mapCanvasRefreshed();
810 
811  // ### QGIS 3: rename to mapRefreshStarted()
813  void renderStarting();
814 
816  void layersChanged();
817 
819  void keyPressed( QKeyEvent *e );
820 
822  void keyReleased( QKeyEvent *e );
823 
828  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
829 
830 
832  void selectionChanged( QgsVectorLayer *layer );
833 
835  void zoomLastStatusChanged( bool );
836 
838  void zoomNextStatusChanged( bool );
839 
844  void destinationCrsChanged();
845 
850  void transformContextChanged();
851 
856  void currentLayerChanged( QgsMapLayer *layer );
857 
862  void layerStyleOverridesChanged();
863 
869  void themeChanged( const QString &theme );
870 
872  void messageEmitted( const QString &title, const QString &message, Qgis::MessageLevel = Qgis::Info );
873 
881  void renderErrorOccurred( const QString &error, QgsMapLayer *layer );
882 
883  protected:
884 
885  bool event( QEvent *e ) override;
886  void keyPressEvent( QKeyEvent *e ) override;
887  void keyReleaseEvent( QKeyEvent *e ) override;
888  void mouseDoubleClickEvent( QMouseEvent *e ) override;
889  void mouseMoveEvent( QMouseEvent *e ) override;
890  void mousePressEvent( QMouseEvent *e ) override;
891  void mouseReleaseEvent( QMouseEvent *e ) override;
892  void wheelEvent( QWheelEvent *e ) override;
893  void resizeEvent( QResizeEvent *e ) override;
894  void paintEvent( QPaintEvent *e ) override;
895  void dragEnterEvent( QDragEnterEvent *e ) override;
896 
898  void moveCanvasContents( bool reset = false );
899 
900  void dropEvent( QDropEvent *event ) override;
901 
902 
905 
907  std::unique_ptr<CanvasProperties> mCanvasProperties;
908 
909 #if 0
910 
915  void connectNotify( const char *signal ) override;
916 #endif
917 
918  protected slots:
920  void updateCanvasItemPositions();
921 
922  private slots:
923 
924  void layerRepaintRequested( bool deferred );
925 
926  void autoRefreshTriggered();
927 
928  void updateAutoRefreshTimer();
929 
930  void projectThemesChanged();
931 
932  void startPreviewJob( int number );
933 
934  private:
935 
937  QgsMapSettings mSettings;
938 
940  QgsMapCanvasMap *mMap = nullptr;
941 
943  bool mFrozen = false;
944 
946  bool mRefreshScheduled = false;
947 
949  bool mRenderFlag = true;
950 
952  QPointer< QgsMapLayer > mCurrentLayer;
953 
955  QGraphicsScene *mScene = nullptr;
956 
958  QgsMapTool *mMapTool = nullptr;
959 
961  QgsMapTool *mLastNonZoomMapTool = nullptr;
962 
964  QList <QgsRectangle> mLastExtent;
965  int mLastExtentIndex = -1;
966 
968  double mWheelZoomFactor = 2.0;
969 
971  QTimer mMapUpdateTimer;
972 
974  QgsMapRendererQImageJob *mJob = nullptr;
975 
977  bool mJobCanceled = false;
978 
980  QgsLabelingResults *mLabelingResults = nullptr;
981 
983  bool mUseParallelRendering = false;
984 
986  bool mDrawRenderingStats = false;
987 
989  QgsMapRendererCache *mCache = nullptr;
990 
991  QTimer *mResizeTimer = nullptr;
992  QTimer *mRefreshTimer = nullptr;
993 
994  QgsPreviewEffect *mPreviewEffect = nullptr;
995 
996  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
997 
998  QgsSnappingUtils *mSnappingUtils = nullptr;
999 
1000  QList< QgsMapRendererQImageJob * > mPreviewJobs;
1001 
1003  bool mScaleLocked = false;
1004 
1005  QgsExpressionContextScope mExpressionContextScope;
1006 
1008  QRect mZoomRect;
1009 
1011  bool mZoomDragging = false;
1012 
1014  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
1015 
1016  QCursor mZoomCursor;
1017 
1018  QTimer mAutoRefreshTimer;
1019 
1020  QTimer mPreviewTimer;
1021  QMetaObject::Connection mPreviewTimerConnection;
1022 
1023  QString mTheme;
1024 
1025  QgsPointXY mCursorPoint;
1026 
1027  bool mAnnotationsVisible = true;
1028 
1029  bool mUsePreviewJobs = false;
1030 
1031  QHash< QString, int > mLastLayerRenderTime;
1032 
1033  QVector<QPointer<QgsCustomDropHandler >> mDropHandlers;
1034 
1039  QgsPointXY cursorPoint() const;
1040 
1045  void updateMapSize();
1046 
1052  void beginZoomRect( QPoint pos );
1053 
1059  void endZoomRect( QPoint pos );
1060 
1068  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
1069 
1070  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
1071 
1072  void startPreviewJobs();
1073  void stopPreviewJobs();
1074  void schedulePreviewJob( int number );
1075 
1076  friend class TestQgsMapCanvas;
1077 
1078 }; // class QgsMapCanvas
1079 
1080 // clazy:excludeall=qstring-allocations
1081 
1082 #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:904
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:613
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: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: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
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:563
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:66
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:437
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:573
bool antiAliasingEnabled() const
true if antialising 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:582
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.