QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 
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 
104 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
105 {
106  Q_OBJECT
107 
108  public:
109 
110  enum WheelAction { WheelZoom, WheelZoomAndRecenter, WheelZoomToMouseCursor, WheelNothing };
111 
113  QgsMapCanvas( QWidget * parent = 0, const char *name = 0 );
114 
116  ~QgsMapCanvas();
117 
118  void setLayerSet( QList<QgsMapCanvasLayer>& layers );
119 
120  void setCurrentLayer( QgsMapLayer* layer );
121 
122  // ### QGIS 3: make QgsMapCanvas independent from overview
123  void updateOverview();
124 
125  // ### QGIS 3: make QgsMapCanvas independent from overview
126  void enableOverviewMode( QgsMapOverviewCanvas* overview );
127 
130  const QgsMapSettings& mapSettings() const;
131 
134  void setCrsTransformEnabled( bool enabled );
135 
138  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );
139 
142  const QgsLabelingResults* labelingResults() const;
143 
146  void setCachingEnabled( bool enabled );
147 
150  bool isCachingEnabled() const;
151 
154  void clearCache();
155 
158  void setParallelRenderingEnabled( bool enabled );
159 
162  bool isParallelRenderingEnabled() const;
163 
166  void setMapUpdateInterval( int timeMiliseconds );
167 
170  int mapUpdateInterval() const;
171 
173  QgsMapCanvasMap* map();
174 
180  Q_DECL_DEPRECATED QgsMapRenderer* mapRenderer();
181 
184  Q_DECL_DEPRECATED QPaintDevice &canvasPaintDevice();
185 
187  double scale();
188 
191  Q_DECL_DEPRECATED void clear();
192 
194  double mapUnitsPerPixel() const;
195 
197  QgsRectangle extent() const;
199  QgsRectangle fullExtent() const;
200 
202  void setExtent( const QgsRectangle &r );
203 
205  void zoomToFullExtent();
206 
208  void zoomToPreviousExtent();
209 
211  void zoomToNextExtent();
212 
213  // ! Clears the list of extents and sets current extent as first item
214  void clearExtentHistory();
215 
218  void zoomToSelected( QgsVectorLayer* layer = NULL );
219 
222  void panToSelected( QgsVectorLayer* layer = NULL );
223 
225  void setMapTool( QgsMapTool* mapTool );
226 
233  void unsetMapTool( QgsMapTool* mapTool );
234 
236  QgsMapTool* mapTool();
237 
239  virtual void setCanvasColor( const QColor & _newVal );
241  virtual QColor canvasColor() const;
242 
244  void setSelectionColor( const QColor& color );
246 
248  void updateScale();
249 
251  Q_DECL_DEPRECATED void updateFullExtent() {}
253 
255  QgsMapLayer *layer( int index );
256 
258  int layerCount() const;
259 
261  QList<QgsMapLayer*> layers() const;
262 
268  void freeze( bool frz = true );
269 
271  bool isFrozen();
272 
275  Q_DECL_DEPRECATED void setDirty( bool _dirty );
276 
279  Q_DECL_DEPRECATED bool isDirty() const;
280 
282  void setMapUnits( QGis::UnitType mapUnits );
284 
285  QGis::UnitType mapUnits() const;
286 
288  const QgsMapToPixel* getCoordinateTransform();
289 
291  bool isDrawing();
292 
294  QgsMapLayer* currentLayer();
295 
297  void setWheelAction( WheelAction action, double factor = 2 );
298 
300  void zoomIn();
301 
303  void zoomOut();
304 
306  // added in 1.5
307  void zoomScale( double scale );
308 
311  void zoomByFactor( double scaleFactor, const QgsPoint *center = 0 );
312 
314  void zoomWithCenter( int x, int y, bool zoomIn );
315 
317  void enableAntiAliasing( bool theFlag );
318 
320  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
321 
324  Q_DECL_DEPRECATED void useImageToRender( bool theFlag );
325 
326  // following 2 methods should be moved elsewhere or changed to private
327  // currently used by pan map tool
329  void panActionEnd( QPoint releasePoint );
330 
332  void panAction( QMouseEvent * event );
333 
335  QPoint mouseLastXY();
336 
341  void setPreviewModeEnabled( bool previewEnabled );
342 
348  bool previewModeEnabled() const;
349 
357  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
358 
365  QgsPreviewEffect::PreviewMode previewMode() const;
366 
367  public slots:
368 
370  void refresh();
371 
373  void selectionChangedSlot();
374 
376  void saveAsImage( QString theFileName, QPixmap * QPixmap = 0, QString = "PNG" );
377 
379  void layerStateChange();
380 
382  void setRenderFlag( bool theFlag );
384  bool renderFlag() {return mRenderFlag;};
385 
387  bool hasCrsTransformEnabled();
388 
390  Q_DECL_DEPRECATED void updateMap();
391 
394  void stopRendering();
395 
397  Q_DECL_DEPRECATED void showError( QgsMapLayer * mapLayer );
398 
400  void readProject( const QDomDocument & );
401 
403  void writeProject( QDomDocument & );
404 
406  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
407 
408  private slots:
410  void mapToolDestroyed();
411 
413  void rendererJobFinished();
414 
415  void mapUpdateTimeout();
416 
417  void refreshMap();
418 
419  signals:
421  Q_DECL_DEPRECATED void setProgress( int, int );
423 
426  void xyCoordinates( const QgsPoint &p );
427 
429  void scaleChanged( double );
430 
432  void extentsChanged();
433 
442  void renderComplete( QPainter * );
446 
447  // ### QGIS 3: renamte to mapRefreshFinished()
450  void mapCanvasRefreshed();
451 
452  // ### QGIS 3: rename to mapRefreshStarted()
455  void renderStarting();
456 
458  void layersChanged();
459 
461  void keyPressed( QKeyEvent * e );
462 
464  void keyReleased( QKeyEvent * e );
465 
467  void mapToolSet( QgsMapTool *tool );
468 
472  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
473 
474  // ### QGIS 3: remove the signal
476  void selectionChanged( QgsMapLayer * layer );
477 
480  void zoomLastStatusChanged( bool );
481 
484  void zoomNextStatusChanged( bool );
485 
488  void hasCrsTransformEnabledChanged( bool flag );
489 
492  void destinationCrsChanged();
493 
496  void mapUnitsChanged();
497 
498  protected:
499 #ifdef HAVE_TOUCH
500  bool event( QEvent * e );
502 #endif
503 
505  void keyPressEvent( QKeyEvent * e );
506 
508  void keyReleaseEvent( QKeyEvent * e );
509 
511  void mouseDoubleClickEvent( QMouseEvent * e );
512 
514  void mouseMoveEvent( QMouseEvent * e );
515 
517  void mousePressEvent( QMouseEvent * e );
518 
520  void mouseReleaseEvent( QMouseEvent * e );
521 
523  void wheelEvent( QWheelEvent * e );
524 
526  void resizeEvent( QResizeEvent * e );
527 
529  void paintEvent( QPaintEvent * e );
530 
532  void dragEnterEvent( QDragEnterEvent * e );
533 
535  void moveCanvasContents( bool reset = false );
536 
538  void updateCanvasItemPositions();
539 
542 
544  std::auto_ptr<CanvasProperties> mCanvasProperties;
545 
549  void connectNotify( const char * signal );
550 
552  void updateDatumTransformEntries();
553 
554  private:
556 
562  QgsMapCanvas( QgsMapCanvas const & );
563 
565  QgsMapSettings mSettings;
566 
568  QgsMapRenderer* mMapRenderer;
569 
571  QgsMapCanvasMap* mMap;
572 
574  QgsMapOverviewCanvas* mMapOverview;
575 
577  bool mFrozen;
578 
580  bool mRefreshScheduled;
581 
583  bool mRenderFlag;
584 
586  QgsMapLayer* mCurrentLayer;
587 
589  QGraphicsScene* mScene;
590 
592  QgsMapTool* mMapTool;
593 
595  QgsMapTool* mLastNonZoomMapTool;
596 
598  QList <QgsRectangle> mLastExtent;
599  int mLastExtentIndex;
600 
602  double mWheelZoomFactor;
603 
605  WheelAction mWheelAction;
606 
608  QTimer mMapUpdateTimer;
609 
612 
614  bool mJobCancelled;
615 
617  QgsLabelingResults* mLabelingResults;
618 
620  bool mUseParallelRendering;
621 
623  bool mDrawRenderingStats;
624 
626  QgsMapRendererCache* mCache;
627 
628  QTimer *mResizeTimer;
629 
630  QgsPreviewEffect* mPreviewEffect;
631 
632 }; // class QgsMapCanvas
633 
634 
635 
636 
644 class QgsMapCanvasRendererSync : public QObject
645 {
646  Q_OBJECT
647  public:
649 
650  protected slots:
651  void onExtentC2R();
652  void onExtentR2C();
653 
654  void onMapUnitsC2R();
655  void onMapUnitsR2C();
656 
657  void onCrsTransformC2R();
658  void onCrsTransformR2C();
659 
660  void onDestCrsC2R();
661  void onDestCrsR2C();
662 
663  void onLayersC2R();
664 
665  protected:
668 
670 };
671 
672 
673 #endif
void onCrsTransformC2R()
Definition: qgsmapcanvas.cpp:145
static unsigned index
Definition: mersenne-twister.cpp:36
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Base class for all map layer types.
Definition: qgsmaplayer.h:48
void setInOverview(bool isInOverview)
Definition: qgsmapcanvas.h:81
bool mSyncingExtent
Definition: qgsmapcanvas.h:669
A widget that displays an overview map.
Definition: qgsmapoverviewcanvas.h:39
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:74
bool isVisible() const
Definition: qgsmapcanvas.h:83
void onDestCrsC2R()
Definition: qgsmapcanvas.cpp:155
A non GUI class for rendering a map layer set onto a QPainter.
Definition: qgsmaprenderer.h:133
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:320
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:104
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...
Definition: qgsprevieweffect.h:28
bool renderFlag()
State of render suppression flag.
Definition: qgsmapcanvas.h:384
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:52
Definition: qgsmapcanvas.cpp:68
QgsMapRenderer * mRenderer
Definition: qgsmapcanvas.h:667
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:33
A rectangular graphics item representing the map on the canvas.
Definition: qgsmapcanvasmap.h:34
QgsMapCanvas * mCanvas
Definition: qgsmapcanvas.h:666
Enable anti-aliasin for map rendering.
Definition: qgsmapsettings.h:114
A class for highlight features on the map.
Definition: qgshighlight.h:36
const QgsMapLayer * layer() const
Definition: qgsmapcanvas.h:87
void onLayersC2R()
Definition: qgsmapcanvas.cpp:165
void onMapUnitsC2R()
Definition: qgsmapcanvas.cpp:135
void onExtentR2C()
Definition: qgsmapcanvas.cpp:124
A class to represent a point geometry.
Definition: qgspoint.h:63
void onMapUnitsR2C()
Definition: qgsmapcanvas.cpp:140
Abstract base class for all map tools.
Definition: qgsmaptool.h:48
bool isInOverview() const
Definition: qgsmapcanvas.h:84
PreviewMode
Definition: qgsprevieweffect.h:33
void onCrsTransformR2C()
Definition: qgsmapcanvas.cpp:150
Intermediate base class adding functionality that allows client to query the rendered image...
Definition: qgsmaprendererjob.h:181
Class for storing a coordinate reference system (CRS)
Definition: qgscoordinatereferencesystem.h:50
QgsMapCanvasRendererSync(QgsMapCanvas *canvas, QgsMapRenderer *renderer)
Definition: qgsmapcanvas.cpp:90
QgsMapCanvasLayer(QgsMapLayer *layer, bool visible=true, bool isInOverview=false)
Definition: qgsmapcanvas.h:77
WheelAction
Definition: qgsmapcanvas.h:110
void onDestCrsR2C()
Definition: qgsmapcanvas.cpp:160
Class that stores computed placement from labeling engine.
Definition: qgspallabeling.h:685
This class is responsible for keeping cache of rendered images of individual layers.
Definition: qgsmaprenderercache.h:37
Class that does synchronization between QgsMapCanvas and its associated QgsMapRenderer: ...
Definition: qgsmapcanvas.h:644
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:470
QgsMapLayer * layer()
Definition: qgsmapcanvas.h:86
std::auto_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:541
Definition: qgsmapcanvas.h:110
void onExtentC2R()
Definition: qgsmapcanvas.cpp:113