QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsmapcanvastracer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmapcanvastracer.cpp
3  ---------------------
4  begin : January 2016
5  copyright : (C) 2016 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "qgsmapcanvastracer.h"
16 
17 #include "qgsapplication.h"
18 #include "qgsmapcanvas.h"
19 #include "qgsproject.h"
20 #include "qgsmessagebar.h"
21 #include "qgsmessagebaritem.h"
22 #include "qgssnappingutils.h"
23 #include "qgsvectorlayer.h"
24 #include "qgssnappingconfig.h"
25 #include "qgssettings.h"
26 
27 #include <QAction>
28 
29 
30 QHash<QgsMapCanvas *, QgsMapCanvasTracer *> QgsMapCanvasTracer::sTracers;
31 
32 
34  : mCanvas( canvas )
35  , mMessageBar( messageBar )
36 
37 {
38  sTracers.insert( canvas, this );
39 
40  // when things change we just invalidate the graph - and set up new parameters again only when necessary
45  connect( canvas, &QgsMapCanvas::currentLayerChanged, this, &QgsMapCanvasTracer::onCurrentLayerChanged );
47 
48  // arbitrarily chosen limit that should allow for fairly fast initialization
49  // of the underlying graph structure
50  setMaxFeatureCount( QgsSettings().value( QStringLiteral( "/qgis/digitizing/tracing_max_feature_count" ), 10000 ).toInt() );
51 }
52 
54 {
55  sTracers.remove( mCanvas );
56 }
57 
59 {
60  return sTracers.value( canvas, nullptr );
61 }
62 
64 {
65  Q_UNUSED( addingVertex )
66 
67  if ( !mMessageBar )
68  return;
69 
70  // remove previous message (if any)
71  mMessageBar->popWidget( mLastMessage );
72  mLastMessage = nullptr;
73 
74  QString message;
75  switch ( err )
76  {
77  case ErrTooManyFeatures:
78  message = tr( "Disabled - there are too many features displayed. Try zooming in or disable some layers." );
79  break;
80  case ErrNone:
81  default:
82  break;
83  }
84 
85  if ( message.isEmpty() && hasTopologyProblem() )
86  {
87  message = tr( "Tracing may not work correctly. Please check topology of the input layers." );
88  }
89 
90  if ( message.isEmpty() )
91  return;
92 
93  mLastMessage = new QgsMessageBarItem( tr( "Tracing" ), message, Qgis::Warning,
94  QgsSettings().value( QStringLiteral( "/qgis/messageTimeout" ), 5 ).toInt() );
95  mMessageBar->pushItem( mLastMessage );
96 }
97 
99 {
102  setRenderContext( &ctx );
103  setExtent( mCanvas->extent() );
104 
105  QList<QgsVectorLayer *> layers;
106  QList<QgsMapLayer *> visibleLayers = mCanvas->mapSettings().layers();
107 
108  switch ( mCanvas->snappingUtils()->config().mode() )
109  {
110  default:
112  {
113  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
114  if ( vl && visibleLayers.contains( vl ) )
115  layers << vl;
116  }
117  break;
119  {
120  const auto constVisibleLayers = visibleLayers;
121  for ( QgsMapLayer *layer : constVisibleLayers )
122  {
123  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
124  if ( vl )
125  layers << vl;
126  }
127  }
128  break;
130  {
131  const auto constLayers = mCanvas->snappingUtils()->layers();
132  for ( const QgsSnappingUtils::LayerConfig &cfg : constLayers )
133  {
134  if ( visibleLayers.contains( cfg.layer ) )
135  layers << cfg.layer;
136  }
137  }
138  break;
139  }
140 
141  setLayers( layers );
142 }
143 
144 void QgsMapCanvasTracer::onCurrentLayerChanged()
145 {
146  // no need to bother if we are not snapping
147  if ( mCanvas->snappingUtils()->config().mode() == QgsSnappingConfig::ActiveLayer )
148  invalidateGraph();
149 }
QList< QgsVectorLayer * > layers() const
Gets layers used for tracing.
Definition: qgstracer.h:55
Base class for all map layer types.
Definition: qgsmaplayer.h:79
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
SnappingMode mode() const
Returns the mode (all layers, active layer, per layer settings)
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
void setRenderContext(const QgsRenderContext *renderContext)
Sets the renderContext used for tracing only on visible features.
Definition: qgstracer.cpp:644
void setExtent(const QgsRectangle &extent)
Sets extent to which graph&#39;s features will be limited (empty extent means no limit) ...
Definition: qgstracer.cpp:650
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:45
On all vector layers.
QList< QgsMapLayer * > layers() const
Gets list of layers for map rendering The layers are stored in the reverse order of how they are rend...
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:75
bool hasTopologyProblem() const
Whether there was an error during graph creation due to noding exception, indicating some input data ...
Definition: qgstracer.h:129
QgsCoordinateReferenceSystem destinationCrs() const
returns CRS of destination coordinate reference system
Extension of QgsTracer that provides extra functionality:
static QgsMapCanvasTracer * tracerForCanvas(QgsMapCanvas *canvas)
Retrieve instance of this class associated with given canvas (if any).
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the crs and transform context used for tracing.
Definition: qgstracer.cpp:637
bool popWidget(QgsMessageBarItem *item)
Remove the passed widget from the bar (if previously added), then display the next one in the stack i...
Max feature count threshold was reached while reading features.
Definition: qgstracer.h:135
QList< QgsSnappingUtils::LayerConfig > layers() const
Query layers used for snapping.
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
PathError
Possible errors that may happen when calling findShortestPath()
Definition: qgstracer.h:132
void destinationCrsChanged()
Emitted when map CRS has changed.
On a per layer configuration basis.
void transformContextChanged()
Emitted when the canvas transform context is changed.
void currentLayerChanged(QgsMapLayer *layer)
Emitted when the current layer is changed.
void configChanged(const QgsSnappingConfig &snappingConfig)
Emitted when the snapping settings object changes.
QgsMapLayer * currentLayer()
returns current layer (set by legend widget)
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
void reportError(PathError err, bool addingVertex)
Report a path finding error to the user.
Contains information about the context of a rendering operation.
void pushItem(QgsMessageBarItem *item)
Display a message item on the bar after hiding the currently visible one and putting it in a stack...
void setMaxFeatureCount(int count)
Gets maximum possible number of features in graph. If the number is exceeded, graph is not created...
Definition: qgstracer.h:111
QgsSnappingConfig config
void configure() override
Sets configuration from current snapping settings and canvas settings.
Configures how a certain layer should be handled in a snapping operation.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
void invalidateGraph()
Destroy the existing graph structure if any (de-initialize)
Definition: qgstracer.cpp:690
QgsMapCanvasTracer(QgsMapCanvas *canvas, QgsMessageBar *messageBar=nullptr)
Create tracer associated with a particular map canvas, optionally message bar for reporting...
No error.
Definition: qgstracer.h:134
QgsSnappingUtils * snappingUtils() const
Returns snapping utility class that is associated with map canvas.
Represents a vector layer which manages a vector based data sets.
void extentsChanged()
Emitted when the extents of the map change.
void setLayers(const QList< QgsVectorLayer *> &layers)
Sets layers used for tracing.
Definition: qgstracer.cpp:605
void layersChanged()
Emitted when a new set of layers has been received.