QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsgeometryoptions.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometryoptions.cpp
3  -------------------
4  begin : Aug 23, 2018
5  copyright : (C) 2018 by Matthias Kuhn
6  email : [email protected]
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsgeometryoptions.h"
19 
20 #include "qgsxmlutils.h"
21 
22 #include "qgssettings.h"
23 
25 {
26  mGeometryChecks = QgsSettings().value( QStringLiteral( "geometry_validation/default_checks" ) ).toString().split( ',' ) ;
27 }
28 
30 {
31  return mRemoveDuplicateNodes;
32 }
33 
35 {
36  mRemoveDuplicateNodes = value;
38 }
39 
41 {
42  return mGeometryPrecision;
43 }
44 
46 {
47  mGeometryPrecision = value;
49 }
50 
52 {
53  return mGeometryPrecision != 0.0 || mRemoveDuplicateNodes;
54 }
55 
56 void QgsGeometryOptions::apply( QgsGeometry &geometry ) const
57 {
58  if ( mGeometryPrecision != 0.0 )
59  geometry = geometry.snappedToGrid( mGeometryPrecision, mGeometryPrecision );
60 
61  if ( mRemoveDuplicateNodes )
62  geometry.removeDuplicateNodes( 4 * std::numeric_limits<double>::epsilon(), true );
63 }
64 
66 {
67  return mGeometryChecks;
68 }
69 
71 {
72  mGeometryChecks = geometryChecks;
73  emit geometryChecksChanged();
74 }
75 
76 QVariantMap QgsGeometryOptions::checkConfiguration( const QString &checkId ) const
77 {
78  return mCheckConfiguration.value( checkId ).toMap();
79 }
80 
81 void QgsGeometryOptions::setCheckConfiguration( const QString &checkId, const QVariantMap &checkConfiguration )
82 {
83  mCheckConfiguration[checkId] = checkConfiguration;
85 }
86 
87 void QgsGeometryOptions::writeXml( QDomNode &node ) const
88 {
89  QDomDocument doc = node.ownerDocument();
90  QDomElement geometryOptionsElement = doc.createElement( QStringLiteral( "geometryOptions" ) );
91  node.appendChild( geometryOptionsElement );
92 
93  geometryOptionsElement.setAttribute( QStringLiteral( "removeDuplicateNodes" ), mRemoveDuplicateNodes ? 1 : 0 );
94  geometryOptionsElement.setAttribute( QStringLiteral( "geometryPrecision" ), mGeometryPrecision );
95 
96  QDomElement activeCheckListElement = QgsXmlUtils::writeVariant( mGeometryChecks, doc );
97  activeCheckListElement.setTagName( QStringLiteral( "activeChecks" ) );
98  geometryOptionsElement.appendChild( activeCheckListElement );
99  QDomElement checkConfigurationElement = QgsXmlUtils::writeVariant( mCheckConfiguration, doc );
100  checkConfigurationElement.setTagName( QStringLiteral( "checkConfiguration" ) );
101  geometryOptionsElement.appendChild( checkConfigurationElement );
102 }
103 
104 void QgsGeometryOptions::readXml( const QDomNode &node )
105 {
106  QDomElement geometryOptionsElement = node.toElement();
107  setGeometryPrecision( geometryOptionsElement.attribute( QStringLiteral( "geometryPrecision" ), QStringLiteral( "0.0" ) ).toDouble() );
108  setRemoveDuplicateNodes( geometryOptionsElement.attribute( QStringLiteral( "removeDuplicateNodes" ), QStringLiteral( "0" ) ).toInt() == 1 );
109 
110  QDomElement activeChecksElem = node.namedItem( QStringLiteral( "activeChecks" ) ).toElement();
111  const QVariant activeChecks = QgsXmlUtils::readVariant( activeChecksElem );
112  setGeometryChecks( activeChecks.toStringList() );
113 
114  QDomElement checkConfigurationElem = node.namedItem( QStringLiteral( "checkConfiguration" ) ).toElement();
115  const QVariant checkConfiguration = QgsXmlUtils::readVariant( checkConfigurationElem );
116  mCheckConfiguration = checkConfiguration.toMap();
117 }
void setGeometryChecks(const QStringList &geometryChecks)
A list of activated geometry checks.
void readXml(const QDomNode &node)
Read the geometry options from node.
bool removeDuplicateNodes(double epsilon=4 *std::numeric_limits< double >::epsilon(), bool useZValues=false)
Removes duplicate nodes from the geometry, wherever removing the nodes does not result in a degenerat...
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
double geometryPrecision() const
The precision in which geometries on this layer should be saved.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:122
void removeDuplicateNodesChanged()
Automatically remove duplicate nodes on all geometries which are edited on this layer.
QStringList geometryChecks() const
A list of activated geometry checks.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
void setGeometryPrecision(double value)
The precision in which geometries on this layer should be saved.
bool removeDuplicateNodes() const
Automatically remove duplicate nodes on all geometries which are edited on this layer.
QgsGeometryOptions()
Create a new QgsGeometryOptions object.
void setRemoveDuplicateNodes(bool value)
Automatically remove duplicate nodes on all geometries which are edited on this layer.
bool isActive() const
Determines if at least one fix is enabled.
void apply(QgsGeometry &geometry) const
Apply any fixes configured on this class to geometry.
void writeXml(QDomNode &node) const
Write the geometry options to the node.
QVariantMap checkConfiguration(const QString &checkId) const
Access the configuration for the check checkId.
QgsGeometry snappedToGrid(double hSpacing, double vSpacing, double dSpacing=0, double mSpacing=0) const
Returns a new geometry with all points or vertices snapped to the closest point of the grid...
void checkConfigurationChanged()
Access the configuration for the check checkId.
void geometryPrecisionChanged()
The precision in which geometries on this layer should be saved.
void setCheckConfiguration(const QString &checkId, const QVariantMap &checkConfiguration)
Set the configuration for the check checkId.
void geometryChecksChanged()
A list of activated geometry checks.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.