QGIS API Documentation 4.1.0-Master (60fea48833c)
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 "qgspointlocator.h"
24#include "qgssnappingconfig.h"
25#include "qgstolerance.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:
61 QgsSnappingUtils( QObject *parent SIP_TRANSFERTHIS = nullptr, bool enableSnappingForInvisibleFeature = true );
62 ~QgsSnappingUtils() override;
63
64 // main actions
65
71
78 QgsPointLocator::Match snapToMap( QPoint point, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
79
86 QgsPointLocator::Match snapToMap( const QgsPointXY &pointMap, QgsPointLocator::MatchFilter *filter = nullptr, bool relaxed = false );
87
90
91 // environment setup
92
94 void setMapSettings( const QgsMapSettings &settings );
95 QgsMapSettings mapSettings() const { return mMapSettings; }
96
98 void setCurrentLayer( QgsVectorLayer *layer );
100 QgsVectorLayer *currentLayer() const { return mCurrentLayer; }
101
102 // configuration
103
111
113 void setIndexingStrategy( IndexingStrategy strategy ) { mStrategy = strategy; }
115 IndexingStrategy indexingStrategy() const { return mStrategy; }
116
121 {
140 : layer( l )
141 , type( t )
142 , tolerance( tol )
143 , unit( u )
144 {}
145
146 // TODO c++20 - replace with = default
147 bool operator==( const QgsSnappingUtils::LayerConfig &other ) const { return layer == other.layer && type == other.type && tolerance == other.tolerance && unit == other.unit; }
148 bool operator!=( const QgsSnappingUtils::LayerConfig &other ) const { return !operator==( other ); }
149
155 double tolerance;
158 };
159
161 QList<QgsSnappingUtils::LayerConfig> layers() const { return mLayers; }
162
166 QString dump();
167
171 QgsSnappingConfig config() const;
172
180 void setEnableSnappingForInvisibleFeature( bool enable );
181
192 void addExtraSnapLayer( QgsVectorLayer *vl ) { mExtraSnapLayers.insert( vl ); }
193
202 void removeExtraSnapLayer( QgsVectorLayer *vl ) { mExtraSnapLayers.remove( vl ); }
203
212 QSet<QgsVectorLayer *> getExtraSnapLayers() { return mExtraSnapLayers; }
213
214
215 public slots:
216
220 void setConfig( const QgsSnappingConfig &snappingConfig );
221
226 void toggleEnabled();
227
228 signals:
229
233 void configChanged( const QgsSnappingConfig &snappingConfig );
234
235 protected:
237 virtual void prepareIndexStarting( int count ) { Q_UNUSED( count ); }
239 virtual void prepareIndexProgress( int index ) { Q_UNUSED( index ); }
240
242 void clearAllLocators();
243
244 private slots:
245
247 void onInitFinished( bool ok );
248
249 private:
250 void onIndividualLayerSettingsChanged( const QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings> &layerSettings );
252 QgsCoordinateReferenceSystem destinationCrs() const;
253
255 QgsPointLocator *locatorForLayerUsingStrategy( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
257 QgsPointLocator *temporaryLocatorForLayer( QgsVectorLayer *vl, const QgsPointXY &pointMap, double tolerance );
258
259 typedef QPair< QgsVectorLayer *, QgsRectangle > LayerAndAreaOfInterest;
260
262 bool isIndexPrepared( QgsPointLocator *loc, const QgsRectangle &areaOfInterest );
264 void prepareIndex( const QList<LayerAndAreaOfInterest> &layers, bool relaxed );
265
266 private:
267 // environment
268 QgsMapSettings mMapSettings;
269 QgsVectorLayer *mCurrentLayer = nullptr;
270
271 QgsSnappingConfig mSnappingConfig;
272
273 // configuration
274 IndexingStrategy mStrategy = IndexHybrid;
275 QList<LayerConfig> mLayers;
276
277 // internal data
278 typedef QMap<QgsVectorLayer *, QgsPointLocator *> LocatorsMap;
280 LocatorsMap mLocators;
282 LocatorsMap mTemporaryLocators;
284 QSet<QString> mHybridNonindexableLayers;
286 QSet<QgsVectorLayer *> mExtraSnapLayers;
287
300 QHash<QString, double> mHybridMaxAreaPerLayer;
302 int mHybridPerLayerFeatureLimit = 50000;
303
305 bool mEnableSnappingForInvisibleFeature = true;
306
308};
309
310
311#endif // QGSSNAPPINGUTILS_H
MapToolUnit
Type of unit of tolerance value from settings.
Definition qgis.h:5138
Represents a coordinate reference system (CRS).
Contains configuration for rendering maps.
Defines the interface for querying point locations.
QFlags< Type > Types
Represents a 2D point.
Definition qgspointxy.h:62
A rectangle specified with double values.
Stores configuration of snapping settings for the project.
QgsPointLocator::Match snapToCurrentLayer(QPoint point, QgsPointLocator::Types type, QgsPointLocator::MatchFilter *filter=nullptr)
Snap to current layer.
void setMapSettings(const QgsMapSettings &settings)
Assign current map settings to the utils - used for conversion between screen coords to map coords.
friend class TestQgsSnappingUtils
@ 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...
@ IndexExtent
For all layer build index of extent given in map settings.
@ IndexNeverFull
For all layers only create temporary indexes of small extent. Low memory usage, slower queries.
QgsMapSettings mapSettings() const
QgsPointLocator * locatorForLayer(QgsVectorLayer *vl)
Gets a point locator for the given layer.
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.
QgsSnappingConfig config
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.
QgsPointLocator::Match snapToMap(QPoint point, QgsPointLocator::MatchFilter *filter=nullptr, bool relaxed=false)
Snap to map according to the current configuration.
QgsSnappingUtils(QObject *parent=nullptr, bool enableSnappingForInvisibleFeature=true)
Constructor for QgsSnappingUtils.
void setConfig(const QgsSnappingConfig &snappingConfig)
The snapping configuration controls the behavior of this object.
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 dataset.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
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