QGIS API Documentation  2.14.0-Essen
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 "qgsmapsettings.h"
21 #include "qgstolerance.h"
22 #include "qgspointlocator.h"
23 
41 class CORE_EXPORT QgsSnappingUtils : public QObject
42 {
43  Q_OBJECT
44  public:
45  QgsSnappingUtils( QObject* parent = nullptr );
47 
48  // main actions
49 
51  QgsPointLocator* locatorForLayer( QgsVectorLayer* vl );
52 
54  QgsPointLocator::Match snapToMap( QPoint point, QgsPointLocator::MatchFilter* filter = nullptr );
55  QgsPointLocator::Match snapToMap( const QgsPoint& pointMap, QgsPointLocator::MatchFilter* filter = nullptr );
56 
58  QgsPointLocator::Match snapToCurrentLayer( QPoint point, int type, QgsPointLocator::MatchFilter* filter = nullptr );
59 
60  // environment setup
61 
63  void setMapSettings( const QgsMapSettings& settings );
64  const QgsMapSettings& mapSettings() const { return mMapSettings; }
65 
67  void setCurrentLayer( QgsVectorLayer* layer );
68  QgsVectorLayer* currentLayer() const { return mCurrentLayer; }
69 
70 
71  // configuration
72 
75  {
79  };
80 
82  void setSnapToMapMode( SnapToMapMode mode );
84  SnapToMapMode snapToMapMode() const { return mSnapToMapMode; }
85 
87  {
90  IndexHybrid
91  };
92 
94  void setIndexingStrategy( IndexingStrategy strategy ) { mStrategy = strategy; }
96  IndexingStrategy indexingStrategy() const { return mStrategy; }
97 
99  void setDefaultSettings( int type, double tolerance, QgsTolerance::UnitType unit );
101  void defaultSettings( int& type, double& tolerance, QgsTolerance::UnitType& unit );
102 
106  struct LayerConfig
107  {
108  LayerConfig( QgsVectorLayer* l, const QgsPointLocator::Types& t, double tol, QgsTolerance::UnitType u ) : layer( l ), type( t ), tolerance( tol ), unit( u ) {}
109 
110  bool operator==( const LayerConfig& other ) const
111  {
112  return layer == other.layer && type == other.type && tolerance == other.tolerance && unit == other.unit;
113  }
114  bool operator!=( const LayerConfig& other ) const
115  {
116  return !operator==( other );
117  }
118 
122  QgsPointLocator::Types type;
124  double tolerance;
127  };
128 
130  void setLayers( const QList<LayerConfig>& layers );
132  QList<LayerConfig> layers() const { return mLayers; }
133 
135  void setSnapOnIntersections( bool enabled );
137  bool snapOnIntersections() const { return mSnapOnIntersection; }
138 
142  QString dump();
143 
144  public slots:
146  void readConfigFromProject();
147 
148  signals:
152  void configChanged();
153 
154  protected:
156  virtual void prepareIndexStarting( int count ) { Q_UNUSED( count ); }
158  virtual void prepareIndexProgress( int index ) { Q_UNUSED( index ); }
159 
160  private slots:
161  void onLayersWillBeRemoved( const QStringList& layerIds );
162 
163  private:
165  const QgsCoordinateReferenceSystem* destCRS();
166 
168  void clearAllLocators();
169 
171  QgsPointLocator* locatorForLayerUsingStrategy( QgsVectorLayer* vl, const QgsPoint& pointMap, double tolerance );
173  QgsPointLocator* temporaryLocatorForLayer( QgsVectorLayer* vl, const QgsPoint& pointMap, double tolerance );
174 
176 
178  bool isIndexPrepared( QgsVectorLayer* vl, const QgsRectangle& areaOfInterest );
180  void prepareIndex( const QList<LayerAndAreaOfInterest>& layers );
181 
182  private:
183  // environment
184  QgsMapSettings mMapSettings;
185  QgsVectorLayer* mCurrentLayer;
186 
187  // configuration
188  SnapToMapMode mSnapToMapMode;
189  IndexingStrategy mStrategy;
190  int mDefaultType;
191  double mDefaultTolerance;
192  QgsTolerance::UnitType mDefaultUnit;
193  QList<LayerConfig> mLayers;
194  bool mSnapOnIntersection;
195 
196  // internal data
199  LocatorsMap mLocators;
201  LocatorsMap mTemporaryLocators;
203  QSet<QString> mHybridNonindexableLayers;
212  QHash<QString, double> mHybridMaxAreaPerLayer;
214  int mHybridPerLayerFeatureLimit;
215 
217  bool mIsIndexing;
218 };
219 
220 
221 #endif // QGSSNAPPINGUTILS_H
SnapToMapMode snapToMapMode() const
Find out how the snapping to map is done.
The class defines interface for querying point location:
static unsigned index
A rectangle specified with double values.
Definition: qgsrectangle.h:35
virtual void prepareIndexProgress(int index)
Called when finished indexing a layer. When index == count the indexing is complete.
snap to all rendered layers (tolerance and type from defaultSettings())
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
LayerConfig(QgsVectorLayer *l, const QgsPointLocator::Types &t, double tol, QgsTolerance::UnitType u)
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:33
IndexingStrategy indexingStrategy() const
Find out which strategy is used for indexing - by default hybrid indexing is used.
Interface that allows rejection of some matches in intersection queries (e.g.
void setIndexingStrategy(IndexingStrategy strategy)
Set a strategy for indexing geometry data - determines how fast and memory consuming the data structu...
The QgsMapSettings class contains configuration for rendering of the map.
bool operator==(const LayerConfig &other) const
QList< LayerConfig > layers() const
Query layers used for snapping.
For all layers build index of full extent. Uses more memory, but queries are faster.
QgsTolerance::UnitType unit
The units in which the tolerance is specified.
bool snapOnIntersections() const
Query whether to consider intersections of nearby segments for snapping.
snap according to the configuration set in setLayers()
A class to represent a point.
Definition: qgspoint.h:65
double tolerance
The range around snapping targets in which snapping should occur.
const QgsMapSettings & mapSettings() const
For all layers only create temporary indexes of small extent. Low memory usage, slower queries...
virtual void prepareIndexStarting(int count)
Called when starting to index - can be overridden and e.g. progress dialog can be provided...
QgsVectorLayer * layer
The layer to configure.
Configures how a certain layer should be handled in a snapping operation.
Class for storing a coordinate reference system (CRS)
This class has all the configuration of snapping and can return answers to snapping queries...
QgsPointLocator::Types type
To which geometry properties of this layers a snapping should happen.
bool operator!=(const LayerConfig &other) const
QgsVectorLayer * currentLayer() const
snap just to current layer (tolerance and type from defaultSettings())
Represents a vector layer which manages a vector based data sets.
SnapToMapMode
modes for "snap to background"