QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  if ( !vl || !vl->isSpatial() || mIndividualLayerSettings.value( vl ) == individualLayerSettings )
379  {
380  return;
381  }
382  mIndividualLayerSettings.insert( vl, individualLayerSettings );
383 }
384 
386 {
387  return mEnabled != other.mEnabled
388  || mMode != other.mMode
389  || mType != other.mType
390  || mTolerance != other.mTolerance
391  || mUnits != other.mUnits
392  || mIndividualLayerSettings != other.mIndividualLayerSettings
393  || mScaleDependencyMode != other.mScaleDependencyMode
394  || mMinimumScale != other.mMinimumScale
395  || mMaximumScale != other.mMaximumScale;
396 }
397 
398 void QgsSnappingConfig::readProject( const QDomDocument &doc )
399 {
400  QDomElement snapSettingsElem = doc.firstChildElement( QStringLiteral( "qgis" ) ).firstChildElement( QStringLiteral( "snapping-settings" ) );
401  if ( snapSettingsElem.isNull() )
402  {
403  readLegacySettings();
404  return;
405  }
406 
407  if ( snapSettingsElem.hasAttribute( QStringLiteral( "enabled" ) ) )
408  mEnabled = snapSettingsElem.attribute( QStringLiteral( "enabled" ) ) == QLatin1String( "1" );
409 
410  if ( snapSettingsElem.hasAttribute( QStringLiteral( "mode" ) ) )
411  mMode = ( SnappingMode )snapSettingsElem.attribute( QStringLiteral( "mode" ) ).toInt();
412 
413  if ( snapSettingsElem.hasAttribute( QStringLiteral( "type" ) ) )
414  {
415  int type = snapSettingsElem.attribute( QStringLiteral( "type" ) ).toInt();
416  QDomElement versionElem = doc.firstChildElement( QStringLiteral( "qgis" ) );
417  QString version;
418  bool before3_14 = false;
419  if ( versionElem.hasAttribute( QStringLiteral( "version" ) ) )
420  {
421  version = versionElem.attribute( QStringLiteral( "version" ) );
422  QRegularExpression re( "([\\d]+)\\.([\\d]+)" );
423  QRegularExpressionMatch match = re.match( version );
424  if ( match.hasMatch() )
425  {
426  if ( ( match.captured( 1 ).toInt() <= 3 ) && ( match.captured( 2 ).toInt() <= 12 ) )
427  before3_14 = true;
428  }
429  }
430  if ( before3_14 )
431  {
432  // BEFORE 3.12:
433  // 1 = vertex
434  // 2 = vertexandsegment
435  // 3 = segment
436  switch ( type )
437  {
438  case 1:
439  mType = VertexFlag;
440  break;
441  case 2:
442  mType = static_cast<QgsSnappingConfig::SnappingTypeFlag>( QgsSnappingConfig::VertexFlag | QgsSnappingConfig::SegmentFlag );
443  break;
444  case 3:
445  mType = SegmentFlag;
446  break;
447  default:
448  mType = NoSnapFlag;
449  break;
450  }
451  }
452  else
453  mType = static_cast<QgsSnappingConfig::SnappingTypeFlag>( type );
454  }
455 
456  if ( snapSettingsElem.hasAttribute( QStringLiteral( "tolerance" ) ) )
457  mTolerance = snapSettingsElem.attribute( QStringLiteral( "tolerance" ) ).toDouble();
458 
459  if ( snapSettingsElem.hasAttribute( QStringLiteral( "scaleDependencyMode" ) ) )
460  mScaleDependencyMode = static_cast<QgsSnappingConfig::ScaleDependencyMode>( snapSettingsElem.attribute( QStringLiteral( "scaleDependencyMode" ) ).toInt() );
461 
462  if ( snapSettingsElem.hasAttribute( QStringLiteral( "minScale" ) ) )
463  mMinimumScale = snapSettingsElem.attribute( QStringLiteral( "minScale" ) ).toDouble();
464 
465  if ( snapSettingsElem.hasAttribute( QStringLiteral( "maxScale" ) ) )
466  mMaximumScale = snapSettingsElem.attribute( QStringLiteral( "maxScale" ) ).toDouble();
467 
468  if ( snapSettingsElem.hasAttribute( QStringLiteral( "unit" ) ) )
469  mUnits = ( QgsTolerance::UnitType )snapSettingsElem.attribute( QStringLiteral( "unit" ) ).toInt();
470 
471  if ( snapSettingsElem.hasAttribute( QStringLiteral( "intersection-snapping" ) ) )
472  mIntersectionSnapping = snapSettingsElem.attribute( QStringLiteral( "intersection-snapping" ) ) == QLatin1String( "1" );
473 
474  if ( snapSettingsElem.hasAttribute( QStringLiteral( "self-snapping" ) ) )
475  mSelfSnapping = snapSettingsElem.attribute( QStringLiteral( "self-snapping" ) ) == QLatin1String( "1" );
476 
477  // do not clear the settings as they must be automatically synchronized with current layers
478  QDomNodeList nodes = snapSettingsElem.elementsByTagName( QStringLiteral( "individual-layer-settings" ) );
479  if ( nodes.count() )
480  {
481  QDomNode node = nodes.item( 0 );
482  QDomNodeList settingNodes = node.childNodes();
483  int layerCount = settingNodes.count();
484  for ( int i = 0; i < layerCount; ++i )
485  {
486  QDomElement settingElement = settingNodes.at( i ).toElement();
487  if ( settingElement.tagName() != QLatin1String( "layer-setting" ) )
488  {
489  QgsLogger::warning( QApplication::translate( "QgsProjectSnappingSettings", "Cannot read individual settings. Unexpected tag '%1'" ).arg( settingElement.tagName() ) );
490  continue;
491  }
492 
493  QString layerId = settingElement.attribute( QStringLiteral( "id" ) );
494  bool enabled = settingElement.attribute( QStringLiteral( "enabled" ) ) == QLatin1String( "1" );
495  QgsSnappingConfig::SnappingTypeFlag type = static_cast<QgsSnappingConfig::SnappingTypeFlag>( settingElement.attribute( QStringLiteral( "type" ) ).toInt() );
496  double tolerance = settingElement.attribute( QStringLiteral( "tolerance" ) ).toDouble();
497  QgsTolerance::UnitType units = ( QgsTolerance::UnitType )settingElement.attribute( QStringLiteral( "units" ) ).toInt();
498  double minScale = settingElement.attribute( QStringLiteral( "minScale" ) ).toDouble();
499  double maxScale = settingElement.attribute( QStringLiteral( "maxScale" ) ).toDouble();
500 
501  QgsMapLayer *ml = mProject->mapLayer( layerId );
502  if ( !ml || ml->type() != QgsMapLayerType::VectorLayer )
503  continue;
504 
505  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
506 
507  IndividualLayerSettings setting = IndividualLayerSettings( enabled, type, tolerance, units, minScale, maxScale );
508  mIndividualLayerSettings.insert( vl, setting );
509  }
510  }
511 }
512 
513 void QgsSnappingConfig::writeProject( QDomDocument &doc )
514 {
515  QDomElement snapSettingsElem = doc.createElement( QStringLiteral( "snapping-settings" ) );
516  snapSettingsElem.setAttribute( QStringLiteral( "enabled" ), QString::number( mEnabled ) );
517  snapSettingsElem.setAttribute( QStringLiteral( "mode" ), static_cast<int>( mMode ) );
518  snapSettingsElem.setAttribute( QStringLiteral( "type" ), static_cast<int>( mType ) );
519  snapSettingsElem.setAttribute( QStringLiteral( "tolerance" ), mTolerance );
520  snapSettingsElem.setAttribute( QStringLiteral( "unit" ), static_cast<int>( mUnits ) );
521  snapSettingsElem.setAttribute( QStringLiteral( "intersection-snapping" ), QString::number( mIntersectionSnapping ) );
522  snapSettingsElem.setAttribute( QStringLiteral( "self-snapping" ), QString::number( mSelfSnapping ) );
523  snapSettingsElem.setAttribute( QStringLiteral( "scaleDependencyMode" ), QString::number( mScaleDependencyMode ) );
524  snapSettingsElem.setAttribute( QStringLiteral( "minScale" ), mMinimumScale );
525  snapSettingsElem.setAttribute( QStringLiteral( "maxScale" ), mMaximumScale );
526 
527  QDomElement ilsElement = doc.createElement( QStringLiteral( "individual-layer-settings" ) );
528  QHash<QgsVectorLayer *, IndividualLayerSettings>::const_iterator layerIt = mIndividualLayerSettings.constBegin();
529  for ( ; layerIt != mIndividualLayerSettings.constEnd(); ++layerIt )
530  {
531  const IndividualLayerSettings &setting = layerIt.value();
532 
533  QDomElement layerElement = doc.createElement( QStringLiteral( "layer-setting" ) );
534  layerElement.setAttribute( QStringLiteral( "id" ), layerIt.key()->id() );
535  layerElement.setAttribute( QStringLiteral( "enabled" ), QString::number( setting.enabled() ) );
536  layerElement.setAttribute( QStringLiteral( "type" ), static_cast<int>( setting.typeFlag() ) );
537  layerElement.setAttribute( QStringLiteral( "tolerance" ), setting.tolerance() );
538  layerElement.setAttribute( QStringLiteral( "units" ), static_cast<int>( setting.units() ) );
539  layerElement.setAttribute( QStringLiteral( "minScale" ), setting.minimumScale() );
540  layerElement.setAttribute( QStringLiteral( "maxScale" ), setting.maximumScale() );
541  ilsElement.appendChild( layerElement );
542  }
543  snapSettingsElem.appendChild( ilsElement );
544 
545  doc.firstChildElement( QStringLiteral( "qgis" ) ).appendChild( snapSettingsElem );
546 }
547 
548 bool QgsSnappingConfig::addLayers( const QList<QgsMapLayer *> &layers )
549 {
550  bool changed = false;
551  bool enabled = QgsSettings().value( QStringLiteral( "/qgis/digitizing/default_snap_enabled" ), true ).toBool();
552  QgsSnappingConfig::SnappingTypeFlag type = QgsSettings().enumValue( QStringLiteral( "/qgis/digitizing/default_snap_type" ), VertexFlag );
553  double tolerance = QgsSettings().value( QStringLiteral( "/qgis/digitizing/default_snapping_tolerance" ), Qgis::DEFAULT_SNAP_TOLERANCE ).toDouble();
554  QgsTolerance::UnitType units = QgsSettings().enumValue( QStringLiteral( "/qgis/digitizing/default_snapping_tolerance_unit" ), Qgis::DEFAULT_SNAP_UNITS );
555 
556  const auto constLayers = layers;
557  for ( QgsMapLayer *ml : constLayers )
558  {
559  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
560  if ( vl && vl->isSpatial() )
561  {
562  mIndividualLayerSettings.insert( vl, IndividualLayerSettings( enabled, type, tolerance, units, 0.0, 0.0 ) );
563  changed = true;
564  }
565  }
566  return changed;
567 }
568 
569 bool QgsSnappingConfig::removeLayers( const QList<QgsMapLayer *> &layers )
570 {
571  bool changed = false;
572  const auto constLayers = layers;
573  for ( QgsMapLayer *ml : constLayers )
574  {
575  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
576  if ( vl )
577  {
578  mIndividualLayerSettings.remove( vl );
579  changed = true;
580  }
581  }
582  return changed;
583 }
584 
585 void QgsSnappingConfig::readLegacySettings()
586 {
587  //
588  mMode = ActiveLayer;
589 
590  QString snapMode = mProject->readEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/SnappingMode" ) );
591 
592  mTolerance = mProject->readDoubleEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/DefaultSnapTolerance" ), 0 );
593  mUnits = static_cast< QgsTolerance::UnitType >( mProject->readNumEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/DefaultSnapToleranceUnit" ), QgsTolerance::ProjectUnits ) );
594 
595  mIntersectionSnapping = mProject->readNumEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/IntersectionSnapping" ), 0 );
596 
597  //read snapping settings from project
598  QStringList layerIdList = mProject->readListEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/LayerSnappingList" ), QStringList() );
599  QStringList enabledList = mProject->readListEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/LayerSnappingEnabledList" ), QStringList() );
600  QStringList toleranceList = mProject->readListEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/LayerSnappingToleranceList" ), QStringList() );
601  QStringList toleranceUnitList = mProject->readListEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/LayerSnappingToleranceUnitList" ), QStringList() );
602  QStringList snapToList = mProject->readListEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/LayerSnapToList" ), QStringList() );
603 
604  // lists must have the same size, otherwise something is wrong
605  if ( layerIdList.size() != enabledList.size() ||
606  layerIdList.size() != toleranceList.size() ||
607  layerIdList.size() != toleranceUnitList.size() ||
608  layerIdList.size() != snapToList.size() )
609  return;
610 
611  // Use snapping information from the project
612  if ( snapMode == QLatin1String( "current_layer" ) )
613  mMode = ActiveLayer;
614  else if ( snapMode == QLatin1String( "all_layers" ) )
615  mMode = AllLayers;
616  else // either "advanced" or empty (for background compatibility)
617  mMode = AdvancedConfiguration;
618 
619  // load layers, tolerances, snap type
620  QStringList::const_iterator layerIt( layerIdList.constBegin() );
621  QStringList::const_iterator tolIt( toleranceList.constBegin() );
622  QStringList::const_iterator tolUnitIt( toleranceUnitList.constBegin() );
623  QStringList::const_iterator snapIt( snapToList.constBegin() );
624  QStringList::const_iterator enabledIt( enabledList.constBegin() );
625  for ( ; layerIt != layerIdList.constEnd(); ++layerIt, ++tolIt, ++tolUnitIt, ++snapIt, ++enabledIt )
626  {
627  QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mProject->mapLayer( *layerIt ) );
628  if ( !vlayer || !vlayer->isSpatial() )
629  continue;
630 
631  QgsSnappingConfig::SnappingTypeFlag t( *snapIt == QLatin1String( "to_vertex" ) ? VertexFlag :
632  ( *snapIt == QLatin1String( "to_segment" ) ? SegmentFlag :
633  static_cast<QgsSnappingConfig::SnappingTypeFlag>( QgsSnappingConfig::VertexFlag | QgsSnappingConfig::SegmentFlag )
634  )
635  );
636 
637  mIndividualLayerSettings.insert( vlayer, IndividualLayerSettings( *enabledIt == QLatin1String( "enabled" ), t, tolIt->toDouble(), static_cast<QgsTolerance::UnitType>( tolUnitIt->toInt() ), 0.0, 0.0 ) );
638  }
639 
640  QString snapType = mProject->readEntry( QStringLiteral( "Digitizing" ), QStringLiteral( "/DefaultSnapType" ), QStringLiteral( "off" ) );
641  mEnabled = true;
642  if ( snapType == QLatin1String( "to segment" ) )
643  mType = SegmentFlag;
644  else if ( snapType == QLatin1String( "to vertex and segment" ) )
645  mType = static_cast<QgsSnappingConfig::SnappingTypeFlag>( QgsSnappingConfig::VertexFlag | QgsSnappingConfig::SegmentFlag );
646  else if ( snapType == QLatin1String( "to vertex" ) )
647  mType = VertexFlag;
648  else if ( mMode != AdvancedConfiguration ) // Type is off but mode is advanced
649  {
650  mEnabled = false;
651  }
652 }
653 
655 {
656  return mProject;
657 }
658 
660 {
661  if ( mProject != project )
662  mProject = project;
663 
664  reset();
665 }
666 
668 {
669  return mMinimumScale;
670 }
671 
672 void QgsSnappingConfig::setMinimumScale( double minScale )
673 {
674  mMinimumScale = minScale;
675 }
676 
678 {
679  return mMaximumScale;
680 }
681 
682 void QgsSnappingConfig::setMaximumScale( double maxScale )
683 {
684  mMaximumScale = maxScale;
685 }
686 
688 {
689  mScaleDependencyMode = mode;
690 }
691 
693 {
694  return mScaleDependencyMode;
695 }
696 
697 
698 
699 
700 
QgsSnappingConfig::readProject
void readProject(const QDomDocument &doc)
Reads the configuration from the specified QGIS project document.
Definition: qgssnappingconfig.cpp:398
QgsSnappingConfig::SnappingType
SnappingType
SnappingType defines on what object the snapping is performed.
Definition: qgssnappingconfig.h:56
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:3347
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:201
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:3591
QgsTolerance::ProjectUnits
@ ProjectUnits
Map (project) units. Added in 2.8.
Definition: qgstolerance.h:47
QgsSnappingConfig::setSelfSnapping
void setSelfSnapping(bool enabled)
Sets if self snapping (snapping to the currently digitised feature) is enabled.
Definition: qgssnappingconfig.cpp:353
QgsSnappingConfig::setMode
void setMode(SnappingMode mode)
define the mode of snapping
Definition: qgssnappingconfig.cpp:259
QgsSettings
Definition: qgssettings.h:61
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
Definition: qgsproject.cpp:2448
QgsSnappingConfig::SnappingMode
SnappingMode
SnappingMode defines on which layer the snapping is performed.
Definition: qgssnappingconfig.h:44
QgsProject::mapLayer
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
Definition: qgsproject.cpp:3124
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:672
QgsProject
Definition: qgsproject.h:92
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:752
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:513
QgsSnappingConfig::minimumScale
double minimumScale() const
Returns the min scale (i.e.
Definition: qgssnappingconfig.cpp:667
QgsSnappingConfig::removeLayers
bool removeLayers(const QList< QgsMapLayer * > &layers)
Removes the specified layers from the individual layer configuration.
Definition: qgssnappingconfig.cpp:569
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:40
QgsSnappingConfig::IndividualLayerSettings::IndividualLayerSettings
IndividualLayerSettings()=default
Constructs an invalid setting.
QgsSnappingConfig::scaleDependencyMode
ScaleDependencyMode scaleDependencyMode() const
Returns the scale dependency mode.
Definition: qgssnappingconfig.cpp:692
QgsSnappingConfig::selfSnapping
bool selfSnapping() const
Returns if self snapping (snapping to the currently digitised 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:687
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:43
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
Definition: qgssnappingconfig.h:33
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
Definition: qgsproject.cpp:2499
QgsSnappingConfig::setUnits
void setUnits(QgsTolerance::UnitType units)
Sets the type of units.
Definition: qgssnappingconfig.cpp:329
QgsSnappingConfig::IndividualLayerSettings
Definition: qgssnappingconfig.h:121
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:376
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::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:548
QgsSnappingConfig::setProject
void setProject(QgsProject *project)
The project from which the snapped layers should be retrieved.
Definition: qgssnappingconfig.cpp:659
QgsSnappingConfig::individualLayerSettings
QHash< QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings > individualLayerSettings() const
Returns individual snapping settings for all layers.
Definition: qgssnappingconfig.cpp:358
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsSnappingConfig::IndividualLayerSettings::typeFlag
QgsSnappingConfig::SnappingTypeFlag typeFlag() const
Returns the flags type (vertices | segments | area | centroid | middle)
Definition: qgssnappingconfig.cpp:64
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsSnappingConfig::project
QgsProject project
Definition: qgssnappingconfig.h:37
QgsSnappingConfig::operator!=
bool operator!=(const QgsSnappingConfig &other) const
Compare this configuration to other.
Definition: qgssnappingconfig.cpp:385
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:677
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:87
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
Key value accessors.
Definition: qgsproject.cpp:2421
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:751
Qgis::DEFAULT_SNAP_UNITS
static const QgsTolerance::UnitType DEFAULT_SNAP_UNITS
Default snapping distance units.
Definition: qgis.h:207
QgsMapLayer::type
QgsMapLayerType type() const
Returns the type of the layer.
Definition: qgsmaplayer.cpp:129
QgsSnappingConfig::setMaximumScale
void setMaximumScale(double maxScale)
Set the max scale on which snapping is enabled, 0.0 disable scale limit.
Definition: qgssnappingconfig.cpp:682
qgsproject.h
QgsProject::readNumEntry
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=nullptr) const
Definition: qgsproject.cpp:2472
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