QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 
651  void refreshAllLayers();
652 
654  void selectionChangedSlot();
655 
657  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
658 
660  void layerStateChange();
661 
669  void setRenderFlag( bool flag );
670 
675  void stopRendering();
676 
678  void readProject( const QDomDocument & );
679 
681  void writeProject( QDomDocument & );
682 
689  void setMagnificationFactor( double factor );
690 
696  void setScaleLocked( bool isLocked );
697 
699  void zoomIn();
700 
702  void zoomOut();
703 
708  void zoomToSelected( QgsVectorLayer *layer = nullptr );
709 
710  private slots:
712  void mapToolDestroyed();
713 
715  void rendererJobFinished();
716 
718  void previewJobFinished();
719 
720  void mapUpdateTimeout();
721 
722  void refreshMap();
723 
724  void mapThemeChanged( const QString &theme );
725 
726  signals:
727 
731  void xyCoordinates( const QgsPointXY &p );
732 
734  void scaleChanged( double );
735 
737  void extentsChanged();
738 
743  void rotationChanged( double );
744 
749  void magnificationChanged( double );
750 
755  void canvasColorChanged();
756 
771  void renderComplete( QPainter * );
772 
773  // ### QGIS 3: renamte to mapRefreshFinished()
775  void mapCanvasRefreshed();
776 
777  // ### QGIS 3: rename to mapRefreshStarted()
779  void renderStarting();
780 
782  void layersChanged();
783 
785  void keyPressed( QKeyEvent *e );
786 
788  void keyReleased( QKeyEvent *e );
789 
794  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
795 
796 
798  void selectionChanged( QgsVectorLayer *layer );
799 
801  void zoomLastStatusChanged( bool );
802 
804  void zoomNextStatusChanged( bool );
805 
810  void destinationCrsChanged();
811 
816  void transformContextChanged();
817 
822  void currentLayerChanged( QgsMapLayer *layer );
823 
828  void layerStyleOverridesChanged();
829 
835  void themeChanged( const QString &theme );
836 
838  void messageEmitted( const QString &title, const QString &message, Qgis::MessageLevel = Qgis::Info );
839 
840  protected:
841 
843  bool event( QEvent *e ) override;
844 
846  void keyPressEvent( QKeyEvent *e ) override;
847 
849  void keyReleaseEvent( QKeyEvent *e ) override;
850 
852  void mouseDoubleClickEvent( QMouseEvent *e ) override;
853 
855  void mouseMoveEvent( QMouseEvent *e ) override;
856 
858  void mousePressEvent( QMouseEvent *e ) override;
859 
861  void mouseReleaseEvent( QMouseEvent *e ) override;
862 
864  void wheelEvent( QWheelEvent *e ) override;
865 
867  void resizeEvent( QResizeEvent *e ) override;
868 
870  void paintEvent( QPaintEvent *e ) override;
871 
873  void dragEnterEvent( QDragEnterEvent *e ) override;
874 
876  void moveCanvasContents( bool reset = false );
877 
880 
882  std::unique_ptr<CanvasProperties> mCanvasProperties;
883 
884 #if 0
885 
890  void connectNotify( const char *signal ) override;
891 #endif
892 
893  protected slots:
895  void updateCanvasItemPositions();
896 
897  private slots:
898 
899  void layerRepaintRequested( bool deferred );
900 
901  void autoRefreshTriggered();
902 
903  void updateAutoRefreshTimer();
904 
905  void projectThemesChanged();
906 
907  void startPreviewJob( int number );
908 
909  private:
910 
912  QgsMapSettings mSettings;
913 
915  QgsMapCanvasMap *mMap = nullptr;
916 
918  bool mFrozen = false;
919 
921  bool mRefreshScheduled = false;
922 
924  bool mRenderFlag = true;
925 
927  QPointer< QgsMapLayer > mCurrentLayer;
928 
930  QGraphicsScene *mScene = nullptr;
931 
933  QgsMapTool *mMapTool = nullptr;
934 
936  QgsMapTool *mLastNonZoomMapTool = nullptr;
937 
939  QList <QgsRectangle> mLastExtent;
940  int mLastExtentIndex = -1;
941 
943  double mWheelZoomFactor = 2.0;
944 
946  QTimer mMapUpdateTimer;
947 
949  QgsMapRendererQImageJob *mJob = nullptr;
950 
952  bool mJobCanceled = false;
953 
955  QgsLabelingResults *mLabelingResults = nullptr;
956 
958  bool mUseParallelRendering = false;
959 
961  bool mDrawRenderingStats = false;
962 
964  QgsMapRendererCache *mCache = nullptr;
965 
966  QTimer *mResizeTimer = nullptr;
967  QTimer *mRefreshTimer = nullptr;
968 
969  QgsPreviewEffect *mPreviewEffect = nullptr;
970 
971  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
972 
973  QgsSnappingUtils *mSnappingUtils = nullptr;
974 
975  QList< QgsMapRendererQImageJob * > mPreviewJobs;
976 
978  bool mScaleLocked = false;
979 
980  QgsExpressionContextScope mExpressionContextScope;
981 
983  QRect mZoomRect;
984 
986  bool mZoomDragging = false;
987 
989  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
990 
991  QCursor mZoomCursor;
992 
993  QTimer mAutoRefreshTimer;
994 
995  QTimer mPreviewTimer;
996  QMetaObject::Connection mPreviewTimerConnection;
997 
998  QString mTheme;
999 
1000  QgsPointXY mCursorPoint;
1001 
1002  bool mAnnotationsVisible = true;
1003 
1004  bool mUsePreviewJobs = false;
1005 
1006  QHash< QString, int > mLastLayerRenderTime;
1007 
1012  QgsPointXY cursorPoint() const;
1013 
1018  void updateMapSize();
1019 
1025  void beginZoomRect( QPoint pos );
1026 
1032  void endZoomRect( QPoint pos );
1033 
1041  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
1042 
1043  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
1044 
1045  void startPreviewJobs();
1046  void stopPreviewJobs();
1047  void schedulePreviewJob( int number );
1048 
1049  friend class TestQgsMapCanvas;
1050 
1051 }; // class QgsMapCanvas
1052 
1053 // clazy:excludeall=qstring-allocations
1054 
1055 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Base class for all map layer types.
Definition: qgsmaplayer.h:78
std::unique_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:879
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
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:111
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:119
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:40
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:182
#define SIP_KEEPREFERENCE
Definition: qgis_sip.h:79
#define SIP_FACTORY
Definition: qgis_sip.h:69
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:165
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.