QGIS API Documentation
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 "qgspoint.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 #ifdef HAVE_TOUCH
38 #include <QGestureEvent>
39 #endif
40 
41 class QWheelEvent;
42 class QPixmap;
43 class QPaintEvent;
44 class QKeyEvent;
45 class ResizeEvent;
46 
47 class QColor;
48 class QDomDocument;
49 class QPaintDevice;
50 class QMouseEvent;
51 class QRubberBand;
52 class QGraphicsScene;
53 
54 class QgsMapToPixel;
55 class QgsMapLayer;
56 class QgsHighlight;
57 class QgsVectorLayer;
58 
59 class QgsLabelingResults;
60 class QgsMapRenderer;
63 class QgsMapSettings;
64 class QgsMapCanvasMap;
66 class QgsMapTool;
67 class QgsSnappingUtils;
68 
74 class GUI_EXPORT QgsMapCanvasLayer
75 {
76  public:
77  QgsMapCanvasLayer( QgsMapLayer* layer, bool visible = true, bool isInOverview = false )
78  : mLayer( layer ), mVisible( visible ), mInOverview( isInOverview ) {}
79 
80  void setVisible( bool visible ) { mVisible = visible; }
81  void setInOverview( bool isInOverview ) { mInOverview = isInOverview; }
82 
83  bool isVisible() const { return mVisible; }
84  bool isInOverview() const { return mInOverview; }
85 
86  QgsMapLayer* layer() { return mLayer; }
87  const QgsMapLayer* layer() const { return mLayer; }
88 
89  private:
90  QgsMapLayer* mLayer;
91 
93  bool mVisible;
94 
96  bool mInOverview;
97 };
98 
99 
105 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
106 {
107  Q_OBJECT
108 
109  public:
110 
111  enum WheelAction { WheelZoom, WheelZoomAndRecenter, WheelZoomToMouseCursor, WheelNothing };
112 
114  QgsMapCanvas( QWidget * parent = nullptr, const char *name = nullptr );
115 
117  ~QgsMapCanvas();
118 
119  void setLayerSet( QList<QgsMapCanvasLayer>& layers );
120 
121  void setCurrentLayer( QgsMapLayer* layer );
122 
123  // ### QGIS 3: make QgsMapCanvas independent from overview
124  void updateOverview();
125 
126  // ### QGIS 3: make QgsMapCanvas independent from overview
127  void enableOverviewMode( QgsMapOverviewCanvas* overview );
128 
131  const QgsMapSettings& mapSettings() const;
132 
135  void setCrsTransformEnabled( bool enabled );
136 
139  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );
140 
143  const QgsLabelingResults* labelingResults() const;
144 
147  void setCachingEnabled( bool enabled );
148 
151  bool isCachingEnabled() const;
152 
155  void clearCache();
156 
159  void refreshAllLayers();
160 
163  void setParallelRenderingEnabled( bool enabled );
164 
167  bool isParallelRenderingEnabled() const;
168 
171  void setMapUpdateInterval( int timeMilliseconds );
172 
175  int mapUpdateInterval() const;
176 
178  Q_DECL_DEPRECATED QgsMapCanvasMap *map();
179 
185  Q_DECL_DEPRECATED QgsMapRenderer *mapRenderer();
186 
189  Q_DECL_DEPRECATED QPaintDevice &canvasPaintDevice();
190 
192  double scale();
193 
196  Q_DECL_DEPRECATED void clear();
197 
199  double mapUnitsPerPixel() const;
200 
202  QgsRectangle extent() const;
204  QgsRectangle fullExtent() const;
205 
207  void setExtent( const QgsRectangle &r );
208 
211  double rotation() const;
212 
215  void setRotation( double degrees );
216 
219  void setCenter( const QgsPoint& center );
220 
223  QgsPoint center() const;
224 
226  void zoomToFullExtent();
227 
229  void zoomToPreviousExtent();
230 
232  void zoomToNextExtent();
233 
234  // ! Clears the list of extents and sets current extent as first item
235  void clearExtentHistory();
236 
240  void zoomToSelected( QgsVectorLayer* layer = nullptr );
241 
245  void zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
246 
248  void panToSelected( QgsVectorLayer* layer = nullptr );
249 
251  void setMapTool( QgsMapTool* mapTool );
252 
259  void unsetMapTool( QgsMapTool* mapTool );
260 
262  QgsMapTool* mapTool();
263 
265  virtual void setCanvasColor( const QColor & _newVal );
267  virtual QColor canvasColor() const;
268 
270  void setSelectionColor( const QColor& color );
272 
274  void updateScale();
275 
277  Q_DECL_DEPRECATED void updateFullExtent() {}
279 
281  QgsMapLayer *layer( int index );
282 
284  int layerCount() const;
285 
287  QList<QgsMapLayer*> layers() const;
288 
297  void freeze( bool frz = true );
298 
304  bool isFrozen();
305 
308  Q_DECL_DEPRECATED void setDirty( bool _dirty );
309 
312  Q_DECL_DEPRECATED bool isDirty() const;
313 
315  void setMapUnits( QGis::UnitType mapUnits );
316 
318  QGis::UnitType mapUnits() const;
319 
322  QMap<QString, QString> layerStyleOverrides() const;
323 
326  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
327 
329  const QgsMapToPixel* getCoordinateTransform();
330 
332  bool isDrawing();
333 
335  QgsMapLayer* currentLayer();
336 
338  void setWheelAction( WheelAction action, double factor = 2 );
339 
341  void zoomIn();
342 
344  void zoomOut();
345 
347  void zoomScale( double scale );
348 
351  void zoomByFactor( double scaleFactor, const QgsPoint *center = nullptr );
352 
354  void zoomWithCenter( int x, int y, bool zoomIn );
355 
357  void enableAntiAliasing( bool theFlag );
358 
360  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
361 
363  void enableMapTileRendering( bool theFlag );
364 
367  Q_DECL_DEPRECATED void useImageToRender( bool theFlag );
368 
369  // following 2 methods should be moved elsewhere or changed to private
370  // currently used by pan map tool
372  void panActionEnd( QPoint releasePoint );
373 
375  void panAction( QMouseEvent * event );
376 
378  QPoint mouseLastXY();
379 
384  void setPreviewModeEnabled( bool previewEnabled );
385 
391  bool previewModeEnabled() const;
392 
400  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
401 
408  QgsPreviewEffect::PreviewMode previewMode() const;
409 
417  QgsSnappingUtils* snappingUtils() const;
425  void setSnappingUtils( QgsSnappingUtils* utils );
426 
435  void setExpressionContextScope( const QgsExpressionContextScope& scope ) { mExpressionContextScope = scope; }
436 
443  QgsExpressionContextScope& expressionContextScope() { return mExpressionContextScope; }
444 
450  const QgsExpressionContextScope& expressionContextScope() const { return mExpressionContextScope; }
451 
452  public slots:
453 
455  void refresh();
456 
458  void selectionChangedSlot();
459 
461  void saveAsImage( const QString& theFileName, QPixmap * QPixmap = nullptr, const QString& = "PNG" );
462 
464  void layerStateChange();
465 
467  void layerCrsChange();
468 
470  void setRenderFlag( bool theFlag );
472  bool renderFlag() {return mRenderFlag;}
473 
475  bool hasCrsTransformEnabled();
476 
478  Q_DECL_DEPRECATED void updateMap();
479 
482  void stopRendering();
483 
485  Q_DECL_DEPRECATED void showError( QgsMapLayer * mapLayer );
486 
488  void readProject( const QDomDocument & );
489 
491  void writeProject( QDomDocument & );
492 
494  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
495 
498  static bool rotationEnabled();
499 
502  static void enableRotation( bool enabled );
503 
504  private slots:
506  void mapToolDestroyed();
507 
509  void rendererJobFinished();
510 
511  void mapUpdateTimeout();
512 
513  void refreshMap();
514 
515  signals:
517  Q_DECL_DEPRECATED void setProgress( int, int );
519 
522  void xyCoordinates( const QgsPoint &p );
523 
525  void scaleChanged( double );
526 
528  void extentsChanged();
529 
532  void rotationChanged( double );
533 
541  void renderComplete( QPainter * );
545 
546  // ### QGIS 3: renamte to mapRefreshFinished()
548  void mapCanvasRefreshed();
549 
550  // ### QGIS 3: rename to mapRefreshStarted()
552  void renderStarting();
553 
555  void layersChanged();
556 
558  void keyPressed( QKeyEvent * e );
559 
561  void keyReleased( QKeyEvent * e );
562 
564  void mapToolSet( QgsMapTool *tool );
565 
569  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
570 
571  // ### QGIS 3: remove the signal
573  void selectionChanged( QgsMapLayer * layer );
574 
576  void zoomLastStatusChanged( bool );
577 
579  void zoomNextStatusChanged( bool );
580 
583  void hasCrsTransformEnabledChanged( bool flag );
584 
587  void destinationCrsChanged();
588 
591  void mapUnitsChanged();
592 
595  void currentLayerChanged( QgsMapLayer* layer );
596 
599  void layerStyleOverridesChanged();
600 
602  void messageEmitted( const QString& title, const QString& message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
603 
604  protected:
605 #ifdef HAVE_TOUCH
606  bool event( QEvent * e ) override;
608 #endif
609 
611  void keyPressEvent( QKeyEvent * e ) override;
612 
614  void keyReleaseEvent( QKeyEvent * e ) override;
615 
617  void mouseDoubleClickEvent( QMouseEvent * e ) override;
618 
620  void mouseMoveEvent( QMouseEvent * e ) override;
621 
623  void mousePressEvent( QMouseEvent * e ) override;
624 
626  void mouseReleaseEvent( QMouseEvent * e ) override;
627 
629  void wheelEvent( QWheelEvent * e ) override;
630 
632  void resizeEvent( QResizeEvent * e ) override;
633 
635  void paintEvent( QPaintEvent * e ) override;
636 
638  void dragEnterEvent( QDragEnterEvent * e ) override;
639 
641  void moveCanvasContents( bool reset = false );
642 
645  void zoomToFeatureExtent( QgsRectangle& rect );
646 
648  void updateCanvasItemPositions();
649 
652 
655 
656 #if 0
657 
660  void connectNotify( const char * signal ) override;
661 #endif
662  void updateDatumTransformEntries();
664 
665  private:
667 
673  QgsMapCanvas( QgsMapCanvas const & );
674 
676  QgsMapSettings mSettings;
677 
679  QgsMapRenderer* mMapRenderer;
680 
682  QgsMapCanvasMap* mMap;
683 
685  QgsMapOverviewCanvas* mMapOverview;
686 
688  bool mFrozen;
689 
691  bool mRefreshScheduled;
692 
694  bool mRenderFlag;
695 
697  QgsMapLayer* mCurrentLayer;
698 
700  QGraphicsScene* mScene;
701 
703  QgsMapTool* mMapTool;
704 
706  QgsMapTool* mLastNonZoomMapTool;
707 
709  QList <QgsRectangle> mLastExtent;
710  int mLastExtentIndex;
711 
713  double mWheelZoomFactor;
714 
716  WheelAction mWheelAction;
717 
719  QTimer mMapUpdateTimer;
720 
723 
725  bool mJobCancelled;
726 
728  QgsLabelingResults* mLabelingResults;
729 
731  bool mUseParallelRendering;
732 
734  bool mDrawRenderingStats;
735 
737  QgsMapRendererCache* mCache;
738 
739  QTimer *mResizeTimer;
740 
741  QgsPreviewEffect* mPreviewEffect;
742 
743  QgsRectangle imageRect( const QImage& img, const QgsMapSettings& mapSettings );
744 
745  QgsSnappingUtils* mSnappingUtils;
746 
747  QgsExpressionContextScope mExpressionContextScope;
748 
749 }; // class QgsMapCanvas
751 
752 
753 
754 
764 {
765  Q_OBJECT
766  public:
768 
769  protected slots:
770  void onExtentC2R();
771  void onExtentR2C();
772 
773  void onMapUnitsC2R();
774  void onMapUnitsR2C();
775 
777  void onMapRotationC2R();
779  void onMapRotationR2C();
780 
781  void onCrsTransformC2R();
782  void onCrsTransformR2C();
783 
784  void onDestCrsC2R();
785  void onDestCrsR2C();
786 
787  void onLayersC2R();
788 
789  protected:
792 
794 };
795 
796 
797 #endif
virtual void mouseMoveEvent(QMouseEvent *event)
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Base class for all map layer types.
Definition: qgsmaplayer.h:49
void setInOverview(bool isInOverview)
Definition: qgsmapcanvas.h:81
virtual void keyReleaseEvent(QKeyEvent *event)
A widget that displays an overview map.
virtual void dragEnterEvent(QDragEnterEvent *event)
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:474
A class that stores visibility and presence in overview flags together with pointer to the layer...
Definition: qgsmapcanvas.h:74
virtual void mouseReleaseEvent(QMouseEvent *event)
bool isVisible() const
Definition: qgsmapcanvas.h:83
A non GUI class for rendering a map layer set onto a QPainter.
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:360
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:105
void setVisible(bool visible)
Definition: qgsmapcanvas.h:80
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:472
The QgsMapSettings class contains configuration for rendering of the map.
Deprecated to be deleted, stuff from here should be moved elsewhere.
QgsMapRenderer * mRenderer
Definition: qgsmapcanvas.h:791
void scale(qreal sx, qreal sy)
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
A rectangular graphics item representing the map on the canvas.
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:450
virtual bool event(QEvent *event)
QgsMapCanvas * mCanvas
Definition: qgsmapcanvas.h:790
Enable anti-aliasin for map rendering.
A class for highlight features on the map.
Definition: qgshighlight.h:36
const QgsMapLayer * layer() const
Definition: qgsmapcanvas.h:87
virtual void keyPressEvent(QKeyEvent *event)
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:435
A class to represent a point.
Definition: qgspoint.h:117
virtual void connectNotify(const char *signal)
QScopedPointer< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:651
virtual void mousePressEvent(QMouseEvent *event)
Abstract base class for all map tools.
Definition: qgsmaptool.h:50
virtual void paintEvent(QPaintEvent *event)
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:475
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:443
bool isInOverview() const
Definition: qgsmapcanvas.h:84
Intermediate base class adding functionality that allows client to query the rendered image...
Class for storing a coordinate reference system (CRS)
This class has all the configuration of snapping and can return answers to snapping queries...
QgsMapCanvasLayer(QgsMapLayer *layer, bool visible=true, bool isInOverview=false)
Definition: qgsmapcanvas.h:77
UnitType
Map units that qgis supports.
Definition: qgis.h:155
Class that stores computed placement from labeling engine.
This class is responsible for keeping cache of rendered images of individual layers.
virtual void mouseDoubleClickEvent(QMouseEvent *event)
Class that does synchronization between QgsMapCanvas and its associated QgsMapRenderer: ...
Definition: qgsmapcanvas.h:763
Represents a vector layer which manages a vector based data sets.
QgsMapLayer * layer()
Definition: qgsmapcanvas.h:86
virtual void wheelEvent(QWheelEvent *event)
virtual void resizeEvent(QResizeEvent *event)