QGIS API Documentation  2.99.0-Master (64819bc)
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;
66 
67 
72 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
73 {
74  Q_OBJECT
75 
76  public:
77 
79  QgsMapCanvas( QWidget * parent = nullptr );
80 
81  ~QgsMapCanvas();
82 
85  double magnificationFactor() const;
86 
89  void setLayers( const QList<QgsMapLayer*>& layers );
90 
91  void setCurrentLayer( QgsMapLayer* layer );
92 
95  const QgsMapSettings& mapSettings() const;
96 
99  void setCrsTransformEnabled( bool enabled );
100 
103  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );
104 
107  const QgsLabelingResults* labelingResults() const;
108 
111  void setCachingEnabled( bool enabled );
112 
115  bool isCachingEnabled() const;
116 
119  void clearCache();
120 
123  void refreshAllLayers();
124 
127  void setParallelRenderingEnabled( bool enabled );
128 
131  bool isParallelRenderingEnabled() const;
132 
135  void setMapUpdateInterval( int timeMilliseconds );
136 
139  int mapUpdateInterval() const;
140 
142  double scale();
143 
145  double mapUnitsPerPixel() const;
146 
148  QgsRectangle extent() const;
150  QgsRectangle fullExtent() const;
151 
153  void setExtent( const QgsRectangle &r, bool magnified = false );
154 
157  double rotation() const;
158 
161  void setRotation( double degrees );
162 
165  void setCenter( const QgsPoint& center );
166 
169  QgsPoint center() const;
170 
172  void zoomToFullExtent();
173 
175  void zoomToPreviousExtent();
176 
178  void zoomToNextExtent();
179 
180  // ! Clears the list of extents and sets current extent as first item
181  void clearExtentHistory();
182 
186  void zoomToSelected( QgsVectorLayer* layer = nullptr );
187 
191  void zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
192 
196  void panToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
197 
199  void panToSelected( QgsVectorLayer* layer = nullptr );
200 
202  void setMapTool( QgsMapTool* mapTool );
203 
210  void unsetMapTool( QgsMapTool* mapTool );
211 
213  QgsMapTool* mapTool();
214 
216  void setCanvasColor( const QColor & _newVal );
218  QColor canvasColor() const;
219 
222  void setSelectionColor( const QColor& color );
223 
225  void updateScale();
226 
228  QgsMapLayer *layer( int index );
229 
231  int layerCount() const;
232 
234  QList<QgsMapLayer*> layers() const;
235 
244  void freeze( bool frz = true );
245 
251  bool isFrozen();
252 
254  void setMapUnits( QgsUnitTypes::DistanceUnit mapUnits );
255 
257  QgsUnitTypes::DistanceUnit mapUnits() const;
258 
261  QMap<QString, QString> layerStyleOverrides() const;
262 
265  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
266 
268  const QgsMapToPixel* getCoordinateTransform();
269 
271  bool isDrawing();
272 
274  QgsMapLayer* currentLayer();
275 
277  void setWheelFactor( double factor );
278 
280  void zoomScale( double scale );
281 
284  void zoomByFactor( double scaleFactor, const QgsPoint *center = nullptr );
285 
287  void zoomWithCenter( int x, int y, bool zoomIn );
288 
291  void zoomToFeatureExtent( QgsRectangle& rect );
292 
296  bool scaleLocked() const { return mScaleLocked;}
297 
299  void enableAntiAliasing( bool theFlag );
300 
302  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
303 
305  void enableMapTileRendering( bool theFlag );
306 
307  // following 2 methods should be moved elsewhere or changed to private
308  // currently used by pan map tool
310  void panActionEnd( QPoint releasePoint );
311 
313  void panAction( QMouseEvent * event );
314 
316  QPoint mouseLastXY();
317 
322  void setPreviewModeEnabled( bool previewEnabled );
323 
329  bool previewModeEnabled() const;
330 
338  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
339 
346  QgsPreviewEffect::PreviewMode previewMode() const;
347 
355  QgsSnappingUtils* snappingUtils() const;
356 
364  void setSnappingUtils( QgsSnappingUtils* utils );
365 
374  void setExpressionContextScope( const QgsExpressionContextScope& scope ) { mExpressionContextScope = scope; }
375 
382  QgsExpressionContextScope& expressionContextScope() { return mExpressionContextScope; }
383 
389  const QgsExpressionContextScope& expressionContextScope() const { return mExpressionContextScope; }
390 
393  void setSegmentationTolerance( double tolerance );
394 
397  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
398 
399  public slots:
400 
402  void refresh();
403 
405  void selectionChangedSlot();
406 
408  void saveAsImage( const QString& theFileName, QPixmap * QPixmap = nullptr, const QString& = "PNG" );
409 
411  void layerStateChange();
412 
414  void layerCrsChange();
415 
417  void setRenderFlag( bool theFlag );
419  bool renderFlag() {return mRenderFlag;}
420 
422  bool hasCrsTransformEnabled();
423 
426  void stopRendering();
427 
429  void readProject( const QDomDocument & );
430 
432  void writeProject( QDomDocument & );
433 
435  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
436 
441  void setMagnificationFactor( double factor );
442 
446  void setScaleLocked( bool isLocked );
447 
449  void zoomIn();
450 
452  void zoomOut();
453 
454  private slots:
456  void mapToolDestroyed();
457 
459  void rendererJobFinished();
460 
461  void mapUpdateTimeout();
462 
463  void refreshMap();
464 
465  signals:
466 
469  void xyCoordinates( const QgsPoint &p );
470 
472  void scaleChanged( double );
473 
475  void extentsChanged();
476 
479  void rotationChanged( double );
480 
483  void magnificationChanged( double );
484 
492  void renderComplete( QPainter * );
496 
497  // ### QGIS 3: renamte to mapRefreshFinished()
499  void mapCanvasRefreshed();
500 
501  // ### QGIS 3: rename to mapRefreshStarted()
503  void renderStarting();
504 
506  void layersChanged();
507 
509  void keyPressed( QKeyEvent * e );
510 
512  void keyReleased( QKeyEvent * e );
513 
515  void mapToolSet( QgsMapTool *tool );
516 
520  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
521 
522  // ### QGIS 3: remove the signal
524  void selectionChanged( QgsMapLayer * layer );
525 
527  void zoomLastStatusChanged( bool );
528 
530  void zoomNextStatusChanged( bool );
531 
534  void hasCrsTransformEnabledChanged( bool flag );
535 
538  void destinationCrsChanged();
539 
542  void mapUnitsChanged();
543 
546  void currentLayerChanged( QgsMapLayer* layer );
547 
550  void layerStyleOverridesChanged();
551 
553  void messageEmitted( const QString& title, const QString& message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
554 
555  protected:
556 
558  bool event( QEvent * e ) override;
559 
561  void keyPressEvent( QKeyEvent * e ) override;
562 
564  void keyReleaseEvent( QKeyEvent * e ) override;
565 
567  void mouseDoubleClickEvent( QMouseEvent * e ) override;
568 
570  void mouseMoveEvent( QMouseEvent * e ) override;
571 
573  void mousePressEvent( QMouseEvent * e ) override;
574 
576  void mouseReleaseEvent( QMouseEvent * e ) override;
577 
579  void wheelEvent( QWheelEvent * e ) override;
580 
582  void resizeEvent( QResizeEvent * e ) override;
583 
585  void paintEvent( QPaintEvent * e ) override;
586 
588  void dragEnterEvent( QDragEnterEvent * e ) override;
589 
591  void moveCanvasContents( bool reset = false );
592 
594  void updateCanvasItemPositions();
595 
598 
600  std::unique_ptr<CanvasProperties> mCanvasProperties;
601 
602 #if 0
603 
607  void connectNotify( const char * signal ) override;
608 #endif
609  void updateDatumTransformEntries();
611 
612  private slots:
613 
614  void layerRepaintRequested( bool deferred );
615 
616  void autoRefreshTriggered();
617 
618  void updateAutoRefreshTimer();
619 
620  private:
622 
629  QgsMapCanvas( QgsMapCanvas const & );
630 
632  QgsMapSettings mSettings;
633 
635  QgsMapCanvasMap* mMap = nullptr;
636 
638  bool mFrozen;
639 
641  bool mRefreshScheduled;
642 
644  bool mRenderFlag;
645 
647  QgsMapLayer* mCurrentLayer = nullptr;
648 
650  QGraphicsScene* mScene = nullptr;
651 
653  QgsMapTool* mMapTool = nullptr;
654 
656  QgsMapTool* mLastNonZoomMapTool = nullptr;
657 
659  QList <QgsRectangle> mLastExtent;
660  int mLastExtentIndex;
661 
663  double mWheelZoomFactor;
664 
666  QTimer mMapUpdateTimer;
667 
669  QgsMapRendererQImageJob* mJob = nullptr;
670 
672  bool mJobCanceled;
673 
675  QgsLabelingResults* mLabelingResults = nullptr;
676 
678  bool mUseParallelRendering;
679 
681  bool mDrawRenderingStats;
682 
684  QgsMapRendererCache* mCache = nullptr;
685 
686  QTimer *mResizeTimer = nullptr;
687 
688  QgsPreviewEffect* mPreviewEffect = nullptr;
689 
690  QgsRectangle imageRect( const QImage& img, const QgsMapSettings& mapSettings );
691 
692  QgsSnappingUtils* mSnappingUtils = nullptr;
693 
695  bool mScaleLocked;
696 
697  QgsExpressionContextScope mExpressionContextScope;
698 
700  QRect mZoomRect;
701 
703  bool mZoomDragging;
704 
706  std::unique_ptr< QgsRubberBand > mZoomRubberBand;
707 
708  QCursor mZoomCursor;
709 
710  QTimer mAutoRefreshTimer;
711 
714  void updateMapSize();
715 
720  void beginZoomRect( QPoint pos );
721 
726  void endZoomRect( QPoint pos );
727 
734  bool boundingBoxOfFeatureIds( const QgsFeatureIds& ids, QgsVectorLayer* layer, QgsRectangle& bbox, QString& errorMsg ) const;
735 
736  friend class TestQgsMapCanvas;
737 
738 }; // class QgsMapCanvas
739 
740 
741 
742 #endif
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:36
Base class for all map layer types.
Definition: qgsmaplayer.h:52
std::unique_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:597
A widget that displays an overview map.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:355
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...
bool renderFlag()
State of render suppression flag.
Definition: qgsmapcanvas.h:419
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:33
Enable anti-aliasing for map rendering.
A class for highlight features on the map.
Definition: qgshighlight.h:36
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:374
A class to represent a point.
Definition: qgspoint.h:143
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:43
Abstract base class for all map tools.
Definition: qgsmaptool.h:49
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:382
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:302
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:296
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:389
Intermediate base class adding functionality that allows client to query the rendered image...
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.