20 #include <QRegularExpression>
24 , mDefaultValue( defaultValue )
26 , mDescription( description )
33 , mDefaultValue( defaultValue )
35 , mDescription( description )
36 , mPluginName( pluginName )
46 QStringList dynamicKeyPartList;
47 if ( !dynamicKeyPart.isNull() )
48 dynamicKeyPartList.append( dynamicKeyPart );
50 return key( dynamicKeyPartList );
55 QString completeKey = mKey;
56 if ( !mPluginName.isEmpty() )
58 if ( !completeKey.startsWith(
'/' ) )
59 completeKey.prepend(
'/' );
60 completeKey.prepend( mPluginName );
63 if ( dynamicKeyPartList.isEmpty() )
66 QgsDebugMsg( QStringLiteral(
"Settings '%1' have a dynamic key but the dynamic key part was not provided" ).arg( completeKey ) );
74 QgsDebugMsg( QStringLiteral(
"Settings '%1' don't have a dynamic key, the provided dynamic key part will be ignored" ).arg( completeKey ) );
78 for (
int i = 0; i < dynamicKeyPartList.size(); i++ )
80 completeKey.replace( QStringLiteral(
"%" ).append( QString::number( i + 1 ) ), dynamicKeyPartList.at( i ) );
95 QString completeKeyToCheck =
key;
98 settingsPrefix.chop( 1 );
99 if ( !completeKeyToCheck.startsWith( settingsPrefix ) )
101 if ( !mPluginName.isEmpty()
102 && !completeKeyToCheck.startsWith( mPluginName ) )
104 if ( !completeKeyToCheck.startsWith(
'/' ) )
105 completeKeyToCheck.prepend(
'/' );
106 completeKeyToCheck.prepend( mPluginName );
109 if ( !completeKeyToCheck.startsWith(
'/' ) )
110 completeKeyToCheck.prepend(
'/' );
111 completeKeyToCheck.prepend( settingsPrefix );
116 if ( !prefixedSettingsKey.startsWith( settingsPrefix ) )
118 if ( !prefixedSettingsKey.startsWith(
'/' ) )
119 prefixedSettingsKey.prepend(
'/' );
120 prefixedSettingsKey.prepend( settingsPrefix );
124 return completeKeyToCheck == prefixedSettingsKey;
126 const QRegularExpression regularExpression( prefixedSettingsKey.replace( QRegularExpression( QStringLiteral(
"%\\d+" ) ), QStringLiteral(
".*" ) ) );
127 const QRegularExpressionMatch regularExpressionMatch = regularExpression.match( completeKeyToCheck );
128 return regularExpressionMatch.hasMatch();
133 QString completeKey = mKey;
134 if ( !mPluginName.isEmpty() )
136 if ( !completeKey.startsWith(
'/' ) )
137 completeKey.prepend(
'/' );
138 completeKey.prepend( mPluginName );
146 const thread_local QRegularExpression regularExpression( QStringLiteral(
"%\\d+" ) );
147 return mKey.contains( regularExpression );
177 QStringList dynamicKeyPartList;
178 if ( !dynamicKeyPart.isNull() )
179 dynamicKeyPartList.append( dynamicKeyPart );
194 QStringList dynamicKeyPartList;
195 if ( !dynamicKeyPart.isNull() )
196 dynamicKeyPartList.append( dynamicKeyPart );
198 return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride );
203 if ( useDefaultValueOverride )
205 defaultValueOverride,
215 return mDefaultValue;
252 return valueAsVariant( dynamicKeyPart, useDefaultValueOverride, defaultValueOverride );
257 return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride );
275 , mMinLength( minLength )
276 , mMaxLength( maxLength )
292 QStringList dynamicKeyPartList;
293 if ( !dynamicKeyPart.isNull() )
294 dynamicKeyPartList.append( dynamicKeyPart );
301 if (
value.length() < mMinLength )
303 QgsDebugMsg( QObject::tr(
"Can't set value for settings with key '%1'. String length '%2' is shorter than minimum length '%3'." )
305 .arg(
value.length() )
306 .arg( mMinLength ) );
311 &&
value.length() > mMaxLength )
313 QgsDebugMsg( QObject::tr(
"Can't set value for settings with key '%1'. String length '%2' is longer than maximum length '%3'." )
315 .arg(
value.length() )
316 .arg( mMinLength ) );
325 return valueAsVariant( dynamicKeyPart, useDefaultValueOverride, defaultValueOverride ).toString();
330 return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ).toString();
391 return valueAsVariant( dynamicKeyPart, useDefaultValueOverride, defaultValueOverride ).toStringList();
396 return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ).toStringList();
437 return valueAsVariant( dynamicKeyPart, useDefaultValueOverride, defaultValueOverride ).toBool();
442 return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ).toBool();
460 , mMinValue( minValue )
461 , mMaxValue( maxValue )
470 , mMinValue( std::numeric_limits<qlonglong>::min() )
471 , mMaxValue( std::numeric_limits<qlonglong>::max() )
477 QStringList dynamicKeyPartList;
478 if ( !dynamicKeyPart.isNull() )
479 dynamicKeyPartList.append( dynamicKeyPart );
486 if (
value < mMinValue )
488 QgsDebugMsg( QObject::tr(
"Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." )
490 QString::number(
value ),
491 QString::number( mMinValue ) ) );
495 if (
value > mMaxValue )
497 QgsDebugMsg( QObject::tr(
"Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." )
499 QString::number(
value ),
500 QString::number( mMinValue ) ) );
509 return valueAsVariant( dynamicKeyPart, useDefaultValueOverride, defaultValueOverride ).toLongLong();
514 return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ).toLongLong();
552 , mMinValue( minValue )
553 , mMaxValue( maxValue )
554 , mDisplayHintDecimals( displayDecimals )
563 , mMinValue( std::numeric_limits<double>::lowest() )
564 , mMaxValue( std::numeric_limits<double>::max() )
565 , mDisplayHintDecimals( 1 )
571 QStringList dynamicKeyPartList;
572 if ( !dynamicKeyPart.isNull() )
573 dynamicKeyPartList.append( dynamicKeyPart );
580 if (
value < mMinValue )
582 QgsDebugMsg( QObject::tr(
"Can't set value for settings with key '%1'. Value '%2' is less than minimum value '%3'." )
584 QString::number(
value ),
585 QString::number( mMinValue ) ) );
589 if (
value > mMaxValue )
591 QgsDebugMsg( QObject::tr(
"Can't set value for settings with key '%1'. Value '%2' is greather than maximum value '%3'." )
593 QString::number(
value ),
594 QString::number( mMinValue ) ) );
603 return valueAsVariant( dynamicKeyPart, useDefaultValueOverride, defaultValueOverride ).toDouble();
608 return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ).toDouble();
648 return mDisplayHintDecimals;
669 QStringList dynamicKeyPartList;
670 if ( !dynamicKeyPart.isNull() )
671 dynamicKeyPartList.append( dynamicKeyPart );
683 return valueAsVariant( dynamicKeyPart, useDefaultValueOverride, defaultValueOverride ).value<QColor>();
688 return valueAsVariant( dynamicKeyPartList, useDefaultValueOverride, defaultValueOverride ).value<QColor>();
Represent settings entry and provides methods for reading and writing settings values.
QgsSettings::Section section() const
Returns settings section.
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.
virtual ~QgsSettingsEntryBase()
Destructor for QgsSettingsEntryBase.
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.
QgsSettingsEntryBase(const QString &key, QgsSettings::Section section, const QVariant &defaultValue=QVariant(), const QString &description=QString())
Constructor for QgsSettingsEntryBase.
virtual bool setVariantValue(const QVariant &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
SettingsType
Types of settings entries.
@ Variant
Generic variant.
@ StringList
List of strings.
@ Double
Double precision numer.
QVariant valueAsVariant(const QString &dynamicKeyPart=QString(), bool useDefaultValueOverride=false, const QVariant &defaultValueOverride=QVariant()) const
Returns settings value.
QString definitionKey() const
Returns settings entry defining key.
QString key(const QString &dynamicKeyPart=QString()) const
Returns settings entry key.
bool value(const QString &dynamicKeyPart=QString(), bool useDefaultValueOverride=false, bool defaultValueOverride=false) const
Returns settings value.
QgsSettingsEntryBool(const QString &key, QgsSettings::Section section, bool defaultValue=false, const QString &description=QString())
Constructor for QgsSettingsEntryBool.
virtual SettingsType settingsType() const override
Returns the settings entry type.
bool setValue(bool value, const QString &dynamicKeyPart=QString()) const
Set settings value.
bool defaultValue() const
Returns settings default value.
bool setValue(const QColor &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
virtual SettingsType settingsType() const override
Returns the settings entry type.
QColor defaultValue() const
Returns settings default value.
QColor value(const QString &dynamicKeyPart=QString(), bool useDefaultValueOverride=false, const QString &defaultValueOverride=QString()) const
Returns settings value.
QgsSettingsEntryColor(const QString &key, QgsSettings::Section section, const QColor &defaultValue=QColor(), const QString &description=QString())
Constructor for QgsSettingsEntryColor.
void setDisplayHintDecimals(int displayHintDecimals)
Set the display hint decimals.
bool setValue(double value, const QString &dynamicKeyPart=QString()) const
Set settings value.
double value(const QString &dynamicKeyPart=QString(), bool useDefaultValueOverride=false, double defaultValueOverride=0.0) const
Returns settings value.
int displayHintDecimals() const
Returns how much decimals should be shown in the Gui.
QgsSettingsEntryDouble(const QString &key, QgsSettings::Section section, double defaultValue=0.0, const QString &description=QString(), double minValue=std::numeric_limits< double >::lowest(), double maxValue=std::numeric_limits< double >::max(), int displayDecimals=1)
Constructor for QgsSettingsEntryDouble.
double minValue() const
Returns the minimum value.
void setMinValue(double minValue)
Set the minimum value.
double maxValue() const
Returns the maximum value.
void setMaxValue(double maxValue)
Set the maximum value.
double defaultValue() const
Returns settings default value.
virtual SettingsType settingsType() const override
Returns the settings entry type.
qlonglong minValue()
Returns the minimum value.
bool setValue(qlonglong value, const QString &dynamicKeyPart=QString()) const
Set settings value.
QgsSettingsEntryInteger(const QString &key, QgsSettings::Section section, qlonglong defaultValue=0, const QString &description=QString(), qlonglong minValue=std::numeric_limits< qlonglong >::min(), qlonglong maxValue=std::numeric_limits< qlonglong >::max())
Constructor for QgsSettingsEntryInteger.
virtual SettingsType settingsType() const override
Returns the settings entry type.
qlonglong maxValue()
Returns the maximum value.
qlonglong defaultValue() const
Returns settings default value.
void setMaxValue(qlonglong maxValue)
Set the maximum value.
qlonglong value(const QString &dynamicKeyPart=QString(), bool useDefaultValueOverride=false, qlonglong defaultValueOverride=0) const
Returns settings value.
void setMinValue(qlonglong minValue)
Set the minimum value.
bool setValue(const QStringList &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
virtual SettingsType settingsType() const override
Returns the settings entry type.
QgsSettingsEntryStringList(const QString &key, QgsSettings::Section section, const QStringList &defaultValue=QStringList(), const QString &description=QString())
Constructor for QgsSettingsEntryStringList.
QStringList value(const QString &dynamicKeyPart=QString(), bool useDefaultValueOverride=false, const QStringList &defaultValueOverride=QStringList()) const
Returns settings value.
QStringList defaultValue() const
Returns settings default value.
QString defaultValue() const
Returns settings default value.
QString value(const QString &dynamicKeyPart=QString(), bool useDefaultValueOverride=false, const QString &defaultValueOverride=QString()) const
Returns settings value.
virtual SettingsType settingsType() const override
Returns the settings entry type.
void setMinLength(int minLength)
Set the string minimum length.
QgsSettingsEntryString(const QString &key, QgsSettings::Section section, const QString &defaultValue=QString(), const QString &description=QString(), int minLength=0, int maxLength=-1)
Constructor for QgsSettingsEntryString.
int maxLength()
Returns the string maximum length.
void setMaxLength(int maxLength)
Set the string maximum length.
int minLength()
Returns the string minimum length.
bool setValue(const QString &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
QVariant defaultValue() const
Returns settings default value.
virtual SettingsType settingsType() const override
Returns the settings entry type.
QgsSettingsEntryVariant(const QString &key, QgsSettings::Section section, const QVariant &defaultValue=QVariant(), const QString &description=QString())
Constructor for QgsSettingsEntryVariant.
QVariant value(const QString &dynamicKeyPart=QString(), bool useDefaultValueOverride=false, const QVariant &defaultValueOverride=QVariant()) const
Returns settings value.
bool setValue(const QVariant &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
This class is a composition of two QSettings instances:
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.
QString prefixedKey(const QString &key, QgsSettings::Section section) const
Returns the sanitized and prefixed key.
Section
Sections for namespaced settings.
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.