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;
150 mIndividualLayerSettings = QHash<QgsVectorLayer *, IndividualLayerSettings>();
151 const auto constMapLayers = mProject->
mapLayers();
169 if ( mEnabled == enabled )
211 if ( mTolerance == tolerance )
225 if ( mUnits == units )
234 return mIntersectionSnapping;
239 mIntersectionSnapping =
enabled;
244 return mIndividualLayerSettings;
249 if ( vl && mIndividualLayerSettings.contains( vl ) )
251 return mIndividualLayerSettings.value( vl );
266 mIndividualLayerSettings.insert( vl, individualLayerSettings );
271 return mEnabled != other.mEnabled
272 || mMode != other.mMode
273 || mType != other.mType
274 || mTolerance != other.mTolerance
275 || mUnits != other.mUnits
276 || mIndividualLayerSettings != other.mIndividualLayerSettings;
281 QDomElement snapSettingsElem = doc.firstChildElement( QStringLiteral(
"qgis" ) ).firstChildElement( QStringLiteral(
"snapping-settings" ) );
282 if ( snapSettingsElem.isNull() )
284 readLegacySettings();
288 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"enabled" ) ) )
289 mEnabled = snapSettingsElem.attribute( QStringLiteral(
"enabled" ) ) == QLatin1String(
"1" );
291 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"mode" ) ) )
292 mMode = (
SnappingMode )snapSettingsElem.attribute( QStringLiteral(
"mode" ) ).toInt();
294 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"type" ) ) )
295 mType = (
SnappingType )snapSettingsElem.attribute( QStringLiteral(
"type" ) ).toInt();
297 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"tolerance" ) ) )
298 mTolerance = snapSettingsElem.attribute( QStringLiteral(
"tolerance" ) ).toDouble();
300 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"unit" ) ) )
303 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"intersection-snapping" ) ) )
304 mIntersectionSnapping = snapSettingsElem.attribute( QStringLiteral(
"intersection-snapping" ) ) == QLatin1String(
"1" );
307 QDomNodeList nodes = snapSettingsElem.elementsByTagName( QStringLiteral(
"individual-layer-settings" ) );
310 QDomNode node = nodes.item( 0 );
311 QDomNodeList settingNodes = node.childNodes();
312 int layerCount = settingNodes.count();
313 for (
int i = 0; i < layerCount; ++i )
315 QDomElement settingElement = settingNodes.at( i ).toElement();
316 if ( settingElement.tagName() != QLatin1String(
"layer-setting" ) )
318 QgsLogger::warning( QApplication::translate(
"QgsProjectSnappingSettings",
"Cannot read individual settings. Unexpected tag '%1'" ).arg( settingElement.tagName() ) );
322 QString layerId = settingElement.attribute( QStringLiteral(
"id" ) );
323 bool enabled = settingElement.attribute( QStringLiteral(
"enabled" ) ) == QLatin1String(
"1" );
325 double tolerance = settingElement.attribute( QStringLiteral(
"tolerance" ) ).toDouble();
335 mIndividualLayerSettings.insert( vl, setting );
342 QDomElement snapSettingsElem = doc.createElement( QStringLiteral(
"snapping-settings" ) );
343 snapSettingsElem.setAttribute( QStringLiteral(
"enabled" ), QString::number( mEnabled ) );
344 snapSettingsElem.setAttribute( QStringLiteral(
"mode" ), static_cast<int>( mMode ) );
345 snapSettingsElem.setAttribute( QStringLiteral(
"type" ), static_cast<int>( mType ) );
346 snapSettingsElem.setAttribute( QStringLiteral(
"tolerance" ), mTolerance );
347 snapSettingsElem.setAttribute( QStringLiteral(
"unit" ), static_cast<int>( mUnits ) );
348 snapSettingsElem.setAttribute( QStringLiteral(
"intersection-snapping" ), QString::number( mIntersectionSnapping ) );
350 QDomElement ilsElement = doc.createElement( QStringLiteral(
"individual-layer-settings" ) );
351 QHash<QgsVectorLayer *, IndividualLayerSettings>::const_iterator layerIt = mIndividualLayerSettings.constBegin();
352 for ( ; layerIt != mIndividualLayerSettings.constEnd(); ++layerIt )
356 QDomElement layerElement = doc.createElement( QStringLiteral(
"layer-setting" ) );
357 layerElement.setAttribute( QStringLiteral(
"id" ), layerIt.key()->id() );
358 layerElement.setAttribute( QStringLiteral(
"enabled" ), QString::number( setting.
enabled() ) );
359 layerElement.setAttribute( QStringLiteral(
"type" ), static_cast<int>( setting.
type() ) );
360 layerElement.setAttribute( QStringLiteral(
"tolerance" ), setting.
tolerance() );
361 layerElement.setAttribute( QStringLiteral(
"units" ), static_cast<int>( setting.
units() ) );
362 ilsElement.appendChild( layerElement );
364 snapSettingsElem.appendChild( ilsElement );
366 doc.firstChildElement( QStringLiteral(
"qgis" ) ).appendChild( snapSettingsElem );
371 bool changed =
false;
372 bool enabled =
QgsSettings().
value( QStringLiteral(
"/qgis/digitizing/default_snap_enabled" ),
true ).toBool();
377 const auto constLayers = layers;
392 bool changed =
false;
393 const auto constLayers = layers;
399 mIndividualLayerSettings.remove( vl );
406 void QgsSnappingConfig::readLegacySettings()
411 QString snapMode = mProject->
readEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/SnappingMode" ) );
413 mTolerance = mProject->
readDoubleEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/DefaultSnapTolerance" ), 0 );
416 mIntersectionSnapping = mProject->
readNumEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/IntersectionSnapping" ), 0 );
419 QStringList layerIdList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingList" ), QStringList() );
420 QStringList enabledList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingEnabledList" ), QStringList() );
421 QStringList toleranceList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingToleranceList" ), QStringList() );
422 QStringList toleranceUnitList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingToleranceUnitList" ), QStringList() );
423 QStringList snapToList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnapToList" ), QStringList() );
426 if ( layerIdList.size() != enabledList.size() ||
427 layerIdList.size() != toleranceList.size() ||
428 layerIdList.size() != toleranceUnitList.size() ||
429 layerIdList.size() != snapToList.size() )
433 if ( snapMode == QLatin1String(
"current_layer" ) )
435 else if ( snapMode == QLatin1String(
"all_layers" ) )
441 QStringList::const_iterator layerIt( layerIdList.constBegin() );
442 QStringList::const_iterator tolIt( toleranceList.constBegin() );
443 QStringList::const_iterator tolUnitIt( toleranceUnitList.constBegin() );
444 QStringList::const_iterator snapIt( snapToList.constBegin() );
445 QStringList::const_iterator enabledIt( enabledList.constBegin() );
446 for ( ; layerIt != layerIdList.constEnd(); ++layerIt, ++tolIt, ++tolUnitIt, ++snapIt, ++enabledIt )
453 ( *snapIt == QLatin1String(
"to_segment" ) ?
Segment :
461 QString snapType = mProject->
readEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/DefaultSnapType" ), QStringLiteral(
"off" ) );
463 if ( snapType == QLatin1String(
"to segment" ) )
465 else if ( snapType == QLatin1String(
"to vertex and segment" ) )
467 else if ( snapType == QLatin1String(
"to vertex" ) )
482 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.