QGIS API Documentation  2.12.0-Lyon
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 = 0 );
47 
48  // main actions
49 
51  QgsPointLocator* locatorForLayer( QgsVectorLayer* vl );
52 
54  QgsPointLocator::Match snapToMap( const QPoint& point, QgsPointLocator::MatchFilter* filter = 0 );
55  QgsPointLocator::Match snapToMap( const QgsPoint& pointMap, QgsPointLocator::MatchFilter* filter = 0 );
56 
58  QgsPointLocator::Match snapToCurrentLayer( const QPoint& point, int type, QgsPointLocator::MatchFilter* filter = 0 );
59 
60  // environment setup
61 
63  void setMapSettings( const QgsMapSettings& settings );
64  const QgsMapSettings& mapSettings() const { return mMapSettings; }
65 
67  void setCurrentLayer( QgsVectorLayer* layer ) { mCurrentLayer = layer; }
68  QgsVectorLayer* currentLayer() const { return mCurrentLayer; }
69 
70 
71  // configuration
72 
75  {
79  };
80 
82  void setSnapToMapMode( SnapToMapMode mode ) { mSnapToMapMode = 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 
113  QgsPointLocator::Types type;
115  double tolerance;
118  };
119 
121  void setLayers( const QList<LayerConfig>& layers ) { mLayers = layers; }
123  QList<LayerConfig> layers() const { return mLayers; }
124 
126  void setSnapOnIntersections( bool enabled ) { mSnapOnIntersection = enabled; }
128  bool snapOnIntersections() const { return mSnapOnIntersection; }
129 
130  public slots:
132  void readConfigFromProject();
133 
134  protected:
136  virtual void prepareIndexStarting( int count ) { Q_UNUSED( count ); }
138  virtual void prepareIndexProgress( int index ) { Q_UNUSED( index ); }
139 
140  private slots:
141  void onLayersWillBeRemoved( const QStringList& layerIds );
142 
143  private:
145  const QgsCoordinateReferenceSystem* destCRS();
146 
148  void clearAllLocators();
149 
151  QgsPointLocator* locatorForLayerUsingStrategy( QgsVectorLayer* vl, const QgsPoint& pointMap, double tolerance );
153  QgsPointLocator* temporaryLocatorForLayer( QgsVectorLayer* vl, const QgsPoint& pointMap, double tolerance );
154 
156  bool willUseIndex( QgsVectorLayer* vl ) const;
158  void prepareIndex( const QList<QgsVectorLayer*>& layers );
159 
160  private:
161  // environment
162  QgsMapSettings mMapSettings;
163  QgsVectorLayer* mCurrentLayer;
164 
165  // configuration
166  SnapToMapMode mSnapToMapMode;
167  IndexingStrategy mStrategy;
168  int mDefaultType;
169  double mDefaultTolerance;
170  QgsTolerance::UnitType mDefaultUnit;
171  QList<LayerConfig> mLayers;
172  bool mSnapOnIntersection;
173 
174  // internal data
175  typedef QMap<QgsVectorLayer*, QgsPointLocator*> LocatorsMap;
177  LocatorsMap mLocators;
179  LocatorsMap mTemporaryLocators;
181  QSet<QString> mHybridNonindexableLayers;
182 
184  bool mIsIndexing;
185 };
186 
187 
188 #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
void setSnapOnIntersections(bool enabled)
Set whether to consider intersections of nearby segments for snapping.
void setCurrentLayer(QgsVectorLayer *layer)
Set current layer so that if mode is SnapCurrentLayer we know which layer to use. ...
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())
LayerConfig(QgsVectorLayer *l, const QgsPointLocator::Types &t, double tol, QgsTolerance::UnitType u)
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.
QList< LayerConfig > layers() const
Query layers used for snapping.
For all layers build index of full extent. Uses more memory, but queries are faster.
void setSnapToMapMode(SnapToMapMode mode)
Set how the snapping to map is done.
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:63
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.
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:33
QgsVectorLayer * currentLayer() const
snap just to current layer (tolerance and type from defaultSettings())
Represents a vector layer which manages a vector based data sets.
void setLayers(const QList< LayerConfig > &layers)
Set layers which will be used for snapping.
SnapToMapMode
modes for "snap to background"