QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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"
26 #include "qgsrendercontext.h"
27 
28 #include <QAction>
29 
30 #include <QGlobalStatic>
31 
32 typedef QHash<QgsMapCanvas *, QgsMapCanvasTracer *> TracerCanvasHash;
34 
36  : mCanvas( canvas )
37  , mMessageBar( messageBar )
38 
39 {
40  sTracers()->insert( canvas, this );
41 
42  // when things change we just invalidate the graph - and set up new parameters again only when necessary
47  connect( canvas, &QgsMapCanvas::currentLayerChanged, this, &QgsMapCanvasTracer::onCurrentLayerChanged );
49  connect( canvas, &QObject::destroyed, this, [this]() { mCanvas = nullptr; } );
50 
51  // arbitrarily chosen limit that should allow for fairly fast initialization
52  // of the underlying graph structure
54 }
55 
57 {
58  sTracers->remove( mCanvas );
59 }
60 
62 {
63  return sTracers->value( canvas, nullptr );
64 }
65 
67 {
68  Q_UNUSED( addingVertex )
69 
70  if ( !mMessageBar )
71  return;
72 
73  // remove previous message (if any)
74  mMessageBar->popWidget( mLastMessage );
75  mLastMessage = nullptr;
76 
77  QString message;
78  switch ( err )
79  {
80  case ErrTooManyFeatures:
81  message = tr( "Disabled - there are too many features displayed. Try zooming in or disable some layers." );
82  break;
83  case ErrNone:
84  default:
85  break;
86  }
87 
88  if ( message.isEmpty() && hasTopologyProblem() )
89  {
90  message = tr( "Tracing may not work correctly. Please check topology of the input layers." );
91  }
92 
93  if ( message.isEmpty() )
94  return;
95 
96  mLastMessage = new QgsMessageBarItem( tr( "Tracing" ), message, Qgis::MessageLevel::Warning, QgsMessageBar::defaultMessageTimeout( Qgis::MessageLevel::Info ) );
97  mMessageBar->pushItem( mLastMessage );
98 }
99 
101 {
104  setRenderContext( &ctx );
105  setExtent( mCanvas->extent() );
106 
107  QList<QgsVectorLayer *> layers;
108  const QList<QgsMapLayer *> visibleLayers = mCanvas->mapSettings().layers( true );
109 
110  switch ( mCanvas->snappingUtils()->config().mode() )
111  {
112  default:
114  {
115  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
116  if ( vl && visibleLayers.contains( vl ) )
117  layers << vl;
118  }
119  break;
121  {
122  const auto constVisibleLayers = visibleLayers;
123  for ( QgsMapLayer *layer : constVisibleLayers )
124  {
125  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
126  if ( vl )
127  layers << vl;
128  }
129  }
130  break;
132  {
133  const auto constLayers = mCanvas->snappingUtils()->layers();
134  for ( const QgsSnappingUtils::LayerConfig &cfg : constLayers )
135  {
136  if ( visibleLayers.contains( cfg.layer ) )
137  layers << cfg.layer;
138  }
139  }
140  break;
141  }
142 
143  setLayers( layers );
144 }
145 
146 void QgsMapCanvasTracer::onCurrentLayerChanged()
147 {
148  // no need to bother if we are not snapping
149  if ( mCanvas->snappingUtils()->config().mode() == Qgis::SnappingMode::ActiveLayer )
150  invalidateGraph();
151 }
QgsMapCanvas::currentLayerChanged
void currentLayerChanged(QgsMapLayer *layer)
Emitted when the current layer is changed.
qgssettingsregistrycore.h
Qgis::SnappingMode::AdvancedConfiguration
@ AdvancedConfiguration
On a per layer configuration basis.
QgsMapCanvas::transformContextChanged
void transformContextChanged()
Emitted when the canvas transform context is changed.
QgsMapCanvas::extent
QgsRectangle extent() const
Returns the current zoom extent of the map canvas.
Definition: qgsmapcanvas.cpp:1331
qgsmessagebaritem.h
QgsMapCanvas::destinationCrsChanged
void destinationCrsChanged()
Emitted when map CRS has changed.
qgsmapcanvas.h
QgsRenderContext::fromMapSettings
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
Definition: qgsrendercontext.cpp:234
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:437
QgsSnappingUtils::config
QgsSnappingConfig config
Definition: qgssnappingutils.h:54
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsTracer::layers
QList< QgsVectorLayer * > layers() const
Gets layers used for tracing.
Definition: qgstracer.h:56
QgsMessageBar::popWidget
bool popWidget(QgsMessageBarItem *item)
Remove the specified item from the bar, and display the next most recent one in the stack.
Definition: qgsmessagebar.cpp:161
Q_GLOBAL_STATIC
Q_GLOBAL_STATIC(TracerCanvasHash, sTracers)
QgsMapCanvas::snappingUtils
QgsSnappingUtils * snappingUtils() const
Returns snapping utility class that is associated with map canvas.
Definition: qgsmapcanvas.cpp:2935
qgssnappingconfig.h
qgsapplication.h
QgsTracer::invalidateGraph
void invalidateGraph()
Destroy the existing graph structure if any (de-initialize)
Definition: qgstracer.cpp:687
qgsmapcanvastracer.h
QgsTracer::setRenderContext
void setRenderContext(const QgsRenderContext *renderContext)
Sets the renderContext used for tracing only on visible features.
Definition: qgstracer.cpp:641
QgsTracer::setDestinationCrs
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the crs and transform context used for tracing.
Definition: qgstracer.cpp:634
QgsMapSettings::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
Definition: qgsmapsettings.cpp:463
QgsSnappingUtils::configChanged
void configChanged(const QgsSnappingConfig &snappingConfig)
Emitted when the snapping settings object changes.
QgsMessageBarItem
Represents an item shown within a QgsMessageBar widget.
Definition: qgsmessagebaritem.h:38
QgsMapCanvasTracer
Extension of QgsTracer that provides extra functionality:
Definition: qgsmapcanvastracer.h:41
QgsMapCanvas::layersChanged
void layersChanged()
Emitted when a new set of layers has been received.
QgsMapCanvasTracer::~QgsMapCanvasTracer
~QgsMapCanvasTracer() override
Definition: qgsmapcanvastracer.cpp:56
QgsMessageBar::pushItem
void pushItem(QgsMessageBarItem *item)
Display a message item on the bar, after hiding the currently visible one and putting it in a stack.
Definition: qgsmessagebar.cpp:295
QgsTracer::ErrTooManyFeatures
@ ErrTooManyFeatures
Max feature count threshold was reached while reading features.
Definition: qgstracer.h:140
QgsMapCanvas::extentsChanged
void extentsChanged()
Emitted when the extents of the map change.
QgsTracer::setExtent
void setExtent(const QgsRectangle &extent)
Sets extent to which graph's features will be limited (empty extent means no limit)
Definition: qgstracer.cpp:647
qgsrendercontext.h
QgsMessageBar
A bar for displaying non-blocking messages to the user.
Definition: qgsmessagebar.h:60
QgsTracer::setMaxFeatureCount
void setMaxFeatureCount(int count)
Gets maximum possible number of features in graph. If the number is exceeded, graph is not created.
Definition: qgstracer.h:116
qgsmessagebar.h
QgsMapCanvasTracer::configure
void configure() override
Sets configuration from current snapping settings and canvas settings.
Definition: qgsmapcanvastracer.cpp:100
Qgis::SnappingMode::AllLayers
@ AllLayers
On all vector layers.
QgsMessageBar::defaultMessageTimeout
static int defaultMessageTimeout(Qgis::MessageLevel level=Qgis::MessageLevel::NoLevel)
Returns the default timeout in seconds for timed messages of the specified level.
Definition: qgsmessagebar.cpp:215
QgsTracer::hasTopologyProblem
bool hasTopologyProblem() const
Whether there was an error during graph creation due to noding exception, indicating some input data ...
Definition: qgstracer.h:134
QgsMapCanvasTracer::tracerForCanvas
static QgsMapCanvasTracer * tracerForCanvas(QgsMapCanvas *canvas)
Retrieve instance of this class associated with given canvas (if any).
Definition: qgsmapcanvastracer.cpp:61
qgsvectorlayer.h
QgsMapSettings::destinationCrs
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
Definition: qgsmapsettings.cpp:358
QgsSnappingUtils::layers
QList< QgsSnappingUtils::LayerConfig > layers() const
Query layers used for snapping.
Definition: qgssnappingutils.h:170
QgsTracer::ErrNone
@ ErrNone
No error.
Definition: qgstracer.h:139
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsSnappingConfig::mode
Qgis::SnappingMode mode() const
Returns the mode (all layers, active layer, per layer settings)
Definition: qgssnappingconfig.cpp:248
TracerCanvasHash
QHash< QgsMapCanvas *, QgsMapCanvasTracer * > TracerCanvasHash
Definition: qgsmapcanvastracer.cpp:32
QgsMapCanvasTracer::reportError
void reportError(PathError err, bool addingVertex)
Report a path finding error to the user.
Definition: qgsmapcanvastracer.cpp:66
QgsSettingsRegistryCore::settingsDigitizingTracingMaxFeatureCount
static const QgsSettingsEntryInteger settingsDigitizingTracingMaxFeatureCount
Settings entry digitizing tracing max feature count.
Definition: qgssettingsregistrycore.h:169
QgsMapCanvas::currentLayer
QgsMapLayer * currentLayer()
returns current layer (set by legend widget)
Definition: qgsmapcanvas.cpp:614
Qgis::SnappingMode::ActiveLayer
@ ActiveLayer
On the active layer.
QgsTracer::PathError
PathError
Possible errors that may happen when calling findShortestPath()
Definition: qgstracer.h:137
QgsSnappingUtils::LayerConfig
Configures how a certain layer should be handled in a snapping operation.
Definition: qgssnappingutils.h:122
QgsTracer::setLayers
void setLayers(const QList< QgsVectorLayer * > &layers)
Sets layers used for tracing.
Definition: qgstracer.cpp:602
qgsproject.h
QgsMapSettings::layers
QList< QgsMapLayer * > layers(bool expandGroupLayers=false) const
Returns the list of layers which will be rendered in the map.
Definition: qgsmapsettings.cpp:299
qgssnappingutils.h
QgsMapCanvasTracer::QgsMapCanvasTracer
QgsMapCanvasTracer(QgsMapCanvas *canvas, QgsMessageBar *messageBar=nullptr)
Create tracer associated with a particular map canvas, optionally message bar for reporting.
Definition: qgsmapcanvastracer.cpp:35