18 #include <QDomElement>
19 #include <QHeaderView>
32 , mTolerance( tolerance )
34 , mMinimumScale( minScale )
35 , mMaximumScale( maxScale )
73 return QgsSnappingConfig::SnappingType::VertexAndSegment;
75 return QgsSnappingConfig::SnappingType::Segment;
77 return QgsSnappingConfig::SnappingType::Vertex;
126 return mMinimumScale;
131 mMinimumScale = minScale;
136 return mMaximumScale;
141 mMaximumScale = maxScale;
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;
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;
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;
192 bool enabled =
QgsSettings().
value( QStringLiteral(
"/qgis/digitizing/default_snap_enabled" ),
false ).toBool();
221 mIntersectionSnapping =
false;
222 mSelfSnapping =
false;
227 mIndividualLayerSettings = QHash<QgsVectorLayer *, IndividualLayerSettings>();
228 const auto constMapLayers = mProject->
mapLayers();
276 return QgsSnappingConfig::SnappingType::VertexAndSegment;
278 return QgsSnappingConfig::SnappingType::Segment;
280 return QgsSnappingConfig::SnappingType::Vertex;
287 case SnappingType::Vertex:
290 case SnappingType::VertexAndSegment:
293 case SnappingType::Segment:
331 if ( mUnits ==
units )
340 return mIntersectionSnapping;
345 mIntersectionSnapping =
enabled;
350 return mSelfSnapping;
360 return mIndividualLayerSettings;
365 if ( vl && mIndividualLayerSettings.contains( vl ) )
367 return mIndividualLayerSettings.value( vl );
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;
400 QDomElement snapSettingsElem = doc.firstChildElement( QStringLiteral(
"qgis" ) ).firstChildElement( QStringLiteral(
"snapping-settings" ) );
401 if ( snapSettingsElem.isNull() )
403 readLegacySettings();
407 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"enabled" ) ) )
408 mEnabled = snapSettingsElem.attribute( QStringLiteral(
"enabled" ) ) == QLatin1String(
"1" );
410 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"mode" ) ) )
411 mMode = (
SnappingMode )snapSettingsElem.attribute( QStringLiteral(
"mode" ) ).toInt();
413 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"type" ) ) )
415 int type = snapSettingsElem.attribute( QStringLiteral(
"type" ) ).toInt();
416 QDomElement versionElem = doc.firstChildElement( QStringLiteral(
"qgis" ) );
418 bool before3_14 =
false;
419 if ( versionElem.hasAttribute( QStringLiteral(
"version" ) ) )
421 version = versionElem.attribute( QStringLiteral(
"version" ) );
422 QRegularExpression re(
"([\\d]+)\\.([\\d]+)" );
423 QRegularExpressionMatch match = re.match( version );
424 if ( match.hasMatch() )
426 if ( ( match.captured( 1 ).toInt() <= 3 ) && ( match.captured( 2 ).toInt() <= 12 ) )
453 mType =
static_cast<QgsSnappingConfig::SnappingTypeFlag
>(
type );
456 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"tolerance" ) ) )
457 mTolerance = snapSettingsElem.attribute( QStringLiteral(
"tolerance" ) ).toDouble();
459 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"scaleDependencyMode" ) ) )
462 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"minScale" ) ) )
463 mMinimumScale = snapSettingsElem.attribute( QStringLiteral(
"minScale" ) ).toDouble();
465 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"maxScale" ) ) )
466 mMaximumScale = snapSettingsElem.attribute( QStringLiteral(
"maxScale" ) ).toDouble();
468 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"unit" ) ) )
471 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"intersection-snapping" ) ) )
472 mIntersectionSnapping = snapSettingsElem.attribute( QStringLiteral(
"intersection-snapping" ) ) == QLatin1String(
"1" );
474 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"self-snapping" ) ) )
475 mSelfSnapping = snapSettingsElem.attribute( QStringLiteral(
"self-snapping" ) ) == QLatin1String(
"1" );
478 QDomNodeList nodes = snapSettingsElem.elementsByTagName( QStringLiteral(
"individual-layer-settings" ) );
481 QDomNode node = nodes.item( 0 );
482 QDomNodeList settingNodes = node.childNodes();
483 int layerCount = settingNodes.count();
484 for (
int i = 0; i < layerCount; ++i )
486 QDomElement settingElement = settingNodes.at( i ).toElement();
487 if ( settingElement.tagName() != QLatin1String(
"layer-setting" ) )
489 QgsLogger::warning( QApplication::translate(
"QgsProjectSnappingSettings",
"Cannot read individual settings. Unexpected tag '%1'" ).arg( settingElement.tagName() ) );
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();
498 double minScale = settingElement.attribute( QStringLiteral(
"minScale" ) ).toDouble();
499 double maxScale = settingElement.attribute( QStringLiteral(
"maxScale" ) ).toDouble();
508 mIndividualLayerSettings.insert( vl, setting );
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 );
527 QDomElement ilsElement = doc.createElement( QStringLiteral(
"individual-layer-settings" ) );
528 QHash<QgsVectorLayer *, IndividualLayerSettings>::const_iterator layerIt = mIndividualLayerSettings.constBegin();
529 for ( ; layerIt != mIndividualLayerSettings.constEnd(); ++layerIt )
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 );
543 snapSettingsElem.appendChild( ilsElement );
545 doc.firstChildElement( QStringLiteral(
"qgis" ) ).appendChild( snapSettingsElem );
550 bool changed =
false;
551 bool enabled =
QgsSettings().
value( QStringLiteral(
"/qgis/digitizing/default_snap_enabled" ),
true ).toBool();
556 const auto constLayers = layers;
571 bool changed =
false;
572 const auto constLayers = layers;
578 mIndividualLayerSettings.remove( vl );
585 void QgsSnappingConfig::readLegacySettings()
590 QString snapMode = mProject->
readEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/SnappingMode" ) );
592 mTolerance = mProject->
readDoubleEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/DefaultSnapTolerance" ), 0 );
595 mIntersectionSnapping = mProject->
readNumEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/IntersectionSnapping" ), 0 );
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() );
605 if ( layerIdList.size() != enabledList.size() ||
606 layerIdList.size() != toleranceList.size() ||
607 layerIdList.size() != toleranceUnitList.size() ||
608 layerIdList.size() != snapToList.size() )
612 if ( snapMode == QLatin1String(
"current_layer" ) )
614 else if ( snapMode == QLatin1String(
"all_layers" ) )
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 )
631 QgsSnappingConfig::SnappingTypeFlag t( *snapIt == QLatin1String(
"to_vertex" ) ?
VertexFlag :
632 ( *snapIt == QLatin1String(
"to_segment" ) ?
SegmentFlag :
637 mIndividualLayerSettings.insert( vlayer, IndividualLayerSettings( *enabledIt == QLatin1String(
"enabled" ), t, tolIt->toDouble(),
static_cast<QgsTolerance::UnitType>( tolUnitIt->toInt() ), 0.0, 0.0 ) );
640 QString snapType = mProject->
readEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/DefaultSnapType" ), QStringLiteral(
"off" ) );
642 if ( snapType == QLatin1String(
"to segment" ) )
644 else if ( snapType == QLatin1String(
"to vertex and segment" ) )
646 else if ( snapType == QLatin1String(
"to vertex" ) )
669 return mMinimumScale;
674 mMinimumScale = minScale;
679 return mMaximumScale;
684 mMaximumScale = maxScale;
689 mScaleDependencyMode =
mode;
694 return mScaleDependencyMode;