QGIS API Documentation  3.0.2-Girona (307d082)
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 
252  void zoomToSelected( QgsVectorLayer *layer = nullptr );
253 
258  void zoomToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
259 
264  void panToFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids );
265 
267  void panToSelected( QgsVectorLayer *layer = nullptr );
268 
281  void flashFeatureIds( QgsVectorLayer *layer, const QgsFeatureIds &ids,
282  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
283  int flashes = 3, int duration = 500 );
284 
297  void flashGeometries( const QList< QgsGeometry > &geometries, const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem(),
298  const QColor &startColor = QColor( 255, 0, 0, 255 ), const QColor &endColor = QColor( 255, 0, 0, 0 ),
299  int flashes = 3, int duration = 500 );
300 
302  void setMapTool( QgsMapTool *mapTool, bool clean = false );
303 
311  void unsetMapTool( QgsMapTool *mapTool );
312 
314  QgsMapTool *mapTool();
315 
317  void setCanvasColor( const QColor &_newVal );
319  QColor canvasColor() const;
320 
325  void setSelectionColor( const QColor &color );
326 
331  QColor selectionColor() const;
332 
334  void updateScale();
335 
337  QgsMapLayer *layer( int index );
338 
340  int layerCount() const;
341 
346  QList<QgsMapLayer *> layers() const;
347 
357  void freeze( bool frozen = true );
358 
366  bool isFrozen() const;
367 
376  bool renderFlag() const { return mRenderFlag; }
377 
382  QgsUnitTypes::DistanceUnit mapUnits() const;
383 
389  QMap<QString, QString> layerStyleOverrides() const;
390 
402  void setLayerStyleOverrides( const QMap<QString, QString> &overrides );
403 
422  void setTheme( const QString &theme );
423 
429  QString theme() const { return mTheme; }
430 
432  const QgsMapToPixel *getCoordinateTransform();
433 
435  bool isDrawing();
436 
438  QgsMapLayer *currentLayer();
439 
441  void setWheelFactor( double factor );
442 
447  void zoomScale( double scale );
448 
453  void zoomByFactor( double scaleFactor, const QgsPointXY *center = nullptr );
454 
456  void zoomWithCenter( int x, int y, bool zoomIn );
457 
462  void zoomToFeatureExtent( QgsRectangle &rect );
463 
469  bool scaleLocked() const { return mScaleLocked;}
470 
472  void enableAntiAliasing( bool flag );
473 
475  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
476 
478  void enableMapTileRendering( bool flag );
479 
480  // following 2 methods should be moved elsewhere or changed to private
481  // currently used by pan map tool
483  void panActionEnd( QPoint releasePoint );
484 
486  void panAction( QMouseEvent *event );
487 
489  QPoint mouseLastXY();
490 
496  void setPreviewModeEnabled( bool previewEnabled );
497 
504  bool previewModeEnabled() const;
505 
514  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
515 
523  QgsPreviewEffect::PreviewMode previewMode() const;
524 
533  QgsSnappingUtils *snappingUtils() const;
534 
543  void setSnappingUtils( QgsSnappingUtils *utils );
544 
554  void setExpressionContextScope( const QgsExpressionContextScope &scope ) { mExpressionContextScope = scope; }
555 
563  QgsExpressionContextScope &expressionContextScope() { return mExpressionContextScope; }
564 
571  const QgsExpressionContextScope &expressionContextScope() const { return mExpressionContextScope; } SIP_SKIP
572 
576  void setSegmentationTolerance( double tolerance );
577 
581  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
582 
587  QList< QgsMapCanvasAnnotationItem *> annotationItems() const;
588 
594  bool annotationsVisible() const { return mAnnotationsVisible; }
595 
601  void setAnnotationsVisible( bool visible );
602 
607  void setLabelingEngineSettings( const QgsLabelingEngineSettings &settings );
608 
613  const QgsLabelingEngineSettings &labelingEngineSettings() const;
614 
623  bool previewJobsEnabled() const;
624 
633  void setPreviewJobsEnabled( bool enabled );
634 
635  public slots:
636 
638  void refresh();
639 
644  void refreshAllLayers();
645 
647  void selectionChangedSlot();
648 
650  void saveAsImage( const QString &fileName, QPixmap *QPixmap = nullptr, const QString & = "PNG" );
651 
653  void layerStateChange();
654 
662  void setRenderFlag( bool flag );
663 
668  void stopRendering();
669 
671  void readProject( const QDomDocument & );
672 
674  void writeProject( QDomDocument & );
675 
676 #if 0
677  void getDatumTransformInfo( const QgsCoordinateReferenceSystem &source, const QgsCoordinateReferenceSystem &destination );
679 #endif
680 
687  void setMagnificationFactor( double factor );
688 
694  void setScaleLocked( bool isLocked );
695 
697  void zoomIn();
698 
700  void zoomOut();
701 
702  private slots:
704  void mapToolDestroyed();
705 
707  void rendererJobFinished();
708 
710  void previewJobFinished();
711 
712  void mapUpdateTimeout();
713 
714  void refreshMap();
715 
716  void mapThemeChanged( const QString &theme );
717 
718  signals:
719 
723  void xyCoordinates( const QgsPointXY &p );
724 
726  void scaleChanged( double );
727 
729  void extentsChanged();
730 
735  void rotationChanged( double );
736 
741  void magnificationChanged( double );
742 
747  void canvasColorChanged();
748 
763  void renderComplete( QPainter * );
764 
765  // ### QGIS 3: renamte to mapRefreshFinished()
767  void mapCanvasRefreshed();
768 
769  // ### QGIS 3: rename to mapRefreshStarted()
771  void renderStarting();
772 
774  void layersChanged();
775 
777  void keyPressed( QKeyEvent *e );
778 
780  void keyReleased( QKeyEvent *e );
781 
786  void mapToolSet( QgsMapTool *newTool, QgsMapTool *oldTool );
787 
788  // ### QGIS 3: remove the signal
790  void selectionChanged( QgsMapLayer *layer );
791 
793  void zoomLastStatusChanged( bool );
794 
796  void zoomNextStatusChanged( bool );
797 
802  void destinationCrsChanged();
803 
808  void transformContextChanged();
809 
814  void currentLayerChanged( QgsMapLayer *layer );
815 
820  void layerStyleOverridesChanged();
821 
827  void themeChanged( const QString &theme );
828 
830  void messageEmitted( const QString &title, const QString &message, Qgis::MessageLevel = Qgis::Info );
831 
832  protected:
833 
835  bool event( QEvent *e ) override;
836 
838  void keyPressEvent( QKeyEvent *e ) override;
839 
841  void keyReleaseEvent( QKeyEvent *e ) override;
842 
844  void mouseDoubleClickEvent( QMouseEvent *e ) override;
845 
847  void mouseMoveEvent( QMouseEvent *e ) override;
848 
850  void mousePressEvent( QMouseEvent *e ) override;
851 
853  void mouseReleaseEvent( QMouseEvent *e ) override;
854 
856  void wheelEvent( QWheelEvent *e ) override;
857 
859  void resizeEvent( QResizeEvent *e ) override;
860 
862  void paintEvent( QPaintEvent *e ) override;
863 
865  void dragEnterEvent( QDragEnterEvent *e ) override;
866 
868  void moveCanvasContents( bool reset = false );
869 
872 
874  std::unique_ptr<CanvasProperties> mCanvasProperties;
875 
876 #if 0
877 
882  void connectNotify( const char *signal ) override;
883 #endif
884 
885  protected slots:
887  void updateCanvasItemPositions();
888 
889  private slots:
890 
891  void layerRepaintRequested( bool deferred );
892 
893  void autoRefreshTriggered();
894 
895  void updateAutoRefreshTimer();
896 
897  void projectThemesChanged();
898 
899  void startPreviewJob( int number );
900 
901  private:
903 
910  QgsMapCanvas( QgsMapCanvas const & );
911 
913  QgsMapSettings mSettings;
914 
916  QgsMapCanvasMap *mMap = nullptr;
917 
919  bool mFrozen = false;
920 
922  bool mRefreshScheduled = false;
923 
925  bool mRenderFlag = true;
926 
928  QgsMapLayer *mCurrentLayer = nullptr;
929 
931  QGraphicsScene *mScene = nullptr;
932 
934  QgsMapTool *mMapTool = nullptr;
935 
937  QgsMapTool *mLastNonZoomMapTool = nullptr;
938 
940  QList <QgsRectangle> mLastExtent;
941  int mLastExtentIndex = -1;
942 
944  double mWheelZoomFactor = 2.0;
945 
947  QTimer mMapUpdateTimer;
948 
950  QgsMapRendererQImageJob *mJob = nullptr;
951 
953  bool mJobCanceled = false;
954 
956  QgsLabelingResults *mLabelingResults = nullptr;
957 
959  bool mUseParallelRendering = false;
960 
962  bool mDrawRenderingStats = false;
963 
965  QgsMapRendererCache *mCache = nullptr;
966 
967  QTimer *mResizeTimer = nullptr;
968  QTimer *mRefreshTimer = nullptr;
969 
970  QgsPreviewEffect *mPreviewEffect = nullptr;
971 
972  QgsRectangle imageRect( const QImage &img, const QgsMapSettings &mapSettings );
973 
974  QgsSnappingUtils *mSnappingUtils = nullptr;
975 
976  QList< QgsMapRendererQImageJob * > mPreviewJobs;
977 
979  bool mScaleLocked = false;
980 
981  QgsExpressionContextScope mExpressionContextScope;
982 
984  QRect mZoomRect;
985 
987  bool mZoomDragging = false;
988 
990  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
991 
992  QCursor mZoomCursor;
993 
994  QTimer mAutoRefreshTimer;
995 
996  QTimer mPreviewTimer;
997  QMetaObject::Connection mPreviewTimerConnection;
998 
999  QString mTheme;
1000 
1001  bool mAnnotationsVisible = true;
1002 
1003  bool mUsePreviewJobs = false;
1004 
1005  QHash< QString, int > mLastLayerRenderTime;
1006 
1011  void updateMapSize();
1012 
1018  void beginZoomRect( QPoint pos );
1019 
1025  void endZoomRect( QPoint pos );
1026 
1034  bool boundingBoxOfFeatureIds( const QgsFeatureIds &ids, QgsVectorLayer *layer, QgsRectangle &bbox, QString &errorMsg ) const;
1035 
1036  void setLayersPrivate( const QList<QgsMapLayer *> &layers );
1037 
1038  void startPreviewJobs();
1039  void stopPreviewJobs();
1040  void schedulePreviewJob( int number );
1041 
1042  friend class TestQgsMapCanvas;
1043 
1044 }; // class QgsMapCanvas
1045 
1046 // clazy:excludeall=qstring-allocations
1047 
1048 #endif
A rectangle specified with double values.
Definition: qgsrectangle.h:39
Base class for all map layer types.
Definition: qgsmaplayer.h:56
std::unique_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:871
#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:594
A class to represent a 2D point.
Definition: qgspointxy.h:43
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
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: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:554
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:429
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:563
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:475
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:469
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:571
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.