QGIS API Documentation  2.99.0-Master (7705179)
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 
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 = 0 );
93 
94  ~QgsMapCanvas();
95 
98  double magnificationFactor() const;
99 
111  void setLayers( const QList<QgsMapLayer *> &layers );
112 
113  void setCurrentLayer( QgsMapLayer *layer );
114 
117  const QgsMapSettings &mapSettings() const SIP_KEEPREFERENCE;
118 
121  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
122 
127  void setMapSettingsFlags( QgsMapSettings::Flags flags );
128 
131  const QgsLabelingResults *labelingResults() const;
132 
135  void setCachingEnabled( bool enabled );
136 
139  bool isCachingEnabled() const;
140 
143  void clearCache();
144 
147  void refreshAllLayers();
148 
158  void waitWhileRendering();
159 
162  void setParallelRenderingEnabled( bool enabled );
163 
166  bool isParallelRenderingEnabled() const;
167 
170  void setMapUpdateInterval( int timeMilliseconds );
171 
174  int mapUpdateInterval() const;
175 
180  double scale() const;
181 
183  double mapUnitsPerPixel() const;
184 
186  QgsRectangle extent() const;
188  QgsRectangle fullExtent() const;
189 
191  void setExtent( const QgsRectangle &r, bool magnified = false );
192 
195  double rotation() const;
196 
199  void setRotation( double degrees );
200 
203  void setCenter( const QgsPointXY &center );
204 
207  QgsPointXY center() const;
208 
210  void zoomToFullExtent();
211 
213  void zoomToPreviousExtent();
214 
216  void zoomToNextExtent();
217 
218  // ! Clears the list of extents and sets current extent as first item
219  void clearExtentHistory();
220 
224  void zoomToSelected( QgsVectorLayer *layer = nullptr );
225 
229  void zoomToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
230 
234  void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
235 
237  void panToSelected( QgsVectorLayer *layer = nullptr );
238 
240  void setMapTool( QgsMapTool *mapTool );
241 
248  void unsetMapTool( QgsMapTool *mapTool );
249 
251  QgsMapTool *mapTool();
252 
254  void setCanvasColor( const QColor &_newVal );
256  QColor canvasColor() const;
257 
260  void setSelectionColor( const QColor &color );
261 
263  void updateScale();
264 
266  QgsMapLayer *layer( int index );
267 
269  int layerCount() const;
270 
275  QList<QgsMapLayer *> layers() const;
276 
286  void freeze( bool frozen = true );
287 
295  bool isFrozen() const;
296 
305  bool renderFlag() const { return mRenderFlag; }
306 
311  QgsUnitTypes::DistanceUnit mapUnits() const;
312 
318  QMap<QString, QString> layerStyleOverrides() const;
319 
331  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
332 
351  void setTheme( const QString &theme );
352 
358  QString theme() const { return mTheme; }
359 
361  const QgsMapToPixel *getCoordinateTransform();
362 
364  bool isDrawing();
365 
367  QgsMapLayer *currentLayer();
368 
370  void setWheelFactor( double factor );
371 
376  void zoomScale( double scale );
377 
380  void zoomByFactor( double scaleFactor, const QgsPointXY *center = nullptr );
381 
383  void zoomWithCenter( int x, int y, bool zoomIn );
384 
387  void zoomToFeatureExtent( QgsRectangle &rect );
388 
392  bool scaleLocked() const { return mScaleLocked;}
393 
395  void enableAntiAliasing( bool flag );
396 
398  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
399 
401  void enableMapTileRendering( bool flag );
402 
403  // following 2 methods should be moved elsewhere or changed to private
404  // currently used by pan map tool
406  void panActionEnd( QPoint releasePoint );
407 
409  void panAction( QMouseEvent *event );
410 
412  QPoint mouseLastXY();
413 
418  void setPreviewModeEnabled( bool previewEnabled );
419 
425  bool previewModeEnabled() const;
426 
434  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
435 
442  QgsPreviewEffect::PreviewMode previewMode() const;
443 
451  QgsSnappingUtils *snappingUtils() const;
452 
460  void setSnappingUtils( QgsSnappingUtils *utils );
461 
470  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
471 
478  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
479 
485  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP
486 
489  void setSegmentationTolerance( double tolerance );
490 
493  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
494 
499  QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
500 
506  bool annotationsVisible() const { return mAnnotationsVisible; }
507 
513  void setAnnotationsVisible( bool visible );
514 
519  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
520 
525  const QgsLabelingEngineSettings &labelingEngineSettings() const;
526 
535  bool previewJobsEnabled() const;
536 
545  void setPreviewJobsEnabled( bool enabled );
546 
547  public slots:
548 
550  void refresh();
551 
553  void selectionChangedSlot();
554 
556  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
557 
559  void layerStateChange();
560 
562  void layerCrsChange();
563 
571  void setRenderFlag( bool flag );
572 
575  void stopRendering();
576 
578  void readProject( const QDomDocument & );
579 
581  void writeProject( QDomDocument & );
582 
584  void getDatumTransformInfo( const QgsMapLayer *ml, const QString &srcAuthId, const QString &destAuthId );
585 
590  void setMagnificationFactor( double factor );
591 
595  void setScaleLocked( bool isLocked );
596 
598  void zoomIn();
599 
601  void zoomOut();
602 
603  private slots:
605  void mapToolDestroyed();
606 
608  void rendererJobFinished();
609 
611  void previewJobFinished();
612 
613  void mapUpdateTimeout();
614 
615  void refreshMap();
616 
617  void mapThemeChanged( const QString &theme );
618 
619  signals:
620 
623  void xyCoordinates( const QgsPointXY &p );
624 
626  void scaleChanged( double );
627 
629  void extentsChanged();
630 
633  void rotationChanged( double );
634 
637  void magnificationChanged( double );
638 
646  void renderComplete( QPainter * );
650 
651  // ### QGIS 3: renamte to mapRefreshFinished()
653  void mapCanvasRefreshed();
654 
655  // ### QGIS 3: rename to mapRefreshStarted()
657  void renderStarting();
658 
660  void layersChanged();
661 
663  void keyPressed( QKeyEvent *e );
664 
666  void keyReleased( QKeyEvent *e );
667 
671  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
672 
673  // ### QGIS 3: remove the signal
675  void selectionChanged( QgsMapLayer *layer );
676 
678  void zoomLastStatusChanged( bool );
679 
681  void zoomNextStatusChanged( bool );
682 
685  void destinationCrsChanged();
686 
689  void currentLayerChanged( QgsMapLayer *layer );
690 
693  void layerStyleOverridesChanged();
694 
700  void themeChanged( const QString &theme );
701 
703  void messageEmitted( const QString &title, const QString &message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
704 
705  protected:
706 
708  bool event( QEvent *e ) override;
709 
711  void keyPressEvent( QKeyEvent *e ) override;
712 
714  void keyReleaseEvent( QKeyEvent *e ) override;
715 
717  void mouseDoubleClickEvent( QMouseEvent *e ) override;
718 
720  void mouseMoveEvent( QMouseEvent *e ) override;
721 
723  void mousePressEvent( QMouseEvent *e ) override;
724 
726  void mouseReleaseEvent( QMouseEvent *e ) override;
727 
729  void wheelEvent( QWheelEvent *e ) override;
730 
732  void resizeEvent( QResizeEvent *e ) override;
733 
735  void paintEvent( QPaintEvent *e ) override;
736 
738  void dragEnterEvent( QDragEnterEvent *e ) override;
739 
741  void moveCanvasContents( bool reset = false );
742 
745 
747  std::unique_ptr<CanvasProperties> mCanvasProperties;
748 
749 #if 0
750 
754  void connectNotify( const char *signal ) override;
755 #endif
756 
758  void updateDatumTransformEntries();
759 
760  protected slots:
762  void updateCanvasItemPositions();
763 
764  private slots:
765 
766  void layerRepaintRequested( bool deferred );
767 
768  void autoRefreshTriggered();
769 
770  void updateAutoRefreshTimer();
771 
772  void projectThemesChanged();
773 
774  void startPreviewJob( int number );
775 
776  private:
778 
785  QgsMapCanvas( QgsMapCanvas const & );
786 
788  QgsMapSettings mSettings;
789 
791  QgsMapCanvasMap *mMap = nullptr;
792 
794  bool mFrozen;
795 
797  bool mRefreshScheduled;
798 
800  bool mRenderFlag;
801 
803  QgsMapLayer *mCurrentLayer = nullptr;
804 
806  QGraphicsScene *mScene = nullptr;
807 
809  QgsMapTool *mMapTool = nullptr;
810 
812  QgsMapTool *mLastNonZoomMapTool = nullptr;
813 
815  QList <QgsRectangle> mLastExtent;
816  int mLastExtentIndex;
817 
819  double mWheelZoomFactor;
820 
822  QTimer mMapUpdateTimer;
823 
825  QgsMapRendererQImageJob *mJob = nullptr;
826 
828  bool mJobCanceled;
829 
831  QgsLabelingResults *mLabelingResults = nullptr;
832 
834  bool mUseParallelRendering;
835 
837  bool mDrawRenderingStats;
838 
840  QgsMapRendererCache *mCache = nullptr;
841 
842  QTimer *mResizeTimer = nullptr;
843  QTimer *mRefreshTimer = nullptr;
844 
845  QgsPreviewEffect *mPreviewEffect = nullptr;
846 
847  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
848 
849  QgsSnappingUtils *mSnappingUtils = nullptr;
850 
851  QList< QgsMapRendererQImageJob * > mPreviewJobs;
852 
854  bool mScaleLocked;
855 
856  QgsExpressionContextScope mExpressionContextScope;
857 
859  QRect mZoomRect;
860 
862  bool mZoomDragging;
863 
865  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
866 
867  QCursor mZoomCursor;
868 
869  QTimer mAutoRefreshTimer;
870 
871  QTimer mPreviewTimer;
872  QMetaObject::Connection mPreviewTimerConnection;
873 
874  QString mTheme;
875 
876  bool mAnnotationsVisible = true;
877 
878  bool mUsePreviewJobs = false;
879 
882  void updateMapSize();
883 
888  void beginZoomRect( QPoint pos );
889 
894  void endZoomRect( QPoint pos );
895 
902  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
903 
904  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
905 
906  void startPreviewJobs();
907  void stopPreviewJobs();
908  void schedulePreviewJob( int number );
909 
910  friend class TestQgsMapCanvas;
911 
912 }; // class QgsMapCanvas
913 
914 // clazy:excludeall=qstring-allocations
915 
916 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:38
Base class for all map layer types.
Definition: qgsmaplayer.h:54
std::unique_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:744
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
A widget that displays an overview map.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:519
bool annotationsVisible() const
Returns true if annotations are visible within the map canvas.
Definition: qgsmapcanvas.h:506
A class to represent a 2D point.
Definition: qgspointxy.h:42
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: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:35
#define SIP_SKIP
Definition: qgis_sip.h:119
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:36
Enable anti-aliasing for map rendering.
A class for highlight features on the map.
Definition: qgshighlight.h:39
#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:470
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:42
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:358
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:478
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:398
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:392
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:485
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.