QGIS API Documentation  2.5.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 
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:
91 
93  bool mVisible;
94 
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 
566 
569 
572 
575 
577  bool mFrozen;
578 
581 
584 
587 
589  QGraphicsScene* mScene;
590 
593 
596 
598  QList <QgsRectangle> mLastExtent;
600 
603 
606 
609 
612 
615 
618 
621 
624 
627 
628  QTimer *mResizeTimer;
629 
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
QgsMapCanvasMap * mMap
owns pixmap with rendered map and controls rendering
Definition: qgsmapcanvas.h:571
QgsMapSettings mSettings
encompases all map settings necessary for map rendering
Definition: qgsmapcanvas.h:565
bool mDrawRenderingStats
Whether to add rendering stats to the rendered image.
Definition: qgsmapcanvas.h:623
static unsigned index
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
QGraphicsScene * mScene
graphics scene manages canvas items
Definition: qgsmapcanvas.h:589
QTimer * mResizeTimer
Definition: qgsmapcanvas.h:628
bool mUseParallelRendering
Whether layers are rendered sequentially or in parallel.
Definition: qgsmapcanvas.h:620
A widget that displays an overview map.
QgsMapRenderer * mMapRenderer
all map rendering is done in this class
Definition: qgsmapcanvas.h:568
bool mRefreshScheduled
Flag that allows squashing multiple refresh() calls into just one delayed rendering job...
Definition: qgsmapcanvas.h:580
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
WheelAction mWheelAction
Mouse wheel action.
Definition: qgsmapcanvas.h:605
bool isVisible() const
Definition: qgsmapcanvas.h:83
QgsMapOverviewCanvas * mMapOverview
map overview widget - it's controlled by QgsMapCanvas
Definition: qgsmapcanvas.h:574
QgsMapLayer * mLayer
Definition: qgsmapcanvas.h:90
A non GUI class for rendering a map layer set onto a QPainter.
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:320
QgsMapTool * mMapTool
pointer to current map tool
Definition: qgsmapcanvas.h:592
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...
bool renderFlag()
State of render suppression flag.
Definition: qgsmapcanvas.h:384
The QgsMapSettings class contains configuration for rendering of the map.
QgsMapLayer * mCurrentLayer
current layer in legend
Definition: qgsmapcanvas.h:586
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.
int mLastExtentIndex
Definition: qgsmapcanvas.h:599
QgsMapTool * mLastNonZoomMapTool
previous tool if current is for zooming/panning
Definition: qgsmapcanvas.h:595
QgsMapCanvas * mCanvas
Definition: qgsmapcanvas.h:666
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:87
bool mInOverview
Flag whether layer is shown in overview.
Definition: qgsmapcanvas.h:96
A class to represent a point geometry.
Definition: qgspoint.h:63
QgsMapRendererQImageJob * mJob
Job that takes care of map rendering in background.
Definition: qgsmapcanvas.h:611
bool mVisible
Flag whether layer is visible.
Definition: qgsmapcanvas.h:93
QgsLabelingResults * mLabelingResults
Labeling results from the recently rendered map.
Definition: qgsmapcanvas.h:617
Abstract base class for all map tools.
Definition: qgsmaptool.h:48
QTimer mMapUpdateTimer
Timer that periodically fires while map rendering is in progress to update the visible map...
Definition: qgsmapcanvas.h:608
double mWheelZoomFactor
Scale factor multiple for default zoom in/out.
Definition: qgsmapcanvas.h:602
bool mFrozen
Flag indicating if the map canvas is frozen.
Definition: qgsmapcanvas.h:577
bool isInOverview() const
Definition: qgsmapcanvas.h:84
QgsMapRendererCache * mCache
Optionally use cache with rendered map layers for the current map settings.
Definition: qgsmapcanvas.h:626
Intermediate base class adding functionality that allows client to query the rendered image...
QgsPreviewEffect * mPreviewEffect
Definition: qgsmapcanvas.h:630
bool mRenderFlag
determines whether user has requested to suppress rendering
Definition: qgsmapcanvas.h:583
Class for storing a coordinate reference system (CRS)
QgsMapCanvasRendererSync(QgsMapCanvas *canvas, QgsMapRenderer *renderer)
QgsMapCanvasLayer(QgsMapLayer *layer, bool visible=true, bool isInOverview=false)
Definition: qgsmapcanvas.h:77
Class that stores computed placement from labeling engine.
This class is responsible for keeping cache of rendered images of individual layers.
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.
QList< QgsRectangle > mLastExtent
recently used extent
Definition: qgsmapcanvas.h:598
QgsMapLayer * layer()
Definition: qgsmapcanvas.h:86
std::auto_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:541
bool mJobCancelled
Flag determining whether the active job has been cancelled.
Definition: qgsmapcanvas.h:614