QGIS API Documentation  2.99.0-Master (6c64c5a)
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 = 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 
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, bool clean = false );
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 
662  void setRenderFlag( bool flag );
663 
668  void stopRendering();
669 
671  void readProject( const QDomDocument & );
672 
674  void writeProject( QDomDocument & );
675 
676 #if 0
677  void getDatumTransformInfo( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination );
679 #endif
680 
687  void setMagnificationFactor( double factor );
688 
694  void setScaleLocked( bool isLocked );
695 
697  void zoomIn();
698 
700  void zoomOut();
701 
702  private slots:
704  void mapToolDestroyed();
705 
707  void rendererJobFinished();
708 
710  void previewJobFinished();
711 
712  void mapUpdateTimeout();
713 
714  void refreshMap();
715 
716  void mapThemeChanged( const QString &theme );
717 
718  signals:
719 
723  void xyCoordinates( const QgsPointXY &p );
724 
726  void scaleChanged( double );
727 
729  void extentsChanged();
730 
735  void rotationChanged( double );
736 
741  void magnificationChanged( double );
742 
747  void canvasColorChanged();
748 
763  void renderComplete( QPainter * );
764 
765  // ### QGIS 3: renamte to mapRefreshFinished()
767  void mapCanvasRefreshed();
768 
769  // ### QGIS 3: rename to mapRefreshStarted()
771  void renderStarting();
772 
774  void layersChanged();
775 
777  void keyPressed( QKeyEvent *e );
778 
780  void keyReleased( QKeyEvent *e );
781 
786  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
787 
788  // ### QGIS 3: remove the signal
790  void selectionChanged( QgsMapLayer *layer );
791 
793  void zoomLastStatusChanged( bool );
794 
796  void zoomNextStatusChanged( bool );
797 
802  void destinationCrsChanged();
803 
808  void transformContextChanged();
809 
814  void currentLayerChanged( QgsMapLayer *layer );
815 
820  void layerStyleOverridesChanged();
821 
827  void themeChanged( const QString &theme );
828 
830  void messageEmitted( const QString &title, const QString &message, Qgis::MessageLevel = Qgis::Info );
831 
832  protected:
833 
835  bool event( QEvent *e ) override;
836 
838  void keyPressEvent( QKeyEvent *e ) override;
839 
841  void keyReleaseEvent( QKeyEvent *e ) override;
842 
844  void mouseDoubleClickEvent( QMouseEvent *e ) override;
845 
847  void mouseMoveEvent( QMouseEvent *e ) override;
848 
850  void mousePressEvent( QMouseEvent *e ) override;
851 
853  void mouseReleaseEvent( QMouseEvent *e ) override;
854 
856  void wheelEvent( QWheelEvent *e ) override;
857 
859  void resizeEvent( QResizeEvent *e ) override;
860 
862  void paintEvent( QPaintEvent *e ) override;
863 
865  void dragEnterEvent( QDragEnterEvent *e ) override;
866 
868  void moveCanvasContents( bool reset = false );
869 
872 
874  std::unique_ptr<CanvasProperties> mCanvasProperties;
875 
876 #if 0
877 
882  void connectNotify( const char *signal ) override;
883 #endif
884 
885  protected slots:
887  void updateCanvasItemPositions();
888 
889  private slots:
890 
891  void layerRepaintRequested( bool deferred );
892 
893  void autoRefreshTriggered();
894 
895  void updateAutoRefreshTimer();
896 
897  void projectThemesChanged();
898 
899  void startPreviewJob( int number );
900 
901  private:
903 
910  QgsMapCanvas( QgsMapCanvas const & );
911 
913  QgsMapSettings mSettings;
914 
916  QgsMapCanvasMap *mMap = nullptr;
917 
919  bool mFrozen = false;
920 
922  bool mRefreshScheduled = false;
923 
925  bool mRenderFlag = true;
926 
928  QgsMapLayer *mCurrentLayer = nullptr;
929 
931  QGraphicsScene *mScene = nullptr;
932 
934  QgsMapTool *mMapTool = nullptr;
935 
937  QgsMapTool *mLastNonZoomMapTool = nullptr;
938 
940  QList <QgsRectangle> mLastExtent;
941  int mLastExtentIndex = -1;
942 
944  double mWheelZoomFactor = 2.0;
945 
947  QTimer mMapUpdateTimer;
948 
950  QgsMapRendererQImageJob *mJob = nullptr;
951 
953  bool mJobCanceled = false;
954 
956  QgsLabelingResults *mLabelingResults = nullptr;
957 
959  bool mUseParallelRendering = false;
960 
962  bool mDrawRenderingStats = false;
963 
965  QgsMapRendererCache *mCache = nullptr;
966 
967  QTimer *mResizeTimer = nullptr;
968  QTimer *mRefreshTimer = nullptr;
969 
970  QgsPreviewEffect *mPreviewEffect = nullptr;
971 
972  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
973 
974  QgsSnappingUtils *mSnappingUtils = nullptr;
975 
976  QList< QgsMapRendererQImageJob * > mPreviewJobs;
977 
979  bool mScaleLocked = false;
980 
981  QgsExpressionContextScope mExpressionContextScope;
982 
984  QRect mZoomRect;
985 
987  bool mZoomDragging = false;
988 
990  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
991 
992  QCursor mZoomCursor;
993 
994  QTimer mAutoRefreshTimer;
995 
996  QTimer mPreviewTimer;
997  QMetaObject::Connection mPreviewTimerConnection;
998 
999  QString mTheme;
1000 
1001  bool mAnnotationsVisible = true;
1002 
1003  bool mUsePreviewJobs = false;
1004 
1005  QHash< QString, int > mLastLayerRenderTime;
1006 
1011  void updateMapSize();
1012 
1018  void beginZoomRect( QPoint pos );
1019 
1025  void endZoomRect( QPoint pos );
1026 
1034  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
1035 
1036  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
1037 
1038  void startPreviewJobs();
1039  void stopPreviewJobs();
1040  void schedulePreviewJob( int number );
1041 
1042  friend class TestQgsMapCanvas;
1043 
1044 }; // class QgsMapCanvas
1045 
1046 // clazy:excludeall=qstring-allocations
1047 
1048 #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:871
#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:111
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:78
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:49
#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.