QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgssnappingconfig.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgssnappingconfig.h - QgsSnappingConfig
3 
4  ---------------------
5  begin : 29.8.2016
6  copyright : (C) 2016 by Denis Rouzaud
7  email : [email protected]
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 #ifndef QGSPROJECTSNAPPINGSETTINGS_H
17 #define QGSPROJECTSNAPPINGSETTINGS_H
18 
19 #include <QHash>
20 #include "qgis_core.h"
21 #include "qgstolerance.h"
22 
23 class QDomDocument;
24 class QgsProject;
25 class QgsVectorLayer;
26 
27 
33 class CORE_EXPORT QgsSnappingConfig
34 {
35  Q_GADGET
36 
37  Q_PROPERTY( QgsProject *project READ project WRITE setProject )
38 
39  public:
40 
45  {
46  ActiveLayer = 1,
47  AllLayers = 2,
48  AdvancedConfiguration = 3,
49  };
50  Q_ENUM( SnappingMode )
51 
52 
57  {
58  Vertex = 1,
59  VertexAndSegment = 2,
60  Segment = 3,
61  };
62  // TODO QGIS 4: remove
63  // this could not be tagged with Q_DECL_DEPRECATED due to Doxygen warning
64  // might be fixed in newer Doxygen (does not on 1.8.15, might be ok on 1.8.16)
65 
71  {
72  NoSnapFlag = 0,
73  VertexFlag = 1,
74  SegmentFlag = 2,
75  AreaFlag = 4,
76  CentroidFlag = 8,
77  MiddleOfSegmentFlag = 16,
78  };
79  Q_ENUM( SnappingTypes )
80  Q_DECLARE_FLAGS( SnappingTypeFlag, SnappingTypes )
81  Q_FLAG( SnappingTypeFlag )
82 
83 
88  {
89  Disabled = 0,
90  Global = 1,
91  PerLayer = 2
92  };
93  Q_ENUM( ScaleDependencyMode )
94 
95 
101  static const QString snappingTypeFlagToString( SnappingTypeFlag type )
102  {
103  switch ( type )
104  {
105  case QgsSnappingConfig::NoSnapFlag: return QObject::tr( "No Snapping" );
106  case QgsSnappingConfig::VertexFlag: return QObject::tr( "Vertex" );
107  case QgsSnappingConfig::SegmentFlag: return QObject::tr( "Segment" );
108  case QgsSnappingConfig::AreaFlag: return QObject::tr( "Area" );
109  case QgsSnappingConfig::CentroidFlag: return QObject::tr( "Centroid" );
110  case QgsSnappingConfig::MiddleOfSegmentFlag: return QObject::tr( "Middle of Segments" );
111  }
112  return QString();
113  }
114 
115 
121  class CORE_EXPORT IndividualLayerSettings
122  {
123  public:
124 
133  Q_DECL_DEPRECATED IndividualLayerSettings( bool enabled, SnappingType type, double tolerance, QgsTolerance::UnitType units ) SIP_DEPRECATED;
134 
145  IndividualLayerSettings( bool enabled, SnappingTypeFlag type, double tolerance, QgsTolerance::UnitType units, double minScale = 0.0, double maxScale = 0.0 );
146 
151 
153  bool valid() const;
154 
156  bool enabled() const;
157 
159  void setEnabled( bool enabled );
160 
165  QgsSnappingConfig::SnappingTypeFlag typeFlag() const;
166 
171  Q_DECL_DEPRECATED QgsSnappingConfig::SnappingType type() const SIP_DEPRECATED;
172 
177  Q_DECL_DEPRECATED void setType( QgsSnappingConfig::SnappingType type ) SIP_DEPRECATED;
178 
183  void setTypeFlag( QgsSnappingConfig::SnappingTypeFlag type );
184 
186  double tolerance() const;
187 
189  void setTolerance( double tolerance );
190 
192  QgsTolerance::UnitType units() const;
193 
195  void setUnits( QgsTolerance::UnitType units );
196 
201  double minimumScale() const;
202 
207  void setMinimumScale( double minScale );
208 
213  double maximumScale() const;
214 
219  void setMaximumScale( double maxScale );
220 
224  bool operator!= ( const QgsSnappingConfig::IndividualLayerSettings &other ) const;
225 
226  bool operator== ( const QgsSnappingConfig::IndividualLayerSettings &other ) const;
227 
228  private:
229  bool mValid = false;
230  bool mEnabled = false;
231  SnappingTypeFlag mType = VertexFlag;
232  double mTolerance = 0;
233  QgsTolerance::UnitType mUnits = QgsTolerance::Pixels;
234  double mMinimumScale = 0.0;
235  double mMaximumScale = 0.0;
236  };
237 
241  explicit QgsSnappingConfig( QgsProject *project = nullptr );
242 
243  bool operator==( const QgsSnappingConfig &other ) const;
244 
246  void reset();
247 
249  bool enabled() const;
250 
252  void setEnabled( bool enabled );
253 
255  SnappingMode mode() const;
256 
258  void setMode( SnappingMode mode );
259 
264  QgsSnappingConfig::SnappingTypeFlag typeFlag() const;
265 
270  Q_DECL_DEPRECATED QgsSnappingConfig::SnappingType type() const SIP_DEPRECATED;
271 
276  Q_DECL_DEPRECATED void setType( QgsSnappingConfig::SnappingType type );
277 
282  void setTypeFlag( QgsSnappingConfig::SnappingTypeFlag type );
283 
285  double tolerance() const;
286 
288  void setTolerance( double tolerance );
289 
294  double minimumScale() const;
295 
300  void setMinimumScale( double minScale );
301 
306  double maximumScale() const;
307 
312  void setMaximumScale( double maxScale );
313 
318  void setScaleDependencyMode( ScaleDependencyMode mode );
319 
324  ScaleDependencyMode scaleDependencyMode() const;
325 
327  QgsTolerance::UnitType units() const;
328 
330  void setUnits( QgsTolerance::UnitType units );
331 
333  bool intersectionSnapping() const;
334 
336  void setIntersectionSnapping( bool enabled );
337 
343  bool selfSnapping() const;
344 
350  void setSelfSnapping( bool enabled );
351 
353 #ifndef SIP_RUN
354  QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings> individualLayerSettings() const;
355 #else
356  SIP_PYDICT individualLayerSettings() const;
357  % MethodCode
358  // Create the dictionary.
359  PyObject *d = PyDict_New();
360  if ( !d )
361  return nullptr;
362  // Set the dictionary elements.
363  QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings> container = sipCpp->individualLayerSettings();
364  QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings>::const_iterator i = container.constBegin();
365  while ( i != container.constEnd() )
366  {
367  QgsVectorLayer *vl = i.key();
369 
370  PyObject *vlobj = sipConvertFromType( vl, sipType_QgsVectorLayer, nullptr );
371  PyObject *ilsobj = sipConvertFromType( ils, sipType_QgsSnappingConfig_IndividualLayerSettings, Py_None );
372 
373  if ( !vlobj || !ilsobj || PyDict_SetItem( d, vlobj, ilsobj ) < 0 )
374  {
375  Py_DECREF( d );
376  if ( vlobj )
377  {
378  Py_DECREF( vlobj );
379  }
380  if ( ilsobj )
381  {
382  Py_DECREF( ilsobj );
383  }
384  else
385  {
386  delete ils;
387  }
388  PyErr_SetString( PyExc_StopIteration, "" );
389  }
390  Py_DECREF( vlobj );
391  Py_DECREF( ilsobj );
392  ++i;
393  }
394  sipRes = d;
395  % End
396 #endif
397 
399  QgsSnappingConfig::IndividualLayerSettings individualLayerSettings( QgsVectorLayer *vl ) const;
400 
402  void setIndividualLayerSettings( QgsVectorLayer *vl, const QgsSnappingConfig::IndividualLayerSettings &individualLayerSettings );
403 
409  void clearIndividualLayerSettings();
410 
414  bool operator!= ( const QgsSnappingConfig &other ) const;
415 
421  void readProject( const QDomDocument &doc );
422 
428  void writeProject( QDomDocument &doc );
429 
440  bool addLayers( const QList<QgsMapLayer *> &layers );
441 
442 
452  bool removeLayers( const QList<QgsMapLayer *> &layers );
453 
459  QgsProject *project() const;
460 
466  void setProject( QgsProject *project );
467 
468  private:
469  void readLegacySettings();
470 
472  QgsProject *mProject = nullptr;
473  bool mEnabled = false;
474  SnappingMode mMode = ActiveLayer;
475  SnappingTypeFlag mType = VertexFlag;
476  double mTolerance = 0.0;
477  ScaleDependencyMode mScaleDependencyMode = Disabled;
478  double mMinimumScale = 0.0;
479  double mMaximumScale = 0.0;
481  bool mIntersectionSnapping = false;
482  bool mSelfSnapping = false;
483 
484  QHash<QgsVectorLayer *, IndividualLayerSettings> mIndividualLayerSettings;
485 
486 };
487 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsSnappingConfig::SnappingTypeFlag )
488 
489 #endif // QGSPROJECTSNAPPINGSETTINGS_H
QgsSnappingConfig::SnappingType
SnappingType
SnappingType defines on what object the snapping is performed.
Definition: qgssnappingconfig.h:57
qgstolerance.h
QgsTolerance::ProjectUnits
@ ProjectUnits
Map (project) units. Added in 2.8.
Definition: qgstolerance.h:48
QgsSnappingConfig::SegmentFlag
@ SegmentFlag
On segments.
Definition: qgssnappingconfig.h:74
QgsSnappingConfig::SnappingMode
SnappingMode
SnappingMode defines on which layer the snapping is performed.
Definition: qgssnappingconfig.h:45
operator!=
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:405
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:95
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsSnappingConfig::CentroidFlag
@ CentroidFlag
On centroid.
Definition: qgssnappingconfig.h:76
QgsSnappingConfig::AreaFlag
@ AreaFlag
On Area.
Definition: qgssnappingconfig.h:75
QgsTolerance::UnitType
UnitType
Type of unit of tolerance value from settings.
Definition: qgstolerance.h:42
QgsSnappingConfig::IndividualLayerSettings::IndividualLayerSettings
IndividualLayerSettings()=default
Constructs an invalid setting.
QgsSnappingConfig::VertexFlag
@ VertexFlag
On vertices.
Definition: qgssnappingconfig.h:73
Q_DECLARE_OPERATORS_FOR_FLAGS
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QgsSnappingConfig
This is a container for configuration of the snapping of the project.
Definition: qgssnappingconfig.h:34
QgsSnappingConfig::SnappingTypes
SnappingTypes
SnappingTypeFlag defines on what object the snapping is performed.
Definition: qgssnappingconfig.h:71
QgsSnappingConfig::IndividualLayerSettings
This is a container of advanced configuration (per layer) of the snapping of the project.
Definition: qgssnappingconfig.h:122
QgsSnappingConfig::NoSnapFlag
@ NoSnapFlag
No snapping.
Definition: qgssnappingconfig.h:72
QgsSnappingConfig::MiddleOfSegmentFlag
@ MiddleOfSegmentFlag
On Middle segment.
Definition: qgssnappingconfig.h:77
QgsTolerance
This is the class is providing tolerance value in map unit values.
Definition: qgstolerance.h:33
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsSnappingConfig::ScaleDependencyMode
ScaleDependencyMode
ScaleDependencyMode the scale dependency mode of snapping.
Definition: qgssnappingconfig.h:88