QGIS API Documentation  2.7.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 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 setParallelRenderingEnabled( bool enabled );
160 
163  bool isParallelRenderingEnabled() const;
164 
167  void setMapUpdateInterval( int timeMiliseconds );
168 
171  int mapUpdateInterval() const;
172 
174  QgsMapCanvasMap* map();
175 
181  Q_DECL_DEPRECATED QgsMapRenderer* mapRenderer();
182 
185  Q_DECL_DEPRECATED QPaintDevice &canvasPaintDevice();
186 
188  double scale();
189 
192  Q_DECL_DEPRECATED void clear();
193 
195  double mapUnitsPerPixel() const;
196 
198  QgsRectangle extent() const;
200  QgsRectangle fullExtent() const;
201 
203  void setExtent( const QgsRectangle &r );
204 
207  double rotation() const;
208 
211  void setRotation( double degrees );
212 
215  void setCenter( const QgsPoint& center );
216 
219  QgsPoint center() const;
220 
222  void zoomToFullExtent();
223 
225  void zoomToPreviousExtent();
226 
228  void zoomToNextExtent();
229 
230  // ! Clears the list of extents and sets current extent as first item
231  void clearExtentHistory();
232 
235  void zoomToSelected( QgsVectorLayer* layer = NULL );
236 
238  void panToSelected( QgsVectorLayer* layer = NULL );
239 
241  void setMapTool( QgsMapTool* mapTool );
242 
249  void unsetMapTool( QgsMapTool* mapTool );
250 
252  QgsMapTool* mapTool();
253 
255  virtual void setCanvasColor( const QColor & _newVal );
257  virtual QColor canvasColor() const;
258 
260  void setSelectionColor( const QColor& color );
262 
264  void updateScale();
265 
267  Q_DECL_DEPRECATED void updateFullExtent() {}
269 
271  QgsMapLayer *layer( int index );
272 
274  int layerCount() const;
275 
277  QList<QgsMapLayer*> layers() const;
278 
284  void freeze( bool frz = true );
285 
287  bool isFrozen();
288 
291  Q_DECL_DEPRECATED void setDirty( bool _dirty );
292 
295  Q_DECL_DEPRECATED bool isDirty() const;
296 
298  void setMapUnits( QGis::UnitType mapUnits );
300 
301  QGis::UnitType mapUnits() const;
302 
304  const QgsMapToPixel* getCoordinateTransform();
305 
307  bool isDrawing();
308 
310  QgsMapLayer* currentLayer();
311 
313  void setWheelAction( WheelAction action, double factor = 2 );
314 
316  void zoomIn();
317 
319  void zoomOut();
320 
322  void zoomScale( double scale );
323 
326  void zoomByFactor( double scaleFactor, const QgsPoint *center = 0 );
327 
329  void zoomWithCenter( int x, int y, bool zoomIn );
330 
332  void enableAntiAliasing( bool theFlag );
333 
335  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
336 
339  Q_DECL_DEPRECATED void useImageToRender( bool theFlag );
340 
341  // following 2 methods should be moved elsewhere or changed to private
342  // currently used by pan map tool
344  void panActionEnd( QPoint releasePoint );
345 
347  void panAction( QMouseEvent * event );
348 
350  QPoint mouseLastXY();
351 
356  void setPreviewModeEnabled( bool previewEnabled );
357 
363  bool previewModeEnabled() const;
364 
372  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
373 
380  QgsPreviewEffect::PreviewMode previewMode() const;
381 
389  QgsSnappingUtils* snappingUtils() const;
397  void setSnappingUtils( QgsSnappingUtils* utils );
398 
399  public slots:
400 
402  void refresh();
403 
405  void selectionChangedSlot();
406 
408  void saveAsImage( QString theFileName, QPixmap * QPixmap = 0, QString = "PNG" );
409 
411  void layerStateChange();
412 
414  void setRenderFlag( bool theFlag );
416  bool renderFlag() {return mRenderFlag;};
417 
419  bool hasCrsTransformEnabled();
420 
422  Q_DECL_DEPRECATED void updateMap();
423 
426  void stopRendering();
427 
429  Q_DECL_DEPRECATED void showError( QgsMapLayer * mapLayer );
430 
432  void readProject( const QDomDocument & );
433 
435  void writeProject( QDomDocument & );
436 
438  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
439 
442  static bool rotationEnabled();
443 
446  static void enableRotation( bool enabled );
447 
448  private slots:
450  void mapToolDestroyed();
451 
453  void rendererJobFinished();
454 
455  void mapUpdateTimeout();
456 
457  void refreshMap();
458 
459  signals:
461  Q_DECL_DEPRECATED void setProgress( int, int );
463 
466  void xyCoordinates( const QgsPoint &p );
467 
469  void scaleChanged( double );
470 
472  void extentsChanged();
473 
476  void rotationChanged( double );
477 
486  void renderComplete( QPainter * );
490 
491  // ### QGIS 3: renamte to mapRefreshFinished()
493  void mapCanvasRefreshed();
494 
495  // ### QGIS 3: rename to mapRefreshStarted()
497  void renderStarting();
498 
500  void layersChanged();
501 
503  void keyPressed( QKeyEvent * e );
504 
506  void keyReleased( QKeyEvent * e );
507 
509  void mapToolSet( QgsMapTool *tool );
510 
514  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
515 
516  // ### QGIS 3: remove the signal
518  void selectionChanged( QgsMapLayer * layer );
519 
521  void zoomLastStatusChanged( bool );
522 
524  void zoomNextStatusChanged( bool );
525 
528  void hasCrsTransformEnabledChanged( bool flag );
529 
532  void destinationCrsChanged();
533 
536  void mapUnitsChanged();
537 
540  void currentLayerChanged( QgsMapLayer* layer );
541 
542  protected:
543 #ifdef HAVE_TOUCH
544  bool event( QEvent * e ) override;
546 #endif
547 
549  void keyPressEvent( QKeyEvent * e ) override;
550 
552  void keyReleaseEvent( QKeyEvent * e ) override;
553 
555  void mouseDoubleClickEvent( QMouseEvent * e ) override;
556 
558  void mouseMoveEvent( QMouseEvent * e ) override;
559 
561  void mousePressEvent( QMouseEvent * e ) override;
562 
564  void mouseReleaseEvent( QMouseEvent * e ) override;
565 
567  void wheelEvent( QWheelEvent * e ) override;
568 
570  void resizeEvent( QResizeEvent * e ) override;
571 
573  void paintEvent( QPaintEvent * e ) override;
574 
576  void dragEnterEvent( QDragEnterEvent * e ) override;
577 
579  void moveCanvasContents( bool reset = false );
580 
582  void updateCanvasItemPositions();
583 
586 
588  QScopedPointer<CanvasProperties> mCanvasProperties;
589 
593  void connectNotify( const char * signal ) override;
594 
596  void updateDatumTransformEntries();
597 
598  private:
600 
606  QgsMapCanvas( QgsMapCanvas const & );
607 
609  QgsMapSettings mSettings;
610 
612  QgsMapRenderer* mMapRenderer;
613 
615  QgsMapCanvasMap* mMap;
616 
618  QgsMapOverviewCanvas* mMapOverview;
619 
621  bool mFrozen;
622 
624  bool mRefreshScheduled;
625 
627  bool mRenderFlag;
628 
630  QgsMapLayer* mCurrentLayer;
631 
633  QGraphicsScene* mScene;
634 
636  QgsMapTool* mMapTool;
637 
639  QgsMapTool* mLastNonZoomMapTool;
640 
642  QList <QgsRectangle> mLastExtent;
643  int mLastExtentIndex;
644 
646  double mWheelZoomFactor;
647 
649  WheelAction mWheelAction;
650 
652  QTimer mMapUpdateTimer;
653 
656 
658  bool mJobCancelled;
659 
661  QgsLabelingResults* mLabelingResults;
662 
664  bool mUseParallelRendering;
665 
667  bool mDrawRenderingStats;
668 
670  QgsMapRendererCache* mCache;
671 
672  QTimer *mResizeTimer;
673 
674  QgsPreviewEffect* mPreviewEffect;
675 
676  QgsRectangle imageRect( const QImage& img );
677 
678  QgsSnappingUtils* mSnappingUtils;
679 
680 }; // class QgsMapCanvas
681 
682 
683 
684 
692 class QgsMapCanvasRendererSync : public QObject
693 {
694  Q_OBJECT
695  public:
697 
698  protected slots:
699  void onExtentC2R();
700  void onExtentR2C();
701 
702  void onMapUnitsC2R();
703  void onMapUnitsR2C();
704 
706  void onMapRotationC2R();
708  void onMapRotationR2C();
709 
710  void onCrsTransformC2R();
711  void onCrsTransformR2C();
712 
713  void onDestCrsC2R();
714  void onDestCrsR2C();
715 
716  void onLayersC2R();
717 
718  protected:
721 
723 };
724 
725 
726 #endif
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
A widget that displays an overview map.
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
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:335
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:416
The QgsMapSettings class contains configuration for rendering of the map.
QgsMapRenderer * mRenderer
Definition: qgsmapcanvas.h:720
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
A rectangular graphics item representing the map on the canvas.
QgsMapCanvas * mCanvas
Definition: qgsmapcanvas.h:719
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
A class to represent a point.
Definition: qgspoint.h:63
QScopedPointer< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:585
Abstract base class for all map tools.
Definition: qgsmaptool.h:48
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.
Class that does synchronization between QgsMapCanvas and its associated QgsMapRenderer: ...
Definition: qgsmapcanvas.h:692
Represents a vector layer which manages a vector based data sets.
QgsMapLayer * layer()
Definition: qgsmapcanvas.h:87