QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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 "qgsmessagebar.h"
20#include "qgsmessagebaritem.h"
21#include "qgsproject.h"
22#include "qgsrendercontext.h"
24#include "qgssnappingconfig.h"
25#include "qgssnappingutils.h"
26#include "qgsvectorlayer.h"
27
28#include <QAction>
29#include <QGlobalStatic>
30
31#include "moc_qgsmapcanvastracer.cpp"
32
33typedef QHash<QgsMapCanvas *, QgsMapCanvasTracer *> TracerCanvasHash;
35
37 : mCanvas( canvas )
38 , mMessageBar( messageBar )
39
40{
41 sTracers()->insert( canvas, this );
42
43 // when things change we just invalidate the graph - and set up new parameters again only when necessary
48 connect( canvas, &QgsMapCanvas::currentLayerChanged, this, &QgsMapCanvasTracer::onCurrentLayerChanged );
50 connect( canvas, &QObject::destroyed, this, [this]() { mCanvas = nullptr; } );
51
52 // arbitrarily chosen limit that should allow for fairly fast initialization
53 // of the underlying graph structure
55}
56
58{
59 sTracers->remove( mCanvas );
60}
61
63{
64 return sTracers->value( canvas, nullptr );
65}
66
68{
69 Q_UNUSED( addingVertex )
70
71 if ( !mMessageBar )
72 return;
73
74 // remove previous message (if any)
75 mMessageBar->popWidget( mLastMessage );
76 mLastMessage = nullptr;
77
78 QString message;
79 switch ( err )
80 {
82 message = tr( "Disabled - there are too many features displayed. Try zooming in or disable some layers." );
83 break;
84 case ErrNone:
85 default:
86 break;
87 }
88
89 if ( message.isEmpty() && hasTopologyProblem() )
90 {
91 message = tr( "Tracing may not work correctly. Please check topology of the input layers." );
92 }
93
94 if ( message.isEmpty() )
95 return;
96
98 mMessageBar->pushItem( mLastMessage );
99}
100
102{
103 setDestinationCrs( mCanvas->mapSettings().destinationCrs(), mCanvas->mapSettings().transformContext() );
104 const QgsRenderContext ctx = QgsRenderContext::fromMapSettings( mCanvas->mapSettings() );
105 setRenderContext( &ctx );
106 setExtent( mCanvas->extent() );
107
108 QList<QgsVectorLayer *> layers;
109 const QList<QgsMapLayer *> visibleLayers = mCanvas->mapSettings().layers( true );
110
111 switch ( mCanvas->snappingUtils()->config().mode() )
112 {
113 default:
115 {
116 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mCanvas->currentLayer() );
117 if ( vl && visibleLayers.contains( vl ) )
118 layers << vl;
119 }
120 break;
122 {
123 const auto constVisibleLayers = visibleLayers;
124 for ( QgsMapLayer *layer : constVisibleLayers )
125 {
126 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
127 if ( vl )
128 layers << vl;
129 }
130 }
131 break;
133 {
134 const auto constLayers = mCanvas->snappingUtils()->layers();
135 for ( const QgsSnappingUtils::LayerConfig &cfg : constLayers )
136 {
137 if ( visibleLayers.contains( cfg.layer ) )
138 layers << cfg.layer;
139 }
140 }
141 break;
142 }
143
144 setLayers( layers );
145}
146
147void QgsMapCanvasTracer::onCurrentLayerChanged()
148{
149 // no need to bother if we are not snapping
152}
@ Warning
Warning message.
Definition qgis.h:158
@ Info
Information message.
Definition qgis.h:157
@ ActiveLayer
On the active layer.
Definition qgis.h:738
@ AdvancedConfiguration
On a per layer configuration basis.
Definition qgis.h:740
@ AllLayers
On all vector layers.
Definition qgis.h:739
QgsMapCanvasTracer(QgsMapCanvas *canvas, QgsMessageBar *messageBar=nullptr)
Create tracer associated with a particular map canvas, optionally message bar for reporting.
void reportError(PathError err, bool addingVertex)
Report a path finding error to the user.
static QgsMapCanvasTracer * tracerForCanvas(QgsMapCanvas *canvas)
Retrieve instance of this class associated with given canvas (if any).
void configure() override
Sets configuration from current snapping settings and canvas settings.
Map canvas is a class for displaying all GIS data types on a canvas.
void extentsChanged()
Emitted when the extents of the map change.
void currentLayerChanged(QgsMapLayer *layer)
Emitted when the current layer is changed.
QgsSnappingUtils * snappingUtils() const
Returns snapping utility class that is associated with map canvas.
void destinationCrsChanged()
Emitted when map CRS has changed.
void transformContextChanged()
Emitted when the canvas transform context is changed.
void layersChanged()
Emitted when a new set of layers has been received.
Base class for all map layer types.
Definition qgsmaplayer.h:80
Represents an item shown within a QgsMessageBar widget.
A bar for displaying non-blocking messages to the user.
static int defaultMessageTimeout(Qgis::MessageLevel level=Qgis::MessageLevel::NoLevel)
Returns the default timeout in seconds for timed messages of the specified level.
Contains information about the context of a rendering operation.
static QgsRenderContext fromMapSettings(const QgsMapSettings &mapSettings)
create initialized QgsRenderContext instance from given QgsMapSettings
static const QgsSettingsEntryInteger * settingsDigitizingTracingMaxFeatureCount
Settings entry digitizing tracing max feature count.
Qgis::SnappingMode mode
void configChanged(const QgsSnappingConfig &snappingConfig)
Emitted when the snapping settings object changes.
QgsSnappingConfig config
void setMaxFeatureCount(int count)
Gets maximum possible number of features in graph. If the number is exceeded, graph is not created.
Definition qgstracer.h:112
void setRenderContext(const QgsRenderContext *renderContext)
Sets the renderContext used for tracing only on visible features.
bool hasTopologyProblem() const
Whether there was an error during graph creation due to noding exception, indicating some input data ...
Definition qgstracer.h:129
void setExtent(const QgsRectangle &extent)
Sets extent to which graph's features will be limited (empty extent means no limit).
PathError
Possible errors that may happen when calling findShortestPath().
Definition qgstracer.h:133
@ ErrNone
No error.
Definition qgstracer.h:134
@ ErrTooManyFeatures
Max feature count threshold was reached while reading features.
Definition qgstracer.h:135
void setLayers(const QList< QgsVectorLayer * > &layers)
Sets layers used for tracing.
void invalidateGraph()
Destroy the existing graph structure if any (de-initialize).
QList< QgsVectorLayer * > layers() const
Gets layers used for tracing.
Definition qgstracer.h:56
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the crs and transform context used for tracing.
Represents a vector layer which manages a vector based dataset.
QHash< QgsMapCanvas *, QgsMapCanvasTracer * > TracerCanvasHash
Q_GLOBAL_STATIC(TracerCanvasHash, sTracers)
Configures how a certain layer should be handled in a snapping operation.