QGIS API Documentation  2.99.0-Master (ae4d26a)
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 "qgsfeature.h"
26 #include "qgsmessagebar.h"
27 #include "qgsrectangle.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 = 0 );
94 
95  ~QgsMapCanvas();
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 
164  void refreshAllLayers();
165 
175  void waitWhileRendering();
176 
181  void setParallelRenderingEnabled( bool enabled );
182 
187  bool isParallelRenderingEnabled() const;
188 
193  void setMapUpdateInterval( int timeMilliseconds );
194 
199  int mapUpdateInterval() const;
200 
205  double scale() const;
206 
208  double mapUnitsPerPixel() const;
209 
211  QgsRectangle extent() const;
213  QgsRectangle fullExtent() const;
214 
216  void setExtent( const QgsRectangle &r, bool magnified = false );
217 
222  double rotation() const;
223 
228  void setRotation( double degrees );
229 
234  void setCenter( const QgsPointXY &center );
235 
240  QgsPointXY center() const;
241 
243  void zoomToFullExtent();
244 
246  void zoomToPreviousExtent();
247 
249  void zoomToNextExtent();
250 
251  // ! Clears the list of extents and sets current extent as first item
252  void clearExtentHistory();
253 
258  void zoomToSelected( QgsVectorLayer *layer = nullptr );
259 
264  void zoomToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
265 
270  void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
271 
273  void panToSelected( QgsVectorLayer *layer = nullptr );
274 
287  void flashFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids,
288  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
289  int flashes = 3, int duration = 500 );
290 
303  void flashGeometries( const QList< QgsGeometry > &geometries, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(),
304  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
305  int flashes = 3, int duration = 500 );
306 
308  void setMapTool( QgsMapTool *mapTool );
309 
317  void unsetMapTool( QgsMapTool *mapTool );
318 
320  QgsMapTool *mapTool();
321 
323  void setCanvasColor( const QColor &_newVal );
325  QColor canvasColor() const;
326 
331  void setSelectionColor( const QColor &color );
332 
337  QColor selectionColor() const;
338 
340  void updateScale();
341 
343  QgsMapLayer *layer( int index );
344 
346  int layerCount() const;
347 
352  QList<QgsMapLayer *> layers() const;
353 
363  void freeze( bool frozen = true );
364 
372  bool isFrozen() const;
373 
382  bool renderFlag() const { return mRenderFlag; }
383 
388  QgsUnitTypes::DistanceUnit mapUnits() const;
389 
395  QMap<QString, QString> layerStyleOverrides() const;
396 
408  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
409 
428  void setTheme( const QString &theme );
429 
435  QString theme() const { return mTheme; }
436 
438  const QgsMapToPixel *getCoordinateTransform();
439 
441  bool isDrawing();
442 
444  QgsMapLayer *currentLayer();
445 
447  void setWheelFactor( double factor );
448 
453  void zoomScale( double scale );
454 
459  void zoomByFactor( double scaleFactor, const QgsPointXY *center = nullptr );
460 
462  void zoomWithCenter( int x, int y, bool zoomIn );
463 
468  void zoomToFeatureExtent( QgsRectangle &rect );
469 
475  bool scaleLocked() const { return mScaleLocked;}
476 
478  void enableAntiAliasing( bool flag );
479 
481  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
482 
484  void enableMapTileRendering( bool flag );
485 
486  // following 2 methods should be moved elsewhere or changed to private
487  // currently used by pan map tool
489  void panActionEnd( QPoint releasePoint );
490 
492  void panAction( QMouseEvent *event );
493 
495  QPoint mouseLastXY();
496 
502  void setPreviewModeEnabled( bool previewEnabled );
503 
510  bool previewModeEnabled() const;
511 
520  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
521 
529  QgsPreviewEffect::PreviewMode previewMode() const;
530 
539  QgsSnappingUtils *snappingUtils() const;
540 
549  void setSnappingUtils( QgsSnappingUtils *utils );
550 
560  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
561 
569  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
570 
577  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP
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 
647  void selectionChangedSlot();
648 
650  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
651 
653  void layerStateChange();
654 
656  void layerCrsChange();
657 
665  void setRenderFlag( bool flag );
666 
671  void stopRendering();
672 
674  void readProject( const QDomDocument & );
675 
677  void writeProject( QDomDocument & );
678 
680  void getDatumTransformInfo( const QgsMapLayer *ml, const QString &srcAuthId, const QString &destAuthId );
681 
688  void setMagnificationFactor( double factor );
689 
695  void setScaleLocked( bool isLocked );
696 
698  void zoomIn();
699 
701  void zoomOut();
702 
703  private slots:
705  void mapToolDestroyed();
706 
708  void rendererJobFinished();
709 
711  void previewJobFinished();
712 
713  void mapUpdateTimeout();
714 
715  void refreshMap();
716 
717  void mapThemeChanged( const QString &theme );
718 
719  signals:
720 
724  void xyCoordinates( const QgsPointXY &p );
725 
727  void scaleChanged( double );
728 
730  void extentsChanged();
731 
736  void rotationChanged( double );
737 
742  void magnificationChanged( double );
743 
748  void canvasColorChanged();
749 
764  void renderComplete( QPainter * );
765 
766  // ### QGIS 3: renamte to mapRefreshFinished()
768  void mapCanvasRefreshed();
769 
770  // ### QGIS 3: rename to mapRefreshStarted()
772  void renderStarting();
773 
775  void layersChanged();
776 
778  void keyPressed( QKeyEvent *e );
779 
781  void keyReleased( QKeyEvent *e );
782 
787  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
788 
789  // ### QGIS 3: remove the signal
791  void selectionChanged( QgsMapLayer *layer );
792 
794  void zoomLastStatusChanged( bool );
795 
797  void zoomNextStatusChanged( bool );
798 
803  void destinationCrsChanged();
804 
809  void currentLayerChanged( QgsMapLayer *layer );
810 
815  void layerStyleOverridesChanged();
816 
822  void themeChanged( const QString &theme );
823 
825  void messageEmitted( const QString &title, const QString &message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
826 
827  protected:
828 
830  bool event( QEvent *e ) override;
831 
833  void keyPressEvent( QKeyEvent *e ) override;
834 
836  void keyReleaseEvent( QKeyEvent *e ) override;
837 
839  void mouseDoubleClickEvent( QMouseEvent *e ) override;
840 
842  void mouseMoveEvent( QMouseEvent *e ) override;
843 
845  void mousePressEvent( QMouseEvent *e ) override;
846 
848  void mouseReleaseEvent( QMouseEvent *e ) override;
849 
851  void wheelEvent( QWheelEvent *e ) override;
852 
854  void resizeEvent( QResizeEvent *e ) override;
855 
857  void paintEvent( QPaintEvent *e ) override;
858 
860  void dragEnterEvent( QDragEnterEvent *e ) override;
861 
863  void moveCanvasContents( bool reset = false );
864 
867 
869  std::unique_ptr<CanvasProperties> mCanvasProperties;
870 
871 #if 0
872 
877  void connectNotify( const char *signal ) override;
878 #endif
879 
881  void updateDatumTransformEntries();
882 
883  protected slots:
885  void updateCanvasItemPositions();
886 
887  private slots:
888 
889  void layerRepaintRequested( bool deferred );
890 
891  void autoRefreshTriggered();
892 
893  void updateAutoRefreshTimer();
894 
895  void projectThemesChanged();
896 
897  void startPreviewJob( int number );
898 
899  private:
901 
908  QgsMapCanvas( QgsMapCanvas const & );
909 
911  QgsMapSettings mSettings;
912 
914  QgsMapCanvasMap *mMap = nullptr;
915 
917  bool mFrozen = false;
918 
920  bool mRefreshScheduled = false;
921 
923  bool mRenderFlag = true;
924 
926  QgsMapLayer *mCurrentLayer = nullptr;
927 
929  QGraphicsScene *mScene = nullptr;
930 
932  QgsMapTool *mMapTool = nullptr;
933 
935  QgsMapTool *mLastNonZoomMapTool = nullptr;
936 
938  QList <QgsRectangle> mLastExtent;
939  int mLastExtentIndex = -1;
940 
942  double mWheelZoomFactor = 2.0;
943 
945  QTimer mMapUpdateTimer;
946 
948  QgsMapRendererQImageJob *mJob = nullptr;
949 
951  bool mJobCanceled = false;
952 
954  QgsLabelingResults *mLabelingResults = nullptr;
955 
957  bool mUseParallelRendering = false;
958 
960  bool mDrawRenderingStats = false;
961 
963  QgsMapRendererCache *mCache = nullptr;
964 
965  QTimer *mResizeTimer = nullptr;
966  QTimer *mRefreshTimer = nullptr;
967 
968  QgsPreviewEffect *mPreviewEffect = nullptr;
969 
970  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
971 
972  QgsSnappingUtils *mSnappingUtils = nullptr;
973 
974  QList< QgsMapRendererQImageJob * > mPreviewJobs;
975 
977  bool mScaleLocked = false;
978 
979  QgsExpressionContextScope mExpressionContextScope;
980 
982  QRect mZoomRect;
983 
985  bool mZoomDragging = false;
986 
988  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
989 
990  QCursor mZoomCursor;
991 
992  QTimer mAutoRefreshTimer;
993 
994  QTimer mPreviewTimer;
995  QMetaObject::Connection mPreviewTimerConnection;
996 
997  QString mTheme;
998 
999  bool mAnnotationsVisible = true;
1000 
1001  bool mUsePreviewJobs = false;
1002 
1007  void updateMapSize();
1008 
1014  void beginZoomRect( QPoint pos );
1015 
1021  void endZoomRect( QPoint pos );
1022 
1030  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
1031 
1032  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
1033 
1034  void startPreviewJobs();
1035  void stopPreviewJobs();
1036  void schedulePreviewJob( int number );
1037 
1038  friend class TestQgsMapCanvas;
1039 
1040 }; // class QgsMapCanvas
1041 
1042 // clazy:excludeall=qstring-allocations
1043 
1044 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:39
Base class for all map layer types.
Definition: qgsmaplayer.h:56
std::unique_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:866
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
A widget that displays an overview map.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:544
bool annotationsVisible() const
Returns true if annotations are visible within the map canvas.
Definition: qgsmapcanvas.h:600
A class to represent a 2D point.
Definition: qgspointxy.h:43
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:94
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
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
#define SIP_SKIP
Definition: qgis_sip.h:119
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:37
Enable anti-aliasing for map rendering.
A class for highlight features on the map.
Definition: qgshighlight.h:40
#define SIP_END
Definition: qgis_sip.h:175
#define SIP_KEEPREFERENCE
Definition: qgis_sip.h:79
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:560
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:43
Abstract base class for all map tools.
Definition: qgsmaptool.h:63
QString theme() const
Returns the map&#39;s theme shown in the canvas, if set.
Definition: qgsmapcanvas.h:435
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:569
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:481
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:475
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:577
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.