18 #include <QDomElement> 19 #include <QHeaderView> 32 , mTolerance( tolerance )
83 return mValid != other.mValid
84 || mEnabled != other.mEnabled
85 || mType != other.mType
86 || mTolerance != other.mTolerance
87 || mUnits != other.mUnits;
92 return mValid == other.mValid
93 && mEnabled == other.mEnabled
94 && mType == other.mType
95 && mTolerance == other.mTolerance
96 && mUnits == other.mUnits;
101 : mProject( project )
109 return mEnabled == other.mEnabled
110 && mMode == other.mMode
111 && mType == other.mType
112 && mTolerance == other.mTolerance
113 && mUnits == other.mUnits
114 && mIntersectionSnapping == other.mIntersectionSnapping
115 && mIndividualLayerSettings == other.mIndividualLayerSettings;
121 bool enabled =
QgsSettings().
value( QStringLiteral(
"/qgis/digitizing/default_snap_enabled" ),
false ).toBool();
147 mIntersectionSnapping =
false;
152 mIndividualLayerSettings = QHash<QgsVectorLayer *, IndividualLayerSettings>();
153 const auto constMapLayers = mProject->
mapLayers();
172 if ( mEnabled == enabled )
214 if ( mTolerance == tolerance )
228 if ( mUnits == units )
237 return mIntersectionSnapping;
242 mIntersectionSnapping =
enabled;
247 return mIndividualLayerSettings;
252 if ( vl && mIndividualLayerSettings.contains( vl ) )
254 return mIndividualLayerSettings.value( vl );
269 mIndividualLayerSettings.insert( vl, individualLayerSettings );
274 return mEnabled != other.mEnabled
275 || mMode != other.mMode
276 || mType != other.mType
277 || mTolerance != other.mTolerance
278 || mUnits != other.mUnits
279 || mIndividualLayerSettings != other.mIndividualLayerSettings;
284 QDomElement snapSettingsElem = doc.firstChildElement( QStringLiteral(
"qgis" ) ).firstChildElement( QStringLiteral(
"snapping-settings" ) );
285 if ( snapSettingsElem.isNull() )
287 readLegacySettings();
291 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"enabled" ) ) )
292 mEnabled = snapSettingsElem.attribute( QStringLiteral(
"enabled" ) ) == QLatin1String(
"1" );
294 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"mode" ) ) )
295 mMode = (
SnappingMode )snapSettingsElem.attribute( QStringLiteral(
"mode" ) ).toInt();
297 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"type" ) ) )
298 mType = (
SnappingType )snapSettingsElem.attribute( QStringLiteral(
"type" ) ).toInt();
300 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"tolerance" ) ) )
301 mTolerance = snapSettingsElem.attribute( QStringLiteral(
"tolerance" ) ).toDouble();
303 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"unit" ) ) )
306 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"intersection-snapping" ) ) )
307 mIntersectionSnapping = snapSettingsElem.attribute( QStringLiteral(
"intersection-snapping" ) ) == QLatin1String(
"1" );
310 QDomNodeList nodes = snapSettingsElem.elementsByTagName( QStringLiteral(
"individual-layer-settings" ) );
313 QDomNode node = nodes.item( 0 );
314 QDomNodeList settingNodes = node.childNodes();
315 int layerCount = settingNodes.count();
316 for (
int i = 0; i < layerCount; ++i )
318 QDomElement settingElement = settingNodes.at( i ).toElement();
319 if ( settingElement.tagName() != QLatin1String(
"layer-setting" ) )
321 QgsLogger::warning( QApplication::translate(
"QgsProjectSnappingSettings",
"Cannot read individual settings. Unexpected tag '%1'" ).arg( settingElement.tagName() ) );
325 QString layerId = settingElement.attribute( QStringLiteral(
"id" ) );
326 bool enabled = settingElement.attribute( QStringLiteral(
"enabled" ) ) == QLatin1String(
"1" );
328 double tolerance = settingElement.attribute( QStringLiteral(
"tolerance" ) ).toDouble();
338 mIndividualLayerSettings.insert( vl, setting );
345 QDomElement snapSettingsElem = doc.createElement( QStringLiteral(
"snapping-settings" ) );
346 snapSettingsElem.setAttribute( QStringLiteral(
"enabled" ), QString::number( mEnabled ) );
347 snapSettingsElem.setAttribute( QStringLiteral(
"mode" ), static_cast<int>( mMode ) );
348 snapSettingsElem.setAttribute( QStringLiteral(
"type" ), static_cast<int>( mType ) );
349 snapSettingsElem.setAttribute( QStringLiteral(
"tolerance" ), mTolerance );
350 snapSettingsElem.setAttribute( QStringLiteral(
"unit" ), static_cast<int>( mUnits ) );
351 snapSettingsElem.setAttribute( QStringLiteral(
"intersection-snapping" ), QString::number( mIntersectionSnapping ) );
353 QDomElement ilsElement = doc.createElement( QStringLiteral(
"individual-layer-settings" ) );
354 QHash<QgsVectorLayer *, IndividualLayerSettings>::const_iterator layerIt = mIndividualLayerSettings.constBegin();
355 for ( ; layerIt != mIndividualLayerSettings.constEnd(); ++layerIt )
359 QDomElement layerElement = doc.createElement( QStringLiteral(
"layer-setting" ) );
360 layerElement.setAttribute( QStringLiteral(
"id" ), layerIt.key()->id() );
361 layerElement.setAttribute( QStringLiteral(
"enabled" ), QString::number( setting.
enabled() ) );
362 layerElement.setAttribute( QStringLiteral(
"type" ), static_cast<int>( setting.
type() ) );
363 layerElement.setAttribute( QStringLiteral(
"tolerance" ), setting.
tolerance() );
364 layerElement.setAttribute( QStringLiteral(
"units" ), static_cast<int>( setting.
units() ) );
365 ilsElement.appendChild( layerElement );
367 snapSettingsElem.appendChild( ilsElement );
369 doc.firstChildElement( QStringLiteral(
"qgis" ) ).appendChild( snapSettingsElem );
374 bool changed =
false;
375 bool enabled =
QgsSettings().
value( QStringLiteral(
"/qgis/digitizing/default_snap_enabled" ),
true ).toBool();
380 const auto constLayers = layers;
395 bool changed =
false;
396 const auto constLayers = layers;
402 mIndividualLayerSettings.remove( vl );
409 void QgsSnappingConfig::readLegacySettings()
414 QString snapMode = mProject->
readEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/SnappingMode" ) );
416 mTolerance = mProject->
readDoubleEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/DefaultSnapTolerance" ), 0 );
419 mIntersectionSnapping = mProject->
readNumEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/IntersectionSnapping" ), 0 );
422 QStringList layerIdList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingList" ), QStringList() );
423 QStringList enabledList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingEnabledList" ), QStringList() );
424 QStringList toleranceList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingToleranceList" ), QStringList() );
425 QStringList toleranceUnitList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingToleranceUnitList" ), QStringList() );
426 QStringList snapToList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnapToList" ), QStringList() );
429 if ( layerIdList.size() != enabledList.size() ||
430 layerIdList.size() != toleranceList.size() ||
431 layerIdList.size() != toleranceUnitList.size() ||
432 layerIdList.size() != snapToList.size() )
436 if ( snapMode == QLatin1String(
"current_layer" ) )
438 else if ( snapMode == QLatin1String(
"all_layers" ) )
444 QStringList::const_iterator layerIt( layerIdList.constBegin() );
445 QStringList::const_iterator tolIt( toleranceList.constBegin() );
446 QStringList::const_iterator tolUnitIt( toleranceUnitList.constBegin() );
447 QStringList::const_iterator snapIt( snapToList.constBegin() );
448 QStringList::const_iterator enabledIt( enabledList.constBegin() );
449 for ( ; layerIt != layerIdList.constEnd(); ++layerIt, ++tolIt, ++tolUnitIt, ++snapIt, ++enabledIt )
456 ( *snapIt == QLatin1String(
"to_segment" ) ?
Segment :
464 QString snapType = mProject->
readEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/DefaultSnapType" ), QStringLiteral(
"off" ) );
466 if ( snapType == QLatin1String(
"to segment" ) )
468 else if ( snapType == QLatin1String(
"to vertex and segment" ) )
470 else if ( snapType == QLatin1String(
"to vertex" ) )
485 if ( mProject != project )
void setEnabled(bool enabled)
enables the snapping
SnappingMode
SnappingMode defines on which layer the snapping is performed.
bool valid() const
Returns if settings are valid.
void setEnabled(bool enabled)
enables the snapping
Base class for all map layer types.
bool operator==(const QgsSnappingConfig &other) const
SnappingType type() const
Returns the type (vertices and/or segments)
QgsMapLayerType type() const
Returns the type of the layer.
SnappingMode mode() const
Returns the mode (all layers, active layer, per layer settings)
QString readEntry(const QString &scope, const QString &key, const QString &def=QString(), bool *ok=nullptr) const
This class is a composition of two QSettings instances:
Both on vertices and segments.
bool operator!=(const QgsSnappingConfig::IndividualLayerSettings &other) const
Compare this configuration to other.
bool enabled() const
Returns if snapping is enabled.
void writeProject(QDomDocument &doc)
Writes the configuration to the specified QGIS project document.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static void warning(const QString &msg)
Goes to qWarning.
bool operator!=(const QgsSnappingConfig &other) const
Compare this configuration to other.
UnitType
Type of unit of tolerance value from settings.
QgsSnappingConfig(QgsProject *project=nullptr)
Constructor with default parameters defined in global settings.
QgsTolerance::UnitType units() const
Returns the type of units.
void reset()
reset to default values
bool isSpatial() const FINAL
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=nullptr) const
void setIndividualLayerSettings(QgsVectorLayer *vl, const QgsSnappingConfig::IndividualLayerSettings &individualLayerSettings)
Sets individual layer snappings settings (applied if mode is AdvancedConfiguration) ...
bool intersectionSnapping() const
Returns if the snapping on intersection is enabled.
void setUnits(QgsTolerance::UnitType units)
Sets the type of units.
void readProject(const QDomDocument &doc)
Reads the configuration from the specified QGIS project document.
static const double DEFAULT_SNAP_TOLERANCE
Default snapping distance tolerance.
void setIntersectionSnapping(bool enabled)
Sets if the snapping on intersection is enabled.
QgsTolerance::UnitType units() const
Returns the type of units.
bool operator==(const QgsSnappingConfig::IndividualLayerSettings &other) const
double tolerance() const
Returns the tolerance.
QStringList readListEntry(const QString &scope, const QString &key, const QStringList &def=QStringList(), bool *ok=nullptr) const
Key value accessors.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
This is a container of advanced configuration (per layer) of the snapping of the project.
On a per layer configuration basis.
static const QgsTolerance::UnitType DEFAULT_SNAP_UNITS
Default snapping distance units.
void setTolerance(double tolerance)
Sets the tolerance.
QMap< QString, QgsMapLayer * > mapLayers(const bool validOnly=false) const
Returns a map of all registered layers by layer ID.
void setType(QgsSnappingConfig::SnappingType type)
define the type of snapping
bool removeLayers(const QList< QgsMapLayer *> &layers)
Removes the specified layers from the individual layer configuration.
void setMode(SnappingMode mode)
define the mode of snapping
SnappingType
SnappingType defines on what object the snapping is performed.
Map (project) units. Added in 2.8.
IndividualLayerSettings()=default
Constructs an invalid setting.
void setType(SnappingType type)
define the type of snapping
QgsSnappingConfig::SnappingType type() const
Returns the type (vertices and/or segments)
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection)
Returns the setting value for a setting based on an enum.
bool enabled() const
Returns if snapping is enabled.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
double readDoubleEntry(const QString &scope, const QString &key, double def=0, bool *ok=nullptr) const
double tolerance() const
Returns the tolerance.
This is a container for configuration of the snapping of the project.
void setUnits(QgsTolerance::UnitType units)
Sets the type of units.
void setProject(QgsProject *project)
The project from which the snapped layers should be retrieved.
QgsProject * project() const
The project from which the snapped layers should be retrieved.
bool addLayers(const QList< QgsMapLayer *> &layers)
Adds the specified layers as individual layers to the configuration with standard configuration...
QHash< QgsVectorLayer *, QgsSnappingConfig::IndividualLayerSettings > individualLayerSettings() const
Returns individual snapping settings for all layers.
Represents a vector layer which manages a vector based data sets.
void setTolerance(double tolerance)
Sets the tolerance.