QGIS API Documentation  2.6.0-Brighton
 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:
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 
221  void panToSelected( QgsVectorLayer* layer = NULL );
222 
224  void setMapTool( QgsMapTool* mapTool );
225 
232  void unsetMapTool( QgsMapTool* mapTool );
233 
235  QgsMapTool* mapTool();
236 
238  virtual void setCanvasColor( const QColor & _newVal );
240  virtual QColor canvasColor() const;
241 
243 
244  void setSelectionColor( const QColor& color );
245 
247  void updateScale();
248 
250 
251  Q_DECL_DEPRECATED void updateFullExtent() {}
252 
254  QgsMapLayer *layer( int index );
255 
257  int layerCount() const;
258 
260  QList<QgsMapLayer*> layers() const;
261 
267  void freeze( bool frz = true );
268 
270  bool isFrozen();
271 
274  Q_DECL_DEPRECATED void setDirty( bool _dirty );
275 
278  Q_DECL_DEPRECATED bool isDirty() const;
279 
281  void setMapUnits( QGis::UnitType mapUnits );
283 
284  QGis::UnitType mapUnits() const;
285 
287  const QgsMapToPixel* getCoordinateTransform();
288 
290  bool isDrawing();
291 
293  QgsMapLayer* currentLayer();
294 
296  void setWheelAction( WheelAction action, double factor = 2 );
297 
299  void zoomIn();
300 
302  void zoomOut();
303 
305  void zoomScale( double scale );
306 
309  void zoomByFactor( double scaleFactor, const QgsPoint *center = 0 );
310 
312  void zoomWithCenter( int x, int y, bool zoomIn );
313 
315  void enableAntiAliasing( bool theFlag );
316 
318  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
319 
322  Q_DECL_DEPRECATED void useImageToRender( bool theFlag );
323 
324  // following 2 methods should be moved elsewhere or changed to private
325  // currently used by pan map tool
327  void panActionEnd( QPoint releasePoint );
328 
330  void panAction( QMouseEvent * event );
331 
333  QPoint mouseLastXY();
334 
339  void setPreviewModeEnabled( bool previewEnabled );
340 
346  bool previewModeEnabled() const;
347 
355  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
356 
363  QgsPreviewEffect::PreviewMode previewMode() const;
364 
365  public slots:
366 
368  void refresh();
369 
371  void selectionChangedSlot();
372 
374  void saveAsImage( QString theFileName, QPixmap * QPixmap = 0, QString = "PNG" );
375 
377  void layerStateChange();
378 
380  void setRenderFlag( bool theFlag );
382  bool renderFlag() {return mRenderFlag;};
383 
385  bool hasCrsTransformEnabled();
386 
388  Q_DECL_DEPRECATED void updateMap();
389 
392  void stopRendering();
393 
395  Q_DECL_DEPRECATED void showError( QgsMapLayer * mapLayer );
396 
398  void readProject( const QDomDocument & );
399 
401  void writeProject( QDomDocument & );
402 
404  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
405 
406  private slots:
408  void mapToolDestroyed();
409 
411  void rendererJobFinished();
412 
413  void mapUpdateTimeout();
414 
415  void refreshMap();
416 
417  signals:
419 
420  Q_DECL_DEPRECATED void setProgress( int, int );
421 
424  void xyCoordinates( const QgsPoint &p );
425 
427  void scaleChanged( double );
428 
430  void extentsChanged();
431 
440 
441 
442 
443  void renderComplete( QPainter * );
444 
445  // ### QGIS 3: renamte to mapRefreshFinished()
447  void mapCanvasRefreshed();
448 
449  // ### QGIS 3: rename to mapRefreshStarted()
451  void renderStarting();
452 
454  void layersChanged();
455 
457  void keyPressed( QKeyEvent * e );
458 
460  void keyReleased( QKeyEvent * e );
461 
463  void mapToolSet( QgsMapTool *tool );
464 
468  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
469 
470  // ### QGIS 3: remove the signal
472  void selectionChanged( QgsMapLayer * layer );
473 
475  void zoomLastStatusChanged( bool );
476 
478  void zoomNextStatusChanged( bool );
479 
482  void hasCrsTransformEnabledChanged( bool flag );
483 
486  void destinationCrsChanged();
487 
490  void mapUnitsChanged();
491 
492  protected:
493 #ifdef HAVE_TOUCH
494 
495  bool event( QEvent * e );
496 #endif
497 
499  void keyPressEvent( QKeyEvent * e );
500 
502  void keyReleaseEvent( QKeyEvent * e );
503 
505  void mouseDoubleClickEvent( QMouseEvent * e );
506 
508  void mouseMoveEvent( QMouseEvent * e );
509 
511  void mousePressEvent( QMouseEvent * e );
512 
514  void mouseReleaseEvent( QMouseEvent * e );
515 
517  void wheelEvent( QWheelEvent * e );
518 
520  void resizeEvent( QResizeEvent * e );
521 
523  void paintEvent( QPaintEvent * e );
524 
526  void dragEnterEvent( QDragEnterEvent * e );
527 
529  void moveCanvasContents( bool reset = false );
530 
532  void updateCanvasItemPositions();
533 
536 
538  std::auto_ptr<CanvasProperties> mCanvasProperties;
539 
543  void connectNotify( const char * signal );
544 
546  void updateDatumTransformEntries();
547 
548  private:
550 
556  QgsMapCanvas( QgsMapCanvas const & );
557 
559  QgsMapSettings mSettings;
560 
562  QgsMapRenderer* mMapRenderer;
563 
565  QgsMapCanvasMap* mMap;
566 
568  QgsMapOverviewCanvas* mMapOverview;
569 
571  bool mFrozen;
572 
574  bool mRefreshScheduled;
575 
577  bool mRenderFlag;
578 
580  QgsMapLayer* mCurrentLayer;
581 
583  QGraphicsScene* mScene;
584 
586  QgsMapTool* mMapTool;
587 
589  QgsMapTool* mLastNonZoomMapTool;
590 
592  QList <QgsRectangle> mLastExtent;
593  int mLastExtentIndex;
594 
596  double mWheelZoomFactor;
597 
599  WheelAction mWheelAction;
600 
602  QTimer mMapUpdateTimer;
603 
606 
608  bool mJobCancelled;
609 
611  QgsLabelingResults* mLabelingResults;
612 
614  bool mUseParallelRendering;
615 
617  bool mDrawRenderingStats;
618 
620  QgsMapRendererCache* mCache;
621 
622  QTimer *mResizeTimer;
623 
624  QgsPreviewEffect* mPreviewEffect;
625 
626 }; // class QgsMapCanvas
627 
628 
629 
630 
638 class QgsMapCanvasRendererSync : public QObject
639 {
640  Q_OBJECT
641  public:
643 
644  protected slots:
645  void onExtentC2R();
646  void onExtentR2C();
647 
648  void onMapUnitsC2R();
649  void onMapUnitsR2C();
650 
651  void onCrsTransformC2R();
652  void onCrsTransformR2C();
653 
654  void onDestCrsC2R();
655  void onDestCrsR2C();
656 
657  void onLayersC2R();
658 
659  protected:
662 
664 };
665 
666 
667 #endif