QGIS API Documentation  2.14.11-Essen
qgsmapcanvastracer.cpp
Go to the documentation of this file.
1 #include "qgsmapcanvastracer.h"
2 
3 #include "qgsapplication.h"
4 #include "qgsmapcanvas.h"
5 #include "qgsmaplayerregistry.h"
6 #include "qgsmessagebar.h"
7 #include "qgsmessagebaritem.h"
8 #include "qgssnappingutils.h"
9 #include "qgsvectorlayer.h"
10 
11 #include <QAction>
12 
13 QHash<QgsMapCanvas*, QgsMapCanvasTracer*> QgsMapCanvasTracer::sTracers;
14 
15 
17  : mCanvas( canvas )
18  , mMessageBar( messageBar )
19  , mLastMessage( nullptr )
20  , mActionEnableTracing( nullptr )
21 {
22  sTracers.insert( canvas, this );
23 
24  // when things change we just invalidate the graph - and set up new parameters again only when necessary
25  connect( canvas, SIGNAL( destinationCrsChanged() ), this, SLOT( invalidateGraph() ) );
26  connect( canvas, SIGNAL( layersChanged() ), this, SLOT( invalidateGraph() ) );
27  connect( canvas, SIGNAL( extentsChanged() ), this, SLOT( invalidateGraph() ) );
28  connect( canvas, SIGNAL( currentLayerChanged( QgsMapLayer* ) ), this, SLOT( onCurrentLayerChanged() ) );
29  connect( canvas->snappingUtils(), SIGNAL( configChanged() ), this, SLOT( invalidateGraph() ) );
30 
31  // arbitrarily chosen limit that should allow for fairly fast initialization
32  // of the underlying graph structure
33  setMaxFeatureCount( QSettings().value( "/qgis/digitizing/tracing_max_feature_count", 10000 ).toInt() );
34 }
35 
37 {
38  sTracers.remove( mCanvas );
39 }
40 
42 {
43  return sTracers.value( canvas, 0 );
44 }
45 
47 {
48  Q_UNUSED( addingVertex );
49 
50  if ( !mMessageBar )
51  return;
52 
53  // remove previous message (if any)
54  mMessageBar->popWidget( mLastMessage );
55  mLastMessage = nullptr;
56 
57  QString message;
58  switch ( err )
59  {
60  case ErrTooManyFeatures:
61  message = tr( "Disabled - there are too many features displayed. Try zooming in or disable some layers." );
62  break;
63  case ErrNone:
64  default:
65  break;
66  }
67 
68  if ( message.isEmpty() && hasTopologyProblem() )
69  {
70  message = tr( "Tracing may not work correctly. Please check topology of the input layers." );
71  }
72 
73  if ( message.isEmpty() )
74  return;
75 
76  mLastMessage = new QgsMessageBarItem( tr( "Tracing" ), message, QgsMessageBar::WARNING,
77  QSettings().value( "/qgis/messageTimeout", 5 ).toInt() );
78  mMessageBar->pushItem( mLastMessage );
79 }
80 
82 {
85  setExtent( mCanvas->extent() );
86 
88  QStringList visibleLayerIds = mCanvas->mapSettings().layers();
89 
90  switch ( mCanvas->snappingUtils()->snapToMapMode() )
91  {
92  default:
94  {
95  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( mCanvas->currentLayer() );
96  if ( vl && visibleLayerIds.contains( vl->id() ) )
97  layers << vl;
98  }
99  break;
101  Q_FOREACH ( const QString& layerId, visibleLayerIds )
102  {
103  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
104  if ( vl )
105  layers << vl;
106  }
107  break;
109  Q_FOREACH ( const QgsSnappingUtils::LayerConfig& cfg, mCanvas->snappingUtils()->layers() )
110  {
111  if ( visibleLayerIds.contains( cfg.layer->id() ) )
112  layers << cfg.layer;
113  }
114  break;
115  }
116 
117  setLayers( layers );
118 }
119 
120 void QgsMapCanvasTracer::onCurrentLayerChanged()
121 {
122  // no need to bother if we are not snapping
124  invalidateGraph();
125 }
virtual void configure()
Sets configuration from current snapping settings and canvas settings.
SnapToMapMode snapToMapMode() const
Find out how the snapping to map is done.
Base class for all map layer types.
Definition: qgsmaplayer.h:49
iterator insert(const Key &key, const T &value)
snap to all rendered layers (tolerance and type from defaultSettings())
QList< LayerConfig > layers() const
Query layers used for snapping.
void setExtent(const QgsRectangle &extent)
Set extent to which graph&#39;s features will be limited (empty extent means no limit) ...
Definition: qgstracer.cpp:605
bool contains(const QString &str, Qt::CaseSensitivity cs) const
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:42
QList< QgsVectorLayer * > layers() const
Get layers used for tracing.
Definition: qgstracer.h:46
QString tr(const char *sourceText, const char *disambiguation, int n)
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:105
bool hasTopologyProblem() const
Whether there was an error during graph creation due to noding exception, indicating some input data ...
Definition: qgstracer.h:82
QgsMapLayer * mapLayer(const QString &theLayerId)
Retrieve a pointer to a loaded layer by id.
Extension of QgsTracer that provides extra functionality:
QString id() const
Get this layer&#39;s unique ID, this ID is used to access this layer from map layer registry.
static QgsMapCanvasTracer * tracerForCanvas(QgsMapCanvas *canvas)
Retrieve instance of this class associated with given canvas (if any).
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:88
bool isEmpty() const
QgsRectangle extent() const
Returns the current zoom exent of the map canvas.
PathError
Possible errors that may happen when calling findShortestPath()
Definition: qgstracer.h:85
int remove(const Key &key)
snap according to the configuration set in setLayers()
const T value(const Key &key) const
QgsMapLayer * currentLayer()
returns current layer (set by legend widget)
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
void reportError(PathError err, bool addingVertex)
Report a path finding error to the user.
void setCrsTransformEnabled(bool enabled)
Set whether to do reprojection to destination CRS.
Definition: qgstracer.cpp:587
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)
Get maximum possible number of features in graph. If the number is exceeded, graph is not created...
Definition: qgstracer.h:68
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
QgsVectorLayer * layer
The layer to configure.
Configures how a certain layer should be handled in a snapping operation.
void invalidateGraph()
Destroy the existing graph structure if any (de-initialize)
Definition: qgstracer.cpp:626
QgsMapCanvasTracer(QgsMapCanvas *canvas, QgsMessageBar *messageBar=0)
Create tracer associated with a particular map canvas, optionally message bar for reporting...
No error.
Definition: qgstracer.h:87
QStringList layers() const
Get list of layer IDs for map rendering The layers are stored in the reverse order of how they are re...
QgsSnappingUtils * snappingUtils() const
Return snapping utility class that is associated with map canvas.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
snap just to current layer (tolerance and type from defaultSettings())
Represents a vector layer which manages a vector based data sets.
void setLayers(const QList< QgsVectorLayer *> &layers)
Set layers used for tracing.
Definition: qgstracer.cpp:561
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Set CRS used for tracing.
Definition: qgstracer.cpp:596