QGIS API Documentation  2.12.0-Lyon
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 <list>
24 #include <memory>
25 #include <deque>
26 
27 #include "qgsexpressioncontext.h"
28 #include "qgsrectangle.h"
29 #include "qgspoint.h"
30 #include "qgis.h"
31 
32 #include <QDomDocument>
33 #include <QGraphicsView>
34 #include <QtCore>
35 
36 #include "qgsmapsettings.h" // TEMPORARY
37 #include "qgsprevieweffect.h" //for QgsPreviewEffect::PreviewMode
38 
39 #ifdef HAVE_TOUCH
40 #include <QGestureEvent>
41 #endif
42 
43 class QWheelEvent;
44 class QPixmap;
45 class QPaintEvent;
46 class QKeyEvent;
47 class ResizeEvent;
48 
49 class QColor;
50 class QDomDocument;
51 class QPaintDevice;
52 class QMouseEvent;
53 class QRubberBand;
54 class QGraphicsScene;
55 
56 class QgsMapToPixel;
57 class QgsMapLayer;
58 class QgsHighlight;
59 class QgsVectorLayer;
60 
61 class QgsLabelingResults;
62 class QgsMapRenderer;
65 class QgsMapSettings;
66 class QgsMapCanvasMap;
68 class QgsMapTool;
69 class QgsSnappingUtils;
70 
76 class GUI_EXPORT QgsMapCanvasLayer
77 {
78  public:
79  QgsMapCanvasLayer( QgsMapLayer* layer, bool visible = true, bool isInOverview = false )
80  : mLayer( layer ), mVisible( visible ), mInOverview( isInOverview ) {}
81 
82  void setVisible( bool visible ) { mVisible = visible; }
83  void setInOverview( bool isInOverview ) { mInOverview = isInOverview; }
84 
85  bool isVisible() const { return mVisible; }
86  bool isInOverview() const { return mInOverview; }
87 
88  QgsMapLayer* layer() { return mLayer; }
89  const QgsMapLayer* layer() const { return mLayer; }
90 
91  private:
92  QgsMapLayer* mLayer;
93 
95  bool mVisible;
96 
98  bool mInOverview;
99 };
100 
101 
107 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
108 {
109  Q_OBJECT
110 
111  public:
112 
113  enum WheelAction { WheelZoom, WheelZoomAndRecenter, WheelZoomToMouseCursor, WheelNothing };
114 
116  QgsMapCanvas( QWidget * parent = 0, const char *name = 0 );
117 
119  ~QgsMapCanvas();
120 
121  void setLayerSet( QList<QgsMapCanvasLayer>& layers );
122 
123  void setCurrentLayer( QgsMapLayer* layer );
124 
125  // ### QGIS 3: make QgsMapCanvas independent from overview
126  void updateOverview();
127 
128  // ### QGIS 3: make QgsMapCanvas independent from overview
129  void enableOverviewMode( QgsMapOverviewCanvas* overview );
130 
133  const QgsMapSettings& mapSettings() const;
134 
137  void setCrsTransformEnabled( bool enabled );
138 
141  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );
142 
145  const QgsLabelingResults* labelingResults() const;
146 
149  void setCachingEnabled( bool enabled );
150 
153  bool isCachingEnabled() const;
154 
157  void clearCache();
158 
161  void refreshAllLayers();
162 
165  void setParallelRenderingEnabled( bool enabled );
166 
169  bool isParallelRenderingEnabled() const;
170 
173  void setMapUpdateInterval( int timeMiliseconds );
174 
177  int mapUpdateInterval() const;
178 
180  QgsMapCanvasMap* map();
181 
187  Q_DECL_DEPRECATED QgsMapRenderer* mapRenderer();
188 
191  Q_DECL_DEPRECATED QPaintDevice &canvasPaintDevice();
192 
194  double scale();
195 
198  Q_DECL_DEPRECATED void clear();
199 
201  double mapUnitsPerPixel() const;
202 
204  QgsRectangle extent() const;
206  QgsRectangle fullExtent() const;
207 
209  void setExtent( const QgsRectangle &r );
210 
213  double rotation() const;
214 
217  void setRotation( double degrees );
218 
221  void setCenter( const QgsPoint& center );
222 
225  QgsPoint center() const;
226 
228  void zoomToFullExtent();
229 
231  void zoomToPreviousExtent();
232 
234  void zoomToNextExtent();
235 
236  // ! Clears the list of extents and sets current extent as first item
237  void clearExtentHistory();
238 
241  void zoomToSelected( QgsVectorLayer* layer = NULL );
242 
244  void panToSelected( QgsVectorLayer* layer = NULL );
245 
247  void setMapTool( QgsMapTool* mapTool );
248 
255  void unsetMapTool( QgsMapTool* mapTool );
256 
258  QgsMapTool* mapTool();
259 
261  virtual void setCanvasColor( const QColor & _newVal );
263  virtual QColor canvasColor() const;
264 
266  void setSelectionColor( const QColor& color );
268 
270  void updateScale();
271 
273  Q_DECL_DEPRECATED void updateFullExtent() {}
275 
277  QgsMapLayer *layer( int index );
278 
280  int layerCount() const;
281 
283  QList<QgsMapLayer*> layers() const;
284 
290  void freeze( bool frz = true );
291 
293  bool isFrozen();
294 
297  Q_DECL_DEPRECATED void setDirty( bool _dirty );
298 
301  Q_DECL_DEPRECATED bool isDirty() const;
302 
304  void setMapUnits( QGis::UnitType mapUnits );
306 
307  QGis::UnitType mapUnits() const;
308 
311  QMap<QString, QString> layerStyleOverrides() const;
312 
315  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
316 
318  const QgsMapToPixel* getCoordinateTransform();
319 
321  bool isDrawing();
322 
324  QgsMapLayer* currentLayer();
325 
327  void setWheelAction( WheelAction action, double factor = 2 );
328 
330  void zoomIn();
331 
333  void zoomOut();
334 
336  void zoomScale( double scale );
337 
340  void zoomByFactor( double scaleFactor, const QgsPoint *center = 0 );
341 
343  void zoomWithCenter( int x, int y, bool zoomIn );
344 
346  void enableAntiAliasing( bool theFlag );
347 
349  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
350 
353  Q_DECL_DEPRECATED void useImageToRender( bool theFlag );
354 
355  // following 2 methods should be moved elsewhere or changed to private
356  // currently used by pan map tool
358  void panActionEnd( QPoint releasePoint );
359 
361  void panAction( QMouseEvent * event );
362 
364  QPoint mouseLastXY();
365 
370  void setPreviewModeEnabled( bool previewEnabled );
371 
377  bool previewModeEnabled() const;
378 
386  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
387 
394  QgsPreviewEffect::PreviewMode previewMode() const;
395 
403  QgsSnappingUtils* snappingUtils() const;
411  void setSnappingUtils( QgsSnappingUtils* utils );
412 
421  void setExpressionContextScope( const QgsExpressionContextScope& scope ) { mExpressionContextScope = scope; }
422 
429  QgsExpressionContextScope& expressionContextScope() { return mExpressionContextScope; }
430 
435  const QgsExpressionContextScope& expressionContextScope() const { return mExpressionContextScope; }
436 
437  public slots:
438 
440  void refresh();
441 
443  void selectionChangedSlot();
444 
446  void saveAsImage( const QString& theFileName, QPixmap * QPixmap = 0, const QString& = "PNG" );
447 
449  void layerStateChange();
450 
452  void layerCrsChange();
453 
455  void setRenderFlag( bool theFlag );
457  bool renderFlag() {return mRenderFlag;};
458 
460  bool hasCrsTransformEnabled();
461 
463  Q_DECL_DEPRECATED void updateMap();
464 
467  void stopRendering();
468 
470  Q_DECL_DEPRECATED void showError( QgsMapLayer * mapLayer );
471 
473  void readProject( const QDomDocument & );
474 
476  void writeProject( QDomDocument & );
477 
479  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
480 
483  static bool rotationEnabled();
484 
487  static void enableRotation( bool enabled );
488 
489  private slots:
491  void mapToolDestroyed();
492 
494  void rendererJobFinished();
495 
496  void mapUpdateTimeout();
497 
498  void refreshMap();
499 
500  signals:
502  Q_DECL_DEPRECATED void setProgress( int, int );
504 
507  void xyCoordinates( const QgsPoint &p );
508 
510  void scaleChanged( double );
511 
513  void extentsChanged();
514 
517  void rotationChanged( double );
518 
527  void renderComplete( QPainter * );
531 
532  // ### QGIS 3: renamte to mapRefreshFinished()
534  void mapCanvasRefreshed();
535 
536  // ### QGIS 3: rename to mapRefreshStarted()
538  void renderStarting();
539 
541  void layersChanged();
542 
544  void keyPressed( QKeyEvent * e );
545 
547  void keyReleased( QKeyEvent * e );
548 
550  void mapToolSet( QgsMapTool *tool );
551 
555  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
556 
557  // ### QGIS 3: remove the signal
559  void selectionChanged( QgsMapLayer * layer );
560 
562  void zoomLastStatusChanged( bool );
563 
565  void zoomNextStatusChanged( bool );
566 
569  void hasCrsTransformEnabledChanged( bool flag );
570 
573  void destinationCrsChanged();
574 
577  void mapUnitsChanged();
578 
581  void currentLayerChanged( QgsMapLayer* layer );
582 
585  void layerStyleOverridesChanged();
586 
587  protected:
588 #ifdef HAVE_TOUCH
589  bool event( QEvent * e ) override;
591 #endif
592 
594  void keyPressEvent( QKeyEvent * e ) override;
595 
597  void keyReleaseEvent( QKeyEvent * e ) override;
598 
600  void mouseDoubleClickEvent( QMouseEvent * e ) override;
601 
603  void mouseMoveEvent( QMouseEvent * e ) override;
604 
606  void mousePressEvent( QMouseEvent * e ) override;
607 
609  void mouseReleaseEvent( QMouseEvent * e ) override;
610 
612  void wheelEvent( QWheelEvent * e ) override;
613 
615  void resizeEvent( QResizeEvent * e ) override;
616 
618  void paintEvent( QPaintEvent * e ) override;
619 
621  void dragEnterEvent( QDragEnterEvent * e ) override;
622 
624  void moveCanvasContents( bool reset = false );
625 
627  void updateCanvasItemPositions();
628 
631 
634 
635 #if 0
636 
639  void connectNotify( const char * signal ) override;
640 #endif
641  void updateDatumTransformEntries();
643 
644  private:
646 
652  QgsMapCanvas( QgsMapCanvas const & );
653 
655  QgsMapSettings mSettings;
656 
658  QgsMapRenderer* mMapRenderer;
659 
661  QgsMapCanvasMap* mMap;
662 
664  QgsMapOverviewCanvas* mMapOverview;
665 
667  bool mFrozen;
668 
670  bool mRefreshScheduled;
671 
673  bool mRenderFlag;
674 
676  QgsMapLayer* mCurrentLayer;
677 
679  QGraphicsScene* mScene;
680 
682  QgsMapTool* mMapTool;
683 
685  QgsMapTool* mLastNonZoomMapTool;
686 
688  QList <QgsRectangle> mLastExtent;
689  int mLastExtentIndex;
690 
692  double mWheelZoomFactor;
693 
695  WheelAction mWheelAction;
696 
698  QTimer mMapUpdateTimer;
699 
702 
704  bool mJobCancelled;
705 
707  QgsLabelingResults* mLabelingResults;
708 
710  bool mUseParallelRendering;
711 
713  bool mDrawRenderingStats;
714 
716  QgsMapRendererCache* mCache;
717 
718  QTimer *mResizeTimer;
719 
720  QgsPreviewEffect* mPreviewEffect;
721 
722  QgsRectangle imageRect( const QImage& img, const QgsMapSettings& mapSettings );
723 
724  QgsSnappingUtils* mSnappingUtils;
725 
726  QgsExpressionContextScope mExpressionContextScope;
727 
728 }; // class QgsMapCanvas
730 
731 
732 
733 
742 {
743  Q_OBJECT
744  public:
746 
747  protected slots:
748  void onExtentC2R();
749  void onExtentR2C();
750 
751  void onMapUnitsC2R();
752  void onMapUnitsR2C();
753 
755  void onMapRotationC2R();
757  void onMapRotationR2C();
758 
759  void onCrsTransformC2R();
760  void onCrsTransformR2C();
761 
762  void onDestCrsC2R();
763  void onDestCrsR2C();
764 
765  void onLayersC2R();
766 
767  protected:
770 
772 };
773 
774 
775 #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:83
virtual void keyReleaseEvent(QKeyEvent *event)
A widget that displays an overview map.
virtual void dragEnterEvent(QDragEnterEvent *event)
UnitType
Map units that qgis supports.
Definition: qgis.h:147
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:390
A class that stores visibility and presence in overview flags together with pointer to the layer...
Definition: qgsmapcanvas.h:76
virtual void mouseReleaseEvent(QMouseEvent *event)
bool isVisible() const
Definition: qgsmapcanvas.h:85
A non GUI class for rendering a map layer set onto a QPainter.
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:349
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:107
void setVisible(bool visible)
Definition: qgsmapcanvas.h:82
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:457
The QgsMapSettings class contains configuration for rendering of the map.
QgsMapRenderer * mRenderer
Definition: qgsmapcanvas.h:769
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:435
virtual bool event(QEvent *event)
QgsMapCanvas * mCanvas
Definition: qgsmapcanvas.h:768
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:89
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:421
A class to represent a point.
Definition: qgspoint.h:63
virtual void connectNotify(const char *signal)
QScopedPointer< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:630
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:391
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:429
bool isInOverview() const
Definition: qgsmapcanvas.h:86
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...
QgsMapCanvasRendererSync(QgsMapCanvas *canvas, QgsMapRenderer *renderer)
QgsMapCanvasLayer(QgsMapLayer *layer, bool visible=true, bool isInOverview=false)
Definition: qgsmapcanvas.h:79
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:741
Represents a vector layer which manages a vector based data sets.
QgsMapLayer * layer()
Definition: qgsmapcanvas.h:88
virtual void wheelEvent(QWheelEvent *event)
virtual void resizeEvent(QResizeEvent *event)