QGIS API Documentation  2.99.0-Master (3450a9f)
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 
23 #include "qgsexpressioncontext.h"
24 #include "qgsfeature.h"
25 #include "qgsmessagebar.h"
26 #include "qgsrectangle.h"
27 #include "qgis.h"
28 
29 #include <QDomDocument>
30 #include <QGraphicsView>
31 #include <QtCore>
32 
33 #include "qgsmapsettings.h" // TEMPORARY
34 #include "qgsprevieweffect.h" //for QgsPreviewEffect::PreviewMode
35 
36 #include <QGestureEvent>
37 #include "qgis_gui.h"
38 
39 class QWheelEvent;
40 class QPixmap;
41 class QPaintEvent;
42 class QKeyEvent;
43 class ResizeEvent;
44 
45 class QColor;
46 class QDomDocument;
47 class QPaintDevice;
48 class QMouseEvent;
49 class QRubberBand;
50 class QGraphicsScene;
51 
52 class QgsMapToPixel;
53 class QgsMapLayer;
54 class QgsHighlight;
55 class QgsVectorLayer;
56 
57 class QgsLabelingResults;
60 class QgsMapSettings;
61 class QgsMapCanvasMap;
63 class QgsMapTool;
64 class QgsSnappingUtils;
65 class QgsRubberBand;
67 
72 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
73 {
74  Q_OBJECT
75  Q_PROPERTY( QString theme READ theme WRITE setTheme NOTIFY themeChanged )
76 
77  public:
78 
80  QgsMapCanvas( QWidget *parent = nullptr );
81 
82  ~QgsMapCanvas();
83 
86  double magnificationFactor() const;
87 
99  void setLayers( const QList<QgsMapLayer *> &layers );
100 
101  void setCurrentLayer( QgsMapLayer *layer );
102 
105  const QgsMapSettings &mapSettings() const;
106 
109  void setDestinationCrs( const QgsCoordinateReferenceSystem &crs );
110 
115  void setMapSettingsFlags( QgsMapSettings::Flags flags );
116 
119  const QgsLabelingResults *labelingResults() const;
120 
123  void setCachingEnabled( bool enabled );
124 
127  bool isCachingEnabled() const;
128 
131  void clearCache();
132 
135  void refreshAllLayers();
136 
146  void waitWhileRendering();
147 
150  void setParallelRenderingEnabled( bool enabled );
151 
154  bool isParallelRenderingEnabled() const;
155 
158  void setMapUpdateInterval( int timeMilliseconds );
159 
162  int mapUpdateInterval() const;
163 
165  double scale();
166 
168  double mapUnitsPerPixel() const;
169 
171  QgsRectangle extent() const;
173  QgsRectangle fullExtent() const;
174 
176  void setExtent( const QgsRectangle &r, bool magnified = false );
177 
180  double rotation() const;
181 
184  void setRotation( double degrees );
185 
188  void setCenter( const QgsPoint &center );
189 
192  QgsPoint center() const;
193 
195  void zoomToFullExtent();
196 
198  void zoomToPreviousExtent();
199 
201  void zoomToNextExtent();
202 
203  // ! Clears the list of extents and sets current extent as first item
204  void clearExtentHistory();
205 
209  void zoomToSelected( QgsVectorLayer *layer = nullptr );
210 
214  void zoomToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
215 
219  void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
220 
222  void panToSelected( QgsVectorLayer *layer = nullptr );
223 
225  void setMapTool( QgsMapTool *mapTool );
226 
233  void unsetMapTool( QgsMapTool *mapTool );
234 
236  QgsMapTool *mapTool();
237 
239  void setCanvasColor( const QColor &_newVal );
241  QColor canvasColor() const;
242 
245  void setSelectionColor( const QColor &color );
246 
248  void updateScale();
249 
251  QgsMapLayer *layer( int index );
252 
254  int layerCount() const;
255 
260  QList<QgsMapLayer *> layers() const;
261 
271  void freeze( bool frozen = true );
272 
280  bool isFrozen() const;
281 
290  bool renderFlag() const { return mRenderFlag; }
291 
296  QgsUnitTypes::DistanceUnit mapUnits() const;
297 
303  QMap<QString, QString> layerStyleOverrides() const;
304 
316  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
317 
336  void setTheme( const QString &theme );
337 
343  QString theme() const { return mTheme; }
344 
346  const QgsMapToPixel *getCoordinateTransform();
347 
349  bool isDrawing();
350 
352  QgsMapLayer *currentLayer();
353 
355  void setWheelFactor( double factor );
356 
358  void zoomScale( double scale );
359 
362  void zoomByFactor( double scaleFactor, const QgsPoint *center = nullptr );
363 
365  void zoomWithCenter( int x, int y, bool zoomIn );
366 
369  void zoomToFeatureExtent( QgsRectangle &rect );
370 
374  bool scaleLocked() const { return mScaleLocked;}
375 
377  void enableAntiAliasing( bool flag );
378 
380  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
381 
383  void enableMapTileRendering( bool flag );
384 
385  // following 2 methods should be moved elsewhere or changed to private
386  // currently used by pan map tool
388  void panActionEnd( QPoint releasePoint );
389 
391  void panAction( QMouseEvent *event );
392 
394  QPoint mouseLastXY();
395 
400  void setPreviewModeEnabled( bool previewEnabled );
401 
407  bool previewModeEnabled() const;
408 
416  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
417 
424  QgsPreviewEffect::PreviewMode previewMode() const;
425 
433  QgsSnappingUtils *snappingUtils() const;
434 
442  void setSnappingUtils( QgsSnappingUtils *utils );
443 
452  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
453 
460  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
461 
467  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; }
468 
471  void setSegmentationTolerance( double tolerance );
472 
475  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
476 
481  QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
482 
488  bool annotationsVisible() const { return mAnnotationsVisible; }
489 
495  void setAnnotationsVisible( bool visible );
496 
501  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
502 
507  const QgsLabelingEngineSettings &labelingEngineSettings() const;
508 
509  public slots:
510 
512  void refresh();
513 
515  void selectionChangedSlot();
516 
518  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
519 
521  void layerStateChange();
522 
524  void layerCrsChange();
525 
533  void setRenderFlag( bool flag );
534 
537  void stopRendering();
538 
540  void readProject( const QDomDocument & );
541 
543  void writeProject( QDomDocument & );
544 
546  void getDatumTransformInfo( const QgsMapLayer *ml, const QString &srcAuthId, const QString &destAuthId );
547 
552  void setMagnificationFactor( double factor );
553 
557  void setScaleLocked( bool isLocked );
558 
560  void zoomIn();
561 
563  void zoomOut();
564 
565  private slots:
567  void mapToolDestroyed();
568 
570  void rendererJobFinished();
571 
572  void mapUpdateTimeout();
573 
574  void refreshMap();
575 
576  void mapThemeChanged( const QString &theme );
577 
578  signals:
579 
582  void xyCoordinates( const QgsPoint &p );
583 
585  void scaleChanged( double );
586 
588  void extentsChanged();
589 
592  void rotationChanged( double );
593 
596  void magnificationChanged( double );
597 
605  void renderComplete( QPainter * );
609 
610  // ### QGIS 3: renamte to mapRefreshFinished()
612  void mapCanvasRefreshed();
613 
614  // ### QGIS 3: rename to mapRefreshStarted()
616  void renderStarting();
617 
619  void layersChanged();
620 
622  void keyPressed( QKeyEvent *e );
623 
625  void keyReleased( QKeyEvent *e );
626 
630  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
631 
632  // ### QGIS 3: remove the signal
634  void selectionChanged( QgsMapLayer *layer );
635 
637  void zoomLastStatusChanged( bool );
638 
640  void zoomNextStatusChanged( bool );
641 
644  void destinationCrsChanged();
645 
648  void currentLayerChanged( QgsMapLayer *layer );
649 
652  void layerStyleOverridesChanged();
653 
659  void themeChanged( const QString &theme );
660 
662  void messageEmitted( const QString &title, const QString &message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
663 
664  protected:
665 
667  bool event( QEvent *e ) override;
668 
670  void keyPressEvent( QKeyEvent *e ) override;
671 
673  void keyReleaseEvent( QKeyEvent *e ) override;
674 
676  void mouseDoubleClickEvent( QMouseEvent *e ) override;
677 
679  void mouseMoveEvent( QMouseEvent *e ) override;
680 
682  void mousePressEvent( QMouseEvent *e ) override;
683 
685  void mouseReleaseEvent( QMouseEvent *e ) override;
686 
688  void wheelEvent( QWheelEvent *e ) override;
689 
691  void resizeEvent( QResizeEvent *e ) override;
692 
694  void paintEvent( QPaintEvent *e ) override;
695 
697  void dragEnterEvent( QDragEnterEvent *e ) override;
698 
700  void moveCanvasContents( bool reset = false );
701 
703  void updateCanvasItemPositions();
704 
707 
709  std::unique_ptr<CanvasProperties> mCanvasProperties;
710 
711 #if 0
712 
716  void connectNotify( const char *signal ) override;
717 #endif
718  void updateDatumTransformEntries();
720 
721  private slots:
722 
723  void layerRepaintRequested( bool deferred );
724 
725  void autoRefreshTriggered();
726 
727  void updateAutoRefreshTimer();
728 
729  void projectThemesChanged();
730 
731  private:
733 
740  QgsMapCanvas( QgsMapCanvas const & );
741 
743  QgsMapSettings mSettings;
744 
746  QgsMapCanvasMap *mMap = nullptr;
747 
749  bool mFrozen;
750 
752  bool mRefreshScheduled;
753 
755  bool mRenderFlag;
756 
758  QgsMapLayer *mCurrentLayer = nullptr;
759 
761  QGraphicsScene *mScene = nullptr;
762 
764  QgsMapTool *mMapTool = nullptr;
765 
767  QgsMapTool *mLastNonZoomMapTool = nullptr;
768 
770  QList <QgsRectangle> mLastExtent;
771  int mLastExtentIndex;
772 
774  double mWheelZoomFactor;
775 
777  QTimer mMapUpdateTimer;
778 
780  QgsMapRendererQImageJob *mJob = nullptr;
781 
783  bool mJobCanceled;
784 
786  QgsLabelingResults *mLabelingResults = nullptr;
787 
789  bool mUseParallelRendering;
790 
792  bool mDrawRenderingStats;
793 
795  QgsMapRendererCache *mCache = nullptr;
796 
797  QTimer *mResizeTimer = nullptr;
798 
799  QgsPreviewEffect *mPreviewEffect = nullptr;
800 
801  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
802 
803  QgsSnappingUtils *mSnappingUtils = nullptr;
804 
806  bool mScaleLocked;
807 
808  QgsExpressionContextScope mExpressionContextScope;
809 
811  QRect mZoomRect;
812 
814  bool mZoomDragging;
815 
817  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
818 
819  QCursor mZoomCursor;
820 
821  QTimer mAutoRefreshTimer;
822 
823  QString mTheme;
824 
825  bool mAnnotationsVisible = true;
826 
829  void updateMapSize();
830 
835  void beginZoomRect( QPoint pos );
836 
841  void endZoomRect( QPoint pos );
842 
849  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
850 
851  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
852 
853  friend class TestQgsMapCanvas;
854 
855 }; // class QgsMapCanvas
856 
857 
858 
859 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:38
Base class for all map layer types.
Definition: qgsmaplayer.h:53
std::unique_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:706
A widget that displays an overview map.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:517
bool annotationsVisible() const
Returns true if annotations are visible within the map canvas.
Definition: qgsmapcanvas.h:488
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:72
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:34
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:35
bool renderFlag() const
Returns true if canvas render is disabled as a result of user disabling renders via the GUI...
Definition: qgsmapcanvas.h:290
Enable anti-aliasing for map rendering.
A class for highlight features on the map.
Definition: qgshighlight.h:39
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:452
A class to represent a point.
Definition: qgspoint.h:37
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:42
Abstract base class for all map tools.
Definition: qgsmaptool.h:49
QString theme() const
Returns the map&#39;s theme shown in the canvas, if set.
Definition: qgsmapcanvas.h:343
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:460
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:380
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:374
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:467
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...
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.