QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
169  void waitWhileRendering();
170 
175  void setParallelRenderingEnabled( bool enabled );
176 
181  bool isParallelRenderingEnabled() const;
182 
187  void setMapUpdateInterval( int timeMilliseconds );
188 
193  int mapUpdateInterval() const;
194 
199  double scale() const;
200 
202  double mapUnitsPerPixel() const;
203 
205  QgsRectangle extent() const;
207  QgsRectangle fullExtent() const;
208 
210  void setExtent( const QgsRectangle &r, bool magnified = false );
211 
216  double rotation() const;
217 
222  void setRotation( double degrees );
223 
228  void setCenter( const QgsPointXY &center );
229 
234  QgsPointXY center() const;
235 
237  void zoomToFullExtent();
238 
240  void zoomToPreviousExtent();
241 
243  void zoomToNextExtent();
244 
245  // ! Clears the list of extents and sets current extent as first item
246  void clearExtentHistory();
247 
248 
253  void zoomToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
254 
259  void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
260 
262  void panToSelected( QgsVectorLayer *layer = nullptr );
263 
276  void flashFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids,
277  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
278  int flashes = 3, int duration = 500 );
279 
292  void flashGeometries( const QList< QgsGeometry > &geometries, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(),
293  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
294  int flashes = 3, int duration = 500 );
295 
297  void setMapTool( QgsMapTool *mapTool, bool clean = false );
298 
306  void unsetMapTool( QgsMapTool *mapTool );
307 
309  QgsMapTool *mapTool();
310 
312  void setCanvasColor( const QColor &_newVal );
314  QColor canvasColor() const;
315 
320  void setSelectionColor( const QColor &color );
321 
326  QColor selectionColor() const;
327 
329  void updateScale();
330 
332  QgsMapLayer *layer( int index );
333 
335  int layerCount() const;
336 
341  QList<QgsMapLayer *> layers() const;
342 
352  void freeze( bool frozen = true );
353 
361  bool isFrozen() const;
362 
371  bool renderFlag() const { return mRenderFlag; }
372 
377  QgsUnitTypes::DistanceUnit mapUnits() const;
378 
384  QMap<QString, QString> layerStyleOverrides() const;
385 
397  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
398 
417  void setTheme( const QString &theme );
418 
424  QString theme() const { return mTheme; }
425 
427  const QgsMapToPixel *getCoordinateTransform();
428 
430  bool isDrawing();
431 
433  QgsMapLayer *currentLayer();
434 
436  void setWheelFactor( double factor );
437 
442  void zoomScale( double scale );
443 
448  void zoomByFactor( double scaleFactor, const QgsPointXY *center = nullptr );
449 
451  void zoomWithCenter( int x, int y, bool zoomIn );
452 
457  void zoomToFeatureExtent( QgsRectangle &rect );
458 
464  bool scaleLocked() const { return mScaleLocked;}
465 
467  void enableAntiAliasing( bool flag );
468 
470  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
471 
473  void enableMapTileRendering( bool flag );
474 
475  // following 2 methods should be moved elsewhere or changed to private
476  // currently used by pan map tool
478  void panActionEnd( QPoint releasePoint );
479 
481  void panAction( QMouseEvent *event );
482 
484  QPoint mouseLastXY();
485 
491  void setPreviewModeEnabled( bool previewEnabled );
492 
499  bool previewModeEnabled() const;
500 
509  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
510 
518  QgsPreviewEffect::PreviewMode previewMode() const;
519 
528  QgsSnappingUtils *snappingUtils() const;
529 
538  void setSnappingUtils( QgsSnappingUtils *utils );
539 
549  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
550 
558  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
559 
566  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP
567 
571  void setSegmentationTolerance( double tolerance );
572 
576  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
577 
582  QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
583 
589  bool annotationsVisible() const { return mAnnotationsVisible; }
590 
596  void setAnnotationsVisible( bool visible );
597 
602  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
603 
608  const QgsLabelingEngineSettings &labelingEngineSettings() const;
609 
618  bool previewJobsEnabled() const;
619 
628  void setPreviewJobsEnabled( bool enabled );
629 
630  public slots:
631 
633  void refresh();
634 
639  void refreshAllLayers();
640 
642  void selectionChangedSlot();
643 
645  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
646 
648  void layerStateChange();
649 
657  void setRenderFlag( bool flag );
658 
663  void stopRendering();
664 
666  void readProject( const QDomDocument & );
667 
669  void writeProject( QDomDocument & );
670 
671 #if 0
672  void getDatumTransformInfo( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination );
674 #endif
675 
682  void setMagnificationFactor( double factor );
683 
689  void setScaleLocked( bool isLocked );
690 
692  void zoomIn();
693 
695  void zoomOut();
696 
701  void zoomToSelected( QgsVectorLayer *layer = nullptr );
702 
703  private slots:
705  void mapToolDestroyed();
706 
708  void rendererJobFinished();
709 
711  void previewJobFinished();
712 
713  void mapUpdateTimeout();
714 
715  void refreshMap();
716 
717  void mapThemeChanged( const QString &theme );
718 
719  signals:
720 
724  void xyCoordinates( const QgsPointXY &p );
725 
727  void scaleChanged( double );
728 
730  void extentsChanged();
731 
736  void rotationChanged( double );
737 
742  void magnificationChanged( double );
743 
748  void canvasColorChanged();
749 
764  void renderComplete( QPainter * );
765 
766  // ### QGIS 3: renamte to mapRefreshFinished()
768  void mapCanvasRefreshed();
769 
770  // ### QGIS 3: rename to mapRefreshStarted()
772  void renderStarting();
773 
775  void layersChanged();
776 
778  void keyPressed( QKeyEvent *e );
779 
781  void keyReleased( QKeyEvent *e );
782 
787  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
788 
789 
791  void selectionChanged( QgsVectorLayer *layer );
792 
794  void zoomLastStatusChanged( bool );
795 
797  void zoomNextStatusChanged( bool );
798 
803  void destinationCrsChanged();
804 
809  void transformContextChanged();
810 
815  void currentLayerChanged( QgsMapLayer *layer );
816 
821  void layerStyleOverridesChanged();
822 
828  void themeChanged( const QString &theme );
829 
831  void messageEmitted( const QString &title, const QString &message, Qgis::MessageLevel = Qgis::Info );
832 
833  protected:
834 
836  bool event( QEvent *e ) override;
837 
839  void keyPressEvent( QKeyEvent *e ) override;
840 
842  void keyReleaseEvent( QKeyEvent *e ) override;
843 
845  void mouseDoubleClickEvent( QMouseEvent *e ) override;
846 
848  void mouseMoveEvent( QMouseEvent *e ) override;
849 
851  void mousePressEvent( QMouseEvent *e ) override;
852 
854  void mouseReleaseEvent( QMouseEvent *e ) override;
855 
857  void wheelEvent( QWheelEvent *e ) override;
858 
860  void resizeEvent( QResizeEvent *e ) override;
861 
863  void paintEvent( QPaintEvent *e ) override;
864 
866  void dragEnterEvent( QDragEnterEvent *e ) override;
867 
869  void moveCanvasContents( bool reset = false );
870 
873 
875  std::unique_ptr<CanvasProperties> mCanvasProperties;
876 
877 #if 0
878 
883  void connectNotify( const char *signal ) override;
884 #endif
885 
886  protected slots:
888  void updateCanvasItemPositions();
889 
890  private slots:
891 
892  void layerRepaintRequested( bool deferred );
893 
894  void autoRefreshTriggered();
895 
896  void updateAutoRefreshTimer();
897 
898  void projectThemesChanged();
899 
900  void startPreviewJob( int number );
901 
902  private:
903 
905  QgsMapSettings mSettings;
906 
908  QgsMapCanvasMap *mMap = nullptr;
909 
911  bool mFrozen = false;
912 
914  bool mRefreshScheduled = false;
915 
917  bool mRenderFlag = true;
918 
920  QgsMapLayer *mCurrentLayer = nullptr;
921 
923  QGraphicsScene *mScene = nullptr;
924 
926  QgsMapTool *mMapTool = nullptr;
927 
929  QgsMapTool *mLastNonZoomMapTool = nullptr;
930 
932  QList <QgsRectangle> mLastExtent;
933  int mLastExtentIndex = -1;
934 
936  double mWheelZoomFactor = 2.0;
937 
939  QTimer mMapUpdateTimer;
940 
942  QgsMapRendererQImageJob *mJob = nullptr;
943 
945  bool mJobCanceled = false;
946 
948  QgsLabelingResults *mLabelingResults = nullptr;
949 
951  bool mUseParallelRendering = false;
952 
954  bool mDrawRenderingStats = false;
955 
957  QgsMapRendererCache *mCache = nullptr;
958 
959  QTimer *mResizeTimer = nullptr;
960  QTimer *mRefreshTimer = nullptr;
961 
962  QgsPreviewEffect *mPreviewEffect = nullptr;
963 
964  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
965 
966  QgsSnappingUtils *mSnappingUtils = nullptr;
967 
968  QList< QgsMapRendererQImageJob * > mPreviewJobs;
969 
971  bool mScaleLocked = false;
972 
973  QgsExpressionContextScope mExpressionContextScope;
974 
976  QRect mZoomRect;
977 
979  bool mZoomDragging = false;
980 
982  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
983 
984  QCursor mZoomCursor;
985 
986  QTimer mAutoRefreshTimer;
987 
988  QTimer mPreviewTimer;
989  QMetaObject::Connection mPreviewTimerConnection;
990 
991  QString mTheme;
992 
993  bool mAnnotationsVisible = true;
994 
995  bool mUsePreviewJobs = false;
996 
997  QHash< QString, int > mLastLayerRenderTime;
998 
1003  void updateMapSize();
1004 
1010  void beginZoomRect( QPoint pos );
1011 
1017  void endZoomRect( QPoint pos );
1018 
1026  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
1027 
1028  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
1029 
1030  void startPreviewJobs();
1031  void stopPreviewJobs();
1032  void schedulePreviewJob( int number );
1033 
1034  friend class TestQgsMapCanvas;
1035 
1036 }; // class QgsMapCanvas
1037 
1038 // clazy:excludeall=qstring-allocations
1039 
1040 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:40
Base class for all map layer types.
Definition: qgsmaplayer.h:61
std::unique_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:872
#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:589
A class to represent a 2D point.
Definition: qgspointxy.h:43
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
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:182
#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:549
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:424
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:558
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:470
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:464
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:566
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.