QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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
47{
48 return QString::fromUtf8( sSettingsTypeMetaEnum.valueToKey( static_cast<int>( settingsType() ) ) );
49}
50
51
52QString QgsSettingsEntryBase::key( const QString &dynamicKeyPart ) const
53{
54 return key( dynamicKeyPartToList( dynamicKeyPart ) );
55}
56
57QString QgsSettingsEntryBase::key( const QStringList &dynamicKeyPartList ) const
58{
59 return completeKeyPrivate( mKey, dynamicKeyPartList );
60}
61
62QString QgsSettingsEntryBase::completeKeyPrivate( const QString &key, const QStringList &dynamicKeyPartList ) const
63{
64 QString completeKey = key;
65
66 if ( dynamicKeyPartList.isEmpty() )
67 {
68 if ( hasDynamicKey() )
69 QgsDebugError( QStringLiteral( "Settings '%1' have a dynamic key but the dynamic key part was not provided" ).arg( completeKey ) );
70
71 return completeKey;
72 }
73 else
74 {
75 if ( !hasDynamicKey() )
76 {
77 QgsDebugError( QStringLiteral( "Settings '%1' don't have a dynamic key, the provided dynamic key part will be ignored" ).arg( completeKey ) );
78 return completeKey;
79 }
80
81 for ( int i = 0; i < dynamicKeyPartList.size(); i++ )
82 {
83 completeKey.replace( QStringLiteral( "%" ).append( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) );
84 }
85 }
86 return completeKey;
87}
88
89bool QgsSettingsEntryBase::keyIsValid( const QString &key ) const
90{
91 if ( !hasDynamicKey() )
92 {
93 if ( !key.contains( definitionKey() ) )
94 return false;
95 else
96 return key == definitionKey();
97 }
98
99 const thread_local QRegularExpression digitRx( QStringLiteral( "%\\d+" ) );
100 const QRegularExpression regularExpression( definitionKey().replace( digitRx, QStringLiteral( ".*" ) ) );
101 const QRegularExpressionMatch regularExpressionMatch = regularExpression.match( key );
102 return regularExpressionMatch.hasMatch();
103}
104
106{
107 return mKey;
108}
109
111{
112 const thread_local QRegularExpression regularExpression( QStringLiteral( "%\\d+" ) );
113 return mKey.contains( regularExpression );
114}
115
116bool QgsSettingsEntryBase::exists( const QString &dynamicKeyPart ) const
117{
118 return QgsSettings().contains( key( dynamicKeyPart ) );
119}
120
121bool QgsSettingsEntryBase::exists( const QStringList &dynamicKeyPartList ) const
122{
123 return QgsSettings().contains( key( dynamicKeyPartList ) );
124}
125
126Qgis::SettingsOrigin QgsSettingsEntryBase::origin( const QStringList &dynamicKeyPartList ) const
127{
128 return QgsSettings().origin( key( dynamicKeyPartList ) );
129}
130
131void QgsSettingsEntryBase::remove( const QString &dynamicKeyPart ) const
132{
133 QgsSettings().remove( key( dynamicKeyPart ) );
134}
135
136void QgsSettingsEntryBase::remove( const QStringList &dynamicKeyPartList ) const
137{
138 QgsSettings().remove( key( dynamicKeyPartList ) );
139}
140
142{
144}
145
146bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QString &dynamicKeyPart ) const
147{
148 return setVariantValue( value, dynamicKeyPartToList( dynamicKeyPart ) );
149}
150
151bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const
152{
153 if ( mOptions.testFlag( Qgis::SettingsOption::SaveFormerValue ) )
154 {
155 if ( exists( dynamicKeyPartList ) )
156 {
157 QVariant currentValue = valueAsVariant( key( dynamicKeyPartList ) );
158 if ( value != currentValue )
159 {
160 QgsSettings().setValue( formerValuekey( dynamicKeyPartList ), currentValue );
161 }
162 }
163 }
164 QgsSettings().setValue( key( dynamicKeyPartList ), value );
165 return true;
166}
167
168QStringList QgsSettingsEntryBase::dynamicKeyPartToList( const QString &dynamicKeyPart )
169{
170 QStringList dynamicKeyPartList;
171 if ( !dynamicKeyPart.isNull() )
172 dynamicKeyPartList.append( dynamicKeyPart );
173 return dynamicKeyPartList;
174}
175
176QVariant QgsSettingsEntryBase::valueAsVariant( const QString &dynamicKeyPart ) const
177{
178 return valueAsVariant( dynamicKeyPartToList( dynamicKeyPart ) );
179}
180
181QVariant QgsSettingsEntryBase::valueAsVariant( const QStringList &dynamicKeyPartList ) const
182{
183 return QgsSettings().value( key( dynamicKeyPartList ), mDefaultValue );
184}
185
186QVariant QgsSettingsEntryBase::valueAsVariant( const QString &dynamicKeyPart, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const
187{
189 return valueAsVariant( dynamicKeyPartToList( dynamicKeyPart ), useDefaultValueOverride, defaultValueOverride );
191}
192
193QVariant QgsSettingsEntryBase::valueAsVariant( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const
194{
195 if ( useDefaultValueOverride )
196 return QgsSettings().value( key( dynamicKeyPartList ), defaultValueOverride );
197 else
198 return QgsSettings().value( key( dynamicKeyPartList ), mDefaultValue );
199}
200
201QVariant QgsSettingsEntryBase::valueAsVariantWithDefaultOverride( const QVariant &defaultValueOverride, const QString &dynamicKeyPart ) const
202{
203 return QgsSettings().value( key( dynamicKeyPart ), defaultValueOverride );
204}
205
206QVariant QgsSettingsEntryBase::valueAsVariantWithDefaultOverride( const QVariant &defaultValueOverride, const QStringList &dynamicKeyPartList ) const
207{
208 return QgsSettings().value( key( dynamicKeyPartList ), defaultValueOverride );
209}
210
211
213{
214 return mDefaultValue;
215}
216
218{
219 return mDescription;
220}
221
222QVariant QgsSettingsEntryBase::formerValueAsVariant( const QString &dynamicKeyPart ) const
223{
224 return formerValueAsVariant( dynamicKeyPartToList( dynamicKeyPart ) );
225}
226
227QVariant QgsSettingsEntryBase::formerValueAsVariant( const QStringList &dynamicKeyPartList ) const
228{
229 Q_ASSERT( mOptions.testFlag( Qgis::SettingsOption::SaveFormerValue ) );
230 QVariant defaultValueOverride = valueAsVariant( key( dynamicKeyPartList ) );
231 return QgsSettings().value( formerValuekey( dynamicKeyPartList ), defaultValueOverride );
232}
233
234
235bool QgsSettingsEntryBase::copyValueFromKey( const QString &key, const QStringList &dynamicKeyPartList, bool removeSettingAtKey ) const
236{
237 if ( exists( dynamicKeyPartList ) )
238 return false;
239
240 QgsSettings settings;
241
242 const QString oldCompleteKey = completeKeyPrivate( key, dynamicKeyPartList );
243
244 if ( settings.contains( oldCompleteKey ) )
245 {
246 QVariant oldValue = settings.value( oldCompleteKey, mDefaultValue );
247 setVariantValue( oldValue, dynamicKeyPartList );
248 if ( removeSettingAtKey )
249 settings.remove( oldCompleteKey );
250 return true;
251 }
252 return false;
253}
254
255void QgsSettingsEntryBase::copyValueToKey( const QString &key, const QStringList &dynamicKeyPartList ) const
256{
257 QgsSettings settings;
258 const QString completeKey = completeKeyPrivate( key, dynamicKeyPartList );
259 QgsSettings().setValue( completeKey, valueAsVariant( dynamicKeyPartList ) );
260}
261
262QString QgsSettingsEntryBase::formerValuekey( const QStringList &dynamicKeyPartList ) const
263{
264 return key( dynamicKeyPartList ) + QStringLiteral( "_formervalue" );
265}
266
267
268
269
270
SettingsOrigin
The setting origin describes where a setting is stored.
Definition: qgis.h:2907
virtual QString typeId() const
Returns the id of the type of settings This can be re-implemented in a custom implementation of a set...
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.
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.
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.
virtual Qgis::SettingsType settingsType() const
Returns the settings entry type.
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:4572
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:4571
#define QgsDebugError(str)
Definition: qgslogger.h:38