QGIS API Documentation  2.15.0-Master (af20121)
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 )
79  , mVisible( visible )
80  , mInOverview( isInOverview )
81  {}
82 
83  void setVisible( bool visible ) { mVisible = visible; }
84  void setInOverview( bool isInOverview ) { mInOverview = isInOverview; }
85 
86  bool isVisible() const { return mVisible; }
87  bool isInOverview() const { return mInOverview; }
88 
89  QgsMapLayer* layer() { return mLayer; }
90  const QgsMapLayer* layer() const { return mLayer; }
91 
92  private:
93  QgsMapLayer* mLayer;
94 
96  bool mVisible;
97 
99  bool mInOverview;
100 };
101 
102 
108 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
109 {
110  Q_OBJECT
111 
112  public:
113 
114  enum WheelAction { WheelZoom, WheelZoomAndRecenter, WheelZoomToMouseCursor, WheelNothing };
115 
117  QgsMapCanvas( QWidget * parent = nullptr, const char *name = nullptr );
118 
120  ~QgsMapCanvas();
121 
126  void setMagnificationFactor( double level );
127 
130  double magnificationFactor() const;
131 
132  void setLayerSet( QList<QgsMapCanvasLayer>& layers );
133 
134  void setCurrentLayer( QgsMapLayer* layer );
135 
136  // ### QGIS 3: make QgsMapCanvas independent from overview
137  void updateOverview();
138 
139  // ### QGIS 3: make QgsMapCanvas independent from overview
140  void enableOverviewMode( QgsMapOverviewCanvas* overview );
141 
144  const QgsMapSettings& mapSettings() const;
145 
148  void setCrsTransformEnabled( bool enabled );
149 
152  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );
153 
156  const QgsLabelingResults* labelingResults() const;
157 
160  void setCachingEnabled( bool enabled );
161 
164  bool isCachingEnabled() const;
165 
168  void clearCache();
169 
172  void refreshAllLayers();
173 
176  void setParallelRenderingEnabled( bool enabled );
177 
180  bool isParallelRenderingEnabled() const;
181 
184  void setMapUpdateInterval( int timeMilliseconds );
185 
188  int mapUpdateInterval() const;
189 
191  Q_DECL_DEPRECATED QgsMapCanvasMap *map();
192 
198  Q_DECL_DEPRECATED QgsMapRenderer *mapRenderer();
199 
202  Q_DECL_DEPRECATED QPaintDevice &canvasPaintDevice();
203 
205  double scale();
206 
209  Q_DECL_DEPRECATED void clear();
210 
212  double mapUnitsPerPixel() const;
213 
215  QgsRectangle extent() const;
217  QgsRectangle fullExtent() const;
218 
220  void setExtent( const QgsRectangle &r, bool magnified = false );
221 
224  double rotation() const;
225 
228  void setRotation( double degrees );
229 
232  void setCenter( const QgsPoint& center );
233 
236  QgsPoint center() const;
237 
239  void zoomToFullExtent();
240 
242  void zoomToPreviousExtent();
243 
245  void zoomToNextExtent();
246 
247  // ! Clears the list of extents and sets current extent as first item
248  void clearExtentHistory();
249 
253  void zoomToSelected( QgsVectorLayer* layer = nullptr );
254 
258  void zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
259 
261  void panToSelected( QgsVectorLayer* layer = nullptr );
262 
264  void setMapTool( QgsMapTool* mapTool );
265 
272  void unsetMapTool( QgsMapTool* mapTool );
273 
275  QgsMapTool* mapTool();
276 
278  virtual void setCanvasColor( const QColor & _newVal );
280  virtual QColor canvasColor() const;
281 
283  void setSelectionColor( const QColor& color );
285 
287  void updateScale();
288 
290  Q_DECL_DEPRECATED void updateFullExtent() {}
292 
294  QgsMapLayer *layer( int index );
295 
297  int layerCount() const;
298 
300  QList<QgsMapLayer*> layers() const;
301 
310  void freeze( bool frz = true );
311 
317  bool isFrozen();
318 
321  Q_DECL_DEPRECATED void setDirty( bool _dirty );
322 
325  Q_DECL_DEPRECATED bool isDirty() const;
326 
328  void setMapUnits( QGis::UnitType mapUnits );
329 
331  QGis::UnitType mapUnits() const;
332 
335  QMap<QString, QString> layerStyleOverrides() const;
336 
339  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
340 
342  const QgsMapToPixel* getCoordinateTransform();
343 
345  bool isDrawing();
346 
348  QgsMapLayer* currentLayer();
349 
351  void setWheelAction( WheelAction action, double factor = 2 );
352 
354  void zoomIn();
355 
357  void zoomOut();
358 
360  void zoomScale( double scale );
361 
364  void zoomByFactor( double scaleFactor, const QgsPoint *center = nullptr );
365 
367  void zoomWithCenter( int x, int y, bool zoomIn );
368 
370  void enableAntiAliasing( bool theFlag );
371 
373  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
374 
376  void enableMapTileRendering( bool theFlag );
377 
380  Q_DECL_DEPRECATED void useImageToRender( bool theFlag );
381 
382  // following 2 methods should be moved elsewhere or changed to private
383  // currently used by pan map tool
385  void panActionEnd( QPoint releasePoint );
386 
388  void panAction( QMouseEvent * event );
389 
391  QPoint mouseLastXY();
392 
397  void setPreviewModeEnabled( bool previewEnabled );
398 
404  bool previewModeEnabled() const;
405 
413  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
414 
421  QgsPreviewEffect::PreviewMode previewMode() const;
422 
430  QgsSnappingUtils* snappingUtils() const;
438  void setSnappingUtils( QgsSnappingUtils* utils );
439 
448  void setExpressionContextScope( const QgsExpressionContextScope& scope ) { mExpressionContextScope = scope; }
449 
456  QgsExpressionContextScope& expressionContextScope() { return mExpressionContextScope; }
457 
463  const QgsExpressionContextScope& expressionContextScope() const { return mExpressionContextScope; }
464 
467  void setSegmentationTolerance( double tolerance );
470  void setSegmentationToleranceType( QgsAbstractGeometryV2::SegmentationToleranceType type );
471 
472  public slots:
473 
475  void refresh();
476 
478  void selectionChangedSlot();
479 
481  void saveAsImage( const QString& theFileName, QPixmap * QPixmap = nullptr, const QString& = "PNG" );
482 
484  void layerStateChange();
485 
487  void layerCrsChange();
488 
490  void setRenderFlag( bool theFlag );
492  bool renderFlag() {return mRenderFlag;}
493 
495  bool hasCrsTransformEnabled();
496 
498  Q_DECL_DEPRECATED void updateMap();
499 
502  void stopRendering();
503 
505  Q_DECL_DEPRECATED void showError( QgsMapLayer * mapLayer );
506 
508  void readProject( const QDomDocument & );
509 
511  void writeProject( QDomDocument & );
512 
514  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
515 
518  static bool rotationEnabled();
519 
522  static void enableRotation( bool enabled );
523 
524 
525  private slots:
527  void mapToolDestroyed();
528 
530  void rendererJobFinished();
531 
532  void mapUpdateTimeout();
533 
534  void refreshMap();
535 
536  signals:
538  Q_DECL_DEPRECATED void setProgress( int, int );
540 
543  void xyCoordinates( const QgsPoint &p );
544 
546  void scaleChanged( double );
547 
549  void extentsChanged();
550 
553  void rotationChanged( double );
554 
562  void renderComplete( QPainter * );
566 
567  // ### QGIS 3: renamte to mapRefreshFinished()
569  void mapCanvasRefreshed();
570 
571  // ### QGIS 3: rename to mapRefreshStarted()
573  void renderStarting();
574 
576  void layersChanged();
577 
579  void keyPressed( QKeyEvent * e );
580 
582  void keyReleased( QKeyEvent * e );
583 
585  void mapToolSet( QgsMapTool *tool );
586 
590  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
591 
592  // ### QGIS 3: remove the signal
594  void selectionChanged( QgsMapLayer * layer );
595 
597  void zoomLastStatusChanged( bool );
598 
600  void zoomNextStatusChanged( bool );
601 
604  void hasCrsTransformEnabledChanged( bool flag );
605 
608  void destinationCrsChanged();
609 
612  void mapUnitsChanged();
613 
616  void currentLayerChanged( QgsMapLayer* layer );
617 
620  void layerStyleOverridesChanged();
621 
623  void messageEmitted( const QString& title, const QString& message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
624 
625  protected:
626 #ifdef HAVE_TOUCH
627  bool event( QEvent * e ) override;
629 #endif
630 
632  void keyPressEvent( QKeyEvent * e ) override;
633 
635  void keyReleaseEvent( QKeyEvent * e ) override;
636 
638  void mouseDoubleClickEvent( QMouseEvent * e ) override;
639 
641  void mouseMoveEvent( QMouseEvent * e ) override;
642 
644  void mousePressEvent( QMouseEvent * e ) override;
645 
647  void mouseReleaseEvent( QMouseEvent * e ) override;
648 
650  void wheelEvent( QWheelEvent * e ) override;
651 
653  void resizeEvent( QResizeEvent * e ) override;
654 
656  void paintEvent( QPaintEvent * e ) override;
657 
659  void dragEnterEvent( QDragEnterEvent * e ) override;
660 
662  void moveCanvasContents( bool reset = false );
663 
666  void zoomToFeatureExtent( QgsRectangle& rect );
667 
669  void updateCanvasItemPositions();
670 
673 
676 
677 #if 0
678 
681  void connectNotify( const char * signal ) override;
682 #endif
683  void updateDatumTransformEntries();
685 
686  private:
688 
694  QgsMapCanvas( QgsMapCanvas const & );
695 
697  QgsMapSettings mSettings;
698 
700  QgsMapRenderer* mMapRenderer;
701 
703  QgsMapCanvasMap* mMap;
704 
706  QgsMapOverviewCanvas* mMapOverview;
707 
709  bool mFrozen;
710 
712  bool mRefreshScheduled;
713 
715  bool mRenderFlag;
716 
718  QgsMapLayer* mCurrentLayer;
719 
721  QGraphicsScene* mScene;
722 
724  QgsMapTool* mMapTool;
725 
727  QgsMapTool* mLastNonZoomMapTool;
728 
730  QList <QgsRectangle> mLastExtent;
731  int mLastExtentIndex;
732 
734  double mWheelZoomFactor;
735 
737  WheelAction mWheelAction;
738 
740  QTimer mMapUpdateTimer;
741 
743  double mMagnificationFactor;
744 
747 
749  bool mJobCancelled;
750 
752  QgsLabelingResults* mLabelingResults;
753 
755  bool mUseParallelRendering;
756 
758  bool mDrawRenderingStats;
759 
761  QgsMapRendererCache* mCache;
762 
763  QTimer *mResizeTimer;
764 
765  QgsPreviewEffect* mPreviewEffect;
766 
767  QgsRectangle imageRect( const QImage& img, const QgsMapSettings& mapSettings );
768 
769  QgsSnappingUtils* mSnappingUtils;
770 
771  QgsExpressionContextScope mExpressionContextScope;
772 
775  void updateMapSize();
776 
777 }; // class QgsMapCanvas
779 
780 
781 
782 
792 {
793  Q_OBJECT
794  public:
796 
797  protected slots:
798  void onExtentC2R();
799  void onExtentR2C();
800 
801  void onMapUnitsC2R();
802  void onMapUnitsR2C();
803 
805  void onMapRotationC2R();
807  void onMapRotationR2C();
808 
809  void onCrsTransformC2R();
810  void onCrsTransformR2C();
811 
812  void onDestCrsC2R();
813  void onDestCrsR2C();
814 
815  void onLayersC2R();
816 
817  protected:
820 
822 };
823 
824 
825 #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:84
virtual void keyReleaseEvent(QKeyEvent *event)
A widget that displays an overview map.
virtual void dragEnterEvent(QDragEnterEvent *event)
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:504
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:86
A non GUI class for rendering a map layer set onto a QPainter.
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:373
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:108
void setVisible(bool visible)
Definition: qgsmapcanvas.h:83
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:492
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:819
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:463
virtual bool event(QEvent *event)
QgsMapCanvas * mCanvas
Definition: qgsmapcanvas.h:818
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:90
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:448
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:672
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:505
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:456
bool isInOverview() const
Definition: qgsmapcanvas.h:87
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:159
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:791
Represents a vector layer which manages a vector based data sets.
QgsMapLayer * layer()
Definition: qgsmapcanvas.h:89
virtual void wheelEvent(QWheelEvent *event)
virtual void resizeEvent(QResizeEvent *event)