QGIS API Documentation 3.38.0-Grenoble (exported)
Loading...
Searching...
No Matches
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
49class CORE_EXPORT QgsSnappingUtils : public QObject
50{
51 Q_OBJECT
52
53 Q_PROPERTY( QgsSnappingConfig config READ config WRITE setConfig NOTIFY configChanged )
54
55 public:
56
62 QgsSnappingUtils( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enableSnappingForInvisibleFeature = true );
63 ~QgsSnappingUtils() override;
64
65 // main actions
66
71 QgsPointLocator *locatorForLayer( QgsVectorLayer *vl );
72
79 QgsPointLocator::Match snapToMap( QPoint point, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
80
87 QgsPointLocator::Match snapToMap( const QgsPointXY &pointMap, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
88
90 QgsPointLocator::Match snapToCurrentLayer( QPoint point, QgsPointLocator::Types type, QgsPointLocator::MatchFilter *filter = nullptr );
91
92 // environment setup
93
95 void setMapSettings( const QgsMapSettings &settings );
96 QgsMapSettings mapSettings() const { return mMapSettings; }
97
99 void setCurrentLayer( QgsVectorLayer *layer );
101 QgsVectorLayer *currentLayer() const { return mCurrentLayer; }
102
103 // configuration
104
112
114 void setIndexingStrategy( IndexingStrategy strategy ) { mStrategy = strategy; }
116 IndexingStrategy indexingStrategy() const { return mStrategy; }
117
122 {
123
142 : layer( l )
143 , type( t )
144 , tolerance( tol )
145 , unit( u )
146 {}
147
148 // TODO c++20 - replace with = default
149 bool operator==( const QgsSnappingUtils::LayerConfig &other ) const
150 {
151 return layer == other.layer && type == other.type && tolerance == other.tolerance && unit == other.unit;
152 }
153 bool operator!=( const QgsSnappingUtils::LayerConfig &other ) const
154 {
155 return !operator==( other );
156 }
157
159 QgsVectorLayer *layer = nullptr;
163 double tolerance;
166 };
167
169 QList<QgsSnappingUtils::LayerConfig> layers() const { return mLayers; }
170
174 QString dump();
175
179 QgsSnappingConfig config() const;
180
188 void setEnableSnappingForInvisibleFeature( bool enable );
189
201 {
202 mExtraSnapLayers.insert( vl );
203 }
204
214 {
215 mExtraSnapLayers.remove( vl );
216 }
217
226 QSet<QgsVectorLayer *> getExtraSnapLayers()
227 {
228 return mExtraSnapLayers;
229 }
230
231
232 public slots:
233
237 void setConfig( const QgsSnappingConfig &snappingConfig );
238
243 void toggleEnabled();
244
245 signals:
246
250 void configChanged( const QgsSnappingConfig &snappingConfig );
251
252 protected:
253
255 virtual void prepareIndexStarting( int count ) { Q_UNUSED( count ); }
257 virtual void prepareIndexProgress( int index ) { Q_UNUSED( index ); }
258
260 void clearAllLocators();
261
262 private slots:
263
265 void onInitFinished( bool ok );
266
267 private:
268 void onIndividualLayerSettingsChanged( const QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings> &layerSettings );
270 QgsCoordinateReferenceSystem destinationCrs() const;
271
273 QgsPointLocator *locatorForLayerUsingStrategy( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
275 QgsPointLocator *temporaryLocatorForLayer( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
276
277 typedef QPair< QgsVectorLayer *, QgsRectangle > LayerAndAreaOfInterest;
278
280 bool isIndexPrepared( QgsPointLocator *loc, const QgsRectangle &areaOfInterest );
282 void prepareIndex( const QList<LayerAndAreaOfInterest> &layers, bool relaxed );
283
284 private:
285 // environment
286 QgsMapSettings mMapSettings;
287 QgsVectorLayer *mCurrentLayer = nullptr;
288
289 QgsSnappingConfig mSnappingConfig;
290
291 // configuration
292 IndexingStrategy mStrategy = IndexHybrid;
293 QList<LayerConfig> mLayers;
294
295 // internal data
296 typedef QMap<QgsVectorLayer *, QgsPointLocator *> LocatorsMap;
298 LocatorsMap mLocators;
300 LocatorsMap mTemporaryLocators;
302 QSet<QString> mHybridNonindexableLayers;
304 QSet<QgsVectorLayer *> mExtraSnapLayers;
305
318 QHash<QString, double> mHybridMaxAreaPerLayer;
320 int mHybridPerLayerFeatureLimit = 50000;
321
323 bool mEnableSnappingForInvisibleFeature = true;
324};
325
326
327#endif // QGSSNAPPINGUTILS_H
MapToolUnit
Type of unit of tolerance value from settings.
Definition qgis.h:4326
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:
QFlags< Type > Types
A class to represent a 2D point.
Definition qgspointxy.h:60
A rectangle specified with double values.
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