QGIS API Documentation  2.99.0-Master (b698612)
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 
589  void mapUpdateTimeout();
590 
591  void refreshMap();
592 
593  void mapThemeChanged( const QString &theme );
594 
595  signals:
596 
599  void xyCoordinates( const QgsPointXY &p );
600 
602  void scaleChanged( double );
603 
605  void extentsChanged();
606 
609  void rotationChanged( double );
610 
613  void magnificationChanged( double );
614 
622  void renderComplete( QPainter * );
626 
627  // ### QGIS 3: renamte to mapRefreshFinished()
629  void mapCanvasRefreshed();
630 
631  // ### QGIS 3: rename to mapRefreshStarted()
633  void renderStarting();
634 
636  void layersChanged();
637 
639  void keyPressed( QKeyEvent *e );
640 
642  void keyReleased( QKeyEvent *e );
643 
647  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
648 
649  // ### QGIS 3: remove the signal
651  void selectionChanged( QgsMapLayer *layer );
652 
654  void zoomLastStatusChanged( bool );
655 
657  void zoomNextStatusChanged( bool );
658 
661  void destinationCrsChanged();
662 
665  void currentLayerChanged( QgsMapLayer *layer );
666 
669  void layerStyleOverridesChanged();
670 
676  void themeChanged( const QString &theme );
677 
679  void messageEmitted( const QString &title, const QString &message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
680 
681  protected:
682 
684  bool event( QEvent *e ) override;
685 
687  void keyPressEvent( QKeyEvent *e ) override;
688 
690  void keyReleaseEvent( QKeyEvent *e ) override;
691 
693  void mouseDoubleClickEvent( QMouseEvent *e ) override;
694 
696  void mouseMoveEvent( QMouseEvent *e ) override;
697 
699  void mousePressEvent( QMouseEvent *e ) override;
700 
702  void mouseReleaseEvent( QMouseEvent *e ) override;
703 
705  void wheelEvent( QWheelEvent *e ) override;
706 
708  void resizeEvent( QResizeEvent *e ) override;
709 
711  void paintEvent( QPaintEvent *e ) override;
712 
714  void dragEnterEvent( QDragEnterEvent *e ) override;
715 
717  void moveCanvasContents( bool reset = false );
718 
721 
723  std::unique_ptr<CanvasProperties> mCanvasProperties;
724 
725 #if 0
726 
730  void connectNotify( const char *signal ) override;
731 #endif
732  void updateDatumTransformEntries();
734 
735  protected slots:
737  void updateCanvasItemPositions();
738 
739  private slots:
740 
741  void layerRepaintRequested( bool deferred );
742 
743  void autoRefreshTriggered();
744 
745  void updateAutoRefreshTimer();
746 
747  void projectThemesChanged();
748 
749  private:
751 
758  QgsMapCanvas( QgsMapCanvas const & );
759 
761  QgsMapSettings mSettings;
762 
764  QgsMapCanvasMap *mMap = nullptr;
765 
767  bool mFrozen;
768 
770  bool mRefreshScheduled;
771 
773  bool mRenderFlag;
774 
776  QgsMapLayer *mCurrentLayer = nullptr;
777 
779  QGraphicsScene *mScene = nullptr;
780 
782  QgsMapTool *mMapTool = nullptr;
783 
785  QgsMapTool *mLastNonZoomMapTool = nullptr;
786 
788  QList <QgsRectangle> mLastExtent;
789  int mLastExtentIndex;
790 
792  double mWheelZoomFactor;
793 
795  QTimer mMapUpdateTimer;
796 
798  QgsMapRendererQImageJob *mJob = nullptr;
799 
801  bool mJobCanceled;
802 
804  QgsLabelingResults *mLabelingResults = nullptr;
805 
807  bool mUseParallelRendering;
808 
810  bool mDrawRenderingStats;
811 
813  QgsMapRendererCache *mCache = nullptr;
814 
815  QTimer *mResizeTimer = nullptr;
816 
817  QgsPreviewEffect *mPreviewEffect = nullptr;
818 
819  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
820 
821  QgsSnappingUtils *mSnappingUtils = nullptr;
822 
824  bool mScaleLocked;
825 
826  QgsExpressionContextScope mExpressionContextScope;
827 
829  QRect mZoomRect;
830 
832  bool mZoomDragging;
833 
835  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
836 
837  QCursor mZoomCursor;
838 
839  QTimer mAutoRefreshTimer;
840 
841  QString mTheme;
842 
843  bool mAnnotationsVisible = true;
844 
847  void updateMapSize();
848 
853  void beginZoomRect( QPoint pos );
854 
859  void endZoomRect( QPoint pos );
860 
867  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
868 
869  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
870 
871  friend class TestQgsMapCanvas;
872 
873 }; // class QgsMapCanvas
874 
875 
876 
877 #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:720
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:34
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:107
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:158
#define SIP_KEEPREFERENCE
Definition: qgis_sip.h:67
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:153
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.