QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgssnappingconfig.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsprojectsnappingsettings.cpp - QgsProjectSnappingSettings
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 #include "qgssnappingconfig.h"
17 
18 #include <QDomElement>
19 #include <QHeaderView>
20 
21 #include "qgssettings.h"
22 #include "qgslogger.h"
23 #include "qgsvectorlayer.h"
24 #include "qgsproject.h"
25 #include "qgsapplication.h"
26 
27 
28 QgsSnappingConfig::IndividualLayerSettings::IndividualLayerSettings( bool enabled, SnappingTypeFlag type, double tolerance, QgsTolerance::UnitType units, double minScale, double maxScale )
29  : mValid( true )
30  , mEnabled( enabled )
31  , mType( type )
32  , mTolerance( tolerance )
33  , mUnits( units )
34  , mMinimumScale( minScale )
35  , mMaximumScale( maxScale )
36 {}
37 
39  : mValid( true )
40  , mEnabled( enabled )
41  , mTolerance( tolerance )
42  , mUnits( units )
43 {
45  setType( type );
47 }
48 
50 {
51  return mValid;
52 }
53 
55 {
56  return mEnabled;
57 }
58 
60 {
61  mEnabled = enabled;
62 }
63 
64 QgsSnappingConfig::SnappingTypeFlag QgsSnappingConfig::IndividualLayerSettings::typeFlag() const
65 {
66  return mType;
67 }
68 
70 {
71 
72  if ( ( mType & SegmentFlag ) && ( mType & VertexFlag ) )
73  return QgsSnappingConfig::SnappingType::VertexAndSegment;
74  else if ( mType & SegmentFlag )
75  return QgsSnappingConfig::SnappingType::Segment;
76  else
77  return QgsSnappingConfig::SnappingType::Vertex;
78 
79 }
80 
82 {
83  switch ( type )
84  {
85  case 1:
86  mType = VertexFlag;
87  break;
88  case 2:
89  mType = VertexFlag | SegmentFlag;
90  break;
91  case 3:
92  mType = SegmentFlag;
93  break;
94  default:
95  mType = NoSnapFlag;
96  break;
97  }
98 }
99 void QgsSnappingConfig::IndividualLayerSettings::setTypeFlag( QgsSnappingConfig::SnappingTypeFlag type )
100 {
101  mType = type;
102 }
103 
105 {
106  return mTolerance;
107 }
108 
110 {
111  mTolerance = tolerance;
112 }
113 
115 {
116  return mUnits;
117 }
118 
120 {
121  mUnits = units;
122 }
123 
125 {
126  return mMinimumScale;
127 }
128 
130 {
131  mMinimumScale = minScale;
132 }
133 
135 {
136  return mMaximumScale;
137 }
138 
140 {
141  mMaximumScale = maxScale;
142 }
143 
145 {
146  return mValid != other.mValid
147  || mEnabled != other.mEnabled
148  || mType != other.mType
149  || mTolerance != other.mTolerance
150  || mUnits != other.mUnits
151  || mMinimumScale != other.mMinimumScale
152  || mMaximumScale != other.mMaximumScale;
153 }
154 
156 {
157  return mValid == other.mValid
158  && mEnabled == other.mEnabled
159  && mType == other.mType
160  && mTolerance == other.mTolerance
161  && mUnits == other.mUnits
162  && mMinimumScale == other.mMinimumScale
163  && mMaximumScale == other.mMaximumScale;
164 }
165 
166 
168  : mProject( project )
169 {
170  if ( project )
171  reset();
172 }
173 
175 {
176  return mEnabled == other.mEnabled
177  && mMode == other.mMode
178  && mType == other.mType
179  && mTolerance == other.mTolerance
180  && mUnits == other.mUnits
181  && mIntersectionSnapping == other.mIntersectionSnapping
182  && mSelfSnapping == other.mSelfSnapping
183  && mIndividualLayerSettings == other.mIndividualLayerSettings
184  && mScaleDependencyMode == other.mScaleDependencyMode
185  && mMinimumScale == other.mMinimumScale
186  && mMaximumScale == other.mMaximumScale;
187 }
188 
190 {
191  // get defaults values. They are both used for standard and advanced configuration (per layer)
192  bool enabled = QgsSettings().value( QStringLiteral( "/qgis/digitizing/default_snap_enabled" ), false ).toBool();
193  SnappingMode mode = QgsSettings().enumValue( QStringLiteral( "/qgis/digitizing/default_snap_mode" ), AllLayers );
194  if ( mode == 0 )
195  {
196  // backward compatibility with QGIS 2.x
197  // could be removed in 3.4+
198  mode = AllLayers;
199  }
200  QgsSnappingConfig::SnappingTypeFlag type = QgsSettings().flagValue( QStringLiteral( "/qgis/digitizing/default_snap_type" ), VertexFlag );
201  double tolerance = QgsSettings().value( QStringLiteral( "/qgis/digitizing/default_snapping_tolerance" ), Qgis::DEFAULT_SNAP_TOLERANCE ).toDouble();
202  QgsTolerance::UnitType units = QgsSettings().enumValue( QStringLiteral( "/qgis/digitizing/default_snapping_tolerance_unit" ), Qgis::DEFAULT_SNAP_UNITS );
203 
204  // assign main (standard) config
205  mEnabled = enabled;
206  mMode = mode;
207  mType = type;
208  mTolerance = tolerance;
209  mScaleDependencyMode = Disabled;
210  mMinimumScale = 0.0;
211  mMaximumScale = 0.0;
212  // do not allow unit to be "layer" if not in advanced configuration
213  if ( mUnits == QgsTolerance::LayerUnits && mMode != AdvancedConfiguration )
214  {
216  }
217  else
218  {
219  mUnits = units;
220  }
221  mIntersectionSnapping = false;
222  mSelfSnapping = false;
223 
224  // set advanced config
225  if ( mProject )
226  {
227  mIndividualLayerSettings = QHash<QgsVectorLayer *, IndividualLayerSettings>();
228  const auto constMapLayers = mProject->mapLayers();
229  for ( QgsMapLayer *ml : constMapLayers )
230  {
231  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
232  if ( vl )
233  {
234  mIndividualLayerSettings.insert( vl, IndividualLayerSettings( enabled, type, tolerance, units, 0.0, 0.0 ) );
235  }
236  }
237  }
238 }
239 
241 {
242  return mEnabled;
243 }
244 
245 void QgsSnappingConfig::setEnabled( bool enabled )
246 {
247  if ( mEnabled == enabled )
248  {
249  return;
250  }
251  mEnabled = enabled;
252 }
253 
255 {
256  return mMode;
257 }
258 
260 {
261  if ( mMode == mode )
262  {
263  return;
264  }
265  mMode = mode;
266 }
267 
268 QgsSnappingConfig::SnappingTypeFlag QgsSnappingConfig::typeFlag() const
269 {
270  return mType;
271 }
272 
274 {
275  if ( ( mType & SegmentFlag ) && ( mType & VertexFlag ) )
276  return QgsSnappingConfig::SnappingType::VertexAndSegment;
277  else if ( mType & SegmentFlag )
278  return QgsSnappingConfig::SnappingType::Segment;
279  else
280  return QgsSnappingConfig::SnappingType::Vertex;
281 }
282 
284 {
285  switch ( type )
286  {
287  case SnappingType::Vertex:
288  mType = VertexFlag;
289  break;
290  case SnappingType::VertexAndSegment:
291  mType = static_cast<QgsSnappingConfig::SnappingTypeFlag>( QgsSnappingConfig::VertexFlag | QgsSnappingConfig::SegmentFlag );
292  break;
293  case SnappingType::Segment:
294  mType = SegmentFlag;
295  break;
296  default:
297  mType = NoSnapFlag;
298  break;
299  }
300 }
301 void QgsSnappingConfig::setTypeFlag( QgsSnappingConfig::SnappingTypeFlag type )
302 {
303  if ( mType == type )
304  {
305  return;
306  }
307  mType = type;
308 }
309 
311 {
312  return mTolerance;
313 }
314 
315 void QgsSnappingConfig::setTolerance( double tolerance )
316 {
317  if ( mTolerance == tolerance )
318  {
319  return;
320  }
321  mTolerance = tolerance;
322 }
323 
325 {
326  return mUnits;
327 }
328 
330 {
331  if ( mUnits == units )
332  {
333  return;
334  }
335  mUnits = units;
336 }
337 
339 {
340  return mIntersectionSnapping;
341 }
342 
344 {
345  mIntersectionSnapping = enabled;
346 }
347 
349 {
350  return mSelfSnapping;
351 }
352 
354 {
355  mSelfSnapping = enabled;
356 }
357 
358 QHash<QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings> QgsSnappingConfig::individualLayerSettings() const
359 {
360  return mIndividualLayerSettings;
361 }
362 
364 {
365  if ( vl && mIndividualLayerSettings.contains( vl ) )
366  {
367  return mIndividualLayerSettings.value( vl );
368  }
369  else
370  {
371  // return invalid settings
372  return IndividualLayerSettings();
373  }
374 }
375 
377 {
378  mIndividualLayerSettings.clear();
379 }
380 
382 {
383  if ( !vl || !vl->isSpatial() || mIndividualLayerSettings.value( vl ) == individualLayerSettings )
384  {
385  return;
386  }
387  mIndividualLayerSettings.insert( vl, individualLayerSettings );
388 }
389 
391 {
392  return mEnabled != other.mEnabled
393  || mMode != other.mMode
394  || mType != other.mType
395  || mTolerance != other.mTolerance
396  || mUnits != other.mUnits
397  || mIndividualLayerSettings != other.mIndividualLayerSettings
398  || mScaleDependencyMode != other.mScaleDependencyMode
399  || mMinimumScale != other.mMinimumScale
400  || mMaximumScale != other.mMaximumScale;
401 }
402 
403 void QgsSnappingConfig::readProject( const QDomDocument &doc )
404 {
405  QDomElement snapSettingsElem = doc.firstChildElement( QStringLiteral( "qgis" ) ).firstChildElement( QStringLiteral( "snapping-settings" ) );
406  if ( snapSettingsElem.isNull() )
407  {
408  readLegacySettings();
409  return;
410  }
411 
412  if ( snapSettingsElem.hasAttribute( QStringLiteral( "enabled" ) ) )
413  mEnabled = snapSettingsElem.attribute( QStringLiteral( "enabled" ) ) == QLatin1String( "1" );
414 
415  if ( snapSettingsElem.hasAttribute( QStringLiteral( "mode" ) ) )
416  mMode = ( SnappingMode )snapSettingsElem.attribute( QStringLiteral( "mode" ) ).toInt();
417 
418  if ( snapSettingsElem.hasAttribute( QStringLiteral( "type" ) ) )
419  {
420  int type = snapSettingsElem.attribute( QStringLiteral( "type" ) ).toInt();
421  QDomElement versionElem = doc.firstChildElement( QStringLiteral( "qgis" ) );
422  QString version;
423  bool before3_14 = false;
424  if ( versionElem.hasAttribute( QStringLiteral( "version" ) ) )
425  {
426  version = versionElem.attribute( QStringLiteral( "version" ) );
427  QRegularExpression re( "([\\d]+)\\.([\\d]+)" );
428  QRegularExpressionMatch match = re.match( version );
429  if ( match.hasMatch() )
430  {
431  if ( ( match.captured( 1 ).toInt() <= 3 ) && ( match.captured( 2 ).toInt() <= 12 ) )
432  before3_14 = true;
433  }
434  }
435  if ( before3_14 )
436  {
437  // BEFORE 3.12:
438  // 1 = vertex
439  // 2 = vertexandsegment
440  // 3 = segment
441  switch ( type )
442  {
443  case 1:
444  mType = VertexFlag;
445  break;
446  case 2:
447  mType = static_cast<QgsSnappingConfig::SnappingTypeFlag>( QgsSnappingConfig::VertexFlag | QgsSnappingConfig::SegmentFlag );
448  break;
449  case 3:
450  mType = SegmentFlag;
451  break;
452  default:
453  mType = NoSnapFlag;
454  break;
455  }
456  }
457  else
458  mType = static_cast<QgsSnappingConfig::SnappingTypeFlag>( type );
459  }
460 
461  if ( snapSettingsElem.hasAttribute( QStringLiteral( "tolerance" ) ) )
462  mTolerance = snapSettingsElem.attribute( QStringLiteral( "tolerance" ) ).toDouble();
463 
464  if ( snapSettingsElem.hasAttribute( QStringLiteral( "scaleDependencyMode" ) ) )
465  mScaleDependencyMode = static_cast<QgsSnappingConfig::ScaleDependencyMode>( snapSettingsElem.attribute( QStringLiteral( "scaleDependencyMode" ) ).toInt() );
466 
467  if ( snapSettingsElem.hasAttribute( QStringLiteral( "minScale" ) ) )
468  mMinimumScale = snapSettingsElem.attribute( QStringLiteral( "minScale" ) ).toDouble();
469 
470  if ( snapSettingsElem.hasAttribute( QStringLiteral( "maxScale" ) ) )
471  mMaximumScale = snapSettingsElem.attribute( QStringLiteral( "maxScale" ) ).toDouble();
472 
473  if ( snapSettingsElem.hasAttribute( QStringLiteral( "unit" ) ) )
474  mUnits = ( QgsTolerance::UnitType )snapSettingsElem.attribute( QStringLiteral( "unit" ) ).toInt();
475 
476  if ( snapSettingsElem.hasAttribute( QStringLiteral( "intersection-snapping" ) ) )
477  mIntersectionSnapping = snapSettingsElem.attribute( QStringLiteral( "intersection-snapping" ) ) == QLatin1String( "1" );
478 
479  if ( snapSettingsElem.hasAttribute( QStringLiteral( "self-snapping" ) ) )
480  mSelfSnapping = snapSettingsElem.attribute( QStringLiteral( "self-snapping" ) ) == QLatin1String( "1" );
481 
482  // do not clear the settings as they must be automatically synchronized with current layers
483  QDomNodeList nodes = snapSettingsElem.elementsByTagName( QStringLiteral( "individual-layer-settings" ) );
484  if ( nodes.count() )
485  {
486  QDomNode node = nodes.item( 0 );
487  QDomNodeList settingNodes = node.childNodes();
488  int layerCount = settingNodes.count();
489  for ( int i = 0; i < layerCount; ++i )
490  {
491  QDomElement settingElement = settingNodes.at( i ).toElement();
492  if ( settingElement.tagName() != QLatin1String( "layer-setting" ) )
493  {
494  QgsLogger::warning( QApplication::translate( "QgsProjectSnappingSettings", "Cannot read individual settings. Unexpected tag '%1'" ).arg( settingElement.tagName() ) );
495  continue;
496  }
497 
498  QString layerId = settingElement.attribute( QStringLiteral( "id" ) );
499  bool enabled = settingElement.attribute( QStringLiteral( "enabled" ) ) == QLatin1String( "1" );
500  QgsSnappingConfig::SnappingTypeFlag type = static_cast<QgsSnappingConfig::SnappingTypeFlag>( settingElement.attribute( QStringLiteral( "type" ) ).toInt() );
501  double tolerance = settingElement.attribute( QStringLiteral( "tolerance" ) ).toDouble();
502  QgsTolerance::UnitType units = ( QgsTolerance::UnitType )settingElement.attribute( QStringLiteral( "units" ) ).toInt();
503  double minScale = settingElement.attribute( QStringLiteral( "minScale" ) ).toDouble();
504  double maxScale = settingElement.attribute( QStringLiteral( "maxScale" ) ).toDouble();
505 
506  QgsMapLayer *ml = mProject->mapLayer( layerId );
507  if ( !ml || ml->type() != QgsMapLayerType::VectorLayer )
508  continue;
509 
510  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
511 
512  IndividualLayerSettings setting = IndividualLayerSettings( enabled, type, tolerance, units, minScale, maxScale );
513  mIndividualLayerSettings.insert( vl, setting );
514  }
515  }
516 }
517 
518 void QgsSnappingConfig::writeProject( QDomDocument &doc )
519 {
520  QDomElement snapSettingsElem = doc.createElement( QStringLiteral( "snapping-settings" ) );
521  snapSettingsElem.setAttribute( QStringLiteral( "enabled" ), QString::number( mEnabled ) );
522  snapSettingsElem.setAttribute( QStringLiteral( "mode" ), static_cast<int>( mMode ) );
523  snapSettingsElem.setAttribute( QStringLiteral( "type" ), static_cast<int>( mType ) );
524  snapSettingsElem.setAttribute( QStringLiteral( "tolerance" ), mTolerance );
525  snapSettingsElem.setAttribute( QStringLiteral( "unit" ), static_cast<int>( mUnits ) );
526  snapSettingsElem.setAttribute( QStringLiteral( "intersection-snapping" ), QString::number( mIntersectionSnapping ) );
527  snapSettingsElem.setAttribute( QStringLiteral( "self-snapping" ), QString::number( mSelfSnapping ) );
528  snapSettingsElem.setAttribute( QStringLiteral( "scaleDependencyMode" ), QString::number( mScaleDependencyMode ) );
529  snapSettingsElem.setAttribute( QStringLiteral( "minScale" ), mMinimumScale );
530  snapSettingsElem.setAttribute( QStringLiteral( "maxScale" ), mMaximumScale );
531 
532  QDomElement ilsElement = doc.createElement( QStringLiteral( "individual-layer-settings" ) );
533  QHash<QgsVectorLayer *, IndividualLayerSettings>::const_iterator layerIt = mIndividualLayerSettings.constBegin();
534  for ( ; layerIt != mIndividualLayerSettings.constEnd(); ++layerIt )
535  {
536  const IndividualLayerSettings &setting = layerIt.value();
537 
538  QDomElement layerElement = doc.createElement( QStringLiteral( "layer-setting" ) );
539  layerElement.setAttribute( QStringLiteral( "id" ), layerIt.key()->id() );
540  layerElement.setAttribute( QStringLiteral( "enabled" ), QString::number( setting.enabled() ) );
541  layerElement.setAttribute( QStringLiteral( "type" ), static_cast<int>( setting.typeFlag() ) );
542  layerElement.setAttribute( QStringLiteral( "tolerance" ), setting.tolerance() );
543  layerElement.setAttribute( QStringLiteral( "units" ), static_cast<int>( setting.units() ) );
544  layerElement.setAttribute( QStringLiteral( "minScale" ), setting.minimumScale() );
545  layerElement.setAttribute( QStringLiteral( "maxScale" ), setting.maximumScale() );
546  ilsElement.appendChild( layerElement );
547  }
548  snapSettingsElem.appendChild( ilsElement );
549 
550  doc.firstChildElement( QStringLiteral( "qgis" ) ).appendChild( snapSettingsElem );
551 }
552 
553 bool QgsSnappingConfig::addLayers( const QList<QgsMapLayer *> &layers )
554 {
555  bool changed = false;
556  bool enabled = QgsSettings().value( QStringLiteral( "/qgis/digitizing/default_snap_enabled" ), true ).toBool();
557  QgsSnappingConfig::SnappingTypeFlag type = QgsSettings().enumValue( QStringLiteral( "/qgis/digitizing/default_snap_type" ), VertexFlag );
558  double tolerance = QgsSettings().value( QStringLiteral( "/qgis/digitizing/default_snapping_tolerance" ), Qgis::DEFAULT_SNAP_TOLERANCE ).toDouble();
559  QgsTolerance::UnitType units = QgsSettings().enumValue( QStringLiteral( "/qgis/digitizing/default_snapping_tolerance_unit" ), Qgis::DEFAULT_SNAP_UNITS );
560 
561  const auto constLayers = layers;
562  for ( QgsMapLayer *ml : constLayers )
563  {
564  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
565  if ( vl && vl->isSpatial() )
566  {
567  mIndividualLayerSettings.insert( vl, IndividualLayerSettings( enabled, type, tolerance, units, 0.0, 0.0 ) );
568  changed = true;
569  }
570  }
571  return changed;
572 }
573 
574 bool QgsSnappingConfig::removeLayers( const QList<QgsMapLayer *> &layers )
575 {
576  bool changed = false;
577  const auto constLayers = layers;
578  for ( QgsMapLayer *ml : constLayers )
579  {
580  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
581  if ( vl )
582  {
583  mIndividualLayerSettings.remove( vl );
584  changed = true;
585  }
586  }
587  return changed;
588 }
589 
590 void QgsSnappingConfig::readLegacySettings()
591 {
592  //
593  mMode = ActiveLayer;
594 
595  QString snapMode = mProject->readEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/SnappingMode" ) );
596 
597  mTolerance = mProject->readDoubleEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/DefaultSnapTolerance" ), 0 );
598  mUnits = static_cast< QgsTolerance::UnitType >( mProject->readNumEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/DefaultSnapToleranceUnit" ), QgsTolerance::ProjectUnits ) );
599 
600  mIntersectionSnapping = mProject->readNumEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/IntersectionSnapping" ), 0 );
601 
602  //read snapping settings from project
603  QStringList layerIdList = mProject->readListEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/LayerSnappingList" ), QStringList() );
604  QStringList enabledList = mProject->readListEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/LayerSnappingEnabledList" ), QStringList() );
605  QStringList toleranceList = mProject->readListEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/LayerSnappingToleranceList" ), QStringList() );
606  QStringList toleranceUnitList = mProject->readListEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/LayerSnappingToleranceUnitList" ), QStringList() );
607  QStringList snapToList = mProject->readListEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/LayerSnapToList" ), QStringList() );
608 
609  // lists must have the same size, otherwise something is wrong
610  if ( layerIdList.size() != enabledList.size() ||
611  layerIdList.size() != toleranceList.size() ||
612  layerIdList.size() != toleranceUnitList.size() ||
613  layerIdList.size() != snapToList.size() )
614  return;
615 
616  // Use snapping information from the project
617  if ( snapMode == QLatin1String( "current_layer" ) )
618  mMode = ActiveLayer;
619  else if ( snapMode == QLatin1String( "all_layers" ) )
620  mMode = AllLayers;
621  else // either "advanced" or empty (for background compatibility)
622  mMode = AdvancedConfiguration;
623 
624  // load layers, tolerances, snap type
625  QStringList::const_iterator layerIt( layerIdList.constBegin() );
626  QStringList::const_iterator tolIt( toleranceList.constBegin() );
627  QStringList::const_iterator tolUnitIt( toleranceUnitList.constBegin() );
628  QStringList::const_iterator snapIt( snapToList.constBegin() );
629  QStringList::const_iterator enabledIt( enabledList.constBegin() );
630  for ( ; layerIt != layerIdList.constEnd(); ++layerIt, ++tolIt, ++tolUnitIt, ++snapIt, ++enabledIt )
631  {
632  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mProject->mapLayer( *layerIt ) );
633  if ( !vlayer || !vlayer->isSpatial() )
634  continue;
635 
636  QgsSnappingConfig::SnappingTypeFlag t( *snapIt == QLatin1String( "to_vertex" ) ? VertexFlag :
637  ( *snapIt == QLatin1String( "to_segment" ) ? SegmentFlag :
638  static_cast<QgsSnappingConfig::SnappingTypeFlag>( QgsSnappingConfig::VertexFlag | QgsSnappingConfig::SegmentFlag )
639  )
640  );
641 
642  mIndividualLayerSettings.insert( vlayer, IndividualLayerSettings( *enabledIt == QLatin1String( "enabled" ), t, tolIt->toDouble(), static_cast<QgsTolerance::UnitType>( tolUnitIt->toInt() ), 0.0, 0.0 ) );
643  }
644 
645  QString snapType = mProject->readEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/DefaultSnapType" ), QStringLiteral( "off" ) );
646  mEnabled = true;
647  if ( snapType == QLatin1String( "to segment" ) )
648  mType = SegmentFlag;
649  else if ( snapType == QLatin1String( "to vertex and segment" ) )
650  mType = static_cast<QgsSnappingConfig::SnappingTypeFlag>( QgsSnappingConfig::VertexFlag | QgsSnappingConfig::SegmentFlag );
651  else if ( snapType == QLatin1String( "to vertex" ) )
652  mType = VertexFlag;
653  else if ( mMode != AdvancedConfiguration ) // Type is off but mode is advanced
654  {
655  mEnabled = false;
656  }
657 }
658 
660 {
661  return mProject;
662 }
663 
665 {
666  if ( mProject != project )
667  mProject = project;
668 
669  reset();
670 }
671 
673 {
674  return mMinimumScale;
675 }
676 
677 void QgsSnappingConfig::setMinimumScale( double minScale )
678 {
679  mMinimumScale = minScale;
680 }
681 
683 {
684  return mMaximumScale;
685 }
686 
687 void QgsSnappingConfig::setMaximumScale( double maxScale )
688 {
689  mMaximumScale = maxScale;
690 }
691 
693 {
694  mScaleDependencyMode = mode;
695 }
696 
698 {
699  return mScaleDependencyMode;
700 }
701 
702 
703 
704 
705 
QgsSnappingConfig::readProject
void readProject(const QDomDocument &doc)
Reads the configuration from the specified QGIS project document.
Definition: qgssnappingconfig.cpp:403
QgsSnappingConfig::SnappingType
SnappingType
SnappingType defines on what object the snapping is performed.
Definition: qgssnappingconfig.h:57
QgsSnappingConfig::IndividualLayerSettings::setType
Q_DECL_DEPRECATED void setType(QgsSnappingConfig::SnappingType type)
define the type of snapping
Definition: qgssnappingconfig.cpp:81
QgsSnappingConfig::IndividualLayerSettings::enabled
bool enabled() const
Returns if snapping is enabled.
Definition: qgssnappingconfig.cpp:54
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsSnappingConfig::setIntersectionSnapping
void setIntersectionSnapping(bool enabled)
Sets if the snapping on intersection is enabled.
Definition: qgssnappingconfig.cpp:343
QgsSnappingConfig::IndividualLayerSettings::setTolerance
void setTolerance(double tolerance)
Sets the tolerance.
Definition: qgssnappingconfig.cpp:109
QgsSnappingConfig::IndividualLayerSettings::setUnits
void setUnits(QgsTolerance::UnitType units)
Sets the type of units.
Definition: qgssnappingconfig.cpp:119
QgsMapLayerType::VectorLayer
@ VectorLayer
QgsSnappingConfig::setTolerance
void setTolerance(double tolerance)
Sets the tolerance.
Definition: qgssnappingconfig.cpp:315
QgsProject::mapLayers
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
Definition: qgsproject.cpp:3436
QgsSnappingConfig::IndividualLayerSettings::setTypeFlag
void setTypeFlag(QgsSnappingConfig::SnappingTypeFlag type)
define the type of snapping
Definition: qgssnappingconfig.cpp:99
Qgis::DEFAULT_SNAP_TOLERANCE
static const double DEFAULT_SNAP_TOLERANCE
Default snapping distance tolerance.
Definition: qgis.h:188
QgsSnappingConfig::IndividualLayerSettings::valid
bool valid() const
Returns if settings are valid.
Definition: qgssnappingconfig.cpp:49
QgsSnappingConfig::setType
Q_DECL_DEPRECATED void setType(QgsSnappingConfig::SnappingType type)
define the type of snapping
Definition: qgssnappingconfig.cpp:283
QgsSnappingConfig::IndividualLayerSettings::tolerance
double tolerance() const
Returns the tolerance.
Definition: qgssnappingconfig.cpp:104
QgsVectorLayer::isSpatial
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
Definition: qgsvectorlayer.cpp:3599
QgsTolerance::ProjectUnits
@ ProjectUnits
Map (project) units. Added in 2.8.
Definition: qgstolerance.h:48
QgsSnappingConfig::setSelfSnapping
void setSelfSnapping(bool enabled)
Sets if self snapping (snapping to the currently digitized feature) is enabled.
Definition: qgssnappingconfig.cpp:353
QgsSnappingConfig::setMode
void setMode(SnappingMode mode)
define the mode of snapping
Definition: qgssnappingconfig.cpp:259
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QgsSnappingConfig::SegmentFlag
@ SegmentFlag
On segments.
Definition: qgssnappingconfig.h:74
QgsProject::readEntry
QString readEntry(const QString &scope, const QString &key, const QString &def=QString(), bool *ok=nullptr) const
Reads a string from the specified scope and key.
Definition: qgsproject.cpp:2526
QgsSnappingConfig::SnappingMode
SnappingMode
SnappingMode defines on which layer the snapping is performed.
Definition: qgssnappingconfig.h:45
QgsProject::mapLayer
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Definition: qgsproject.cpp:3208
QgsSnappingConfig::IndividualLayerSettings::setMaximumScale
void setMaximumScale(double maxScale)
Sets the max scale value on which snapping is used, 0.0 disable scale limit.
Definition: qgssnappingconfig.cpp:139
QgsSnappingConfig::setMinimumScale
void setMinimumScale(double minScale)
Sets the min scale on which snapping is enabled, 0.0 disable scale limit.
Definition: qgssnappingconfig.cpp:677
QgsProject
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition: qgsproject.h:95
qgssnappingconfig.h
qgsapplication.h
QgsSnappingConfig::QgsSnappingConfig
QgsSnappingConfig(QgsProject *project=nullptr)
Constructor with default parameters defined in global settings.
Definition: qgssnappingconfig.cpp:167
QgsSnappingConfig::IndividualLayerSettings::operator!=
bool operator!=(const QgsSnappingConfig::IndividualLayerSettings &other) const
Compare this configuration to other.
Definition: qgssnappingconfig.cpp:144
Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:797
QgsSnappingConfig::setTypeFlag
void setTypeFlag(QgsSnappingConfig::SnappingTypeFlag type)
define the type of snapping
Definition: qgssnappingconfig.cpp:301
QgsSnappingConfig::ActiveLayer
@ ActiveLayer
On the active layer.
Definition: qgssnappingconfig.h:46
QgsSnappingConfig::writeProject
void writeProject(QDomDocument &doc)
Writes the configuration to the specified QGIS project document.
Definition: qgssnappingconfig.cpp:518
QgsSnappingConfig::minimumScale
double minimumScale() const
Returns the min scale (i.e.
Definition: qgssnappingconfig.cpp:672
QgsSnappingConfig::removeLayers
bool removeLayers(const QList< QgsMapLayer * > &layers)
Removes the specified layers from the individual layer configuration.
Definition: qgssnappingconfig.cpp:574
QgsSnappingConfig::clearIndividualLayerSettings
void clearIndividualLayerSettings()
Removes all individual layer snapping settings.
Definition: qgssnappingconfig.cpp:376
QgsSnappingConfig::tolerance
double tolerance() const
Returns the tolerance.
Definition: qgssnappingconfig.cpp:310
QgsSnappingConfig::IndividualLayerSettings::setMinimumScale
void setMinimumScale(double minScale)
Sets the min scale value on which snapping is used, 0.0 disable scale limit.
Definition: qgssnappingconfig.cpp:129
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::scaleDependencyMode
ScaleDependencyMode scaleDependencyMode() const
Returns the scale dependency mode.
Definition: qgssnappingconfig.cpp:697
QgsSnappingConfig::selfSnapping
bool selfSnapping() const
Returns if self snapping (snapping to the currently digitized feature) is enabled.
Definition: qgssnappingconfig.cpp:348
QgsSettings::flagValue
T flagValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on a flag.
Definition: qgssettings.h:330
QgsSnappingConfig::VertexFlag
@ VertexFlag
On vertices.
Definition: qgssnappingconfig.h:73
QgsSnappingConfig::setScaleDependencyMode
void setScaleDependencyMode(ScaleDependencyMode mode)
Set the scale dependency mode.
Definition: qgssnappingconfig.cpp:692
QgsSnappingConfig::IndividualLayerSettings::setEnabled
void setEnabled(bool enabled)
enables the snapping
Definition: qgssnappingconfig.cpp:59
QgsSnappingConfig::operator==
bool operator==(const QgsSnappingConfig &other) const
Definition: qgssnappingconfig.cpp:174
QgsTolerance::LayerUnits
@ LayerUnits
Layer unit value.
Definition: qgstolerance.h:44
QgsLogger::warning
static void warning(const QString &msg)
Goes to qWarning.
Definition: qgslogger.cpp:122
QgsSnappingConfig::setEnabled
void setEnabled(bool enabled)
enables the snapping
Definition: qgssnappingconfig.cpp:245
QgsSnappingConfig
This is a container for configuration of the snapping of the project.
Definition: qgssnappingconfig.h:34
QgsSnappingConfig::AdvancedConfiguration
@ AdvancedConfiguration
On a per layer configuration basis.
Definition: qgssnappingconfig.h:48
QgsProject::readDoubleEntry
double readDoubleEntry(const QString &scope, const QString &key, double def=0, bool *ok=nullptr) const
Reads a double from the specified scope and key.
Definition: qgsproject.cpp:2579
QgsSnappingConfig::setUnits
void setUnits(QgsTolerance::UnitType units)
Sets the type of units.
Definition: qgssnappingconfig.cpp:329
QgsSnappingConfig::IndividualLayerSettings
This is a container of advanced configuration (per layer) of the snapping of the project.
Definition: qgssnappingconfig.h:122
QgsSnappingConfig::typeFlag
QgsSnappingConfig::SnappingTypeFlag typeFlag() const
Returns the flags type (vertices | segments | area | centroid | middle)
Definition: qgssnappingconfig.cpp:268
QgsSnappingConfig::setIndividualLayerSettings
void setIndividualLayerSettings(QgsVectorLayer *vl, const QgsSnappingConfig::IndividualLayerSettings &individualLayerSettings)
Sets individual layer snappings settings (applied if mode is AdvancedConfiguration)
Definition: qgssnappingconfig.cpp:381
qgsvectorlayer.h
QgsSnappingConfig::type
Q_DECL_DEPRECATED QgsSnappingConfig::SnappingType type() const
Returns the flags type (vertices | segments | area | centroid | middle)
Definition: qgssnappingconfig.cpp:273
QgsSnappingConfig::mode
SnappingMode mode() const
Returns the mode (all layers, active layer, per layer settings)
Definition: qgssnappingconfig.cpp:254
QgsSnappingConfig::units
QgsTolerance::UnitType units() const
Returns the type of units.
Definition: qgssnappingconfig.cpp:324
QgsSnappingConfig::project
Q_GADGET QgsProject * project
Definition: qgssnappingconfig.h:37
QgsSnappingConfig::NoSnapFlag
@ NoSnapFlag
No snapping.
Definition: qgssnappingconfig.h:72
QgsSnappingConfig::IndividualLayerSettings::operator==
bool operator==(const QgsSnappingConfig::IndividualLayerSettings &other) const
Definition: qgssnappingconfig.cpp:155
QgsSnappingConfig::addLayers
bool addLayers(const QList< QgsMapLayer * > &layers)
Adds the specified layers as individual layers to the configuration with standard configuration.
Definition: qgssnappingconfig.cpp:553
QgsSnappingConfig::setProject
void setProject(QgsProject *project)
The project from which the snapped layers should be retrieved.
Definition: qgssnappingconfig.cpp:664
QgsSnappingConfig::individualLayerSettings
QHash< QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings > individualLayerSettings() const
Returns individual snapping settings for all layers.
Definition: qgssnappingconfig.cpp:358
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsSnappingConfig::IndividualLayerSettings::typeFlag
QgsSnappingConfig::SnappingTypeFlag typeFlag() const
Returns the flags type (vertices | segments | area | centroid | middle)
Definition: qgssnappingconfig.cpp:64
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsSnappingConfig::operator!=
bool operator!=(const QgsSnappingConfig &other) const
Compare this configuration to other.
Definition: qgssnappingconfig.cpp:390
qgssettings.h
QgsSnappingConfig::intersectionSnapping
bool intersectionSnapping() const
Returns if the snapping on intersection is enabled.
Definition: qgssnappingconfig.cpp:338
QgsSnappingConfig::maximumScale
double maximumScale() const
Returns the max scale (i.e.
Definition: qgssnappingconfig.cpp:682
QgsSnappingConfig::IndividualLayerSettings::type
Q_DECL_DEPRECATED QgsSnappingConfig::SnappingType type() const
Returns the flags type (vertices | segments | area | centroid | middle)
Definition: qgssnappingconfig.cpp:69
QgsSnappingConfig::IndividualLayerSettings::minimumScale
double minimumScale() const
Returns minimum scale on which snapping is limited.
Definition: qgssnappingconfig.cpp:124
QgsSnappingConfig::AllLayers
@ AllLayers
On all vector layers.
Definition: qgssnappingconfig.h:47
QgsSnappingConfig::ScaleDependencyMode
ScaleDependencyMode
ScaleDependencyMode the scale dependency mode of snapping.
Definition: qgssnappingconfig.h:88
QgsSnappingConfig::Disabled
@ Disabled
No scale dependency.
Definition: qgssnappingconfig.h:89
QgsSettings::enumValue
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
Definition: qgssettings.h:252
QgsProject::readListEntry
QStringList readListEntry(const QString &scope, const QString &key, const QStringList &def=QStringList(), bool *ok=nullptr) const
Reads a string list from the specified scope and key.
Definition: qgsproject.cpp:2496
qgslogger.h
QgsSnappingConfig::IndividualLayerSettings::maximumScale
double maximumScale() const
Returns max scale on which snapping is limited.
Definition: qgssnappingconfig.cpp:134
Q_NOWARN_DEPRECATED_PUSH
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:796
Qgis::DEFAULT_SNAP_UNITS
static const QgsTolerance::UnitType DEFAULT_SNAP_UNITS
Default snapping distance units.
Definition: qgis.h:194
QgsSnappingConfig::setMaximumScale
void setMaximumScale(double maxScale)
Set the max scale on which snapping is enabled, 0.0 disable scale limit.
Definition: qgssnappingconfig.cpp:687
qgsproject.h
QgsProject::readNumEntry
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=nullptr) const
Reads an integer from the specified scope and key.
Definition: qgsproject.cpp:2552
QgsSnappingConfig::reset
void reset()
reset to default values
Definition: qgssnappingconfig.cpp:189
QgsSnappingConfig::enabled
bool enabled() const
Returns if snapping is enabled.
Definition: qgssnappingconfig.cpp:240
QgsSnappingConfig::IndividualLayerSettings::units
QgsTolerance::UnitType units() const
Returns the type of units.
Definition: qgssnappingconfig.cpp:114
QgsMapLayer::type
QgsMapLayerType type
Definition: qgsmaplayer.h:90