18 #include <QDomElement>
19 #include <QHeaderView>
20 #include <QRegularExpression>
33 , mTolerance( tolerance )
35 , mMinimumScale( minScale )
36 , mMaximumScale( maxScale )
74 return QgsSnappingConfig::SnappingType::VertexAndSegment;
76 return QgsSnappingConfig::SnappingType::Segment;
78 return QgsSnappingConfig::SnappingType::Vertex;
127 return mMinimumScale;
132 mMinimumScale = minScale;
137 return mMaximumScale;
142 mMaximumScale = maxScale;
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;
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;
177 return mEnabled == other.mEnabled
178 && mMode == other.mMode
179 && mType == other.mType
180 && mTolerance == other.mTolerance
181 && mUnits == other.mUnits
182 && mIntersectionSnapping == other.mIntersectionSnapping
183 && mSelfSnapping == other.mSelfSnapping
184 && mIndividualLayerSettings == other.mIndividualLayerSettings
185 && mScaleDependencyMode == other.mScaleDependencyMode
186 && mMinimumScale == other.mMinimumScale
187 && mMaximumScale == other.mMaximumScale;
193 bool enabled =
QgsSettings().
value( QStringLiteral(
"/qgis/digitizing/default_snap_enabled" ),
false ).toBool();
222 mIntersectionSnapping =
false;
223 mSelfSnapping =
false;
228 mIndividualLayerSettings = QHash<QgsVectorLayer *, IndividualLayerSettings>();
229 const auto constMapLayers = mProject->
mapLayers();
277 return QgsSnappingConfig::SnappingType::VertexAndSegment;
279 return QgsSnappingConfig::SnappingType::Segment;
281 return QgsSnappingConfig::SnappingType::Vertex;
288 case SnappingType::Vertex:
291 case SnappingType::VertexAndSegment:
294 case SnappingType::Segment:
332 if ( mUnits ==
units )
341 return mIntersectionSnapping;
346 mIntersectionSnapping =
enabled;
351 return mSelfSnapping;
361 return mIndividualLayerSettings;
366 if ( vl && mIndividualLayerSettings.contains( vl ) )
368 return mIndividualLayerSettings.value( vl );
379 mIndividualLayerSettings.clear();
393 return mEnabled != other.mEnabled
394 || mMode != other.mMode
395 || mType != other.mType
396 || mTolerance != other.mTolerance
397 || mUnits != other.mUnits
398 || mIndividualLayerSettings != other.mIndividualLayerSettings
399 || mScaleDependencyMode != other.mScaleDependencyMode
400 || mMinimumScale != other.mMinimumScale
401 || mMaximumScale != other.mMaximumScale;
406 QDomElement snapSettingsElem = doc.firstChildElement( QStringLiteral(
"qgis" ) ).firstChildElement( QStringLiteral(
"snapping-settings" ) );
407 if ( snapSettingsElem.isNull() )
409 readLegacySettings();
413 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"enabled" ) ) )
414 mEnabled = snapSettingsElem.attribute( QStringLiteral(
"enabled" ) ) == QLatin1String(
"1" );
416 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"mode" ) ) )
417 mMode = (
SnappingMode )snapSettingsElem.attribute( QStringLiteral(
"mode" ) ).toInt();
419 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"type" ) ) )
421 int type = snapSettingsElem.attribute( QStringLiteral(
"type" ) ).toInt();
422 QDomElement versionElem = doc.firstChildElement( QStringLiteral(
"qgis" ) );
424 bool before3_14 =
false;
425 if ( versionElem.hasAttribute( QStringLiteral(
"version" ) ) )
427 version = versionElem.attribute( QStringLiteral(
"version" ) );
428 QRegularExpression re(
"([\\d]+)\\.([\\d]+)" );
429 QRegularExpressionMatch match = re.match( version );
430 if ( match.hasMatch() )
432 if ( ( match.captured( 1 ).toInt() <= 3 ) && ( match.captured( 2 ).toInt() <= 12 ) )
459 mType =
static_cast<QgsSnappingConfig::SnappingTypeFlag
>(
type );
462 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"tolerance" ) ) )
463 mTolerance = snapSettingsElem.attribute( QStringLiteral(
"tolerance" ) ).toDouble();
465 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"scaleDependencyMode" ) ) )
468 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"minScale" ) ) )
469 mMinimumScale = snapSettingsElem.attribute( QStringLiteral(
"minScale" ) ).toDouble();
471 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"maxScale" ) ) )
472 mMaximumScale = snapSettingsElem.attribute( QStringLiteral(
"maxScale" ) ).toDouble();
474 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"unit" ) ) )
477 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"intersection-snapping" ) ) )
478 mIntersectionSnapping = snapSettingsElem.attribute( QStringLiteral(
"intersection-snapping" ) ) == QLatin1String(
"1" );
480 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"self-snapping" ) ) )
481 mSelfSnapping = snapSettingsElem.attribute( QStringLiteral(
"self-snapping" ) ) == QLatin1String(
"1" );
484 QDomNodeList nodes = snapSettingsElem.elementsByTagName( QStringLiteral(
"individual-layer-settings" ) );
487 QDomNode node = nodes.item( 0 );
488 QDomNodeList settingNodes = node.childNodes();
489 int layerCount = settingNodes.count();
490 for (
int i = 0; i < layerCount; ++i )
492 QDomElement settingElement = settingNodes.at( i ).toElement();
493 if ( settingElement.tagName() != QLatin1String(
"layer-setting" ) )
495 QgsLogger::warning( QApplication::translate(
"QgsProjectSnappingSettings",
"Cannot read individual settings. Unexpected tag '%1'" ).arg( settingElement.tagName() ) );
499 QString layerId = settingElement.attribute( QStringLiteral(
"id" ) );
500 bool enabled = settingElement.attribute( QStringLiteral(
"enabled" ) ) == QLatin1String(
"1" );
501 QgsSnappingConfig::SnappingTypeFlag
type =
static_cast<QgsSnappingConfig::SnappingTypeFlag
>( settingElement.attribute( QStringLiteral(
"type" ) ).toInt() );
502 double tolerance = settingElement.attribute( QStringLiteral(
"tolerance" ) ).toDouble();
504 double minScale = settingElement.attribute( QStringLiteral(
"minScale" ) ).toDouble();
505 double maxScale = settingElement.attribute( QStringLiteral(
"maxScale" ) ).toDouble();
514 mIndividualLayerSettings.insert( vl, setting );
521 QDomElement snapSettingsElem = doc.createElement( QStringLiteral(
"snapping-settings" ) );
522 snapSettingsElem.setAttribute( QStringLiteral(
"enabled" ), QString::number( mEnabled ) );
523 snapSettingsElem.setAttribute( QStringLiteral(
"mode" ),
static_cast<int>( mMode ) );
524 snapSettingsElem.setAttribute( QStringLiteral(
"type" ),
static_cast<int>( mType ) );
525 snapSettingsElem.setAttribute( QStringLiteral(
"tolerance" ), mTolerance );
526 snapSettingsElem.setAttribute( QStringLiteral(
"unit" ),
static_cast<int>( mUnits ) );
527 snapSettingsElem.setAttribute( QStringLiteral(
"intersection-snapping" ), QString::number( mIntersectionSnapping ) );
528 snapSettingsElem.setAttribute( QStringLiteral(
"self-snapping" ), QString::number( mSelfSnapping ) );
529 snapSettingsElem.setAttribute( QStringLiteral(
"scaleDependencyMode" ), QString::number( mScaleDependencyMode ) );
530 snapSettingsElem.setAttribute( QStringLiteral(
"minScale" ), mMinimumScale );
531 snapSettingsElem.setAttribute( QStringLiteral(
"maxScale" ), mMaximumScale );
533 QDomElement ilsElement = doc.createElement( QStringLiteral(
"individual-layer-settings" ) );
534 QHash<QgsVectorLayer *, IndividualLayerSettings>::const_iterator layerIt = mIndividualLayerSettings.constBegin();
535 for ( ; layerIt != mIndividualLayerSettings.constEnd(); ++layerIt )
539 QDomElement layerElement = doc.createElement( QStringLiteral(
"layer-setting" ) );
540 layerElement.setAttribute( QStringLiteral(
"id" ), layerIt.key()->id() );
541 layerElement.setAttribute( QStringLiteral(
"enabled" ), QString::number( setting.
enabled() ) );
542 layerElement.setAttribute( QStringLiteral(
"type" ),
static_cast<int>( setting.
typeFlag() ) );
543 layerElement.setAttribute( QStringLiteral(
"tolerance" ), setting.
tolerance() );
544 layerElement.setAttribute( QStringLiteral(
"units" ),
static_cast<int>( setting.
units() ) );
545 layerElement.setAttribute( QStringLiteral(
"minScale" ), setting.
minimumScale() );
546 layerElement.setAttribute( QStringLiteral(
"maxScale" ), setting.
maximumScale() );
547 ilsElement.appendChild( layerElement );
549 snapSettingsElem.appendChild( ilsElement );
551 doc.firstChildElement( QStringLiteral(
"qgis" ) ).appendChild( snapSettingsElem );
556 bool changed =
false;
557 bool enabled =
QgsSettings().
value( QStringLiteral(
"/qgis/digitizing/default_snap_enabled" ),
true ).toBool();
562 const auto constLayers = layers;
577 bool changed =
false;
578 const auto constLayers = layers;
584 mIndividualLayerSettings.remove( vl );
591 void QgsSnappingConfig::readLegacySettings()
596 QString snapMode = mProject->
readEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/SnappingMode" ) );
598 mTolerance = mProject->
readDoubleEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/DefaultSnapTolerance" ), 0 );
601 mIntersectionSnapping = mProject->
readNumEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/IntersectionSnapping" ), 0 );
604 QStringList layerIdList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingList" ), QStringList() );
605 QStringList enabledList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingEnabledList" ), QStringList() );
606 QStringList toleranceList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingToleranceList" ), QStringList() );
607 QStringList toleranceUnitList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingToleranceUnitList" ), QStringList() );
608 QStringList snapToList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnapToList" ), QStringList() );
611 if ( layerIdList.size() != enabledList.size() ||
612 layerIdList.size() != toleranceList.size() ||
613 layerIdList.size() != toleranceUnitList.size() ||
614 layerIdList.size() != snapToList.size() )
618 if ( snapMode == QLatin1String(
"current_layer" ) )
620 else if ( snapMode == QLatin1String(
"all_layers" ) )
626 QStringList::const_iterator layerIt( layerIdList.constBegin() );
627 QStringList::const_iterator tolIt( toleranceList.constBegin() );
628 QStringList::const_iterator tolUnitIt( toleranceUnitList.constBegin() );
629 QStringList::const_iterator snapIt( snapToList.constBegin() );
630 QStringList::const_iterator enabledIt( enabledList.constBegin() );
631 for ( ; layerIt != layerIdList.constEnd(); ++layerIt, ++tolIt, ++tolUnitIt, ++snapIt, ++enabledIt )
637 QgsSnappingConfig::SnappingTypeFlag t( *snapIt == QLatin1String(
"to_vertex" ) ?
VertexFlag :
638 ( *snapIt == QLatin1String(
"to_segment" ) ?
SegmentFlag :
643 mIndividualLayerSettings.insert( vlayer, IndividualLayerSettings( *enabledIt == QLatin1String(
"enabled" ), t, tolIt->toDouble(),
static_cast<QgsTolerance::UnitType>( tolUnitIt->toInt() ), 0.0, 0.0 ) );
646 QString snapType = mProject->
readEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/DefaultSnapType" ), QStringLiteral(
"off" ) );
648 if ( snapType == QLatin1String(
"to segment" ) )
650 else if ( snapType == QLatin1String(
"to vertex and segment" ) )
652 else if ( snapType == QLatin1String(
"to vertex" ) )
675 return mMinimumScale;
680 mMinimumScale = minScale;
685 return mMaximumScale;
690 mMaximumScale = maxScale;
695 mScaleDependencyMode =
mode;
700 return mScaleDependencyMode;
static const double DEFAULT_SNAP_TOLERANCE
Default snapping distance tolerance.
static const QgsTolerance::UnitType DEFAULT_SNAP_UNITS
Default snapping distance units.
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.
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
This class is a composition of two QSettings instances:
T flagValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on a flag.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
This is 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
void setUnits(QgsTolerance::UnitType units)
Sets the type of units.
bool operator!=(const QgsSnappingConfig::IndividualLayerSettings &other) const
Compare this configuration to other.
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.
QgsTolerance::UnitType units() const
Returns the type of units.
void setMinimumScale(double minScale)
Sets the min scale value on which snapping is used, 0.0 disable scale limit.
void setMaximumScale(double maxScale)
Sets the max scale value on which snapping is used, 0.0 disable scale limit.
bool enabled() const
Returns if snapping is enabled.
void setTypeFlag(QgsSnappingConfig::SnappingTypeFlag type)
define the type of snapping
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.
QgsSnappingConfig::SnappingTypeFlag typeFlag() const
Returns the flags type (vertices | segments | area | centroid | middle)
Q_DECL_DEPRECATED void setType(QgsSnappingConfig::SnappingType type)
define the type of snapping
void setTolerance(double tolerance)
Sets the tolerance.
This is a container for configuration of the snapping of the project.
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.
@ SegmentFlag
On segments.
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.
QgsTolerance::UnitType units() const
Returns the type of units.
void setUnits(QgsTolerance::UnitType units)
Sets the type of units.
bool intersectionSnapping() const
Returns if the snapping on intersection is enabled.
void setTypeFlag(QgsSnappingConfig::SnappingTypeFlag type)
define the type of snapping
void setMode(SnappingMode mode)
define the mode of snapping
ScaleDependencyMode
ScaleDependencyMode the scale dependency mode of snapping.
@ Disabled
No scale dependency.
double minimumScale() const
Returns the min scale (i.e.
double tolerance() const
Returns the tolerance.
Q_GADGET QgsProject * project
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.
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.
SnappingMode mode() const
Returns the mode (all layers, active layer, per layer settings)
bool operator!=(const QgsSnappingConfig &other) const
Compare this configuration to other.
QgsSnappingConfig::SnappingTypeFlag typeFlag() const
Returns the flags type (vertices | segments | area | centroid | middle)
bool operator==(const QgsSnappingConfig &other) const
SnappingType
SnappingType defines on what object the snapping is performed.
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
bool enabled() const
Returns if snapping is enabled.
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.
UnitType
Type of unit of tolerance value from settings.
@ ProjectUnits
Map (project) units. Added in 2.8.
@ LayerUnits
Layer unit value.
Represents a vector layer which manages a vector based data sets.
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