QGIS API Documentation  3.6.0-Noosa (5873452)
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 ( dynamic_cast<QgsMapCanvas *>( sipCpp ) != NULL )
79  sipType = sipType_QgsMapCanvas;
80  else
81  sipType = NULL;
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 
258  void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
259 
261  void panToSelected( QgsVectorLayer *layer = nullptr );
262 
275  void flashFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids,
276  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
277  int flashes = 3, int duration = 500 );
278 
291  void flashGeometries( const QList< QgsGeometry > &geometries, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(),
292  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
293  int flashes = 3, int duration = 500 );
294 
296  void setMapTool( QgsMapTool *mapTool, bool clean = false );
297 
305  void unsetMapTool( QgsMapTool *mapTool );
306 
308  QgsMapTool *mapTool();
309 
311  void setCanvasColor( const QColor &_newVal );
313  QColor canvasColor() const;
314 
319  void setSelectionColor( const QColor &color );
320 
325  QColor selectionColor() const;
326 
328  void updateScale();
329 
331  QgsMapLayer *layer( int index );
332 
334  int layerCount() const;
335 
340  QList<QgsMapLayer *> layers() const;
341 
351  void freeze( bool frozen = true );
352 
360  bool isFrozen() const;
361 
370  bool renderFlag() const { return mRenderFlag; }
371 
376  QgsUnitTypes::DistanceUnit mapUnits() const;
377 
383  QMap<QString, QString> layerStyleOverrides() const;
384 
396  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
397 
416  void setTheme( const QString &theme );
417 
423  QString theme() const { return mTheme; }
424 
426  const QgsMapToPixel *getCoordinateTransform();
427 
429  bool isDrawing();
430 
432  QgsMapLayer *currentLayer();
433 
435  void setWheelFactor( double factor );
436 
441  void zoomScale( double scale );
442 
447  void zoomByFactor( double scaleFactor, const QgsPointXY *center = nullptr );
448 
450  void zoomWithCenter( int x, int y, bool zoomIn );
451 
456  void zoomToFeatureExtent( QgsRectangle &rect );
457 
463  bool scaleLocked() const { return mScaleLocked;}
464 
466  void enableAntiAliasing( bool flag );
467 
469  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
470 
472  void enableMapTileRendering( bool flag );
473 
474  // following 2 methods should be moved elsewhere or changed to private
475  // currently used by pan map tool
477  void panActionEnd( QPoint releasePoint );
478 
480  void panAction( QMouseEvent *event );
481 
483  QPoint mouseLastXY();
484 
490  void setPreviewModeEnabled( bool previewEnabled );
491 
498  bool previewModeEnabled() const;
499 
508  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
509 
517  QgsPreviewEffect::PreviewMode previewMode() const;
518 
527  QgsSnappingUtils *snappingUtils() const;
528 
537  void setSnappingUtils( QgsSnappingUtils *utils );
538 
549  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
550 
559  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
560 
568  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP
569 
576  QgsExpressionContextScope *defaultExpressionContextScope() SIP_FACTORY;
577 
581  void setSegmentationTolerance( double tolerance );
582 
586  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
587 
592  QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
593 
599  bool annotationsVisible() const { return mAnnotationsVisible; }
600 
606  void setAnnotationsVisible( bool visible );
607 
612  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
613 
618  const QgsLabelingEngineSettings &labelingEngineSettings() const;
619 
628  bool previewJobsEnabled() const;
629 
638  void setPreviewJobsEnabled( bool enabled );
639 
640  public slots:
641 
643  void refresh();
644 
649  void refreshAllLayers();
650 
652  void selectionChangedSlot();
653 
655  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
656 
658  void layerStateChange();
659 
667  void setRenderFlag( bool flag );
668 
673  void stopRendering();
674 
676  void readProject( const QDomDocument & );
677 
679  void writeProject( QDomDocument & );
680 
681 #if 0
682  void getDatumTransformInfo( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination );
684 #endif
685 
692  void setMagnificationFactor( double factor );
693 
699  void setScaleLocked( bool isLocked );
700 
702  void zoomIn();
703 
705  void zoomOut();
706 
711  void zoomToSelected( QgsVectorLayer *layer = nullptr );
712 
713  private slots:
715  void mapToolDestroyed();
716 
718  void rendererJobFinished();
719 
721  void previewJobFinished();
722 
723  void mapUpdateTimeout();
724 
725  void refreshMap();
726 
727  void mapThemeChanged( const QString &theme );
728 
729  signals:
730 
734  void xyCoordinates( const QgsPointXY &p );
735 
737  void scaleChanged( double );
738 
740  void extentsChanged();
741 
746  void rotationChanged( double );
747 
752  void magnificationChanged( double );
753 
758  void canvasColorChanged();
759 
774  void renderComplete( QPainter * );
775 
776  // ### QGIS 3: renamte to mapRefreshFinished()
778  void mapCanvasRefreshed();
779 
780  // ### QGIS 3: rename to mapRefreshStarted()
782  void renderStarting();
783 
785  void layersChanged();
786 
788  void keyPressed( QKeyEvent *e );
789 
791  void keyReleased( QKeyEvent *e );
792 
797  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
798 
799 
801  void selectionChanged( QgsVectorLayer *layer );
802 
804  void zoomLastStatusChanged( bool );
805 
807  void zoomNextStatusChanged( bool );
808 
813  void destinationCrsChanged();
814 
819  void transformContextChanged();
820 
825  void currentLayerChanged( QgsMapLayer *layer );
826 
831  void layerStyleOverridesChanged();
832 
838  void themeChanged( const QString &theme );
839 
841  void messageEmitted( const QString &title, const QString &message, Qgis::MessageLevel = Qgis::Info );
842 
843  protected:
844 
846  bool event( QEvent *e ) override;
847 
849  void keyPressEvent( QKeyEvent *e ) override;
850 
852  void keyReleaseEvent( QKeyEvent *e ) override;
853 
855  void mouseDoubleClickEvent( QMouseEvent *e ) override;
856 
858  void mouseMoveEvent( QMouseEvent *e ) override;
859 
861  void mousePressEvent( QMouseEvent *e ) override;
862 
864  void mouseReleaseEvent( QMouseEvent *e ) override;
865 
867  void wheelEvent( QWheelEvent *e ) override;
868 
870  void resizeEvent( QResizeEvent *e ) override;
871 
873  void paintEvent( QPaintEvent *e ) override;
874 
876  void dragEnterEvent( QDragEnterEvent *e ) override;
877 
879  void moveCanvasContents( bool reset = false );
880 
883 
885  std::unique_ptr<CanvasProperties> mCanvasProperties;
886 
887 #if 0
888 
893  void connectNotify( const char *signal ) override;
894 #endif
895 
896  protected slots:
898  void updateCanvasItemPositions();
899 
900  private slots:
901 
902  void layerRepaintRequested( bool deferred );
903 
904  void autoRefreshTriggered();
905 
906  void updateAutoRefreshTimer();
907 
908  void projectThemesChanged();
909 
910  void startPreviewJob( int number );
911 
912  private:
913 
915  QgsMapSettings mSettings;
916 
918  QgsMapCanvasMap *mMap = nullptr;
919 
921  bool mFrozen = false;
922 
924  bool mRefreshScheduled = false;
925 
927  bool mRenderFlag = true;
928 
930  QPointer< QgsMapLayer > mCurrentLayer;
931 
933  QGraphicsScene *mScene = nullptr;
934 
936  QgsMapTool *mMapTool = nullptr;
937 
939  QgsMapTool *mLastNonZoomMapTool = nullptr;
940 
942  QList <QgsRectangle> mLastExtent;
943  int mLastExtentIndex = -1;
944 
946  double mWheelZoomFactor = 2.0;
947 
949  QTimer mMapUpdateTimer;
950 
952  QgsMapRendererQImageJob *mJob = nullptr;
953 
955  bool mJobCanceled = false;
956 
958  QgsLabelingResults *mLabelingResults = nullptr;
959 
961  bool mUseParallelRendering = false;
962 
964  bool mDrawRenderingStats = false;
965 
967  QgsMapRendererCache *mCache = nullptr;
968 
969  QTimer *mResizeTimer = nullptr;
970  QTimer *mRefreshTimer = nullptr;
971 
972  QgsPreviewEffect *mPreviewEffect = nullptr;
973 
974  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
975 
976  QgsSnappingUtils *mSnappingUtils = nullptr;
977 
978  QList< QgsMapRendererQImageJob * > mPreviewJobs;
979 
981  bool mScaleLocked = false;
982 
983  QgsExpressionContextScope mExpressionContextScope;
984 
986  QRect mZoomRect;
987 
989  bool mZoomDragging = false;
990 
992  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
993 
994  QCursor mZoomCursor;
995 
996  QTimer mAutoRefreshTimer;
997 
998  QTimer mPreviewTimer;
999  QMetaObject::Connection mPreviewTimerConnection;
1000 
1001  QString mTheme;
1002 
1003  QgsPointXY mCursorPoint;
1004 
1005  bool mAnnotationsVisible = true;
1006 
1007  bool mUsePreviewJobs = false;
1008 
1009  QHash< QString, int > mLastLayerRenderTime;
1010 
1015  QgsPointXY cursorPoint() const;
1016 
1021  void updateMapSize();
1022 
1028  void beginZoomRect( QPoint pos );
1029 
1035  void endZoomRect( QPoint pos );
1036 
1044  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
1045 
1046  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
1047 
1048  void startPreviewJobs();
1049  void stopPreviewJobs();
1050  void schedulePreviewJob( int number );
1051 
1052  friend class TestQgsMapCanvas;
1053 
1054 }; // class QgsMapCanvas
1055 
1056 // clazy:excludeall=qstring-allocations
1057 
1058 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Base class for all map layer types.
Definition: qgsmaplayer.h:64
std::unique_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:882
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:599
A class to represent a 2D point.
Definition: qgspointxy.h:43
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:106
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:549
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:423
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:559
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:469
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:463
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:568
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.