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>();
168 if ( mEnabled == enabled )
210 if ( mTolerance == tolerance )
224 if ( mUnits == units )
233 return mIntersectionSnapping;
238 mIntersectionSnapping =
enabled;
243 return mIndividualLayerSettings;
248 if ( vl && mIndividualLayerSettings.contains( vl ) )
250 return mIndividualLayerSettings.value( vl );
265 mIndividualLayerSettings.insert( vl, individualLayerSettings );
270 return mEnabled != other.mEnabled
271 || mMode != other.mMode
272 || mType != other.mType
273 || mTolerance != other.mTolerance
274 || mUnits != other.mUnits
275 || mIndividualLayerSettings != other.mIndividualLayerSettings;
280 QDomElement snapSettingsElem = doc.firstChildElement( QStringLiteral(
"qgis" ) ).firstChildElement( QStringLiteral(
"snapping-settings" ) );
281 if ( snapSettingsElem.isNull() )
283 readLegacySettings();
287 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"enabled" ) ) )
288 mEnabled = snapSettingsElem.attribute( QStringLiteral(
"enabled" ) ) == QLatin1String(
"1" );
290 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"mode" ) ) )
291 mMode = (
SnappingMode )snapSettingsElem.attribute( QStringLiteral(
"mode" ) ).toInt();
293 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"type" ) ) )
294 mType = (
SnappingType )snapSettingsElem.attribute( QStringLiteral(
"type" ) ).toInt();
296 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"tolerance" ) ) )
297 mTolerance = snapSettingsElem.attribute( QStringLiteral(
"tolerance" ) ).toDouble();
299 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"unit" ) ) )
302 if ( snapSettingsElem.hasAttribute( QStringLiteral(
"intersection-snapping" ) ) )
303 mIntersectionSnapping = snapSettingsElem.attribute( QStringLiteral(
"intersection-snapping" ) ) == QLatin1String(
"1" );
306 QDomNodeList nodes = snapSettingsElem.elementsByTagName( QStringLiteral(
"individual-layer-settings" ) );
309 QDomNode node = nodes.item( 0 );
310 QDomNodeList settingNodes = node.childNodes();
311 int layerCount = settingNodes.count();
312 for (
int i = 0; i < layerCount; ++i )
314 QDomElement settingElement = settingNodes.at( i ).toElement();
315 if ( settingElement.tagName() != QLatin1String(
"layer-setting" ) )
317 QgsLogger::warning( QApplication::translate(
"QgsProjectSnappingSettings",
"Cannot read individual settings. Unexpected tag '%1'" ).arg( settingElement.tagName() ) );
321 QString layerId = settingElement.attribute( QStringLiteral(
"id" ) );
322 bool enabled = settingElement.attribute( QStringLiteral(
"enabled" ) ) == QLatin1String(
"1" );
324 double tolerance = settingElement.attribute( QStringLiteral(
"tolerance" ) ).toDouble();
334 mIndividualLayerSettings.insert( vl, setting );
341 QDomElement snapSettingsElem = doc.createElement( QStringLiteral(
"snapping-settings" ) );
342 snapSettingsElem.setAttribute( QStringLiteral(
"enabled" ), QString::number( mEnabled ) );
343 snapSettingsElem.setAttribute( QStringLiteral(
"mode" ), static_cast<int>( mMode ) );
344 snapSettingsElem.setAttribute( QStringLiteral(
"type" ), static_cast<int>( mType ) );
345 snapSettingsElem.setAttribute( QStringLiteral(
"tolerance" ), mTolerance );
346 snapSettingsElem.setAttribute( QStringLiteral(
"unit" ), static_cast<int>( mUnits ) );
347 snapSettingsElem.setAttribute( QStringLiteral(
"intersection-snapping" ), QString::number( mIntersectionSnapping ) );
349 QDomElement ilsElement = doc.createElement( QStringLiteral(
"individual-layer-settings" ) );
350 QHash<QgsVectorLayer *, IndividualLayerSettings>::const_iterator layerIt = mIndividualLayerSettings.constBegin();
351 for ( ; layerIt != mIndividualLayerSettings.constEnd(); ++layerIt )
355 QDomElement layerElement = doc.createElement( QStringLiteral(
"layer-setting" ) );
356 layerElement.setAttribute( QStringLiteral(
"id" ), layerIt.key()->id() );
357 layerElement.setAttribute( QStringLiteral(
"enabled" ), QString::number( setting.
enabled() ) );
358 layerElement.setAttribute( QStringLiteral(
"type" ), static_cast<int>( setting.
type() ) );
359 layerElement.setAttribute( QStringLiteral(
"tolerance" ), setting.
tolerance() );
360 layerElement.setAttribute( QStringLiteral(
"units" ), static_cast<int>( setting.
units() ) );
361 ilsElement.appendChild( layerElement );
363 snapSettingsElem.appendChild( ilsElement );
365 doc.firstChildElement( QStringLiteral(
"qgis" ) ).appendChild( snapSettingsElem );
370 bool changed =
false;
371 bool enabled =
QgsSettings().
value( QStringLiteral(
"/qgis/digitizing/default_snap_enabled" ),
true ).toBool();
390 bool changed =
false;
396 mIndividualLayerSettings.remove( vl );
403 void QgsSnappingConfig::readLegacySettings()
408 QString snapMode = mProject->
readEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/SnappingMode" ) );
410 mTolerance = mProject->
readDoubleEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/DefaultSnapTolerance" ), 0 );
413 mIntersectionSnapping = mProject->
readNumEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/IntersectionSnapping" ), 0 );
416 QStringList layerIdList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingList" ), QStringList() );
417 QStringList enabledList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingEnabledList" ), QStringList() );
418 QStringList toleranceList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingToleranceList" ), QStringList() );
419 QStringList toleranceUnitList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnappingToleranceUnitList" ), QStringList() );
420 QStringList snapToList = mProject->
readListEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/LayerSnapToList" ), QStringList() );
423 if ( layerIdList.size() != enabledList.size() ||
424 layerIdList.size() != toleranceList.size() ||
425 layerIdList.size() != toleranceUnitList.size() ||
426 layerIdList.size() != snapToList.size() )
430 if ( snapMode == QLatin1String(
"current_layer" ) )
432 else if ( snapMode == QLatin1String(
"all_layers" ) )
438 QStringList::const_iterator layerIt( layerIdList.constBegin() );
439 QStringList::const_iterator tolIt( toleranceList.constBegin() );
440 QStringList::const_iterator tolUnitIt( toleranceUnitList.constBegin() );
441 QStringList::const_iterator snapIt( snapToList.constBegin() );
442 QStringList::const_iterator enabledIt( enabledList.constBegin() );
443 for ( ; layerIt != layerIdList.constEnd(); ++layerIt, ++tolIt, ++tolUnitIt, ++snapIt, ++enabledIt )
450 ( *snapIt == QLatin1String(
"to_segment" ) ?
Segment :
458 QString snapType = mProject->
readEntry( QStringLiteral(
"Digitizing" ), QStringLiteral(
"/DefaultSnapType" ), QStringLiteral(
"off" ) );
460 if ( snapType == QLatin1String(
"to segment" ) )
462 else if ( snapType == QLatin1String(
"to vertex and segment" ) )
464 else if ( snapType == QLatin1String(
"to vertex" ) )
479 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)
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.
QgsMapLayer::LayerType type() const
Returns the type of the layer.
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.
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)
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.
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.