18 #include <QDomElement> 19 #include <QHeaderView> 31 , mTolerance( tolerance )
82 return mValid != other.mValid
83 || mEnabled != other.mEnabled
84 || mType != other.mType
85 || mTolerance != other.mTolerance
86 || mUnits != other.mUnits;
91 return mValid == other.mValid
92 && mEnabled == other.mEnabled
93 && mType == other.mType
94 && mTolerance == other.mTolerance
95 && mUnits == other.mUnits;
100 : mProject( project )
108 return mEnabled == other.mEnabled
109 && mMode == other.mMode
110 && mType == other.mType
111 && mTolerance == other.mTolerance
112 && mUnits == other.mUnits
113 && mIntersectionSnapping == other.mIntersectionSnapping
114 && mIndividualLayerSettings == other.mIndividualLayerSettings;
120 bool enabled =
QgsSettings().
value( QStringLiteral(
"/qgis/digitizing/default_snap_enabled" ),
false ).toBool();
146 mIntersectionSnapping =
false;
149 mIndividualLayerSettings = QHash<QgsVectorLayer *, IndividualLayerSettings>();
167 if ( mEnabled == enabled )
209 if ( mTolerance == tolerance )
223 if ( mUnits == units )
232 return mIntersectionSnapping;
237 mIntersectionSnapping =
enabled;
242 return mIndividualLayerSettings;
247 if ( vl && mIndividualLayerSettings.contains( vl ) )
249 return mIndividualLayerSettings.value( vl );
264 mIndividualLayerSettings.insert( vl, individualLayerSettings );
269 return mEnabled != other.mEnabled
270 || mMode != other.mMode
271 || mType != other.mType
272 || mTolerance != other.mTolerance
273 || mUnits != other.mUnits
274 || mIndividualLayerSettings != other.mIndividualLayerSettings;
279 QDomElement snapSettingsElem = doc.firstChildElement( QStringLiteral(
"qgis" ) ).firstChildElement( QStringLiteral(
"snapping-settings" ) );
280 if ( snapSettingsElem.isNull() )
282 readLegacySettings();
286 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"enabled" ) ) )
287 mEnabled = snapSettingsElem.attribute( QStringLiteral(
"enabled" ) ) == QLatin1String(
"1" );
289 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"mode" ) ) )
290 mMode = (
SnappingMode )snapSettingsElem.attribute( QStringLiteral(
"mode" ) ).toInt();
292 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"type" ) ) )
293 mType = (
SnappingType )snapSettingsElem.attribute( QStringLiteral(
"type" ) ).toInt();
295 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"tolerance" ) ) )
296 mTolerance = snapSettingsElem.attribute( QStringLiteral(
"tolerance" ) ).toDouble();
298 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"unit" ) ) )
301 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"intersection-snapping" ) ) )
302 mIntersectionSnapping = snapSettingsElem.attribute( QStringLiteral(
"intersection-snapping" ) ) == QLatin1String(
"1" );
305 QDomNodeList nodes = snapSettingsElem.elementsByTagName( QStringLiteral(
"individual-layer-settings" ) );
308 QDomNode node = nodes.item( 0 );
309 QDomNodeList settingNodes = node.childNodes();
310 int layerCount = settingNodes.count();
311 for (
int i = 0; i < layerCount; ++i )
313 QDomElement settingElement = settingNodes.at( i ).toElement();
314 if ( settingElement.tagName() != QLatin1String(
"layer-setting" ) )
316 QgsLogger::warning( QApplication::translate(
"QgsProjectSnappingSettings",
"Cannot read individual settings. Unexpected tag '%1'" ).arg( settingElement.tagName() ) );
320 QString layerId = settingElement.attribute( QStringLiteral(
"id" ) );
321 bool enabled = settingElement.attribute( QStringLiteral(
"enabled" ) ) == QLatin1String(
"1" );
323 double tolerance = settingElement.attribute( QStringLiteral(
"tolerance" ) ).toDouble();
333 mIndividualLayerSettings.insert( vl, setting );
340 QDomElement snapSettingsElem = doc.createElement( QStringLiteral(
"snapping-settings" ) );
341 snapSettingsElem.setAttribute( QStringLiteral(
"enabled" ), QString::number( mEnabled ) );
342 snapSettingsElem.setAttribute( QStringLiteral(
"mode" ), (
int )mMode );
343 snapSettingsElem.setAttribute( QStringLiteral(
"type" ), (
int )mType );
344 snapSettingsElem.setAttribute( QStringLiteral(
"tolerance" ), mTolerance );
345 snapSettingsElem.setAttribute( QStringLiteral(
"unit" ), (
int )mUnits );
346 snapSettingsElem.setAttribute( QStringLiteral(
"intersection-snapping" ), QString::number( mIntersectionSnapping ) );
348 QDomElement ilsElement = doc.createElement( QStringLiteral(
"individual-layer-settings" ) );
349 QHash<QgsVectorLayer *, IndividualLayerSettings>::const_iterator layerIt = mIndividualLayerSettings.constBegin();
350 for ( ; layerIt != mIndividualLayerSettings.constEnd(); ++layerIt )
354 QDomElement layerElement = doc.createElement( QStringLiteral(
"layer-setting" ) );
355 layerElement.setAttribute( QStringLiteral(
"id" ), layerIt.key()->id() );
356 layerElement.setAttribute( QStringLiteral(
"enabled" ), QString::number( setting.
enabled() ) );
357 layerElement.setAttribute( QStringLiteral(
"type" ), (
int )setting.
type() );
358 layerElement.setAttribute( QStringLiteral(
"tolerance" ), setting.
tolerance() );
359 layerElement.setAttribute( QStringLiteral(
"units" ), (
int )setting.
units() );
360 ilsElement.appendChild( layerElement );
362 snapSettingsElem.appendChild( ilsElement );
364 doc.firstChildElement( QStringLiteral(
"qgis" ) ).appendChild( snapSettingsElem );
369 bool changed =
false;
370 bool enabled =
QgsSettings().
value( QStringLiteral(
"/qgis/digitizing/default_snap_enabled" ),
true ).toBool();
389 bool changed =
false;
395 mIndividualLayerSettings.remove( vl );
402 void QgsSnappingConfig::readLegacySettings()
407 QString snapMode = mProject->
readEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/SnappingMode" ) );
409 mTolerance = mProject->
readDoubleEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/DefaultSnapTolerance" ), 0 );
412 mIntersectionSnapping = mProject->
readNumEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/IntersectionSnapping" ), 0 );
415 QStringList layerIdList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingList" ), QStringList() );
416 QStringList enabledList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingEnabledList" ), QStringList() );
417 QStringList toleranceList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingToleranceList" ), QStringList() );
418 QStringList toleranceUnitList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingToleranceUnitList" ), QStringList() );
419 QStringList snapToList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnapToList" ), QStringList() );
422 if ( layerIdList.size() != enabledList.size() ||
423 layerIdList.size() != toleranceList.size() ||
424 layerIdList.size() != toleranceUnitList.size() ||
425 layerIdList.size() != snapToList.size() )
429 if ( snapMode == QLatin1String(
"current_layer" ) )
431 else if ( snapMode == QLatin1String(
"all_layers" ) )
437 QStringList::const_iterator layerIt( layerIdList.constBegin() );
438 QStringList::const_iterator tolIt( toleranceList.constBegin() );
439 QStringList::const_iterator tolUnitIt( toleranceUnitList.constBegin() );
440 QStringList::const_iterator snapIt( snapToList.constBegin() );
441 QStringList::const_iterator enabledIt( enabledList.constBegin() );
442 for ( ; layerIt != layerIdList.constEnd(); ++layerIt, ++tolIt, ++tolUnitIt, ++snapIt, ++enabledIt )
449 ( *snapIt == QLatin1String(
"to_segment" ) ?
Segment :
457 QString snapType = mProject->
readEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/DefaultSnapType" ), QStringLiteral(
"off" ) );
459 if ( snapType == QLatin1String(
"to segment" ) )
461 else if ( snapType == QLatin1String(
"to vertex and segment" ) )
463 else if ( snapType == QLatin1String(
"to vertex" ) )
478 if ( mProject != project )
void setEnabled(bool enabled)
enables the snapping
SnappingMode
SnappingMode defines on which layer the snapping is performed.
T enumValue(const QString &key, const T &defaultValue, const Section section=NoSection, bool flag=false) const
Return the setting value for a setting based on an enum.
bool valid() const
return 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
return the type (vertices and/or segments)
SnappingMode mode() const
return 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
return if snapping is enabled
void writeProject(QDomDocument &doc)
Writes the configuration to the specified QGIS project document.
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
return the type of units
QMap< QString, QgsMapLayer * > mapLayers() const
Returns a map of all registered layers by layer ID.
QgsMapLayer::LayerType type() const
Returns the type of the layer.
void reset()
reset to default values
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=nullptr) const
void setIndividualLayerSettings(QgsVectorLayer *vl, const QgsSnappingConfig::IndividualLayerSettings &individualLayerSettings)
set individual layer snappings settings (applied if mode is AdvancedConfiguration) ...
bool intersectionSnapping() const
return if the snapping on intersection is enabled
void setUnits(QgsTolerance::UnitType units)
set 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)
set if the snapping on intersection is enabled
QgsTolerance::UnitType units() const
return the type of units
bool operator==(const QgsSnappingConfig::IndividualLayerSettings &other) const
double tolerance() const
return the tolerance
bool isSpatial() const override
Returns true if this is a geometry layer and false in case of NoGeometry (table only) or UnknownGeome...
QStringList readListEntry(const QString &scope, const QString &key, const QStringList &def=QStringList(), bool *ok=nullptr) const
Key value accessors.
Reads and writes project states.
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)
set the tolerance
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.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), const Section section=NoSection) const
Returns the value for setting key.
IndividualLayerSettings()=default
Constructs an invalid setting.
void setType(SnappingType type)
define the type of snapping
QgsSnappingConfig::SnappingType type() const
return the type (vertices and/or segments)
bool enabled() const
return if snapping is enabled
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
return the tolerance
This is a container for configuration of the snapping of the project.
void setUnits(QgsTolerance::UnitType units)
set 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
return individual snapping settings for all layers
Represents a vector layer which manages a vector based data sets.
void setTolerance(double tolerance)
set the tolerance