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