QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
310  void zoomByFactor( double scaleFactor );
311 
313  void zoomWithCenter( int x, int y, bool zoomIn );
314 
316  void enableAntiAliasing( bool theFlag );
317 
319  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
320 
323  Q_DECL_DEPRECATED void useImageToRender( bool theFlag );
324 
325  // following 2 methods should be moved elsewhere or changed to private
326  // currently used by pan map tool
328  void panActionEnd( QPoint releasePoint );
329 
331  void panAction( QMouseEvent * event );
332 
334  QPoint mouseLastXY();
335 
340  void setPreviewModeEnabled( bool previewEnabled );
341 
347  bool previewModeEnabled() const;
348 
356  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
357 
364  QgsPreviewEffect::PreviewMode previewMode() const;
365 
366  public slots:
367 
369  void refresh();
370 
372  void selectionChangedSlot();
373 
375  void saveAsImage( QString theFileName, QPixmap * QPixmap = 0, QString = "PNG" );
376 
378  void layerStateChange();
379 
381  void setRenderFlag( bool theFlag );
383  bool renderFlag() {return mRenderFlag;};
384 
386  bool hasCrsTransformEnabled();
387 
389  Q_DECL_DEPRECATED void updateMap();
390 
393  void stopRendering();
394 
396  Q_DECL_DEPRECATED void showError( QgsMapLayer * mapLayer );
397 
399  void readProject( const QDomDocument & );
400 
402  void writeProject( QDomDocument & );
403 
405  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
406 
407  private slots:
409  void mapToolDestroyed();
410 
412  void rendererJobFinished();
413 
414  void mapUpdateTimeout();
415 
416  void refreshMap();
417 
418  signals:
420  Q_DECL_DEPRECATED void setProgress( int, int );
422 
425  void xyCoordinates( const QgsPoint &p );
426 
428  void scaleChanged( double );
429 
431  void extentsChanged();
432 
441  void renderComplete( QPainter * );
445 
446  // ### QGIS 3: renamte to mapRefreshFinished()
449  void mapCanvasRefreshed();
450 
451  // ### QGIS 3: rename to mapRefreshStarted()
454  void renderStarting();
455 
457  void layersChanged();
458 
460  void keyPressed( QKeyEvent * e );
461 
463  void keyReleased( QKeyEvent * e );
464 
466  void mapToolSet( QgsMapTool *tool );
467 
471  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
472 
473  // ### QGIS 3: remove the signal
475  void selectionChanged( QgsMapLayer * layer );
476 
479  void zoomLastStatusChanged( bool );
480 
483  void zoomNextStatusChanged( bool );
484 
487  void hasCrsTransformEnabledChanged( bool flag );
488 
491  void destinationCrsChanged();
492 
495  void mapUnitsChanged();
496 
497  protected:
498 #ifdef HAVE_TOUCH
499  bool event( QEvent * e );
501 #endif
502 
504  void keyPressEvent( QKeyEvent * e );
505 
507  void keyReleaseEvent( QKeyEvent * e );
508 
510  void mouseDoubleClickEvent( QMouseEvent * e );
511 
513  void mouseMoveEvent( QMouseEvent * e );
514 
516  void mousePressEvent( QMouseEvent * e );
517 
519  void mouseReleaseEvent( QMouseEvent * e );
520 
522  void wheelEvent( QWheelEvent * e );
523 
525  void resizeEvent( QResizeEvent * e );
526 
528  void paintEvent( QPaintEvent * e );
529 
531  void dragEnterEvent( QDragEnterEvent * e );
532 
534  void moveCanvasContents( bool reset = false );
535 
537  void updateCanvasItemPositions();
538 
541 
543  std::auto_ptr<CanvasProperties> mCanvasProperties;
544 
548  void connectNotify( const char * signal );
549 
551  void updateDatumTransformEntries();
552 
553  private:
555 
561  QgsMapCanvas( QgsMapCanvas const & );
562 
565 
568 
571 
574 
576  bool mFrozen;
577 
580 
583 
586 
588  QGraphicsScene* mScene;
589 
592 
595 
597  QList <QgsRectangle> mLastExtent;
599 
602 
605 
608 
611 
614 
617 
620 
623 
626 
627  QTimer *mResizeTimer;
628 
630 
631 }; // class QgsMapCanvas
632 
633 
634 
635 
643 class QgsMapCanvasRendererSync : public QObject
644 {
645  Q_OBJECT
646  public:
648 
649  protected slots:
650  void onExtentC2R();
651  void onExtentR2C();
652 
653  void onMapUnitsC2R();
654  void onMapUnitsR2C();
655 
656  void onCrsTransformC2R();
657  void onCrsTransformR2C();
658 
659  void onDestCrsC2R();
660  void onDestCrsR2C();
661 
662  void onLayersC2R();
663 
664  protected:
667 
669 };
670 
671 
672 #endif
QgsMapCanvasMap * mMap
owns pixmap with rendered map and controls rendering
Definition: qgsmapcanvas.h:570
QgsMapSettings mSettings
encompases all map settings necessary for map rendering
Definition: qgsmapcanvas.h:564
bool mDrawRenderingStats
Whether to add rendering stats to the rendered image.
Definition: qgsmapcanvas.h:622
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:588
QTimer * mResizeTimer
Definition: qgsmapcanvas.h:627
bool mUseParallelRendering
Whether layers are rendered sequentially or in parallel.
Definition: qgsmapcanvas.h:619
A widget that displays an overview map.
QgsMapRenderer * mMapRenderer
all map rendering is done in this class
Definition: qgsmapcanvas.h:567
bool mRefreshScheduled
Flag that allows squashing multiple refresh() calls into just one delayed rendering job...
Definition: qgsmapcanvas.h:579
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:604
bool isVisible() const
Definition: qgsmapcanvas.h:83
QgsMapOverviewCanvas * mMapOverview
map overview widget - it's controlled by QgsMapCanvas
Definition: qgsmapcanvas.h:573
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:319
QgsMapTool * mMapTool
pointer to current map tool
Definition: qgsmapcanvas.h:591
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:383
The QgsMapSettings class contains configuration for rendering of the map.
QgsMapLayer * mCurrentLayer
current layer in legend
Definition: qgsmapcanvas.h:585
QgsMapRenderer * mRenderer
Definition: qgsmapcanvas.h:666
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:598
QgsMapTool * mLastNonZoomMapTool
previous tool if current is for zooming/panning
Definition: qgsmapcanvas.h:594
QgsMapCanvas * mCanvas
Definition: qgsmapcanvas.h:665
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:610
bool mVisible
Flag whether layer is visible.
Definition: qgsmapcanvas.h:93
QgsLabelingResults * mLabelingResults
Labeling results from the recently rendered map.
Definition: qgsmapcanvas.h:616
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:607
double mWheelZoomFactor
Scale factor multiple for default zoom in/out.
Definition: qgsmapcanvas.h:601
bool mFrozen
Flag indicating if the map canvas is frozen.
Definition: qgsmapcanvas.h:576
bool isInOverview() const
Definition: qgsmapcanvas.h:84
QgsMapRendererCache * mCache
Optionally use cache with rendered map layers for the current map settings.
Definition: qgsmapcanvas.h:625
Intermediate base class adding functionality that allows client to query the rendered image...
QgsPreviewEffect * mPreviewEffect
Definition: qgsmapcanvas.h:629
bool mRenderFlag
determines whether user has requested to suppress rendering
Definition: qgsmapcanvas.h:582
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
UnitType
Map units that qgis supports.
Definition: qgis.h:229
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:643
Represents a vector layer which manages a vector based data sets.
QList< QgsRectangle > mLastExtent
recently used extent
Definition: qgsmapcanvas.h:597
QgsMapLayer * layer()
Definition: qgsmapcanvas.h:86
std::auto_ptr< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:540
bool mJobCancelled
Flag determining whether the active job has been cancelled.
Definition: qgsmapcanvas.h:613