QGIS API Documentation  2.11.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 <list>
24 #include <memory>
25 #include <deque>
26 
27 #include "qgsrectangle.h"
28 #include "qgspoint.h"
29 #include "qgis.h"
30 
31 #include <QDomDocument>
32 #include <QGraphicsView>
33 #include <QtCore>
34 
35 #include "qgsmapsettings.h" // TEMPORARY
36 #include "qgsprevieweffect.h" //for QgsPreviewEffect::PreviewMode
37 
38 #ifdef HAVE_TOUCH
39 #include <QGestureEvent>
40 #endif
41 
42 class QWheelEvent;
43 class QPixmap;
44 class QPaintEvent;
45 class QKeyEvent;
46 class ResizeEvent;
47 
48 class QColor;
49 class QDomDocument;
50 class QPaintDevice;
51 class QMouseEvent;
52 class QRubberBand;
53 class QGraphicsScene;
54 
55 class QgsMapToPixel;
56 class QgsMapLayer;
57 class QgsHighlight;
58 class QgsVectorLayer;
59 
60 class QgsLabelingResults;
61 class QgsMapRenderer;
64 class QgsMapSettings;
65 class QgsMapCanvasMap;
67 class QgsMapTool;
68 class QgsSnappingUtils;
69 
75 class GUI_EXPORT QgsMapCanvasLayer
76 {
77  public:
78  QgsMapCanvasLayer( QgsMapLayer* layer, bool visible = true, bool isInOverview = false )
79  : mLayer( layer ), mVisible( visible ), mInOverview( isInOverview ) {}
80 
81  void setVisible( bool visible ) { mVisible = visible; }
82  void setInOverview( bool isInOverview ) { mInOverview = isInOverview; }
83 
84  bool isVisible() const { return mVisible; }
85  bool isInOverview() const { return mInOverview; }
86 
87  QgsMapLayer* layer() { return mLayer; }
88  const QgsMapLayer* layer() const { return mLayer; }
89 
90  private:
91  QgsMapLayer* mLayer;
92 
94  bool mVisible;
95 
97  bool mInOverview;
98 };
99 
100 
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 = 0, const char *name = 0 );
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 timeMiliseconds );
172 
175  int mapUpdateInterval() const;
176 
178  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 = NULL );
240 
242  void panToSelected( QgsVectorLayer* layer = NULL );
243 
245  void setMapTool( QgsMapTool* mapTool );
246 
253  void unsetMapTool( QgsMapTool* mapTool );
254 
256  QgsMapTool* mapTool();
257 
259  virtual void setCanvasColor( const QColor & _newVal );
261  virtual QColor canvasColor() const;
262 
264  void setSelectionColor( const QColor& color );
266 
268  void updateScale();
269 
271  Q_DECL_DEPRECATED void updateFullExtent() {}
273 
275  QgsMapLayer *layer( int index );
276 
278  int layerCount() const;
279 
281  QList<QgsMapLayer*> layers() const;
282 
288  void freeze( bool frz = true );
289 
291  bool isFrozen();
292 
295  Q_DECL_DEPRECATED void setDirty( bool _dirty );
296 
299  Q_DECL_DEPRECATED bool isDirty() const;
300 
302  void setMapUnits( QGis::UnitType mapUnits );
304 
305  QGis::UnitType mapUnits() const;
306 
308  const QgsMapToPixel* getCoordinateTransform();
309 
311  bool isDrawing();
312 
314  QgsMapLayer* currentLayer();
315 
317  void setWheelAction( WheelAction action, double factor = 2 );
318 
320  void zoomIn();
321 
323  void zoomOut();
324 
326  void zoomScale( double scale );
327 
330  void zoomByFactor( double scaleFactor, const QgsPoint *center = 0 );
331 
333  void zoomWithCenter( int x, int y, bool zoomIn );
334 
336  void enableAntiAliasing( bool theFlag );
337 
339  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
340 
343  Q_DECL_DEPRECATED void useImageToRender( bool theFlag );
344 
345  // following 2 methods should be moved elsewhere or changed to private
346  // currently used by pan map tool
348  void panActionEnd( QPoint releasePoint );
349 
351  void panAction( QMouseEvent * event );
352 
354  QPoint mouseLastXY();
355 
360  void setPreviewModeEnabled( bool previewEnabled );
361 
367  bool previewModeEnabled() const;
368 
376  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
377 
384  QgsPreviewEffect::PreviewMode previewMode() const;
385 
393  QgsSnappingUtils* snappingUtils() const;
401  void setSnappingUtils( QgsSnappingUtils* utils );
402 
403  public slots:
404 
406  void refresh();
407 
409  void selectionChangedSlot();
410 
412  void saveAsImage( QString theFileName, QPixmap * QPixmap = 0, QString = "PNG" );
413 
415  void layerStateChange();
416 
418  void layerCrsChange();
419 
421  void setRenderFlag( bool theFlag );
423  bool renderFlag() {return mRenderFlag;};
424 
426  bool hasCrsTransformEnabled();
427 
429  Q_DECL_DEPRECATED void updateMap();
430 
433  void stopRendering();
434 
436  Q_DECL_DEPRECATED void showError( QgsMapLayer * mapLayer );
437 
439  void readProject( const QDomDocument & );
440 
442  void writeProject( QDomDocument & );
443 
445  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
446 
449  static bool rotationEnabled();
450 
453  static void enableRotation( bool enabled );
454 
455  private slots:
457  void mapToolDestroyed();
458 
460  void rendererJobFinished();
461 
462  void mapUpdateTimeout();
463 
464  void refreshMap();
465 
466  signals:
468  Q_DECL_DEPRECATED void setProgress( int, int );
470 
473  void xyCoordinates( const QgsPoint &p );
474 
476  void scaleChanged( double );
477 
479  void extentsChanged();
480 
483  void rotationChanged( double );
484 
493  void renderComplete( QPainter * );
497 
498  // ### QGIS 3: renamte to mapRefreshFinished()
500  void mapCanvasRefreshed();
501 
502  // ### QGIS 3: rename to mapRefreshStarted()
504  void renderStarting();
505 
507  void layersChanged();
508 
510  void keyPressed( QKeyEvent * e );
511 
513  void keyReleased( QKeyEvent * e );
514 
516  void mapToolSet( QgsMapTool *tool );
517 
521  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
522 
523  // ### QGIS 3: remove the signal
525  void selectionChanged( QgsMapLayer * layer );
526 
528  void zoomLastStatusChanged( bool );
529 
531  void zoomNextStatusChanged( bool );
532 
535  void hasCrsTransformEnabledChanged( bool flag );
536 
539  void destinationCrsChanged();
540 
543  void mapUnitsChanged();
544 
547  void currentLayerChanged( QgsMapLayer* layer );
548 
549  protected:
550 #ifdef HAVE_TOUCH
551  bool event( QEvent * e ) override;
553 #endif
554 
556  void keyPressEvent( QKeyEvent * e ) override;
557 
559  void keyReleaseEvent( QKeyEvent * e ) override;
560 
562  void mouseDoubleClickEvent( QMouseEvent * e ) override;
563 
565  void mouseMoveEvent( QMouseEvent * e ) override;
566 
568  void mousePressEvent( QMouseEvent * e ) override;
569 
571  void mouseReleaseEvent( QMouseEvent * e ) override;
572 
574  void wheelEvent( QWheelEvent * e ) override;
575 
577  void resizeEvent( QResizeEvent * e ) override;
578 
580  void paintEvent( QPaintEvent * e ) override;
581 
583  void dragEnterEvent( QDragEnterEvent * e ) override;
584 
586  void moveCanvasContents( bool reset = false );
587 
589  void updateCanvasItemPositions();
590 
593 
596 
597 #if 0
598 
601  void connectNotify( const char * signal ) override;
602 #endif
603  void updateDatumTransformEntries();
605 
606  private:
608 
614  QgsMapCanvas( QgsMapCanvas const & );
615 
617  QgsMapSettings mSettings;
618 
620  QgsMapRenderer* mMapRenderer;
621 
623  QgsMapCanvasMap* mMap;
624 
626  QgsMapOverviewCanvas* mMapOverview;
627 
629  bool mFrozen;
630 
632  bool mRefreshScheduled;
633 
635  bool mRenderFlag;
636 
638  QgsMapLayer* mCurrentLayer;
639 
641  QGraphicsScene* mScene;
642 
644  QgsMapTool* mMapTool;
645 
647  QgsMapTool* mLastNonZoomMapTool;
648 
650  QList <QgsRectangle> mLastExtent;
651  int mLastExtentIndex;
652 
654  double mWheelZoomFactor;
655 
657  WheelAction mWheelAction;
658 
660  QTimer mMapUpdateTimer;
661 
664 
666  bool mJobCancelled;
667 
669  QgsLabelingResults* mLabelingResults;
670 
672  bool mUseParallelRendering;
673 
675  bool mDrawRenderingStats;
676 
678  QgsMapRendererCache* mCache;
679 
680  QTimer *mResizeTimer;
681 
682  QgsPreviewEffect* mPreviewEffect;
683 
684  QgsRectangle imageRect( const QImage& img, const QgsMapSettings& mapSettings );
685 
686  QgsSnappingUtils* mSnappingUtils;
687 
688 }; // class QgsMapCanvas
689 
690 
691 
692 
701 {
702  Q_OBJECT
703  public:
705 
706  protected slots:
707  void onExtentC2R();
708  void onExtentR2C();
709 
710  void onMapUnitsC2R();
711  void onMapUnitsR2C();
712 
714  void onMapRotationC2R();
716  void onMapRotationR2C();
717 
718  void onCrsTransformC2R();
719  void onCrsTransformR2C();
720 
721  void onDestCrsC2R();
722  void onDestCrsR2C();
723 
724  void onLayersC2R();
725 
726  protected:
729 
731 };
732 
733 
734 #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:82
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:229
A class that stores visibility and presence in overview flags together with pointer to the layer...
Definition: qgsmapcanvas.h:75
virtual void mouseReleaseEvent(QMouseEvent *event)
bool isVisible() const
Definition: qgsmapcanvas.h:84
A non GUI class for rendering a map layer set onto a QPainter.
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:339
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:81
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:423
The QgsMapSettings class contains configuration for rendering of the map.
QgsMapRenderer * mRenderer
Definition: qgsmapcanvas.h:728
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.
virtual bool event(QEvent *event)
QgsMapCanvas * mCanvas
Definition: qgsmapcanvas.h:727
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:88
virtual void keyPressEvent(QKeyEvent *event)
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:592
virtual void mousePressEvent(QMouseEvent *event)
Abstract base class for all map tools.
Definition: qgsmaptool.h:48
virtual void paintEvent(QPaintEvent *event)
bool isInOverview() const
Definition: qgsmapcanvas.h:85
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:78
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:700
Represents a vector layer which manages a vector based data sets.
QgsMapLayer * layer()
Definition: qgsmapcanvas.h:87
virtual void wheelEvent(QWheelEvent *event)
virtual void resizeEvent(QResizeEvent *event)