QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsmaplayerregistry.h"
20 #include "qgsmessagebar.h"
21 #include "qgsmessagebaritem.h"
22 #include "qgssnappingutils.h"
23 #include "qgsvectorlayer.h"
24 
25 #include <QAction>
26 
28  : mCanvas( canvas )
29  , mMessageBar( messageBar )
30  , mLastMessage( nullptr )
31  , mActionEnableTracing( nullptr )
32 {
33  tracers().insert( mCanvas, this );
34 
35  // when things change we just invalidate the graph - and set up new parameters again only when necessary
36  connect( canvas, SIGNAL( destinationCrsChanged() ), this, SLOT( invalidateGraph() ) );
37  connect( canvas, SIGNAL( layersChanged() ), this, SLOT( invalidateGraph() ) );
38  connect( canvas, SIGNAL( extentsChanged() ), this, SLOT( invalidateGraph() ) );
39  connect( canvas, SIGNAL( currentLayerChanged( QgsMapLayer* ) ), this, SLOT( onCurrentLayerChanged() ) );
40  connect( canvas->snappingUtils(), SIGNAL( configChanged() ), this, SLOT( invalidateGraph() ) );
41 
42  // arbitrarily chosen limit that should allow for fairly fast initialization
43  // of the underlying graph structure
44  setMaxFeatureCount( QSettings().value( "/qgis/digitizing/tracing_max_feature_count", 10000 ).toInt() );
45 }
46 
48 {
49  tracers().remove( mCanvas );
50 }
51 
53 {
54  return tracers().value( canvas );
55 }
56 
58 {
59  Q_UNUSED( addingVertex );
60 
61  if ( !mMessageBar )
62  return;
63 
64  // remove previous message (if any)
65  mMessageBar->popWidget( mLastMessage );
66  mLastMessage = nullptr;
67 
68  QString message;
69  switch ( err )
70  {
71  case ErrTooManyFeatures:
72  message = tr( "Disabled - there are too many features displayed. Try zooming in or disable some layers." );
73  break;
74  case ErrNone:
75  default:
76  break;
77  }
78 
79  if ( message.isEmpty() && hasTopologyProblem() )
80  {
81  message = tr( "Tracing may not work correctly. Please check topology of the input layers." );
82  }
83 
84  if ( message.isEmpty() )
85  return;
86 
87  mLastMessage = new QgsMessageBarItem( tr( "Tracing" ), message, QgsMessageBar::WARNING,
88  QSettings().value( "/qgis/messageTimeout", 5 ).toInt() );
89  mMessageBar->pushItem( mLastMessage );
90 }
91 
93 {
96  setExtent( mCanvas->extent() );
97 
99  QStringList visibleLayerIds = mCanvas->mapSettings().layers();
100 
101  switch ( mCanvas->snappingUtils()->snapToMapMode() )
102  {
103  default:
105  {
106  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( mCanvas->currentLayer() );
107  if ( vl && visibleLayerIds.contains( vl->id() ) )
108  layers << vl;
109  }
110  break;
112  Q_FOREACH ( const QString& layerId, visibleLayerIds )
113  {
114  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
115  if ( vl )
116  layers << vl;
117  }
118  break;
120  Q_FOREACH ( const QgsSnappingUtils::LayerConfig& cfg, mCanvas->snappingUtils()->layers() )
121  {
122  if ( visibleLayerIds.contains( cfg.layer->id() ) )
123  layers << cfg.layer;
124  }
125  break;
126  }
127 
128  setLayers( layers );
129 }
130 
131 void QgsMapCanvasTracer::onCurrentLayerChanged()
132 {
133  // no need to bother if we are not snapping
135  invalidateGraph();
136 }
137 
138 QHash<QgsMapCanvas*, QgsMapCanvasTracer*> &QgsMapCanvasTracer::tracers()
139 {
141  return sTracers;
142 }
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
snap to all rendered layers (tolerance and type from defaultSettings())
QList< LayerConfig > layers() const
Query layers used for snapping.
QgsMapLayer * mapLayer(const QString &theLayerId) const
Retrieve a pointer to a registered layer by layer ID.
void setExtent(const QgsRectangle &extent)
Set extent to which graph&#39;s features will be limited (empty extent means no limit) ...
Definition: qgstracer.cpp:621
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:109
bool hasTopologyProblem() const
Whether there was an error during graph creation due to noding exception, indicating some input data ...
Definition: qgstracer.h:82
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
snap according to the configuration set in setLayers()
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:603
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:642
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:577
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Set CRS used for tracing.
Definition: qgstracer.cpp:612