QGIS API Documentation  2.9.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 layerCrsChange();
415 
417  void setRenderFlag( bool theFlag );
419  bool renderFlag() {return mRenderFlag;};
420 
422  bool hasCrsTransformEnabled();
423 
425  Q_DECL_DEPRECATED void updateMap();
426 
429  void stopRendering();
430 
432  Q_DECL_DEPRECATED void showError( QgsMapLayer * mapLayer );
433 
435  void readProject( const QDomDocument & );
436 
438  void writeProject( QDomDocument & );
439 
441  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
442 
445  static bool rotationEnabled();
446 
449  static void enableRotation( bool enabled );
450 
451  private slots:
453  void mapToolDestroyed();
454 
456  void rendererJobFinished();
457 
458  void mapUpdateTimeout();
459 
460  void refreshMap();
461 
462  signals:
464  Q_DECL_DEPRECATED void setProgress( int, int );
466 
469  void xyCoordinates( const QgsPoint &p );
470 
472  void scaleChanged( double );
473 
475  void extentsChanged();
476 
479  void rotationChanged( double );
480 
489  void renderComplete( QPainter * );
493 
494  // ### QGIS 3: renamte to mapRefreshFinished()
496  void mapCanvasRefreshed();
497 
498  // ### QGIS 3: rename to mapRefreshStarted()
500  void renderStarting();
501 
503  void layersChanged();
504 
506  void keyPressed( QKeyEvent * e );
507 
509  void keyReleased( QKeyEvent * e );
510 
512  void mapToolSet( QgsMapTool *tool );
513 
517  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
518 
519  // ### QGIS 3: remove the signal
521  void selectionChanged( QgsMapLayer * layer );
522 
524  void zoomLastStatusChanged( bool );
525 
527  void zoomNextStatusChanged( bool );
528 
531  void hasCrsTransformEnabledChanged( bool flag );
532 
535  void destinationCrsChanged();
536 
539  void mapUnitsChanged();
540 
543  void currentLayerChanged( QgsMapLayer* layer );
544 
545  protected:
546 #ifdef HAVE_TOUCH
547  bool event( QEvent * e ) override;
549 #endif
550 
552  void keyPressEvent( QKeyEvent * e ) override;
553 
555  void keyReleaseEvent( QKeyEvent * e ) override;
556 
558  void mouseDoubleClickEvent( QMouseEvent * e ) override;
559 
561  void mouseMoveEvent( QMouseEvent * e ) override;
562 
564  void mousePressEvent( QMouseEvent * e ) override;
565 
567  void mouseReleaseEvent( QMouseEvent * e ) override;
568 
570  void wheelEvent( QWheelEvent * e ) override;
571 
573  void resizeEvent( QResizeEvent * e ) override;
574 
576  void paintEvent( QPaintEvent * e ) override;
577 
579  void dragEnterEvent( QDragEnterEvent * e ) override;
580 
582  void moveCanvasContents( bool reset = false );
583 
585  void updateCanvasItemPositions();
586 
589 
591  QScopedPointer<CanvasProperties> mCanvasProperties;
592 
593 #if 0
594 
597  void connectNotify( const char * signal ) override;
598 #endif
599  void updateDatumTransformEntries();
601 
602  private:
604 
610  QgsMapCanvas( QgsMapCanvas const & );
611 
613  QgsMapSettings mSettings;
614 
616  QgsMapRenderer* mMapRenderer;
617 
619  QgsMapCanvasMap* mMap;
620 
622  QgsMapOverviewCanvas* mMapOverview;
623 
625  bool mFrozen;
626 
628  bool mRefreshScheduled;
629 
631  bool mRenderFlag;
632 
634  QgsMapLayer* mCurrentLayer;
635 
637  QGraphicsScene* mScene;
638 
640  QgsMapTool* mMapTool;
641 
643  QgsMapTool* mLastNonZoomMapTool;
644 
646  QList <QgsRectangle> mLastExtent;
647  int mLastExtentIndex;
648 
650  double mWheelZoomFactor;
651 
653  WheelAction mWheelAction;
654 
656  QTimer mMapUpdateTimer;
657 
660 
662  bool mJobCancelled;
663 
665  QgsLabelingResults* mLabelingResults;
666 
668  bool mUseParallelRendering;
669 
671  bool mDrawRenderingStats;
672 
674  QgsMapRendererCache* mCache;
675 
676  QTimer *mResizeTimer;
677 
678  QgsPreviewEffect* mPreviewEffect;
679 
680  QgsRectangle imageRect( const QImage& img, const QgsMapSettings& mapSettings );
681 
682  QgsSnappingUtils* mSnappingUtils;
683 
684 }; // class QgsMapCanvas
685 
686 
687 
688 
696 class QgsMapCanvasRendererSync : public QObject
697 {
698  Q_OBJECT
699  public:
701 
702  protected slots:
703  void onExtentC2R();
704  void onExtentR2C();
705 
706  void onMapUnitsC2R();
707  void onMapUnitsR2C();
708 
710  void onMapRotationC2R();
712  void onMapRotationR2C();
713 
714  void onCrsTransformC2R();
715  void onCrsTransformR2C();
716 
717  void onDestCrsC2R();
718  void onDestCrsR2C();
719 
720  void onLayersC2R();
721 
722  protected:
725 
727 };
728 
729 
730 #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:419
The QgsMapSettings class contains configuration for rendering of the map.
QgsMapRenderer * mRenderer
Definition: qgsmapcanvas.h:724
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:723
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:588
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:696
Represents a vector layer which manages a vector based data sets.
QgsMapLayer * layer()
Definition: qgsmapcanvas.h:87