QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
103  struct LayerConfig
104  {
105  LayerConfig( QgsVectorLayer* l, int t, double tol, QgsTolerance::UnitType u ) : layer( l ), type( t ), tolerance( tol ), unit( u ) {}
106 
108  int type;
109  double tolerance;
111  };
112 
114  void setLayers( const QList<LayerConfig>& layers ) { mLayers = layers; }
116  QList<LayerConfig> layers() const { return mLayers; }
117 
119  void setSnapOnIntersections( bool enabled ) { mSnapOnIntersection = enabled; }
121  bool snapOnIntersections() const { return mSnapOnIntersection; }
122 
123  public slots:
125  void readConfigFromProject();
126 
127  protected:
129  virtual void prepareIndexStarting( int count ) { Q_UNUSED( count ); }
131  virtual void prepareIndexProgress( int index ) { Q_UNUSED( index ); }
132 
133  private slots:
134  void onLayersWillBeRemoved( QStringList layerIds );
135 
136  private:
138  const QgsCoordinateReferenceSystem* destCRS();
139 
141  void clearAllLocators();
142 
144  QgsPointLocator* locatorForLayerUsingStrategy( QgsVectorLayer* vl, const QgsPoint& pointMap, double tolerance );
146  QgsPointLocator* temporaryLocatorForLayer( QgsVectorLayer* vl, const QgsPoint& pointMap, double tolerance );
147 
149  bool willUseIndex( QgsVectorLayer* vl ) const;
151  void prepareIndex( const QList<QgsVectorLayer*>& layers );
152 
153  private:
154  // environment
155  QgsMapSettings mMapSettings;
156  QgsVectorLayer* mCurrentLayer;
157 
158  // configuration
159  SnapToMapMode mSnapToMapMode;
160  IndexingStrategy mStrategy;
161  int mDefaultType;
162  double mDefaultTolerance;
163  QgsTolerance::UnitType mDefaultUnit;
164  QList<LayerConfig> mLayers;
165  bool mSnapOnIntersection;
166 
167  // internal data
168  typedef QMap<QgsVectorLayer*, QgsPointLocator*> LocatorsMap;
170  LocatorsMap mLocators;
172  LocatorsMap mTemporaryLocators;
174  QSet<QString> mHybridNonindexableLayers;
175 };
176 
177 
178 #endif // QGSSNAPPINGUTILS_H