QGIS API Documentation  2.14.0-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() )
69  return;
70 
71  mLastMessage = new QgsMessageBarItem( tr( "Tracing" ), message, QgsMessageBar::WARNING,
72  QSettings().value( "/qgis/messageTimeout", 5 ).toInt() );
73  mMessageBar->pushItem( mLastMessage );
74 }
75 
77 {
80  setExtent( mCanvas->extent() );
81 
83  QStringList visibleLayerIds = mCanvas->mapSettings().layers();
84 
85  switch ( mCanvas->snappingUtils()->snapToMapMode() )
86  {
87  default:
89  {
90  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( mCanvas->currentLayer() );
91  if ( vl && visibleLayerIds.contains( vl->id() ) )
92  layers << vl;
93  }
94  break;
96  Q_FOREACH ( const QString& layerId, visibleLayerIds )
97  {
98  QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( layerId ) );
99  if ( vl )
100  layers << vl;
101  }
102  break;
104  Q_FOREACH ( const QgsSnappingUtils::LayerConfig& cfg, mCanvas->snappingUtils()->layers() )
105  {
106  if ( visibleLayerIds.contains( cfg.layer->id() ) )
107  layers << cfg.layer;
108  }
109  break;
110  }
111 
112  setLayers( layers );
113 }
114 
115 void QgsMapCanvasTracer::onCurrentLayerChanged()
116 {
117  // no need to bother if we are not snapping
119  invalidateGraph();
120 }
SnapToMapMode snapToMapMode() const
Find out how the snapping to map is done.
virtual void configure()
Sets configuration from current snapping settings and canvas settings.
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())
void setExtent(const QgsRectangle &extent)
Set extent to which graph&#39;s features will be limited (empty extent means no limit) ...
Definition: qgstracer.cpp:587
bool contains(const QString &str, Qt::CaseSensitivity cs) const
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:42
const QgsMapSettings & mapSettings() const
Get access to properties used for map rendering.
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
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
QgsMapLayer * mapLayer(const QString &theLayerId)
Retrieve a pointer to a loaded layer by id.
QList< LayerConfig > layers() const
Query layers used for snapping.
Extension of QgsTracer that provides extra functionality:
QgsSnappingUtils * snappingUtils() const
Return snapping utility class that is associated with map canvas.
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:83
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
void setLayers(const QList< QgsVectorLayer * > &layers)
Set layers used for tracing.
Definition: qgstracer.cpp:543
bool isEmpty() const
PathError
Possible errors that may happen when calling findShortestPath()
Definition: qgstracer.h:80
QString id() const
Get this layer&#39;s unique ID, this ID is used to access this layer from map layer registry.
int remove(const Key &key)
QList< QgsVectorLayer * > layers() const
Get layers used for tracing.
Definition: qgstracer.h:46
snap according to the configuration set in setLayers()
const T value(const Key &key) const
QgsMapLayer * currentLayer()
returns current layer (set by legend widget)
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:569
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:608
QgsMapCanvasTracer(QgsMapCanvas *canvas, QgsMessageBar *messageBar=0)
Create tracer associated with a particular map canvas, optionally message bar for reporting...
No error.
Definition: qgstracer.h:82
QStringList layers() const
Get list of layer IDs for map rendering The layers are stored in the reverse order of how they are re...
QgsRectangle extent() const
Returns the current zoom exent of the 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 setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Set CRS used for tracing.
Definition: qgstracer.cpp:578