QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
qgssnappingutils.h
Go to the documentation of this file.
1/***************************************************************************
2 qgssnappingutils.h
3 --------------------------------------
4 Date : November 2014
5 Copyright : (C) 2014 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
16#ifndef QGSSNAPPINGUTILS_H
17#define QGSSNAPPINGUTILS_H
18
19
20#include "qgis_core.h"
21#include "qgis_sip.h"
22#include "qgsmapsettings.h"
23#include "qgstolerance.h"
24#include "qgspointlocator.h"
25#include "qgssnappingconfig.h"
26
28
50class CORE_EXPORT QgsSnappingUtils : public QObject
51{
52 Q_OBJECT
53
54 Q_PROPERTY( QgsSnappingConfig config READ config WRITE setConfig NOTIFY configChanged )
55
56 public:
57
63 QgsSnappingUtils( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enableSnappingForInvisibleFeature = true );
64 ~QgsSnappingUtils() override;
65
66 // main actions
67
72 QgsPointLocator *locatorForLayer( QgsVectorLayer *vl );
73
80 QgsPointLocator::Match snapToMap( QPoint point, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
81
88 QgsPointLocator::Match snapToMap( const QgsPointXY &pointMap, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
89
91 QgsPointLocator::Match snapToCurrentLayer( QPoint point, QgsPointLocator::Types type, QgsPointLocator::MatchFilter *filter = nullptr );
92
93 // environment setup
94
96 void setMapSettings( const QgsMapSettings &settings );
97 QgsMapSettings mapSettings() const { return mMapSettings; }
98
100 void setCurrentLayer( QgsVectorLayer *layer );
102 QgsVectorLayer *currentLayer() const { return mCurrentLayer; }
103
104 // configuration
105
107 {
111 IndexExtent
112 };
113
115 void setIndexingStrategy( IndexingStrategy strategy ) { mStrategy = strategy; }
117 IndexingStrategy indexingStrategy() const { return mStrategy; }
118
123 {
124
142 LayerConfig( QgsVectorLayer *l, QgsPointLocator::Types t, double tol, Qgis::MapToolUnit u )
143 : layer( l )
144 , type( t )
145 , tolerance( tol )
146 , unit( u )
147 {}
148
149 // TODO c++20 - replace with = default
150 bool operator==( const QgsSnappingUtils::LayerConfig &other ) const
151 {
152 return layer == other.layer && type == other.type && tolerance == other.tolerance && unit == other.unit;
153 }
154 bool operator!=( const QgsSnappingUtils::LayerConfig &other ) const
155 {
156 return !operator==( other );
157 }
158
160 QgsVectorLayer *layer = nullptr;
162 QgsPointLocator::Types type;
164 double tolerance;
167 };
168
170 QList<QgsSnappingUtils::LayerConfig> layers() const { return mLayers; }
171
176 QString dump();
177
181 QgsSnappingConfig config() const;
182
190 void setEnableSnappingForInvisibleFeature( bool enable );
191
203 {
204 mExtraSnapLayers.insert( vl );
205 }
206
216 {
217 mExtraSnapLayers.remove( vl );
218 }
219
228 QSet<QgsVectorLayer *> getExtraSnapLayers()
229 {
230 return mExtraSnapLayers;
231 }
232
233
234 public slots:
235
239 void setConfig( const QgsSnappingConfig &snappingConfig );
240
246 void toggleEnabled();
247
248 signals:
249
253 void configChanged( const QgsSnappingConfig &snappingConfig );
254
255 protected:
256
258 virtual void prepareIndexStarting( int count ) { Q_UNUSED( count ); }
260 virtual void prepareIndexProgress( int index ) { Q_UNUSED( index ); }
261
263 void clearAllLocators();
264
265 private slots:
266
268 void onInitFinished( bool ok );
269
270 private:
271 void onIndividualLayerSettingsChanged( const QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings> &layerSettings );
273 QgsCoordinateReferenceSystem destinationCrs() const;
274
276 QgsPointLocator *locatorForLayerUsingStrategy( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
278 QgsPointLocator *temporaryLocatorForLayer( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
279
280 typedef QPair< QgsVectorLayer *, QgsRectangle > LayerAndAreaOfInterest;
281
283 bool isIndexPrepared( QgsPointLocator *loc, const QgsRectangle &areaOfInterest );
285 void prepareIndex( const QList<LayerAndAreaOfInterest> &layers, bool relaxed );
286
287 private:
288 // environment
289 QgsMapSettings mMapSettings;
290 QgsVectorLayer *mCurrentLayer = nullptr;
291
292 QgsSnappingConfig mSnappingConfig;
293
294 // configuration
295 IndexingStrategy mStrategy = IndexHybrid;
296 QList<LayerConfig> mLayers;
297
298 // internal data
299 typedef QMap<QgsVectorLayer *, QgsPointLocator *> LocatorsMap;
301 LocatorsMap mLocators;
303 LocatorsMap mTemporaryLocators;
305 QSet<QString> mHybridNonindexableLayers;
307 QSet<QgsVectorLayer *> mExtraSnapLayers;
308
321 QHash<QString, double> mHybridMaxAreaPerLayer;
323 int mHybridPerLayerFeatureLimit = 50000;
324
326 bool mEnableSnappingForInvisibleFeature = true;
327};
328
329
330#endif // QGSSNAPPINGUTILS_H
MapToolUnit
Type of unit of tolerance value from settings.
Definition: qgis.h:3278
This class represents a coordinate reference system (CRS).
The QgsMapSettings class contains configuration for rendering of the map.
The class defines interface for querying point location:
A class to represent a 2D point.
Definition: qgspointxy.h:59
A rectangle specified with double values.
Definition: qgsrectangle.h:42
This is a container for configuration of the snapping of the project.
This class has all the configuration of snapping and can return answers to snapping queries.
@ IndexAlwaysFull
For all layers build index of full extent. Uses more memory, but queries are faster.
@ IndexHybrid
For "big" layers using IndexNeverFull, for the rest IndexAlwaysFull. Compromise between speed and mem...
@ IndexNeverFull
For all layers only create temporary indexes of small extent. Low memory usage, slower queries.
QgsMapSettings mapSettings() const
QSet< QgsVectorLayer * > getExtraSnapLayers()
Returns the list of extra snapping layers.
void addExtraSnapLayer(QgsVectorLayer *vl)
Supply an extra snapping layer (typically a memory layer).
virtual void prepareIndexProgress(int index)
Called when finished indexing a layer with snapToMap. When index == count the indexing is complete.
IndexingStrategy indexingStrategy() const
Find out which strategy is used for indexing - by default hybrid indexing is used.
void configChanged(const QgsSnappingConfig &snappingConfig)
Emitted when the snapping settings object changes.
void removeExtraSnapLayer(QgsVectorLayer *vl)
Removes an extra snapping layer.
void setIndexingStrategy(IndexingStrategy strategy)
Sets a strategy for indexing geometry data - determines how fast and memory consuming the data struct...
QgsVectorLayer * currentLayer() const
The current layer used if mode is SnapCurrentLayer.
QList< QgsSnappingUtils::LayerConfig > layers() const
Query layers used for snapping.
virtual void prepareIndexStarting(int count)
Called when starting to index with snapToMap - can be overridden and e.g. progress dialog can be prov...
Represents a vector layer which manages a vector based data sets.
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Interface that allows rejection of some matches in intersection queries (e.g.
Configures how a certain layer should be handled in a snapping operation.
bool operator!=(const QgsSnappingUtils::LayerConfig &other) const
double tolerance
The range around snapping targets in which snapping should occur.
LayerConfig(QgsVectorLayer *l, QgsPointLocator::Types t, double tol, Qgis::MapToolUnit u)
Create a new configuration for a snapping layer.
QgsPointLocator::Types type
To which geometry properties of this layers a snapping should happen.
QgsVectorLayer * layer
The layer to configure.
Qgis::MapToolUnit unit
The units in which the tolerance is specified.
bool operator==(const QgsSnappingUtils::LayerConfig &other) const