QGIS API Documentation  2.99.0-Master (0a63d1f)
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 "qgsexpressioncontext.h"
24 #include "qgsfeature.h"
25 #include "qgsmessagebar.h"
26 #include "qgsrectangle.h"
27 #include "qgis.h"
28 
29 #include <QDomDocument>
30 #include <QGraphicsView>
31 #include <QtCore>
32 
33 #include "qgsmapsettings.h" // TEMPORARY
34 #include "qgsprevieweffect.h" //for QgsPreviewEffect::PreviewMode
35 
36 #ifdef HAVE_TOUCH
37 #include <QGestureEvent>
38 #include "qgis_gui.h"
39 #endif
40 
41 class QWheelEvent;
42 class QPixmap;
43 class QPaintEvent;
44 class QKeyEvent;
45 class ResizeEvent;
46 
47 class QColor;
48 class QDomDocument;
49 class QPaintDevice;
50 class QMouseEvent;
51 class QRubberBand;
52 class QGraphicsScene;
53 
54 class QgsMapToPixel;
55 class QgsMapLayer;
56 class QgsHighlight;
57 class QgsVectorLayer;
58 
59 class QgsLabelingResults;
62 class QgsMapSettings;
63 class QgsMapCanvasMap;
65 class QgsMapTool;
66 class QgsSnappingUtils;
67 class QgsRubberBand;
68 
69 
74 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
75 {
76  Q_OBJECT
77 
78  public:
79 
81  QgsMapCanvas( QWidget * parent = nullptr );
82 
83 
84  ~QgsMapCanvas();
85 
88  double magnificationFactor() const;
89 
92  void setLayers( const QList<QgsMapLayer*>& layers );
93 
94  void setCurrentLayer( QgsMapLayer* layer );
95 
98  const QgsMapSettings& mapSettings() const;
99 
102  void setCrsTransformEnabled( bool enabled );
103 
106  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );
107 
110  const QgsLabelingResults* labelingResults() const;
111 
114  void setCachingEnabled( bool enabled );
115 
118  bool isCachingEnabled() const;
119 
122  void clearCache();
123 
126  void refreshAllLayers();
127 
130  void setParallelRenderingEnabled( bool enabled );
131 
134  bool isParallelRenderingEnabled() const;
135 
138  void setMapUpdateInterval( int timeMilliseconds );
139 
142  int mapUpdateInterval() const;
143 
145  double scale();
146 
148  double mapUnitsPerPixel() const;
149 
151  QgsRectangle extent() const;
153  QgsRectangle fullExtent() const;
154 
156  void setExtent( const QgsRectangle &r, bool magnified = false );
157 
160  double rotation() const;
161 
164  void setRotation( double degrees );
165 
168  void setCenter( const QgsPoint& center );
169 
172  QgsPoint center() const;
173 
175  void zoomToFullExtent();
176 
178  void zoomToPreviousExtent();
179 
181  void zoomToNextExtent();
182 
183  // ! Clears the list of extents and sets current extent as first item
184  void clearExtentHistory();
185 
189  void zoomToSelected( QgsVectorLayer* layer = nullptr );
190 
194  void zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
195 
199  void panToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
200 
202  void panToSelected( QgsVectorLayer* layer = nullptr );
203 
205  void setMapTool( QgsMapTool* mapTool );
206 
213  void unsetMapTool( QgsMapTool* mapTool );
214 
216  QgsMapTool* mapTool();
217 
219  void setCanvasColor( const QColor & _newVal );
221  QColor canvasColor() const;
222 
225  void setSelectionColor( const QColor& color );
226 
228  void updateScale();
229 
231  QgsMapLayer *layer( int index );
232 
234  int layerCount() const;
235 
237  QList<QgsMapLayer*> layers() const;
238 
247  void freeze( bool frz = true );
248 
254  bool isFrozen();
255 
257  void setMapUnits( QgsUnitTypes::DistanceUnit mapUnits );
258 
260  QgsUnitTypes::DistanceUnit mapUnits() const;
261 
264  QMap<QString, QString> layerStyleOverrides() const;
265 
268  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
269 
271  const QgsMapToPixel* getCoordinateTransform();
272 
274  bool isDrawing();
275 
277  QgsMapLayer* currentLayer();
278 
280  void setWheelFactor( double factor );
281 
283  void zoomScale( double scale );
284 
287  void zoomByFactor( double scaleFactor, const QgsPoint *center = nullptr );
288 
290  void zoomWithCenter( int x, int y, bool zoomIn );
291 
294  void zoomToFeatureExtent( QgsRectangle& rect );
295 
299  bool scaleLocked() const { return mScaleLocked;}
300 
302  void enableAntiAliasing( bool theFlag );
303 
305  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
306 
308  void enableMapTileRendering( bool theFlag );
309 
310  // following 2 methods should be moved elsewhere or changed to private
311  // currently used by pan map tool
313  void panActionEnd( QPoint releasePoint );
314 
316  void panAction( QMouseEvent * event );
317 
319  QPoint mouseLastXY();
320 
325  void setPreviewModeEnabled( bool previewEnabled );
326 
332  bool previewModeEnabled() const;
333 
341  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
342 
349  QgsPreviewEffect::PreviewMode previewMode() const;
350 
358  QgsSnappingUtils* snappingUtils() const;
359 
367  void setSnappingUtils( QgsSnappingUtils* utils );
368 
377  void setExpressionContextScope( const QgsExpressionContextScope& scope ) { mExpressionContextScope = scope; }
378 
385  QgsExpressionContextScope& expressionContextScope() { return mExpressionContextScope; }
386 
392  const QgsExpressionContextScope& expressionContextScope() const { return mExpressionContextScope; }
393 
396  void setSegmentationTolerance( double tolerance );
397 
400  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
401 
402  public slots:
403 
405  void refresh();
406 
408  void selectionChangedSlot();
409 
411  void saveAsImage( const QString& theFileName, QPixmap * QPixmap = nullptr, const QString& = "PNG" );
412 
414  void layerStateChange();
415 
417  void layerCrsChange();
418 
420  void setRenderFlag( bool theFlag );
422  bool renderFlag() {return mRenderFlag;}
423 
425  bool hasCrsTransformEnabled();
426 
429  void stopRendering();
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 
444  void setMagnificationFactor( double factor );
445 
449  void setScaleLocked( bool isLocked );
450 
452  void zoomIn();
453 
455  void zoomOut();
456 
457  private slots:
459  void mapToolDestroyed();
460 
462  void rendererJobFinished();
463 
464  void mapUpdateTimeout();
465 
466  void refreshMap();
467 
468  signals:
469 
472  void xyCoordinates( const QgsPoint &p );
473 
475  void scaleChanged( double );
476 
478  void extentsChanged();
479 
482  void rotationChanged( double );
483 
486  void magnificationChanged( double );
487 
495  void renderComplete( QPainter * );
499 
500  // ### QGIS 3: renamte to mapRefreshFinished()
502  void mapCanvasRefreshed();
503 
504  // ### QGIS 3: rename to mapRefreshStarted()
506  void renderStarting();
507 
509  void layersChanged();
510 
512  void keyPressed( QKeyEvent * e );
513 
515  void keyReleased( QKeyEvent * e );
516 
518  void mapToolSet( QgsMapTool *tool );
519 
523  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
524 
525  // ### QGIS 3: remove the signal
527  void selectionChanged( QgsMapLayer * layer );
528 
530  void zoomLastStatusChanged( bool );
531 
533  void zoomNextStatusChanged( bool );
534 
537  void hasCrsTransformEnabledChanged( bool flag );
538 
541  void destinationCrsChanged();
542 
545  void mapUnitsChanged();
546 
549  void currentLayerChanged( QgsMapLayer* layer );
550 
553  void layerStyleOverridesChanged();
554 
556  void messageEmitted( const QString& title, const QString& message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
557 
558  protected:
559 #ifdef HAVE_TOUCH
560  bool event( QEvent * e ) override;
562 #endif
563 
565  void keyPressEvent( QKeyEvent * e ) override;
566 
568  void keyReleaseEvent( QKeyEvent * e ) override;
569 
571  void mouseDoubleClickEvent( QMouseEvent * e ) override;
572 
574  void mouseMoveEvent( QMouseEvent * e ) override;
575 
577  void mousePressEvent( QMouseEvent * e ) override;
578 
580  void mouseReleaseEvent( QMouseEvent * e ) override;
581 
583  void wheelEvent( QWheelEvent * e ) override;
584 
586  void resizeEvent( QResizeEvent * e ) override;
587 
589  void paintEvent( QPaintEvent * e ) override;
590 
592  void dragEnterEvent( QDragEnterEvent * e ) override;
593 
595  void moveCanvasContents( bool reset = false );
596 
598  void updateCanvasItemPositions();
599 
602 
604  QScopedPointer<CanvasProperties> mCanvasProperties;
605 
606 #if 0
607 
611  void connectNotify( const char * signal ) override;
612 #endif
613  void updateDatumTransformEntries();
615 
616  private:
618 
625  QgsMapCanvas( QgsMapCanvas const & );
626 
628  QgsMapSettings mSettings;
629 
631  QgsMapCanvasMap* mMap;
632 
634  bool mFrozen;
635 
637  bool mRefreshScheduled;
638 
640  bool mRenderFlag;
641 
643  QgsMapLayer* mCurrentLayer;
644 
646  QGraphicsScene* mScene;
647 
649  QgsMapTool* mMapTool;
650 
652  QgsMapTool* mLastNonZoomMapTool;
653 
655  QList <QgsRectangle> mLastExtent;
656  int mLastExtentIndex;
657 
659  double mWheelZoomFactor;
660 
662  QTimer mMapUpdateTimer;
663 
666 
668  bool mJobCancelled;
669 
671  QgsLabelingResults* mLabelingResults;
672 
674  bool mUseParallelRendering;
675 
677  bool mDrawRenderingStats;
678 
680  QgsMapRendererCache* mCache;
681 
682  QTimer *mResizeTimer;
683 
684  QgsPreviewEffect* mPreviewEffect;
685 
686  QgsRectangle imageRect( const QImage& img, const QgsMapSettings& mapSettings );
687 
688  QgsSnappingUtils* mSnappingUtils;
689 
691  bool mScaleLocked;
692 
693  QgsExpressionContextScope mExpressionContextScope;
694 
696  QRect mZoomRect;
697 
699  bool mZoomDragging;
700 
702  QScopedPointer< QgsRubberBand > mZoomRubberBand;
703 
704  QCursor mZoomCursor;
705 
708  void updateMapSize();
709 
714  void beginZoomRect( QPoint pos );
715 
720  void endZoomRect( QPoint pos );
721 
728  bool boundingBoxOfFeatureIds( const QgsFeatureIds& ids, QgsVectorLayer* layer, QgsRectangle& bbox, QString& errorMsg ) const;
729 
730  friend class TestQgsMapCanvas;
731 
732 }; // class QgsMapCanvas
733 
734 
735 
736 #endif
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:36
Base class for all map layer types.
Definition: qgsmaplayer.h:50
A widget that displays an overview map.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:348
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle...
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:74
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:422
The QgsMapSettings class contains configuration for rendering of the map.
Deprecated to be deleted, stuff from here should be moved elsewhere.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:33
Enable anti-aliasing for map rendering.
A class for highlight features on the map.
Definition: qgshighlight.h:36
Single scope for storing variables and functions for use within a QgsExpressionContext.
void setExpressionContextScope(const QgsExpressionContextScope &scope)
Sets an expression context scope for the map canvas.
Definition: qgsmapcanvas.h:377
A class to represent a point.
Definition: qgspoint.h:143
QScopedPointer< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:601
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:43
Abstract base class for all map tools.
Definition: qgsmaptool.h:51
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:385
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:305
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:299
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:392
Intermediate base class adding functionality that allows client to query the rendered image...
This class represents a coordinate reference system (CRS).
This class has all the configuration of snapping and can return answers to snapping queries...
Class that stores computed placement from labeling engine.
This class is responsible for keeping cache of rendered images of individual layers.
Represents a vector layer which manages a vector based data sets.