QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
309  QMap<QString, QString> layerStyleOverrides() const;
310 
313  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
314 
316  const QgsMapToPixel* getCoordinateTransform();
317 
319  bool isDrawing();
320 
322  QgsMapLayer* currentLayer();
323 
325  void setWheelAction( WheelAction action, double factor = 2 );
326 
328  void zoomIn();
329 
331  void zoomOut();
332 
334  void zoomScale( double scale );
335 
338  void zoomByFactor( double scaleFactor, const QgsPoint *center = 0 );
339 
341  void zoomWithCenter( int x, int y, bool zoomIn );
342 
344  void enableAntiAliasing( bool theFlag );
345 
347  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
348 
351  Q_DECL_DEPRECATED void useImageToRender( bool theFlag );
352 
353  // following 2 methods should be moved elsewhere or changed to private
354  // currently used by pan map tool
356  void panActionEnd( QPoint releasePoint );
357 
359  void panAction( QMouseEvent * event );
360 
362  QPoint mouseLastXY();
363 
368  void setPreviewModeEnabled( bool previewEnabled );
369 
375  bool previewModeEnabled() const;
376 
384  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
385 
392  QgsPreviewEffect::PreviewMode previewMode() const;
393 
401  QgsSnappingUtils* snappingUtils() const;
409  void setSnappingUtils( QgsSnappingUtils* utils );
410 
411  public slots:
412 
414  void refresh();
415 
417  void selectionChangedSlot();
418 
420  void saveAsImage( QString theFileName, QPixmap * QPixmap = 0, QString = "PNG" );
421 
423  void layerStateChange();
424 
426  void layerCrsChange();
427 
429  void setRenderFlag( bool theFlag );
431  bool renderFlag() {return mRenderFlag;};
432 
434  bool hasCrsTransformEnabled();
435 
437  Q_DECL_DEPRECATED void updateMap();
438 
441  void stopRendering();
442 
444  Q_DECL_DEPRECATED void showError( QgsMapLayer * mapLayer );
445 
447  void readProject( const QDomDocument & );
448 
450  void writeProject( QDomDocument & );
451 
453  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
454 
457  static bool rotationEnabled();
458 
461  static void enableRotation( bool enabled );
462 
463  private slots:
465  void mapToolDestroyed();
466 
468  void rendererJobFinished();
469 
470  void mapUpdateTimeout();
471 
472  void refreshMap();
473 
474  signals:
476  Q_DECL_DEPRECATED void setProgress( int, int );
478 
481  void xyCoordinates( const QgsPoint &p );
482 
484  void scaleChanged( double );
485 
487  void extentsChanged();
488 
491  void rotationChanged( double );
492 
501  void renderComplete( QPainter * );
505 
506  // ### QGIS 3: renamte to mapRefreshFinished()
508  void mapCanvasRefreshed();
509 
510  // ### QGIS 3: rename to mapRefreshStarted()
512  void renderStarting();
513 
515  void layersChanged();
516 
518  void keyPressed( QKeyEvent * e );
519 
521  void keyReleased( QKeyEvent * e );
522 
524  void mapToolSet( QgsMapTool *tool );
525 
529  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
530 
531  // ### QGIS 3: remove the signal
533  void selectionChanged( QgsMapLayer * layer );
534 
536  void zoomLastStatusChanged( bool );
537 
539  void zoomNextStatusChanged( bool );
540 
543  void hasCrsTransformEnabledChanged( bool flag );
544 
547  void destinationCrsChanged();
548 
551  void mapUnitsChanged();
552 
555  void currentLayerChanged( QgsMapLayer* layer );
556 
559  void layerStyleOverridesChanged();
560 
561  protected:
562 #ifdef HAVE_TOUCH
563  bool event( QEvent * e ) override;
565 #endif
566 
568  void keyPressEvent( QKeyEvent * e ) override;
569 
571  void keyReleaseEvent( QKeyEvent * e ) override;
572 
574  void mouseDoubleClickEvent( QMouseEvent * e ) override;
575 
577  void mouseMoveEvent( QMouseEvent * e ) override;
578 
580  void mousePressEvent( QMouseEvent * e ) override;
581 
583  void mouseReleaseEvent( QMouseEvent * e ) override;
584 
586  void wheelEvent( QWheelEvent * e ) override;
587 
589  void resizeEvent( QResizeEvent * e ) override;
590 
592  void paintEvent( QPaintEvent * e ) override;
593 
595  void dragEnterEvent( QDragEnterEvent * e ) override;
596 
598  void moveCanvasContents( bool reset = false );
599 
601  void updateCanvasItemPositions();
602 
605 
608 
609 #if 0
610 
613  void connectNotify( const char * signal ) override;
614 #endif
615  void updateDatumTransformEntries();
617 
618  private:
620 
626  QgsMapCanvas( QgsMapCanvas const & );
627 
629  QgsMapSettings mSettings;
630 
632  QgsMapRenderer* mMapRenderer;
633 
635  QgsMapCanvasMap* mMap;
636 
638  QgsMapOverviewCanvas* mMapOverview;
639 
641  bool mFrozen;
642 
644  bool mRefreshScheduled;
645 
647  bool mRenderFlag;
648 
650  QgsMapLayer* mCurrentLayer;
651 
653  QGraphicsScene* mScene;
654 
656  QgsMapTool* mMapTool;
657 
659  QgsMapTool* mLastNonZoomMapTool;
660 
662  QList <QgsRectangle> mLastExtent;
663  int mLastExtentIndex;
664 
666  double mWheelZoomFactor;
667 
669  WheelAction mWheelAction;
670 
672  QTimer mMapUpdateTimer;
673 
676 
678  bool mJobCancelled;
679 
681  QgsLabelingResults* mLabelingResults;
682 
684  bool mUseParallelRendering;
685 
687  bool mDrawRenderingStats;
688 
690  QgsMapRendererCache* mCache;
691 
692  QTimer *mResizeTimer;
693 
694  QgsPreviewEffect* mPreviewEffect;
695 
696  QgsRectangle imageRect( const QImage& img, const QgsMapSettings& mapSettings );
697 
698  QgsSnappingUtils* mSnappingUtils;
699 
700 }; // class QgsMapCanvas
701 
702 
703 
704 
713 {
714  Q_OBJECT
715  public:
717 
718  protected slots:
719  void onExtentC2R();
720  void onExtentR2C();
721 
722  void onMapUnitsC2R();
723  void onMapUnitsR2C();
724 
726  void onMapRotationC2R();
728  void onMapRotationR2C();
729 
730  void onCrsTransformC2R();
731  void onCrsTransformR2C();
732 
733  void onDestCrsC2R();
734  void onDestCrsR2C();
735 
736  void onLayersC2R();
737 
738  protected:
741 
743 };
744 
745 
746 #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:347
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:431
The QgsMapSettings class contains configuration for rendering of the map.
QgsMapRenderer * mRenderer
Definition: qgsmapcanvas.h:740
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:739
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:604
virtual void mousePressEvent(QMouseEvent *event)
Abstract base class for all map tools.
Definition: qgsmaptool.h:49
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:712
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)