QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
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
18#include "qgslogger.h"
19#include "qgssettings.h"
20#include "qgssettingsproxy.h"
21#include "qgssettingstreenode.h"
22
23#include <QDir>
24#include <QRegularExpression>
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
30 : mParentTreeElement( parent )
31 , mName( key )
32 , mDefaultValue( defaultValue )
33 , mDescription( description )
34 , mOptions( options )
35{
36 mKey = QDir::cleanPath( u"%1/%2"_s.arg( parent ? parent->completeKey() : QString(), key ) );
37
38 if ( parent )
39 {
40 parent->registerChildSetting( this, key );
41 }
42}
43
45{
46 if ( mParentTreeElement )
47 mParentTreeElement->unregisterChildSetting( this );
48}
49
51{
52 return QString::fromUtf8( sSettingsTypeMetaEnum.valueToKey( static_cast<int>( settingsType() ) ) );
53}
54
55
56QString QgsSettingsEntryBase::key( const QString &dynamicKeyPart ) const
57{
58 return key( dynamicKeyPartToList( dynamicKeyPart ) );
59}
60
61QString QgsSettingsEntryBase::key( const QStringList &dynamicKeyPartList ) const
62{
63 return completeKeyPrivate( mKey, dynamicKeyPartList );
64}
65
66QString QgsSettingsEntryBase::completeKeyPrivate( const QString &key, const QStringList &dynamicKeyPartList ) const
67{
68 QString completeKey = key;
69
70 if ( dynamicKeyPartList.isEmpty() )
71 {
72 if ( hasDynamicKey() )
73 QgsDebugError( u"Settings '%1' have a dynamic key but the dynamic key part was not provided"_s.arg( completeKey ) );
74
75 return completeKey;
76 }
77 else
78 {
79 if ( !hasDynamicKey() )
80 {
81 QgsDebugError( u"Settings '%1' don't have a dynamic key, the provided dynamic key part will be ignored"_s.arg( completeKey ) );
82 return completeKey;
83 }
84
85 for ( int i = 0; i < dynamicKeyPartList.size(); i++ )
86 {
87 completeKey.replace( u"%"_s.append( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) );
88 }
89 }
90 return completeKey;
91}
92
93bool QgsSettingsEntryBase::keyIsValid( const QString &key ) const
94{
95 if ( !hasDynamicKey() )
96 {
97 if ( !key.contains( definitionKey() ) )
98 return false;
99 else
100 return key == definitionKey();
101 }
102
103 const thread_local QRegularExpression digitRx( u"%\\d+"_s );
104 const QRegularExpression regularExpression( definitionKey().replace( digitRx, u".*"_s ) );
105 const QRegularExpressionMatch regularExpressionMatch = regularExpression.match( key );
106 return regularExpressionMatch.hasMatch();
107}
108
110{
111 return mKey;
112}
113
115{
116 const thread_local QRegularExpression regularExpression( u"%\\d+"_s );
117 return mKey.contains( regularExpression );
118}
119
120bool QgsSettingsEntryBase::exists( const QString &dynamicKeyPart ) const
121{
122 return QgsSettings::get()->contains( key( dynamicKeyPart ) );
123}
124
125bool QgsSettingsEntryBase::exists( const QStringList &dynamicKeyPartList ) const
126{
127 return QgsSettings::get()->contains( key( dynamicKeyPartList ) );
128}
129
130Qgis::SettingsOrigin QgsSettingsEntryBase::origin( const QStringList &dynamicKeyPartList ) const
131{
132 return QgsSettings::get()->origin( key( dynamicKeyPartList ) );
133}
134
135void QgsSettingsEntryBase::remove( const QString &dynamicKeyPart ) const
136{
137 QgsSettings::get()->remove( key( dynamicKeyPart ) );
138}
139
140void QgsSettingsEntryBase::remove( const QStringList &dynamicKeyPartList ) const
141{
142 QgsSettings::get()->remove( key( dynamicKeyPartList ) );
143}
144
149
150bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QString &dynamicKeyPart ) const
151{
152 return setVariantValue( value, dynamicKeyPartToList( dynamicKeyPart ) );
153}
154
155bool QgsSettingsEntryBase::setVariantValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const
156{
157 mHasChanged = true;
158 auto settings = QgsSettings::get();
159 if ( mOptions.testFlag( Qgis::SettingsOption::SaveFormerValue ) )
160 {
161 if ( exists( dynamicKeyPartList ) )
162 {
163 QVariant currentValue = valueAsVariant( key( dynamicKeyPartList ) );
164 if ( value != currentValue )
165 {
166 settings->setValue( formerValuekey( dynamicKeyPartList ), currentValue );
167 }
168 }
169 }
170 settings->setValue( key( dynamicKeyPartList ), value );
171 return true;
172}
173
174QStringList QgsSettingsEntryBase::dynamicKeyPartToList( const QString &dynamicKeyPart )
175{
176 QStringList dynamicKeyPartList;
177 if ( !dynamicKeyPart.isNull() )
178 dynamicKeyPartList.append( dynamicKeyPart );
179 return dynamicKeyPartList;
180}
181
182QVariant QgsSettingsEntryBase::valueAsVariant( const QString &dynamicKeyPart ) const
183{
184 return valueAsVariant( dynamicKeyPartToList( dynamicKeyPart ) );
185}
186
187QVariant QgsSettingsEntryBase::valueAsVariant( const QStringList &dynamicKeyPartList ) const
188{
189 return QgsSettings::get()->value( key( dynamicKeyPartList ), mDefaultValue );
190}
191
192QVariant QgsSettingsEntryBase::valueAsVariant( const QString &dynamicKeyPart, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const
193{
195 return valueAsVariant( dynamicKeyPartToList( dynamicKeyPart ), useDefaultValueOverride, defaultValueOverride );
197}
198
199QVariant QgsSettingsEntryBase::valueAsVariant( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const
200{
201 if ( useDefaultValueOverride )
202 return QgsSettings::get()->value( key( dynamicKeyPartList ), defaultValueOverride );
203 else
204 return QgsSettings::get()->value( key( dynamicKeyPartList ), mDefaultValue );
205}
206
207QVariant QgsSettingsEntryBase::valueAsVariantWithDefaultOverride( const QVariant &defaultValueOverride, const QString &dynamicKeyPart ) const
208{
209 return QgsSettings::get()->value( key( dynamicKeyPart ), defaultValueOverride );
210}
211
212QVariant QgsSettingsEntryBase::valueAsVariantWithDefaultOverride( const QVariant &defaultValueOverride, const QStringList &dynamicKeyPartList ) const
213{
214 return QgsSettings::get()->value( key( dynamicKeyPartList ), defaultValueOverride );
215}
216
218{
219 return mDefaultValue;
220}
221
223{
224 return mDescription;
225}
226
227QVariant QgsSettingsEntryBase::formerValueAsVariant( const QString &dynamicKeyPart ) const
228{
229 return formerValueAsVariant( dynamicKeyPartToList( dynamicKeyPart ) );
230}
231
232QVariant QgsSettingsEntryBase::formerValueAsVariant( const QStringList &dynamicKeyPartList ) const
233{
234 Q_ASSERT( mOptions.testFlag( Qgis::SettingsOption::SaveFormerValue ) );
235 QVariant defaultValueOverride = valueAsVariant( key( dynamicKeyPartList ) );
236 return QgsSettings::get()->value( formerValuekey( dynamicKeyPartList ), defaultValueOverride );
237}
238
239bool QgsSettingsEntryBase::copyValueFromKey( const QString &key, const QStringList &dynamicKeyPartList, bool removeSettingAtKey ) const
240{
241 auto settings = QgsSettings::get();
242
243 const QString oldCompleteKey = completeKeyPrivate( key, dynamicKeyPartList );
244
245 if ( settings->contains( oldCompleteKey ) )
246 {
247 if ( !exists( dynamicKeyPartList ) )
248 {
249 QVariant oldValue = settings->value( oldCompleteKey, mDefaultValue );
250 // do not copy if it is equal to the default value
251 if ( oldValue != defaultValueAsVariant() )
252 setVariantValue( oldValue, dynamicKeyPartList );
253 }
254 if ( removeSettingAtKey )
255 settings->remove( oldCompleteKey );
256 return true;
257 }
258
259 return false;
260}
261
262void QgsSettingsEntryBase::copyValueToKey( const QString &key, const QStringList &dynamicKeyPartList ) const
263{
264 const QString completeKey = completeKeyPrivate( key, dynamicKeyPartList );
265 QgsSettings::get()->setValue( completeKey, valueAsVariant( dynamicKeyPartList ) );
266}
267
268void QgsSettingsEntryBase::copyValueToKeyIfChanged( const QString &key, const QStringList &dynamicKeyPartList ) const
269{
270 if ( hasChanged() )
271 {
272 copyValueToKey( key, dynamicKeyPartList );
273 }
274}
275
276QString QgsSettingsEntryBase::formerValuekey( const QStringList &dynamicKeyPartList ) const
277{
278 return key( dynamicKeyPartList ) + u"_formervalue"_s;
279}
@ SaveFormerValue
Save the former value of the settings.
Definition qgis.h:744
QFlags< SettingsOption > SettingsOptions
Definition qgis.h:748
SettingsOrigin
The setting origin describes where a setting is stored.
Definition qgis.h:4559
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.
Qgis::SettingsOptions options() const
Returns the settings options.
bool hasChanged() const
Returns true if the setting was changed during the current QGIS session.
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.
void copyValueToKeyIfChanged(const QString &key, const QStringList &dynamicKeyPartList=QStringList()) const
Copies the settings to the given key, if it has changed during the current QGIS session (see hasChang...
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.
A tree node for the settings tree to help organizing and introspecting the tree.
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.
static QgsSettingsProxy get()
Returns a proxy for a QgsSettings object.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:7486
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:7485
#define QgsDebugError(str)
Definition qgslogger.h:59