QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 "qgis.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;
68 
74 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
75 {
76 
77 #ifdef SIP_RUN
79  if ( dynamic_cast<QgsMapCanvas *>( sipCpp ) != NULL )
80  sipType = sipType_QgsMapCanvas;
81  else
82  sipType = NULL;
83  SIP_END
84 #endif
85 
86  Q_OBJECT
87  Q_PROPERTY( QString theme READ theme WRITE setTheme NOTIFY themeChanged )
88  Q_PROPERTY( bool previewJobsEnabled READ previewJobsEnabled WRITE setPreviewJobsEnabled )
89 
90  public:
91 
93  QgsMapCanvas( QWidget *parent SIP_TRANSFERTHIS = nullptr );
94 
95  ~QgsMapCanvas() override;
96 
101  double magnificationFactor() const;
102 
114  void setLayers( const QList<QgsMapLayer *> &layers );
115 
116  void setCurrentLayer( QgsMapLayer *layer );
117 
122  const QgsMapSettings &mapSettings() const SIP_KEEPREFERENCE;
123 
128  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
129 
134  void setMapSettingsFlags( QgsMapSettings::Flags flags );
135 
140  const QgsLabelingResults *labelingResults() const;
141 
146  void setCachingEnabled( bool enabled );
147 
152  bool isCachingEnabled() const;
153 
158  void clearCache();
159 
169  void waitWhileRendering();
170 
175  void setParallelRenderingEnabled( bool enabled );
176 
181  bool isParallelRenderingEnabled() const;
182 
187  void setMapUpdateInterval( int timeMilliseconds );
188 
193  int mapUpdateInterval() const;
194 
199  double scale() const;
200 
202  double mapUnitsPerPixel() const;
203 
205  QgsRectangle extent() const;
207  QgsRectangle fullExtent() const;
208 
210  void setExtent( const QgsRectangle &r, bool magnified = false );
211 
216  double rotation() const;
217 
222  void setRotation( double degrees );
223 
228  void setCenter( const QgsPointXY &center );
229 
234  QgsPointXY center() const;
235 
237  void zoomToFullExtent();
238 
240  void zoomToPreviousExtent();
241 
243  void zoomToNextExtent();
244 
245  // ! Clears the list of extents and sets current extent as first item
246  void clearExtentHistory();
247 
248 
253  void zoomToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
254 
259  void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
260 
262  void panToSelected( QgsVectorLayer *layer = nullptr );
263 
276  void flashFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids,
277  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
278  int flashes = 3, int duration = 500 );
279 
292  void flashGeometries( const QList< QgsGeometry > &geometries, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(),
293  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
294  int flashes = 3, int duration = 500 );
295 
297  void setMapTool( QgsMapTool *mapTool, bool clean = false );
298 
306  void unsetMapTool( QgsMapTool *mapTool );
307 
309  QgsMapTool *mapTool();
310 
312  void setCanvasColor( const QColor &_newVal );
314  QColor canvasColor() const;
315 
320  void setSelectionColor( const QColor &color );
321 
326  QColor selectionColor() const;
327 
329  void updateScale();
330 
332  QgsMapLayer *layer( int index );
333 
335  int layerCount() const;
336 
341  QList<QgsMapLayer *> layers() const;
342 
352  void freeze( bool frozen = true );
353 
361  bool isFrozen() const;
362 
371  bool renderFlag() const { return mRenderFlag; }
372 
377  QgsUnitTypes::DistanceUnit mapUnits() const;
378 
384  QMap<QString, QString> layerStyleOverrides() const;
385 
397  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
398 
417  void setTheme( const QString &theme );
418 
424  QString theme() const { return mTheme; }
425 
427  const QgsMapToPixel *getCoordinateTransform();
428 
430  bool isDrawing();
431 
433  QgsMapLayer *currentLayer();
434 
436  void setWheelFactor( double factor );
437 
442  void zoomScale( double scale );
443 
448  void zoomByFactor( double scaleFactor, const QgsPointXY *center = nullptr );
449 
451  void zoomWithCenter( int x, int y, bool zoomIn );
452 
457  void zoomToFeatureExtent( QgsRectangle &rect );
458 
464  bool scaleLocked() const { return mScaleLocked;}
465 
467  void enableAntiAliasing( bool flag );
468 
470  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
471 
473  void enableMapTileRendering( bool flag );
474 
475  // following 2 methods should be moved elsewhere or changed to private
476  // currently used by pan map tool
478  void panActionEnd( QPoint releasePoint );
479 
481  void panAction( QMouseEvent *event );
482 
484  QPoint mouseLastXY();
485 
491  void setPreviewModeEnabled( bool previewEnabled );
492 
499  bool previewModeEnabled() const;
500 
509  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
510 
518  QgsPreviewEffect::PreviewMode previewMode() const;
519 
528  QgsSnappingUtils *snappingUtils() const;
529 
538  void setSnappingUtils( QgsSnappingUtils *utils );
539 
550  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
551 
560  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
561 
569  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP
570 
577  QgsExpressionContextScope *defaultExpressionContextScope() SIP_FACTORY;
578 
582  void setSegmentationTolerance( double tolerance );
583 
587  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
588 
593  QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
594 
600  bool annotationsVisible() const { return mAnnotationsVisible; }
601 
607  void setAnnotationsVisible( bool visible );
608 
613  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
614 
619  const QgsLabelingEngineSettings &labelingEngineSettings() const;
620 
629  bool previewJobsEnabled() const;
630 
639  void setPreviewJobsEnabled( bool enabled );
640 
641  public slots:
642 
644  void refresh();
645 
650  void refreshAllLayers();
651 
653  void selectionChangedSlot();
654 
656  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
657 
659  void layerStateChange();
660 
668  void setRenderFlag( bool flag );
669 
674  void stopRendering();
675 
677  void readProject( const QDomDocument & );
678 
680  void writeProject( QDomDocument & );
681 
682 #if 0
683  void getDatumTransformInfo( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination );
685 #endif
686 
693  void setMagnificationFactor( double factor );
694 
700  void setScaleLocked( bool isLocked );
701 
703  void zoomIn();
704 
706  void zoomOut();
707 
712  void zoomToSelected( QgsVectorLayer *layer = nullptr );
713 
714  private slots:
716  void mapToolDestroyed();
717 
719  void rendererJobFinished();
720 
722  void previewJobFinished();
723 
724  void mapUpdateTimeout();
725 
726  void refreshMap();
727 
728  void mapThemeChanged( const QString &theme );
729 
730  signals:
731 
735  void xyCoordinates( const QgsPointXY &p );
736 
738  void scaleChanged( double );
739 
741  void extentsChanged();
742 
747  void rotationChanged( double );
748 
753  void magnificationChanged( double );
754 
759  void canvasColorChanged();
760 
775  void renderComplete( QPainter * );
776 
777  // ### QGIS 3: renamte to mapRefreshFinished()
779  void mapCanvasRefreshed();
780 
781  // ### QGIS 3: rename to mapRefreshStarted()
783  void renderStarting();
784 
786  void layersChanged();
787 
789  void keyPressed( QKeyEvent *e );
790 
792  void keyReleased( QKeyEvent *e );
793 
798  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
799 
800 
802  void selectionChanged( QgsVectorLayer *layer );
803 
805  void zoomLastStatusChanged( bool );
806 
808  void zoomNextStatusChanged( bool );
809 
814  void destinationCrsChanged();
815 
820  void transformContextChanged();
821 
826  void currentLayerChanged( QgsMapLayer *layer );
827 
832  void layerStyleOverridesChanged();
833 
839  void themeChanged( const QString &theme );
840 
842  void messageEmitted( const QString &title, const QString &message, Qgis::MessageLevel = Qgis::Info );
843 
844  protected:
845 
847  bool event( QEvent *e ) override;
848 
850  void keyPressEvent( QKeyEvent *e ) override;
851 
853  void keyReleaseEvent( QKeyEvent *e ) override;
854 
856  void mouseDoubleClickEvent( QMouseEvent *e ) override;
857 
859  void mouseMoveEvent( QMouseEvent *e ) override;
860 
862  void mousePressEvent( QMouseEvent *e ) override;
863 
865  void mouseReleaseEvent( QMouseEvent *e ) override;
866 
868  void wheelEvent( QWheelEvent *e ) override;
869 
871  void resizeEvent( QResizeEvent *e ) override;
872 
874  void paintEvent( QPaintEvent *e ) override;
875 
877  void dragEnterEvent( QDragEnterEvent *e ) override;
878 
880  void moveCanvasContents( bool reset = false );
881 
884 
886  std::unique_ptr<CanvasProperties> mCanvasProperties;
887 
888 #if 0
889 
894  void connectNotify( const char *signal ) override;
895 #endif
896 
897  protected slots:
899  void updateCanvasItemPositions();
900 
901  private slots:
902 
903  void layerRepaintRequested( bool deferred );
904 
905  void autoRefreshTriggered();
906 
907  void updateAutoRefreshTimer();
908 
909  void projectThemesChanged();
910 
911  void startPreviewJob( int number );
912 
913  private:
914 
916  QgsMapSettings mSettings;
917 
919  QgsMapCanvasMap *mMap = nullptr;
920 
922  bool mFrozen = false;
923 
925  bool mRefreshScheduled = false;
926 
928  bool mRenderFlag = true;
929 
931  QPointer< QgsMapLayer > mCurrentLayer;
932 
934  QGraphicsScene *mScene = nullptr;
935 
937  QgsMapTool *mMapTool = nullptr;
938 
940  QgsMapTool *mLastNonZoomMapTool = nullptr;
941 
943  QList <QgsRectangle> mLastExtent;
944  int mLastExtentIndex = -1;
945 
947  double mWheelZoomFactor = 2.0;
948 
950  QTimer mMapUpdateTimer;
951 
953  QgsMapRendererQImageJob *mJob = nullptr;
954 
956  bool mJobCanceled = false;
957 
959  QgsLabelingResults *mLabelingResults = nullptr;
960 
962  bool mUseParallelRendering = false;
963 
965  bool mDrawRenderingStats = false;
966 
968  QgsMapRendererCache *mCache = nullptr;
969 
970  QTimer *mResizeTimer = nullptr;
971  QTimer *mRefreshTimer = nullptr;
972 
973  QgsPreviewEffect *mPreviewEffect = nullptr;
974 
975  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
976 
977  QgsSnappingUtils *mSnappingUtils = nullptr;
978 
979  QList< QgsMapRendererQImageJob * > mPreviewJobs;
980 
982  bool mScaleLocked = false;
983 
984  QgsExpressionContextScope mExpressionContextScope;
985 
987  QRect mZoomRect;
988 
990  bool mZoomDragging = false;
991 
993  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
994 
995  QCursor mZoomCursor;
996 
997  QTimer mAutoRefreshTimer;
998 
999  QTimer mPreviewTimer;
1000  QMetaObject::Connection mPreviewTimerConnection;
1001 
1002  QString mTheme;
1003 
1004  QgsPointXY mCursorPoint;
1005 
1006  bool mAnnotationsVisible = true;
1007 
1008  bool mUsePreviewJobs = false;
1009 
1010  QHash< QString, int > mLastLayerRenderTime;
1011 
1016  QgsPointXY cursorPoint() const;
1017 
1022  void updateMapSize();
1023 
1029  void beginZoomRect( QPoint pos );
1030 
1036  void endZoomRect( QPoint pos );
1037 
1045  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
1046 
1047  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
1048 
1049  void startPreviewJobs();
1050  void stopPreviewJobs();
1051  void schedulePreviewJob( int number );
1052 
1053  friend class TestQgsMapCanvas;
1054 
1055 }; // class QgsMapCanvas
1056 
1057 // clazy:excludeall=qstring-allocations
1058 
1059 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:40
Base class for all map layer types.
Definition: qgsmaplayer.h:63
std::unique_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:883
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
A widget that displays an overview map.
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:79
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:470
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
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:36
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:569
#define SIP_SKIP
Definition: qgis_sip.h:119
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:47
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: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:550
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:464
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:53
Abstract base class for all map tools.
Definition: qgsmaptool.h:62
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:560
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.
bool annotationsVisible() const
Returns true if annotations are visible within the map canvas.
Definition: qgsmapcanvas.h:600
QString theme() const
Returns the map&#39;s theme shown in the canvas, if set.
Definition: qgsmapcanvas.h:424