QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgssettingsentry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssettingsentry.cpp
3 --------------------------------------
4 Date : February 2021
5 Copyright : (C) 2021 by Damiano Lombardi
6 Email : damiano at opengis dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgssettingsentry.h"
17#include "qgssettings.h"
18#include "qgssettingstreenode.h"
19#include "qgslogger.h"
20
21#include <QRegularExpression>
22#include <QDir>
23
24
25QgsSettingsEntryBase::QgsSettingsEntryBase( const QString &key, QgsSettingsTreeNode *parent, const QVariant &defaultValue, const QString &description, Qgis::SettingsOptions options )
26 : mParentTreeElement( parent )
27 , mName( key )
28 , mDefaultValue( defaultValue )
29 , mDescription( description )
30 , mOptions( options )
31{
32 mKey = QDir::cleanPath( QStringLiteral( "%1/%2" ).arg( parent ? parent->completeKey() : QString(), key ) );
33
34 if ( parent )
35 {
37 }
38}
39
41{
42 if ( mParentTreeElement )
43 mParentTreeElement->unregisterChildSetting( this );
44}
45
46
47QString QgsSettingsEntryBase::key( const QString &dynamicKeyPart ) const
48{
49 return key( dynamicKeyPartToList( dynamicKeyPart ) );
50}
51
52QString QgsSettingsEntryBase::key( const QStringList &dynamicKeyPartList ) const
53{
54 return completeKeyPrivate( mKey, dynamicKeyPartList );
55}
56
57QString QgsSettingsEntryBase::completeKeyPrivate( const QString &key, const QStringList &dynamicKeyPartList ) const
58{
59 QString completeKey = key;
60
61 if ( dynamicKeyPartList.isEmpty() )
62 {
63 if ( hasDynamicKey() )
64 QgsDebugMsg( QStringLiteral( "Settings '%1' have a dynamic key but the dynamic key part was not provided" ).arg( completeKey ) );
65
66 return completeKey;
67 }
68 else
69 {
70 if ( !hasDynamicKey() )
71 {
72 QgsDebugMsg( QStringLiteral( "Settings '%1' don't have a dynamic key, the provided dynamic key part will be ignored" ).arg( completeKey ) );
73 return completeKey;
74 }
75
76 for ( int i = 0; i < dynamicKeyPartList.size(); i++ )
77 {
78 completeKey.replace( QStringLiteral( "%" ).append( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) );
79 }
80 }
81 return completeKey;
82}
83
84bool QgsSettingsEntryBase::keyIsValid( const QString &key ) const
85{
86 if ( !hasDynamicKey() )
87 {
88 if ( !key.contains( definitionKey() ) )
89 return false;
90 else
91 return key == definitionKey();
92 }
93
94 const QRegularExpression regularExpression( definitionKey().replace( QRegularExpression( QStringLiteral( "%\\d+" ) ), QStringLiteral( ".*" ) ) );
95 const QRegularExpressionMatch regularExpressionMatch = regularExpression.match( key );
96 return regularExpressionMatch.hasMatch();
97}
98
100{
101 return mKey;
102}
103
105{
106 const thread_local QRegularExpression regularExpression( QStringLiteral( "%\\d+" ) );
107 return mKey.contains( regularExpression );
108}
109
110bool QgsSettingsEntryBase::exists( const QString &dynamicKeyPart ) const
111{
112 return QgsSettings().contains( key( dynamicKeyPart ) );
113}
114
115bool QgsSettingsEntryBase::exists( const QStringList &dynamicKeyPartList ) const
116{
117 return QgsSettings().contains( key( dynamicKeyPartList ) );
118}
119
120Qgis::SettingsOrigin QgsSettingsEntryBase::origin( const QStringList &dynamicKeyPartList ) const
121{
122 return QgsSettings().origin( key( dynamicKeyPartList ) );
123}
124
125void QgsSettingsEntryBase::remove( const QString &dynamicKeyPart ) const
126{
127 QgsSettings().remove( key( dynamicKeyPart ) );
128}
129
130void QgsSettingsEntryBase::remove( const QStringList &dynamicKeyPartList ) const
131{
132 QgsSettings().remove( key( dynamicKeyPartList ) );
133}
134
136{
138}
139
140bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QString &dynamicKeyPart ) const
141{
142 return setVariantValuePrivate( value, dynamicKeyPartToList( dynamicKeyPart ) );
143}
144
145bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const
146{
147 return setVariantValuePrivate( value, dynamicKeyPartList );
148}
149
150bool QgsSettingsEntryBase::setVariantValuePrivate( const QVariant &value, const QStringList &dynamicKeyPartList ) const
151{
152 if ( mOptions.testFlag( Qgis::SettingsOption::SaveFormerValue ) )
153 {
154 if ( exists( dynamicKeyPartList ) )
155 {
156 QVariant currentValue = valueAsVariant( key( dynamicKeyPartList ) );
157 if ( value != currentValue )
158 {
159 QgsSettings().setValue( formerValuekey( dynamicKeyPartList ), currentValue );
160 }
161 }
162 }
163 QgsSettings().setValue( key( dynamicKeyPartList ), value );
164 return true;
165}
166
167QStringList QgsSettingsEntryBase::dynamicKeyPartToList( const QString &dynamicKeyPart )
168{
169 QStringList dynamicKeyPartList;
170 if ( !dynamicKeyPart.isNull() )
171 dynamicKeyPartList.append( dynamicKeyPart );
172 return dynamicKeyPartList;
173}
174
175QVariant QgsSettingsEntryBase::valueAsVariant( const QString &dynamicKeyPart ) const
176{
177 return valueAsVariant( dynamicKeyPartToList( dynamicKeyPart ) );
178}
179
180QVariant QgsSettingsEntryBase::valueAsVariant( const QStringList &dynamicKeyPartList ) const
181{
182 return QgsSettings().value( key( dynamicKeyPartList ), mDefaultValue );
183}
184
185QVariant QgsSettingsEntryBase::valueAsVariant( const QString &dynamicKeyPart, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const
186{
188 return valueAsVariant( dynamicKeyPartToList( dynamicKeyPart ), useDefaultValueOverride, defaultValueOverride );
190}
191
192QVariant QgsSettingsEntryBase::valueAsVariant( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const
193{
194 if ( useDefaultValueOverride )
195 return QgsSettings().value( key( dynamicKeyPartList ), defaultValueOverride );
196 else
197 return QgsSettings().value( key( dynamicKeyPartList ), mDefaultValue );
198}
199
200QVariant QgsSettingsEntryBase::valueAsVariantWithDefaultOverride( const QVariant &defaultValueOverride, const QString &dynamicKeyPart ) const
201{
202 return QgsSettings().value( key( dynamicKeyPart ), defaultValueOverride );
203}
204
205QVariant QgsSettingsEntryBase::valueAsVariantWithDefaultOverride( const QVariant &defaultValueOverride, const QStringList &dynamicKeyPartList ) const
206{
207 return QgsSettings().value( key( dynamicKeyPartList ), defaultValueOverride );
208}
209
210
212{
213 return mDefaultValue;
214}
215
217{
218 return mDescription;
219}
220
221QVariant QgsSettingsEntryBase::formerValueAsVariant( const QString &dynamicKeyPart ) const
222{
223 return formerValueAsVariant( dynamicKeyPartToList( dynamicKeyPart ) );
224}
225
226QVariant QgsSettingsEntryBase::formerValueAsVariant( const QStringList &dynamicKeyPartList ) const
227{
228 Q_ASSERT( mOptions.testFlag( Qgis::SettingsOption::SaveFormerValue ) );
229 QVariant defaultValueOverride = valueAsVariant( key( dynamicKeyPartList ) );
230 return QgsSettings().value( formerValuekey( dynamicKeyPartList ), defaultValueOverride );
231}
232
233
234bool QgsSettingsEntryBase::copyValueFromKey( const QString &key, const QStringList &dynamicKeyPartList, bool removeSettingAtKey ) const
235{
236 if ( exists( dynamicKeyPartList ) )
237 return false;
238
239 QgsSettings settings;
240
241 const QString oldCompleteKey = completeKeyPrivate( key, dynamicKeyPartList );
242
243 if ( settings.contains( oldCompleteKey ) )
244 {
245 QVariant oldValue = settings.value( oldCompleteKey, mDefaultValue );
246 setVariantValuePrivate( oldValue, dynamicKeyPartList );
247 if ( removeSettingAtKey )
248 settings.remove( oldCompleteKey );
249 return true;
250 }
251 return false;
252}
253
254void QgsSettingsEntryBase::copyValueToKey( const QString &key, const QStringList &dynamicKeyPartList ) const
255{
256 QgsSettings settings;
257 const QString completeKey = completeKeyPrivate( key, dynamicKeyPartList );
258 QgsSettings().setValue( completeKey, valueAsVariant( dynamicKeyPartList ) );
259}
260
261QString QgsSettingsEntryBase::formerValuekey( const QStringList &dynamicKeyPartList ) const
262{
263 return key( dynamicKeyPartList ) + QStringLiteral( "_formervalue" );
264}
265
266
267
268
269
SettingsOrigin
The setting origin describes where a setting is stored.
Definition: qgis.h:2702
bool copyValueFromKey(const QString &key, bool removeSettingAtKey=false) const
Copies the value from a given key if it exists.
bool exists(const QString &dynamicKeyPart=QString()) const
Returns true if the settings is contained in the underlying QSettings.
QVariant defaultValueAsVariant() const
Returns settings default value.
bool keyIsValid(const QString &key) const
Returns true if the provided key match the settings entry.
QString description() const
Returns the settings entry description.
QgsSettingsTreeNode * parent() const
Returns the parent tree element.
virtual ~QgsSettingsEntryBase()
Destructor for QgsSettingsEntryBase.
Q_DECL_DEPRECATED int section() const
Returns settings section.
Qgis::SettingsOrigin origin(const QStringList &dynamicKeyPartList) const
Returns the origin of the setting if it exists.
bool hasDynamicKey() const
Returns true if a part of the settings key is built dynamically.
void remove(const QString &dynamicKeyPart=QString()) const
Removes the settings from the underlying QSettings.
virtual Q_DECL_DEPRECATED bool setVariantValue(const QVariant &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
QString definitionKey() const
Returns settings entry defining key.
QVariant formerValueAsVariant(const QString &dynamicKeyPart) const
Returns the former value of the settings if it has been enabled in the options.
QVariant valueAsVariant(const QString &dynamicKeyPart=QString()) const
Returns settings value with.
bool setVariantValuePrivate(const QVariant &value, const QStringList &dynamicKeyPartList=QStringList()) const
Sets the settings value with a variant value.
void copyValueToKey(const QString &key, const QStringList &dynamicKeyPartList=QStringList()) const
Copies the settings to the given key.
QString key(const QString &dynamicKeyPart=QString()) const
Returns settings entry key.
static QStringList dynamicKeyPartToList(const QString &dynamicKeyPart)
Transforms a dynamic key part string to list.
QVariant valueAsVariantWithDefaultOverride(const QVariant &defaultValueOverride, const QString &dynamicKeyPart=QString()) const
Returns settings value with a defaultValueOverride.
QgsSettingsEntryBase(const QString &key, const QString &section, const QVariant &defaultValue=QVariant(), const QString &description=QString(), Qgis::SettingsOptions options=Qgis::SettingsOptions())
Constructor for QgsSettingsEntryBase.
QgsSettingsTreeNode is a tree node for the settings tree to help organizing and introspecting the tre...
QString completeKey() const
Returns the complete key of the node (including its parents)
void unregisterChildSetting(const QgsSettingsEntryBase *setting, bool deleteSettingValues=false, const QStringList &parentsNamedItems=QStringList())
Unregisters the child setting.
void registerChildSetting(const QgsSettingsEntryBase *setting, const QString &key) SIP_THROW(QgsSettingsException)
Registers a child setting.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:63
Qgis::SettingsOrigin origin(const QString &key) const
Returns the origin of the setting if it exists at the given key.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:4093
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:4092
#define QgsDebugMsg(str)
Definition: qgslogger.h:38