27#include <QRegularExpression>
30#include "moc_qgssnappingconfig.cpp"
32using namespace Qt::StringLiterals;
40 , mMinimumScale( minScale )
41 , mMaximumScale( maxScale )
130 return mMinimumScale;
135 mMinimumScale = minScale;
140 return mMaximumScale;
145 mMaximumScale = maxScale;
150 return mValid != other.mValid
151 || mEnabled != other.mEnabled
152 || mType != other.mType
153 || mTolerance != other.mTolerance
154 || mUnits != other.mUnits
155 || mMinimumScale != other.mMinimumScale
156 || mMaximumScale != other.mMaximumScale;
161 return mValid == other.mValid
162 && mEnabled == other.mEnabled
163 && mType == other.mType
164 && mTolerance == other.mTolerance
165 && mUnits == other.mUnits
166 && mMinimumScale == other.mMinimumScale
167 && mMaximumScale == other.mMaximumScale;
179 return mEnabled == other.mEnabled
180 && mMode == other.mMode
181 && mType == other.mType
182 && mTolerance == other.mTolerance
183 && mUnits == other.mUnits
184 && mIntersectionSnapping == other.mIntersectionSnapping
185 && mSelfSnapping == other.mSelfSnapping
186 && mIndividualLayerSettings == other.mIndividualLayerSettings
187 && mScaleDependencyMode == other.mScaleDependencyMode
188 && mMinimumScale == other.mMinimumScale
189 && mMaximumScale == other.mMaximumScale;
218 mIntersectionSnapping =
false;
219 mSelfSnapping =
false;
224 mIndividualLayerSettings = QHash<QgsVectorLayer *, IndividualLayerSettings>();
225 const auto constMapLayers = mProject->mapLayers();
285 return QObject::tr(
"No Snapping" );
287 return QObject::tr(
"Vertex" );
289 return QObject::tr(
"Segment" );
291 return QObject::tr(
"Area" );
293 return QObject::tr(
"Centroid" );
295 return QObject::tr(
"Middle of Segments" );
297 return QObject::tr(
"Line Endpoints" );
372 if ( mUnits ==
units )
381 return mIntersectionSnapping;
386 mIntersectionSnapping =
enabled;
391 return mSelfSnapping;
401 return mIndividualLayerSettings;
406 if ( vl && mIndividualLayerSettings.contains( vl ) )
408 return mIndividualLayerSettings.value( vl );
419 mIndividualLayerSettings.clear();
433 return mEnabled != other.mEnabled
434 || mMode != other.mMode
435 || mType != other.mType
436 || mTolerance != other.mTolerance
437 || mUnits != other.mUnits
438 || mIndividualLayerSettings != other.mIndividualLayerSettings
439 || mScaleDependencyMode != other.mScaleDependencyMode
440 || mMinimumScale != other.mMinimumScale
441 || mMaximumScale != other.mMaximumScale;
446 const QDomElement snapSettingsElem = doc.firstChildElement( u
"qgis"_s ).firstChildElement( u
"snapping-settings"_s );
447 if ( snapSettingsElem.isNull() )
449 readLegacySettings();
453 if ( snapSettingsElem.hasAttribute( u
"enabled"_s ) )
454 mEnabled = snapSettingsElem.attribute( u
"enabled"_s ) ==
"1"_L1;
456 if ( snapSettingsElem.hasAttribute( u
"mode"_s ) )
457 mMode =
static_cast< Qgis::SnappingMode >( snapSettingsElem.attribute( u
"mode"_s ).toInt() );
459 if ( snapSettingsElem.hasAttribute( u
"type"_s ) )
461 const int type = snapSettingsElem.attribute( u
"type"_s ).toInt();
462 const QDomElement versionElem = doc.firstChildElement( u
"qgis"_s );
464 bool before3_14 =
false;
465 if ( versionElem.hasAttribute( u
"version"_s ) )
467 version = versionElem.attribute( u
"version"_s );
468 const thread_local QRegularExpression re( u
"([\\d]+)\\.([\\d]+)"_s );
469 const QRegularExpressionMatch match = re.match( version );
470 if ( match.hasMatch() )
472 if ( ( match.captured( 1 ).toInt() <= 3 ) && ( match.captured( 2 ).toInt() <= 12 ) )
502 if ( snapSettingsElem.hasAttribute( u
"tolerance"_s ) )
503 mTolerance = snapSettingsElem.attribute( u
"tolerance"_s ).toDouble();
505 if ( snapSettingsElem.hasAttribute( u
"scaleDependencyMode"_s ) )
508 if ( snapSettingsElem.hasAttribute( u
"minScale"_s ) )
509 mMinimumScale = snapSettingsElem.attribute( u
"minScale"_s ).toDouble();
511 if ( snapSettingsElem.hasAttribute( u
"maxScale"_s ) )
512 mMaximumScale = snapSettingsElem.attribute( u
"maxScale"_s ).toDouble();
514 if ( snapSettingsElem.hasAttribute( u
"unit"_s ) )
515 mUnits =
static_cast< Qgis::MapToolUnit >( snapSettingsElem.attribute( u
"unit"_s ).toInt() );
517 if ( snapSettingsElem.hasAttribute( u
"intersection-snapping"_s ) )
518 mIntersectionSnapping = snapSettingsElem.attribute( u
"intersection-snapping"_s ) ==
"1"_L1;
520 if ( snapSettingsElem.hasAttribute( u
"self-snapping"_s ) )
521 mSelfSnapping = snapSettingsElem.attribute( u
"self-snapping"_s ) ==
"1"_L1;
524 const QDomNodeList nodes = snapSettingsElem.elementsByTagName( u
"individual-layer-settings"_s );
527 const QDomNode node = nodes.item( 0 );
528 const QDomNodeList settingNodes = node.childNodes();
529 const int layerCount = settingNodes.count();
530 for (
int i = 0; i < layerCount; ++i )
532 const QDomElement settingElement = settingNodes.at( i ).toElement();
533 if ( settingElement.tagName() !=
"layer-setting"_L1 )
535 QgsLogger::warning( QApplication::translate(
"QgsProjectSnappingSettings",
"Cannot read individual settings. Unexpected tag '%1'" ).arg( settingElement.tagName() ) );
539 const QString layerId = settingElement.attribute( u
"id"_s );
540 const bool enabled = settingElement.attribute( u
"enabled"_s ) ==
"1"_L1;
542 const double tolerance = settingElement.attribute( u
"tolerance"_s ).toDouble();
544 const double minScale = settingElement.attribute( u
"minScale"_s ).toDouble();
545 const double maxScale = settingElement.attribute( u
"maxScale"_s ).toDouble();
554 mIndividualLayerSettings.insert( vl, setting );
561 QDomElement snapSettingsElem = doc.createElement( u
"snapping-settings"_s );
562 snapSettingsElem.setAttribute( u
"enabled"_s, QString::number( mEnabled ) );
563 snapSettingsElem.setAttribute( u
"mode"_s,
static_cast<int>( mMode ) );
564 snapSettingsElem.setAttribute( u
"type"_s,
static_cast<int>( mType ) );
565 snapSettingsElem.setAttribute( u
"tolerance"_s, mTolerance );
566 snapSettingsElem.setAttribute( u
"unit"_s,
static_cast<int>( mUnits ) );
567 snapSettingsElem.setAttribute( u
"intersection-snapping"_s, QString::number( mIntersectionSnapping ) );
568 snapSettingsElem.setAttribute( u
"self-snapping"_s, QString::number( mSelfSnapping ) );
569 snapSettingsElem.setAttribute( u
"scaleDependencyMode"_s, QString::number( mScaleDependencyMode ) );
570 snapSettingsElem.setAttribute( u
"minScale"_s, mMinimumScale );
571 snapSettingsElem.setAttribute( u
"maxScale"_s, mMaximumScale );
573 QDomElement ilsElement = doc.createElement( u
"individual-layer-settings"_s );
574 QHash<QgsVectorLayer *, IndividualLayerSettings>::const_iterator layerIt = mIndividualLayerSettings.constBegin();
575 for ( ; layerIt != mIndividualLayerSettings.constEnd(); ++layerIt )
579 QDomElement layerElement = doc.createElement( u
"layer-setting"_s );
580 layerElement.setAttribute( u
"id"_s, layerIt.key()->id() );
581 layerElement.setAttribute( u
"enabled"_s, QString::number( setting.
enabled() ) );
582 layerElement.setAttribute( u
"type"_s,
static_cast<int>( setting.
typeFlag() ) );
583 layerElement.setAttribute( u
"tolerance"_s, setting.
tolerance() );
584 layerElement.setAttribute( u
"units"_s,
static_cast<int>( setting.
units() ) );
585 layerElement.setAttribute( u
"minScale"_s, setting.
minimumScale() );
586 layerElement.setAttribute( u
"maxScale"_s, setting.
maximumScale() );
587 ilsElement.appendChild( layerElement );
589 snapSettingsElem.appendChild( ilsElement );
591 doc.firstChildElement( u
"qgis"_s ).appendChild( snapSettingsElem );
596 bool changed =
false;
602 const auto constLayers = layers;
617 bool changed =
false;
618 const auto constLayers = layers;
624 mIndividualLayerSettings.remove( vl );
631void QgsSnappingConfig::readLegacySettings()
636 const QString snapMode = mProject->
readEntry( u
"Digitizing"_s, u
"/SnappingMode"_s );
638 mTolerance = mProject->
readDoubleEntry( u
"Digitizing"_s, u
"/DefaultSnapTolerance"_s, 0 );
641 mIntersectionSnapping = mProject->
readNumEntry( u
"Digitizing"_s, u
"/IntersectionSnapping"_s, 0 );
644 const QStringList layerIdList = mProject->
readListEntry( u
"Digitizing"_s, u
"/LayerSnappingList"_s, QStringList() );
645 const QStringList enabledList = mProject->
readListEntry( u
"Digitizing"_s, u
"/LayerSnappingEnabledList"_s, QStringList() );
646 const QStringList toleranceList = mProject->
readListEntry( u
"Digitizing"_s, u
"/LayerSnappingToleranceList"_s, QStringList() );
647 const QStringList toleranceUnitList = mProject->
readListEntry( u
"Digitizing"_s, u
"/LayerSnappingToleranceUnitList"_s, QStringList() );
648 const QStringList snapToList = mProject->
readListEntry( u
"Digitizing"_s, u
"/LayerSnapToList"_s, QStringList() );
651 if ( layerIdList.size() != enabledList.size() || layerIdList.size() != toleranceList.size() || layerIdList.size() != toleranceUnitList.size() || layerIdList.size() != snapToList.size() )
655 if ( snapMode ==
"current_layer"_L1 )
657 else if ( snapMode ==
"all_layers"_L1 )
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 )
679 mIndividualLayerSettings.insert( vlayer, IndividualLayerSettings( *enabledIt ==
"enabled"_L1, t, tolIt->toDouble(),
static_cast<Qgis::MapToolUnit>( tolUnitIt->toInt() ), 0.0, 0.0 ) );
682 const QString snapType = mProject->readEntry( u
"Digitizing"_s, u
"/DefaultSnapType"_s, u
"off"_s );
684 if ( snapType ==
"to segment"_L1 )
686 else if ( snapType ==
"to vertex and segment"_L1 )
688 else if ( snapType ==
"to vertex"_L1 )
713 return mMinimumScale;
718 mMinimumScale = minScale;
723 return mMaximumScale;
728 mMaximumScale = maxScale;
733 mScaleDependencyMode =
mode;
738 return mScaleDependencyMode;
MapToolUnit
Type of unit of tolerance value from settings.
@ Project
Map (project) units.
SnappingType
SnappingTypeFlag defines on what object the snapping is performed.
@ LineEndpoint
Start or end points of lines, or first vertex in polygon rings only.
@ MiddleOfSegment
On Middle segment.
QFlags< SnappingType > SnappingTypes
Snapping types.
SnappingMode
SnappingMode defines on which layer the snapping is performed.
@ ActiveLayer
On the active layer.
@ AdvancedConfiguration
On a per layer configuration basis.
@ AllLayers
On all vector layers.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static void warning(const QString &msg)
Goes to qWarning.
Base class for all map layer types.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=nullptr) const
Reads an integer from the specified scope and key.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
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.
double readDoubleEntry(const QString &scope, const QString &key, double def=0, bool *ok=nullptr) const
Reads a double from the specified scope and key.
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.
static const QgsSettingsEntryBool * settingsDigitizingDefaultSnapEnabled
Settings entry digitizing default snap enabled.
static const QgsSettingsEntryEnumFlag< Qgis::MapToolUnit > * settingsDigitizingDefaultSnappingToleranceUnit
Settings entry digitizing default snapping tolerance unit.
static const QgsSettingsEntryDouble * settingsDigitizingDefaultSnappingTolerance
Settings entry digitizing default snapping tolerance.
static const QgsSettingsEntryEnumFlag< Qgis::SnappingMode > * settingsDigitizingDefaultSnapMode
Settings entry digitizing default snap type.
static const QgsSettingsEntryEnumFlag< Qgis::SnappingType > * settingsDigitizingDefaultSnapType
Settings entry digitizing default snap type.
A container of advanced configuration (per layer) of the snapping of the project.
double tolerance() const
Returns the tolerance.
void setEnabled(bool enabled)
enables the snapping
bool operator!=(const QgsSnappingConfig::IndividualLayerSettings &other) const
void setTypeFlag(Qgis::SnappingTypes type)
define the type of snapping
bool valid() const
Returns if settings are valid.
IndividualLayerSettings()=default
Constructs an invalid setting.
double maximumScale() const
Returns max scale on which snapping is limited.
void setMinimumScale(double minScale)
Sets the min scale value on which snapping is used, 0.0 disable scale limit.
Qgis::MapToolUnit units() const
Returns the type of units.
void setMaximumScale(double maxScale)
Sets the max scale value on which snapping is used, 0.0 disable scale limit.
Q_DECL_DEPRECATED void setType(SnappingType type)
define the type of snapping
bool enabled() const
Returns if snapping is enabled.
bool operator==(const QgsSnappingConfig::IndividualLayerSettings &other) const
Q_DECL_DEPRECATED QgsSnappingConfig::SnappingType type() const
Returns the flags type (vertices | segments | area | centroid | middle).
double minimumScale() const
Returns minimum scale on which snapping is limited.
void setUnits(Qgis::MapToolUnit units)
Sets the type of units.
Qgis::SnappingTypes typeFlag() const
Returns the flags type (vertices | segments | area | centroid | middle).
void setTolerance(double tolerance)
Sets the tolerance.
bool addLayers(const QList< QgsMapLayer * > &layers)
Adds the specified layers as individual layers to the configuration with standard configuration.
void readProject(const QDomDocument &doc)
Reads the configuration from the specified QGIS project document.
void setScaleDependencyMode(ScaleDependencyMode mode)
Set the scale dependency mode.
QgsSnappingConfig(QgsProject *project=nullptr)
Constructor with default parameters defined in global settings.
void setMinimumScale(double minScale)
Sets the min scale on which snapping is enabled, 0.0 disable scale limit.
void setUnits(Qgis::MapToolUnit units)
Sets the type of units.
bool intersectionSnapping() const
Returns if the snapping on intersection is enabled.
ScaleDependencyMode
ScaleDependencyMode the scale dependency mode of snapping.
@ Disabled
No scale dependency.
void setTypeFlag(Qgis::SnappingTypes type)
define the type of snapping
double minimumScale() const
Returns the min scale (i.e.
double tolerance() const
Returns the tolerance.
void setMode(Qgis::SnappingMode mode)
define the mode of snapping
static QIcon snappingTypeToIcon(Qgis::SnappingType type)
Convenient method to return an icon corresponding to the enum type Qgis::SnappingTypes.
void reset()
reset to default values
Q_DECL_DEPRECATED void setType(QgsSnappingConfig::SnappingType type)
define the type of snapping
void writeProject(QDomDocument &doc)
Writes the configuration to the specified QGIS project document.
void setMaximumScale(double maxScale)
Set the max scale on which snapping is enabled, 0.0 disable scale limit.
bool selfSnapping() const
Returns if self snapping (snapping to the currently digitized feature) is enabled.
void setTolerance(double tolerance)
Sets the tolerance.
Qgis::SnappingTypes typeFlag() const
Returns the flags type (vertices | segments | area | centroid | middle).
void clearIndividualLayerSettings()
Removes all individual layer snapping settings.
Q_DECL_DEPRECATED QgsSnappingConfig::SnappingType type() const
Returns the flags type (vertices | segments | area | centroid | middle).
double maximumScale() const
Returns the max scale (i.e.
void setProject(QgsProject *project)
The project from which the snapped layers should be retrieved.
static QString snappingTypeToString(Qgis::SnappingType type)
Convenient method to returns the translated name of the enum type Qgis::SnappingTypes.
Qgis::MapToolUnit units() const
Returns the type of units.
bool operator!=(const QgsSnappingConfig &other) const
bool operator==(const QgsSnappingConfig &other) const
SnappingType
SnappingType defines on what object the snapping is performed.
@ VertexAndSegment
Both on vertices and segments.
@ Vertex
On vertices only.
@ Segment
On segments only.
void setSelfSnapping(bool enabled)
Sets if self snapping (snapping to the currently digitized feature) is enabled.
QHash< QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings > individualLayerSettings() const
Returns individual snapping settings for all layers.
bool removeLayers(const QList< QgsMapLayer * > &layers)
Removes the specified layers from the individual layer configuration.
ScaleDependencyMode scaleDependencyMode() const
Returns the scale dependency mode.
void setEnabled(bool enabled)
enables the snapping
void setIndividualLayerSettings(QgsVectorLayer *vl, const QgsSnappingConfig::IndividualLayerSettings &individualLayerSettings)
Sets individual layer snappings settings (applied if mode is AdvancedConfiguration).
void setIntersectionSnapping(bool enabled)
Sets if the snapping on intersection is enabled.
Represents a vector layer which manages a vector based dataset.
bool isSpatial() const final
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH