QGIS API Documentation  2.99.0-Master (e077efd)
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 #endif
39 
40 class QWheelEvent;
41 class QPixmap;
42 class QPaintEvent;
43 class QKeyEvent;
44 class ResizeEvent;
45 
46 class QColor;
47 class QDomDocument;
48 class QPaintDevice;
49 class QMouseEvent;
50 class QRubberBand;
51 class QGraphicsScene;
52 
53 class QgsMapToPixel;
54 class QgsMapLayer;
55 class QgsHighlight;
56 class QgsVectorLayer;
57 
58 class QgsLabelingResults;
61 class QgsMapSettings;
62 class QgsMapCanvasMap;
64 class QgsMapTool;
65 class QgsSnappingUtils;
66 class QgsRubberBand;
67 
73 class GUI_EXPORT QgsMapCanvasLayer
74 {
75  public:
76  QgsMapCanvasLayer( QgsMapLayer* layer, bool visible = true, bool isInOverview = false )
77  : mLayer( layer )
78  , mVisible( visible )
79  , mInOverview( isInOverview )
80  {}
81 
82  void setVisible( bool visible ) { mVisible = visible; }
83  void setInOverview( bool isInOverview ) { mInOverview = isInOverview; }
84 
85  bool isVisible() const { return mVisible; }
86  bool isInOverview() const { return mInOverview; }
87 
88  QgsMapLayer* layer() { return mLayer; }
89  const QgsMapLayer* layer() const { return mLayer; }
90 
91  private:
92  QgsMapLayer* mLayer;
93 
95  bool mVisible;
96 
98  bool mInOverview;
99 };
100 
101 
106 class GUI_EXPORT QgsMapCanvas : public QGraphicsView
107 {
108  Q_OBJECT
109 
110  public:
111 
113  QgsMapCanvas( QWidget * parent = nullptr );
114 
116  ~QgsMapCanvas();
117 
120  double magnificationFactor() const;
121 
122  void setLayerSet( QList<QgsMapCanvasLayer>& layers );
123 
124  void setCurrentLayer( QgsMapLayer* layer );
125 
126  // ### QGIS 3: make QgsMapCanvas independent from overview
127  void updateOverview();
128 
129  // ### QGIS 3: make QgsMapCanvas independent from overview
130  void enableOverviewMode( QgsMapOverviewCanvas* overview );
131 
134  const QgsMapSettings& mapSettings() const;
135 
138  void setCrsTransformEnabled( bool enabled );
139 
142  void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );
143 
146  const QgsLabelingResults* labelingResults() const;
147 
150  void setCachingEnabled( bool enabled );
151 
154  bool isCachingEnabled() const;
155 
158  void clearCache();
159 
162  void refreshAllLayers();
163 
166  void setParallelRenderingEnabled( bool enabled );
167 
170  bool isParallelRenderingEnabled() const;
171 
174  void setMapUpdateInterval( int timeMilliseconds );
175 
178  int mapUpdateInterval() const;
179 
181  double scale();
182 
184  double mapUnitsPerPixel() const;
185 
187  QgsRectangle extent() const;
189  QgsRectangle fullExtent() const;
190 
192  void setExtent( const QgsRectangle &r, bool magnified = false );
193 
196  double rotation() const;
197 
200  void setRotation( double degrees );
201 
204  void setCenter( const QgsPoint& center );
205 
208  QgsPoint center() const;
209 
211  void zoomToFullExtent();
212 
214  void zoomToPreviousExtent();
215 
217  void zoomToNextExtent();
218 
219  // ! Clears the list of extents and sets current extent as first item
220  void clearExtentHistory();
221 
225  void zoomToSelected( QgsVectorLayer* layer = nullptr );
226 
230  void zoomToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
231 
235  void panToFeatureIds( QgsVectorLayer* layer, const QgsFeatureIds& ids );
236 
238  void panToSelected( QgsVectorLayer* layer = nullptr );
239 
241  void setMapTool( QgsMapTool* mapTool );
242 
249  void unsetMapTool( QgsMapTool* mapTool );
250 
252  QgsMapTool* mapTool();
253 
255  void setCanvasColor( const QColor & _newVal );
257  QColor canvasColor() const;
258 
261  void setSelectionColor( const QColor& color );
262 
264  void updateScale();
265 
267  QgsMapLayer *layer( int index );
268 
270  int layerCount() const;
271 
273  QList<QgsMapLayer*> layers() const;
274 
283  void freeze( bool frz = true );
284 
290  bool isFrozen();
291 
293  void setMapUnits( QgsUnitTypes::DistanceUnit mapUnits );
294 
296  QgsUnitTypes::DistanceUnit mapUnits() const;
297 
300  QMap<QString, QString> layerStyleOverrides() const;
301 
304  void setLayerStyleOverrides( const QMap<QString, QString>& overrides );
305 
307  const QgsMapToPixel* getCoordinateTransform();
308 
310  bool isDrawing();
311 
313  QgsMapLayer* currentLayer();
314 
316  void setWheelFactor( double factor );
317 
319  void zoomScale( double scale );
320 
323  void zoomByFactor( double scaleFactor, const QgsPoint *center = nullptr );
324 
326  void zoomWithCenter( int x, int y, bool zoomIn );
327 
331  bool scaleLocked() const { return mScaleLocked;}
332 
334  void enableAntiAliasing( bool theFlag );
335 
337  bool antiAliasingEnabled() const { return mSettings.testFlag( QgsMapSettings::Antialiasing ); }
338 
340  void enableMapTileRendering( bool theFlag );
341 
342  // following 2 methods should be moved elsewhere or changed to private
343  // currently used by pan map tool
345  void panActionEnd( QPoint releasePoint );
346 
348  void panAction( QMouseEvent * event );
349 
351  QPoint mouseLastXY();
352 
357  void setPreviewModeEnabled( bool previewEnabled );
358 
364  bool previewModeEnabled() const;
365 
373  void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
374 
381  QgsPreviewEffect::PreviewMode previewMode() const;
382 
390  QgsSnappingUtils* snappingUtils() const;
391 
399  void setSnappingUtils( QgsSnappingUtils* utils );
400 
409  void setExpressionContextScope( const QgsExpressionContextScope& scope ) { mExpressionContextScope = scope; }
410 
417  QgsExpressionContextScope& expressionContextScope() { return mExpressionContextScope; }
418 
424  const QgsExpressionContextScope& expressionContextScope() const { return mExpressionContextScope; }
425 
428  void setSegmentationTolerance( double tolerance );
429 
432  void setSegmentationToleranceType( QgsAbstractGeometry::SegmentationToleranceType type );
433 
434  public slots:
435 
437  void refresh();
438 
440  void selectionChangedSlot();
441 
443  void saveAsImage( const QString& theFileName, QPixmap * QPixmap = nullptr, const QString& = "PNG" );
444 
446  void layerStateChange();
447 
449  void layerCrsChange();
450 
452  void setRenderFlag( bool theFlag );
454  bool renderFlag() {return mRenderFlag;}
455 
457  bool hasCrsTransformEnabled();
458 
461  void stopRendering();
462 
464  void readProject( const QDomDocument & );
465 
467  void writeProject( QDomDocument & );
468 
470  void getDatumTransformInfo( const QgsMapLayer* ml, const QString& srcAuthId, const QString& destAuthId );
471 
476  void setMagnificationFactor( double factor );
477 
481  void setScaleLocked( bool isLocked );
482 
484  void zoomIn();
485 
487  void zoomOut();
488 
489  private slots:
491  void mapToolDestroyed();
492 
494  void rendererJobFinished();
495 
496  void mapUpdateTimeout();
497 
498  void refreshMap();
499 
500  signals:
501 
504  void xyCoordinates( const QgsPoint &p );
505 
507  void scaleChanged( double );
508 
510  void extentsChanged();
511 
514  void rotationChanged( double );
515 
518  void magnificationChanged( double );
519 
527  void renderComplete( QPainter * );
531 
532  // ### QGIS 3: renamte to mapRefreshFinished()
534  void mapCanvasRefreshed();
535 
536  // ### QGIS 3: rename to mapRefreshStarted()
538  void renderStarting();
539 
541  void layersChanged();
542 
544  void keyPressed( QKeyEvent * e );
545 
547  void keyReleased( QKeyEvent * e );
548 
550  void mapToolSet( QgsMapTool *tool );
551 
555  void mapToolSet( QgsMapTool *newTool, QgsMapTool* oldTool );
556 
557  // ### QGIS 3: remove the signal
559  void selectionChanged( QgsMapLayer * layer );
560 
562  void zoomLastStatusChanged( bool );
563 
565  void zoomNextStatusChanged( bool );
566 
569  void hasCrsTransformEnabledChanged( bool flag );
570 
573  void destinationCrsChanged();
574 
577  void mapUnitsChanged();
578 
581  void currentLayerChanged( QgsMapLayer* layer );
582 
585  void layerStyleOverridesChanged();
586 
588  void messageEmitted( const QString& title, const QString& message, QgsMessageBar::MessageLevel = QgsMessageBar::INFO );
589 
590  protected:
591 #ifdef HAVE_TOUCH
592  bool event( QEvent * e ) override;
594 #endif
595 
597  void keyPressEvent( QKeyEvent * e ) override;
598 
600  void keyReleaseEvent( QKeyEvent * e ) override;
601 
603  void mouseDoubleClickEvent( QMouseEvent * e ) override;
604 
606  void mouseMoveEvent( QMouseEvent * e ) override;
607 
609  void mousePressEvent( QMouseEvent * e ) override;
610 
612  void mouseReleaseEvent( QMouseEvent * e ) override;
613 
615  void wheelEvent( QWheelEvent * e ) override;
616 
618  void resizeEvent( QResizeEvent * e ) override;
619 
621  void paintEvent( QPaintEvent * e ) override;
622 
624  void dragEnterEvent( QDragEnterEvent * e ) override;
625 
627  void moveCanvasContents( bool reset = false );
628 
631  void zoomToFeatureExtent( QgsRectangle& rect );
632 
634  void updateCanvasItemPositions();
635 
638 
640  QScopedPointer<CanvasProperties> mCanvasProperties;
641 
642 #if 0
643 
647  void connectNotify( const char * signal ) override;
648 #endif
649  void updateDatumTransformEntries();
651 
652  private:
654 
661  QgsMapCanvas( QgsMapCanvas const & );
662 
664  QgsMapSettings mSettings;
665 
667  QgsMapCanvasMap* mMap;
668 
670  QgsMapOverviewCanvas* mMapOverview;
671 
673  bool mFrozen;
674 
676  bool mRefreshScheduled;
677 
679  bool mRenderFlag;
680 
682  QgsMapLayer* mCurrentLayer;
683 
685  QGraphicsScene* mScene;
686 
688  QgsMapTool* mMapTool;
689 
691  QgsMapTool* mLastNonZoomMapTool;
692 
694  QList <QgsRectangle> mLastExtent;
695  int mLastExtentIndex;
696 
698  double mWheelZoomFactor;
699 
701  QTimer mMapUpdateTimer;
702 
705 
707  bool mJobCancelled;
708 
710  QgsLabelingResults* mLabelingResults;
711 
713  bool mUseParallelRendering;
714 
716  bool mDrawRenderingStats;
717 
719  QgsMapRendererCache* mCache;
720 
721  QTimer *mResizeTimer;
722 
723  QgsPreviewEffect* mPreviewEffect;
724 
725  QgsRectangle imageRect( const QImage& img, const QgsMapSettings& mapSettings );
726 
727  QgsSnappingUtils* mSnappingUtils;
728 
730  bool mScaleLocked;
731 
732  QgsExpressionContextScope mExpressionContextScope;
733 
735  QRect mZoomRect;
736 
738  bool mZoomDragging;
739 
741  QScopedPointer< QgsRubberBand > mZoomRubberBand;
742 
743  QCursor mZoomCursor;
744 
747  void updateMapSize();
748 
753  void beginZoomRect( QPoint pos );
754 
759  void endZoomRect( QPoint pos );
760 
767  bool boundingBoxOfFeatureIds( const QgsFeatureIds& ids, QgsVectorLayer* layer, QgsRectangle& bbox, QString& errorMsg ) const;
768 
769  friend class TestQgsMapCanvas;
770 
771 }; // class QgsMapCanvas
772 
773 
774 
775 #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:83
A widget that displays an overview map.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:355
A class that stores visibility and presence in overview flags together with pointer to the layer...
Definition: qgsmapcanvas.h:73
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:106
void setVisible(bool visible)
Definition: qgsmapcanvas.h:82
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:454
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:33
A class for drawing transient features (e.g.
Definition: qgsrubberband.h:32
Enable anti-aliasing for map rendering.
A class for highlight features on the map.
Definition: qgshighlight.h:35
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:409
A class to represent a point.
Definition: qgspoint.h:111
QScopedPointer< CanvasProperties > mCanvasProperties
Handle pattern for implementation object.
Definition: qgsmapcanvas.h:637
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:42
Abstract base class for all map tools.
Definition: qgsmaptool.h:50
bool isVisible() const
Definition: qgsmapcanvas.h:85
bool isInOverview() const
Definition: qgsmapcanvas.h:86
QgsExpressionContextScope & expressionContextScope()
Returns a reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:417
bool antiAliasingEnabled() const
true if antialising is enabled
Definition: qgsmapcanvas.h:337
bool scaleLocked() const
Returns whether the scale is locked, so zooming can be performed using magnication.
Definition: qgsmapcanvas.h:331
const QgsExpressionContextScope & expressionContextScope() const
Returns a const reference to the expression context scope for the map canvas.
Definition: qgsmapcanvas.h:424
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...
QgsMapCanvasLayer(QgsMapLayer *layer, bool visible=true, bool isInOverview=false)
Definition: qgsmapcanvas.h:76
Class that stores computed placement from labeling engine.
This class is responsible for keeping cache of rendered images of individual layers.
const QgsMapLayer * layer() const
Definition: qgsmapcanvas.h:89
Represents a vector layer which manages a vector based data sets.
QgsMapLayer * layer()
Definition: qgsmapcanvas.h:88