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