QGIS API Documentation  2.99.0-Master (b95d432)
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 
88  public:
89 
91  QgsMapCanvas( QWidget *parent SIP_TRANSFERTHIS = 0 );
92 
93  ~QgsMapCanvas();
94 
97  double magnificationFactor() const;
98 
110  void setLayers( const QList<QgsMapLayer *> &layers );
111 
112  void setCurrentLayer( QgsMapLayer *layer );
113 
116  const QgsMapSettings &mapSettings() const SIP_KEEPREFERENCE;
117 
120  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
121 
126  void setMapSettingsFlags( QgsMapSettings::Flags flags );
127 
130  const QgsLabelingResults *labelingResults() const;
131 
134  void setCachingEnabled( bool enabled );
135 
138  bool isCachingEnabled() const;
139 
142  void clearCache();
143 
146  void refreshAllLayers();
147 
157  void waitWhileRendering();
158 
161  void setParallelRenderingEnabled( bool enabled );
162 
165  bool isParallelRenderingEnabled() const;
166 
169  void setMapUpdateInterval( int timeMilliseconds );
170 
173  int mapUpdateInterval() const;
174 
179  double scale() const;
180 
182  double mapUnitsPerPixel() const;
183 
185  QgsRectangle extent() const;
187  QgsRectangle fullExtent() const;
188 
190  void setExtent( const QgsRectangle &r, bool magnified = false );
191 
194  double rotation() const;
195 
198  void setRotation( double degrees );
199 
202  void setCenter( const QgsPointXY &center );
203 
206  QgsPointXY center() const;
207 
209  void zoomToFullExtent();
210 
212  void zoomToPreviousExtent();
213 
215  void zoomToNextExtent();
216 
217  // ! Clears the list of extents and sets current extent as first item
218  void clearExtentHistory();
219 
223  void zoomToSelected( QgsVectorLayer *layer = nullptr );
224 
228  void zoomToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
229 
233  void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
234 
236  void panToSelected( QgsVectorLayer *layer = nullptr );
237 
239  void setMapTool( QgsMapTool *mapTool );
240 
247  void unsetMapTool( QgsMapTool *mapTool );
248 
250  QgsMapTool *mapTool();
251 
253  void setCanvasColor( const QColor &_newVal );
255  QColor canvasColor() const;
256 
259  void setSelectionColor( const QColor &color );
260 
262  void updateScale();
263 
265  QgsMapLayer *layer( int index );
266 
268  int layerCount() const;
269 
274  QList<QgsMapLayer *> layers() const;
275 
285  void freeze( bool frozen = true );
286 
294  bool isFrozen() const;
295 
304  bool renderFlag() const { return mRenderFlag; }
305 
310  QgsUnitTypes::DistanceUnit mapUnits() const;
311 
317  QMap<QString, QString> layerStyleOverrides() const;
318 
330  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
331 
350  void setTheme( const QString &theme );
351 
357  QString theme() const { return mTheme; }
358 
360  const QgsMapToPixel *getCoordinateTransform();
361 
363  bool isDrawing();
364 
366  QgsMapLayer *currentLayer();
367 
369  void setWheelFactor( double factor );
370 
375  void zoomScale( double scale );
376 
379  void zoomByFactor( double scaleFactor, const QgsPointXY *center = nullptr );
380 
382  void zoomWithCenter( int x, int y, bool zoomIn );
383 
386  void zoomToFeatureExtent( QgsRectangle &rect );
387 
391  bool scaleLocked() const { return mScaleLocked;}
392 
394  void enableAntiAliasing( bool flag );
395 
397  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
398 
400  void enableMapTileRendering( bool flag );
401 
402  // following 2 methods should be moved elsewhere or changed to private
403  // currently used by pan map tool
405  void panActionEnd( QPoint releasePoint );
406 
408  void panAction( QMouseEvent *event );
409 
411  QPoint mouseLastXY();
412 
417  void setPreviewModeEnabled( bool previewEnabled );
418 
424  bool previewModeEnabled() const;
425 
433  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
434 
441  QgsPreviewEffect::PreviewMode previewMode() const;
442 
450  QgsSnappingUtils *snappingUtils() const;
451 
459  void setSnappingUtils( QgsSnappingUtils *utils );
460 
469  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
470 
477  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
478 
484  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP
485 
488  void setSegmentationTolerance( double tolerance );
489 
492  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
493 
498  QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
499 
505  bool annotationsVisible() const { return mAnnotationsVisible; }
506 
512  void setAnnotationsVisible( bool visible );
513 
518  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
519 
524  const QgsLabelingEngineSettings &labelingEngineSettings() const;
525 
526  public slots:
527 
529  void refresh();
530 
532  void selectionChangedSlot();
533 
535  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
536 
538  void layerStateChange();
539 
541  void layerCrsChange();
542 
550  void setRenderFlag( bool flag );
551 
554  void stopRendering();
555 
557  void readProject( const QDomDocument & );
558 
560  void writeProject( QDomDocument & );
561 
563  void getDatumTransformInfo( const QgsMapLayer *ml, const QString &srcAuthId, const QString &destAuthId );
564 
569  void setMagnificationFactor( double factor );
570 
574  void setScaleLocked( bool isLocked );
575 
577  void zoomIn();
578 
580  void zoomOut();
581 
582  private slots:
584  void mapToolDestroyed();
585 
587  void rendererJobFinished();
588 
590  void previewJobFinished();
591 
592  void mapUpdateTimeout();
593 
594  void refreshMap();
595 
596  void mapThemeChanged( const QString &theme );
597 
598  signals:
599 
602  void xyCoordinates( const QgsPointXY &p );
603 
605  void scaleChanged( double );
606 
608  void extentsChanged();
609 
612  void rotationChanged( double );
613 
616  void magnificationChanged( double );
617 
625  void renderComplete( QPainter * );
629 
630  // ### QGIS 3: renamte to mapRefreshFinished()
632  void mapCanvasRefreshed();
633 
634  // ### QGIS 3: rename to mapRefreshStarted()
636  void renderStarting();
637 
639  void layersChanged();
640 
642  void keyPressed( QKeyEvent *e );
643 
645  void keyReleased( QKeyEvent *e );
646 
650  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
651 
652  // ### QGIS 3: remove the signal
654  void selectionChanged( QgsMapLayer *layer );
655 
657  void zoomLastStatusChanged( bool );
658 
660  void zoomNextStatusChanged( bool );
661 
664  void destinationCrsChanged();
665 
668  void currentLayerChanged( QgsMapLayer *layer );
669 
672  void layerStyleOverridesChanged();
673 
679  void themeChanged( const QString &theme );
680 
682  void messageEmitted( const QString &title, const QString &message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
683 
684  protected:
685 
687  bool event( QEvent *e ) override;
688 
690  void keyPressEvent( QKeyEvent *e ) override;
691 
693  void keyReleaseEvent( QKeyEvent *e ) override;
694 
696  void mouseDoubleClickEvent( QMouseEvent *e ) override;
697 
699  void mouseMoveEvent( QMouseEvent *e ) override;
700 
702  void mousePressEvent( QMouseEvent *e ) override;
703 
705  void mouseReleaseEvent( QMouseEvent *e ) override;
706 
708  void wheelEvent( QWheelEvent *e ) override;
709 
711  void resizeEvent( QResizeEvent *e ) override;
712 
714  void paintEvent( QPaintEvent *e ) override;
715 
717  void dragEnterEvent( QDragEnterEvent *e ) override;
718 
720  void moveCanvasContents( bool reset = false );
721 
724 
726  std::unique_ptr<CanvasProperties> mCanvasProperties;
727 
728 #if 0
729 
733  void connectNotify( const char *signal ) override;
734 #endif
735 
737  void updateDatumTransformEntries();
738 
739  protected slots:
741  void updateCanvasItemPositions();
742 
743  private slots:
744 
745  void layerRepaintRequested( bool deferred );
746 
747  void autoRefreshTriggered();
748 
749  void updateAutoRefreshTimer();
750 
751  void projectThemesChanged();
752 
753  private:
755 
762  QgsMapCanvas( QgsMapCanvas const & );
763 
765  QgsMapSettings mSettings;
766 
768  QgsMapCanvasMap *mMap = nullptr;
769 
771  bool mFrozen;
772 
774  bool mRefreshScheduled;
775 
777  bool mRenderFlag;
778 
780  QgsMapLayer *mCurrentLayer = nullptr;
781 
783  QGraphicsScene *mScene = nullptr;
784 
786  QgsMapTool *mMapTool = nullptr;
787 
789  QgsMapTool *mLastNonZoomMapTool = nullptr;
790 
792  QList <QgsRectangle> mLastExtent;
793  int mLastExtentIndex;
794 
796  double mWheelZoomFactor;
797 
799  QTimer mMapUpdateTimer;
800 
802  QgsMapRendererQImageJob *mJob = nullptr;
803 
805  bool mJobCanceled;
806 
808  QgsLabelingResults *mLabelingResults = nullptr;
809 
811  bool mUseParallelRendering;
812 
814  bool mDrawRenderingStats;
815 
817  QgsMapRendererCache *mCache = nullptr;
818 
819  QTimer *mResizeTimer = nullptr;
820  QTimer *mRefreshTimer = nullptr;
821 
822  QgsPreviewEffect *mPreviewEffect = nullptr;
823 
824  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
825 
826  QgsSnappingUtils *mSnappingUtils = nullptr;
827 
828  QList< QgsMapRendererQImageJob * > mPreviewJobs;
829 
831  bool mScaleLocked;
832 
833  QgsExpressionContextScope mExpressionContextScope;
834 
836  QRect mZoomRect;
837 
839  bool mZoomDragging;
840 
842  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
843 
844  QCursor mZoomCursor;
845 
846  QTimer mAutoRefreshTimer;
847 
848  QString mTheme;
849 
850  bool mAnnotationsVisible = true;
851 
854  void updateMapSize();
855 
860  void beginZoomRect( QPoint pos );
861 
866  void endZoomRect( QPoint pos );
867 
874  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
875 
876  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
877 
878  void startPreviewJobs();
879  void stopPreviewJobs();
880 
881  friend class TestQgsMapCanvas;
882 
883 }; // class QgsMapCanvas
884 
885 
886 
887 #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:723
#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:505
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:469
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:357
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:477
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:397
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:391
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:484
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.