QGIS API Documentation  2.13.0-Master
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 
239  void zoomToSelected( QgsVectorLayer* layer = nullptr );
240 
244  void zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
245 
247  void panToSelected( QgsVectorLayer* layer = nullptr );
248 
250  void setMapTool( QgsMapTool* mapTool );
251 
258  void unsetMapTool( QgsMapTool* mapTool );
259 
261  QgsMapTool* mapTool();
262 
264  virtual void setCanvasColor( const QColor & _newVal );
266  virtual QColor canvasColor() const;
267 
269  void setSelectionColor( const QColor& color );
271 
273  void updateScale();
274 
276  Q_DECL_DEPRECATED void updateFullExtent() {}
278 
280  QgsMapLayer *layer( int index );
281 
283  int layerCount() const;
284 
286  QList<QgsMapLayer*> layers() const;
287 
296  void freeze( bool frz = true );
297 
303  bool isFrozen();
304 
307  Q_DECL_DEPRECATED void setDirty( bool _dirty );
308 
311  Q_DECL_DEPRECATED bool isDirty() const;
312 
314  void setMapUnits( QGis::UnitType mapUnits );
316 
317  QGis::UnitType mapUnits() const;
318 
321  QMap<QString, QString> layerStyleOverrides() const;
322 
325  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
326 
328  const QgsMapToPixel* getCoordinateTransform();
329 
331  bool isDrawing();
332 
334  QgsMapLayer* currentLayer();
335 
337  void setWheelAction( WheelAction action, double factor = 2 );
338 
340  void zoomIn();
341 
343  void zoomOut();
344 
346  void zoomScale( double scale );
347 
350  void zoomByFactor( double scaleFactor, const QgsPoint *center = nullptr );
351 
353  void zoomWithCenter( int x, int y, bool zoomIn );
354 
356  void enableAntiAliasing( bool theFlag );
357 
359  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
360 
362  void enableMapTileRendering( bool theFlag );
363 
366  Q_DECL_DEPRECATED void useImageToRender( bool theFlag );
367 
368  // following 2 methods should be moved elsewhere or changed to private
369  // currently used by pan map tool
371  void panActionEnd( QPoint releasePoint );
372 
374  void panAction( QMouseEvent * event );
375 
377  QPoint mouseLastXY();
378 
383  void setPreviewModeEnabled( bool previewEnabled );
384 
390  bool previewModeEnabled() const;
391 
399  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
400 
407  QgsPreviewEffect::PreviewMode previewMode() const;
408 
416  QgsSnappingUtils* snappingUtils() const;
424  void setSnappingUtils( QgsSnappingUtils* utils );
425 
434  void setExpressionContextScope( const QgsExpressionContextScope& scope ) { mExpressionContextScope = scope; }
435 
442  QgsExpressionContextScope& expressionContextScope() { return mExpressionContextScope; }
443 
448  const QgsExpressionContextScope& expressionContextScope() const { return mExpressionContextScope; }
449 
450  public slots:
451 
453  void refresh();
454 
456  void selectionChangedSlot();
457 
459  void saveAsImage( const QString& theFileName, QPixmap * QPixmap = nullptr, const QString& = "PNG" );
460 
462  void layerStateChange();
463 
465  void layerCrsChange();
466 
468  void setRenderFlag( bool theFlag );
470  bool renderFlag() {return mRenderFlag;}
471 
473  bool hasCrsTransformEnabled();
474 
476  Q_DECL_DEPRECATED void updateMap();
477 
480  void stopRendering();
481 
483  Q_DECL_DEPRECATED void showError( QgsMapLayer * mapLayer );
484 
486  void readProject( const QDomDocument & );
487 
489  void writeProject( QDomDocument & );
490 
492  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
493 
496  static bool rotationEnabled();
497 
500  static void enableRotation( bool enabled );
501 
502  private slots:
504  void mapToolDestroyed();
505 
507  void rendererJobFinished();
508 
509  void mapUpdateTimeout();
510 
511  void refreshMap();
512 
513  signals:
515  Q_DECL_DEPRECATED void setProgress( int, int );
517 
520  void xyCoordinates( const QgsPoint &p );
521 
523  void scaleChanged( double );
524 
526  void extentsChanged();
527 
530  void rotationChanged( double );
531 
540  void renderComplete( QPainter * );
544 
545  // ### QGIS 3: renamte to mapRefreshFinished()
547  void mapCanvasRefreshed();
548 
549  // ### QGIS 3: rename to mapRefreshStarted()
551  void renderStarting();
552 
554  void layersChanged();
555 
557  void keyPressed( QKeyEvent * e );
558 
560  void keyReleased( QKeyEvent * e );
561 
563  void mapToolSet( QgsMapTool *tool );
564 
568  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
569 
570  // ### QGIS 3: remove the signal
572  void selectionChanged( QgsMapLayer * layer );
573 
575  void zoomLastStatusChanged( bool );
576 
578  void zoomNextStatusChanged( bool );
579 
582  void hasCrsTransformEnabledChanged( bool flag );
583 
586  void destinationCrsChanged();
587 
590  void mapUnitsChanged();
591 
594  void currentLayerChanged( QgsMapLayer* layer );
595 
598  void layerStyleOverridesChanged();
599 
601  void messageEmitted( const QString& title, const QString& message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
602 
603  protected:
604 #ifdef HAVE_TOUCH
605  bool event( QEvent * e ) override;
607 #endif
608 
610  void keyPressEvent( QKeyEvent * e ) override;
611 
613  void keyReleaseEvent( QKeyEvent * e ) override;
614 
616  void mouseDoubleClickEvent( QMouseEvent * e ) override;
617 
619  void mouseMoveEvent( QMouseEvent * e ) override;
620 
622  void mousePressEvent( QMouseEvent * e ) override;
623 
625  void mouseReleaseEvent( QMouseEvent * e ) override;
626 
628  void wheelEvent( QWheelEvent * e ) override;
629 
631  void resizeEvent( QResizeEvent * e ) override;
632 
634  void paintEvent( QPaintEvent * e ) override;
635 
637  void dragEnterEvent( QDragEnterEvent * e ) override;
638 
640  void moveCanvasContents( bool reset = false );
641 
644  void zoomToFeatureExtent( QgsRectangle& rect );
645 
647  void updateCanvasItemPositions();
648 
651 
654 
655 #if 0
656 
659  void connectNotify( const char * signal ) override;
660 #endif
661  void updateDatumTransformEntries();
663 
664  private:
666 
672  QgsMapCanvas( QgsMapCanvas const & );
673 
675  QgsMapSettings mSettings;
676 
678  QgsMapRenderer* mMapRenderer;
679 
681  QgsMapCanvasMap* mMap;
682 
684  QgsMapOverviewCanvas* mMapOverview;
685 
687  bool mFrozen;
688 
690  bool mRefreshScheduled;
691 
693  bool mRenderFlag;
694 
696  QgsMapLayer* mCurrentLayer;
697 
699  QGraphicsScene* mScene;
700 
702  QgsMapTool* mMapTool;
703 
705  QgsMapTool* mLastNonZoomMapTool;
706 
708  QList <QgsRectangle> mLastExtent;
709  int mLastExtentIndex;
710 
712  double mWheelZoomFactor;
713 
715  WheelAction mWheelAction;
716 
718  QTimer mMapUpdateTimer;
719 
722 
724  bool mJobCancelled;
725 
727  QgsLabelingResults* mLabelingResults;
728 
730  bool mUseParallelRendering;
731 
733  bool mDrawRenderingStats;
734 
736  QgsMapRendererCache* mCache;
737 
738  QTimer *mResizeTimer;
739 
740  QgsPreviewEffect* mPreviewEffect;
741 
742  QgsRectangle imageRect( const QImage& img, const QgsMapSettings& mapSettings );
743 
744  QgsSnappingUtils* mSnappingUtils;
745 
746  QgsExpressionContextScope mExpressionContextScope;
747 
748 }; // class QgsMapCanvas
750 
751 
752 
753 
763 {
764  Q_OBJECT
765  public:
767 
768  protected slots:
769  void onExtentC2R();
770  void onExtentR2C();
771 
772  void onMapUnitsC2R();
773  void onMapUnitsR2C();
774 
776  void onMapRotationC2R();
778  void onMapRotationR2C();
779 
780  void onCrsTransformC2R();
781  void onCrsTransformR2C();
782 
783  void onDestCrsC2R();
784  void onDestCrsR2C();
785 
786  void onLayersC2R();
787 
788  protected:
791 
793 };
794 
795 
796 #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:407
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:359
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:470
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:790
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:448
virtual bool event(QEvent *event)
QgsMapCanvas * mCanvas
Definition: qgsmapcanvas.h:789
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:434
A class to represent a point.
Definition: qgspoint.h:65
virtual void connectNotify(const char *signal)
QScopedPointer< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:650
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:408
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:442
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:762
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)