QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgssettingsentry.h
Go to the documentation of this file.
1/***************************************************************************
2 qgssettingsentry.h
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#ifndef QGSSETTINGSENTRY_H
17#define QGSSETTINGSENTRY_H
18
19#include <QString>
20#include <QColor>
21#include <limits>
22
23#include "qgis.h"
24#include "qgis_core.h"
25#include "qgis_sip.h"
26
27
29
30
31static const inline QMetaEnum sSettingsTypeMetaEnum = QMetaEnum::fromType<Qgis::SettingsType>() SIP_SKIP;
32
33
44class CORE_EXPORT QgsSettingsEntryBase
45{
46
47#ifdef SIP_RUN
49 if ( dynamic_cast< QgsSettingsEntryVariant * >( sipCpp ) )
50 sipType = sipType_QgsSettingsEntryVariant;
51 else if ( dynamic_cast< QgsSettingsEntryString * >( sipCpp ) )
52 sipType = sipType_QgsSettingsEntryString;
53 else if ( dynamic_cast< QgsSettingsEntryStringList * >( sipCpp ) )
54 sipType = sipType_QgsSettingsEntryStringList;
55 else if ( dynamic_cast< QgsSettingsEntryVariantMap * >( sipCpp ) )
56 sipType = sipType_QgsSettingsEntryVariantMap;
57 else if ( dynamic_cast< QgsSettingsEntryBool * >( sipCpp ) )
58 sipType = sipType_QgsSettingsEntryBool;
59 else if ( dynamic_cast< QgsSettingsEntryInteger * >( sipCpp ) )
60 sipType = sipType_QgsSettingsEntryInteger;
61 else if ( dynamic_cast< QgsSettingsEntryDouble * >( sipCpp ) )
62 sipType = sipType_QgsSettingsEntryDouble;
63 else if ( dynamic_cast< QgsSettingsEntryColor * >( sipCpp ) )
64 sipType = sipType_QgsSettingsEntryColor;
65 else if ( dynamic_cast< QgsSettingsEntryBase * >( sipCpp ) )
66 sipType = sipType_QgsSettingsEntryBase;
67 else
68 sipType = NULL;
70#endif
71
72 public:
73
78 static QStringList dynamicKeyPartToList( const QString &dynamicKeyPart );
79
89 QgsSettingsEntryBase( const QString &key,
90 const QString &section,
91 const QVariant &defaultValue = QVariant(),
92 const QString &description = QString(),
93 Qgis::SettingsOptions options = Qgis::SettingsOptions() )
94 : mName( key )
95 , mKey( QStringLiteral( "%1/%2" ).arg( section, key ) )
96 , mDefaultValue( defaultValue )
97 , mDescription( description )
98 , mOptions( options )
99 {}
100
113 QgsSettingsEntryBase( const QString &name,
115 const QVariant &defaultValue = QVariant(),
116 const QString &description = QString(),
117 Qgis::SettingsOptions options = Qgis::SettingsOptions() ) SIP_THROW( QgsSettingsException );
118
122 virtual ~QgsSettingsEntryBase();
123
129 virtual QString typeId() const;
130
135 QString name() const {return mName;}
136
142 QString key( const QString &dynamicKeyPart = QString() ) const;
143
149 QString key( const QStringList &dynamicKeyPartList ) const;
150
160 bool keyIsValid( const QString &key ) const;
161
167 QString definitionKey() const;
168
172 bool hasDynamicKey() const;
173
178 Qgis::SettingsOptions options() const {return mOptions;}
179
185 bool exists( const QString &dynamicKeyPart = QString() ) const;
186
192 bool exists( const QStringList &dynamicKeyPartList ) const;
193
199 Qgis::SettingsOrigin origin( const QStringList &dynamicKeyPartList ) const;
200
206 void remove( const QString &dynamicKeyPart = QString() ) const;
207
213 void remove( const QStringList &dynamicKeyPartList ) const;
214
219 Q_DECL_DEPRECATED int section() const;
220
227 bool setVariantValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;
228
235 bool setVariantValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const;
236
238 QVariant valueAsVariant( const QString &dynamicKeyPart = QString() ) const;
239
241 QVariant valueAsVariant( const QStringList &dynamicKeyPartList ) const;
242
247 QVariant valueAsVariantWithDefaultOverride( const QVariant &defaultValueOverride, const QString &dynamicKeyPart = QString() ) const;
248
256 QVariant valueAsVariantWithDefaultOverride( const QVariant &defaultValueOverride, const QStringList &dynamicKeyPartList ) const;
257
262 Q_DECL_DEPRECATED QVariant valueAsVariant( const QString &dynamicKeyPart, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const SIP_DEPRECATED;
263
268 Q_DECL_DEPRECATED QVariant valueAsVariant( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const SIP_DEPRECATED;
269
270
274 QVariant defaultValueAsVariant() const;
275
280 // This cannot be pure virtual otherwise SIP is failing
281
285 QString description() const;
286
292 QVariant formerValueAsVariant( const QString &dynamicKeyPart ) const;
293
299 QVariant formerValueAsVariant( const QStringList &dynamicKeyPartList ) const;
300
308 bool copyValueFromKey( const QString &key, bool removeSettingAtKey = false ) const {return copyValueFromKey( key, {}, removeSettingAtKey );}
309
318 bool copyValueFromKey( const QString &key, const QStringList &dynamicKeyPartList, bool removeSettingAtKey = false ) const;
319
326 void copyValueToKey( const QString &key, const QStringList &dynamicKeyPartList = QStringList() ) const;
327
332 QgsSettingsTreeNode *parent() const {return mParentTreeElement;}
333
335 virtual bool checkValueVariant( const QVariant &value ) const
336 {
337 Q_UNUSED( value )
338 return true;
339 }
340
341 private:
342 QString formerValuekey( const QStringList &dynamicKeyPartList ) const;
343
344 QString completeKeyPrivate( const QString &key, const QStringList &dynamicKeyPartList ) const;
345
346 QgsSettingsTreeNode *mParentTreeElement = nullptr;
347 QString mName;
348 QString mKey;
349 QVariant mDefaultValue;
350 QString mDescription;
351 Qgis::SettingsOptions mOptions;
352};
353
363template<class T>
365{
366 public:
367
382 const QVariant &defaultValue,
383 const QString &description = QString(),
384 Qgis::SettingsOptions options = Qgis::SettingsOptions() )
386 {}
387
398 const QString &section,
399 const QVariant &defaultValue,
400 const QString &description = QString(),
401 Qgis::SettingsOptions options = Qgis::SettingsOptions() )
403 {}
404
405
406 virtual Qgis::SettingsType settingsType() const override = 0;
407
413 T value( const QString &dynamicKeyPart = QString() ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPart ) );}
414
420 T value( const QStringList &dynamicKeyPartList ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPartList ) );}
421
422
424 inline T valueWithDefaultOverride( const T &defaultValueOverride, const QString &dynamicKeyPart = QString() ) const
425 {
426 return this->convertFromVariant( valueAsVariantWithDefaultOverride( convertToVariant( defaultValueOverride ), dynamicKeyPart ) );
427 }
428
430 inline T valueWithDefaultOverride( const T &defaultValueOverride, const QStringList &dynamicKeyPartList ) const
431 {
432 return this->convertFromVariant( valueAsVariantWithDefaultOverride( convertToVariant( defaultValueOverride ), dynamicKeyPartList ) );
433 }
434
441 bool setValue( const T &value, const QString &dynamicKeyPart = QString() ) const
442 {
443 return setValuePrivate( value, dynamicKeyPartToList( dynamicKeyPart ) );
444 }
445
452 bool setValue( const T &value, const QStringList &dynamicKeyPartList ) const
453 {
454 return setValuePrivate( value, dynamicKeyPartList );
455 }
456
459
464 T formerValue( const QString &dynamicKeyPart = QString() ) const {return convertFromVariant( formerValueAsVariant( dynamicKeyPart ) );}
465
470 T formerValue( const QStringList &dynamicKeyPartList ) const {return convertFromVariant( formerValueAsVariant( dynamicKeyPartList ) );}
471
472 bool checkValueVariant( const QVariant &value ) const override
473 {
475 }
476
478 virtual T convertFromVariant( const QVariant &value ) const = 0;
479
480 protected:
482 virtual bool setValuePrivate( const T &value, const QStringList &dynamicKeyPartList ) const
483 {
484 if ( checkValuePrivate( value ) )
485 return QgsSettingsEntryBase::setVariantValue( convertToVariant( value ), dynamicKeyPartList );
486 else
487 return false;
488 }
489
491 virtual QVariant convertToVariant( const T &value ) const
492 {
493 return QVariant::fromValue( value );
494 }
495
497 virtual bool checkValuePrivate( const T &value ) const
498 {
499 Q_UNUSED( value )
500 return true;
501 }
502};
503
504
505
506
507#endif // QGSSETTINGSENTRY_H
SettingsType
Types of settings entries.
Definition qgis.h:393
@ Custom
Custom implementation.
SettingsOrigin
The setting origin describes where a setting is stored.
Definition qgis.h:3093
Base abstract class for settings entries with typed get and set methods.
QgsSettingsEntryBaseTemplate(const QString &name, QgsSettingsTreeNode *parent, const QVariant &defaultValue, const QString &description=QString(), Qgis::SettingsOptions options=Qgis::SettingsOptions())
Constructor for QgsSettingsEntryByReference.
virtual bool setValuePrivate(const T &value, const QStringList &dynamicKeyPartList) const
Sets the settings value with an optional list of dynamic parts.
T valueWithDefaultOverride(const T &defaultValueOverride, const QString &dynamicKeyPart=QString()) const
Returns the settings value with a defaultValueOverride and with an optional dynamicKeyPart.
T defaultValue() const
Returns settings default value.
virtual T convertFromVariant(const QVariant &value) const =0
Converts the variant value to the value type of the setting.
virtual Qgis::SettingsType settingsType() const override=0
Returns the settings entry type.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
QgsSettingsEntryBaseTemplate(const QString &key, const QString &section, const QVariant &defaultValue, const QString &description=QString(), Qgis::SettingsOptions options=Qgis::SettingsOptions())
Constructor for QgsSettingsEntryByReference.
virtual QVariant convertToVariant(const T &value) const
Converts the value to a variant.
bool checkValueVariant(const QVariant &value) const override
Returns true if the given value is valid towards the setting definition.
T value(const QStringList &dynamicKeyPartList) const
Returns settings value.
bool setValue(const T &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
bool setValue(const T &value, const QStringList &dynamicKeyPartList) const
Set settings value.
virtual bool checkValuePrivate(const T &value) const
Check if the value is valid.
T formerValue(const QString &dynamicKeyPart=QString()) const
Returns the former value.
T valueWithDefaultOverride(const T &defaultValueOverride, const QStringList &dynamicKeyPartList) const
Returns the settings value with a defaultValueOverride for the dynamicKeyPartList.
T formerValue(const QStringList &dynamicKeyPartList) const
Returns the former value Returns the current value (or default) if there is no former value.
Represent settings entry and provides methods for reading and writing settings values.
bool copyValueFromKey(const QString &key, bool removeSettingAtKey=false) const
Copies the value from a given key if it exists.
Qgis::SettingsOptions options() const
Returns the settings options.
QVariant defaultValueAsVariant() const
Returns settings default value.
QString description() const
Returns the settings entry description.
QgsSettingsTreeNode * parent() const
Returns the parent tree element.
Q_DECL_DEPRECATED int section() const
Returns settings section.
virtual bool checkValueVariant(const QVariant &value) const
Returns true if the given value is valid towards the setting definition.
bool setVariantValue(const QVariant &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
QString name() const
Returns the name of the settings.
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.
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 boolean settings entry.
A color settings entry.
A double settings entry.
An integer settings entry.
A string list settings entry.
A string settings entry.
A string list settings entry.
A variant settings entry.
Custom exception class for settings related exceptions.
QgsSettingsTreeNode is a tree node for the settings tree to help organizing and introspecting the tre...
QString key() const
Returns the key of the node (without its parents)
QgsSettingsTreeNode * parent() const
Returns the parent of the node or nullptr if it does not exists.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:186
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_THROW(name,...)
Definition qgis_sip.h:198
#define SIP_END
Definition qgis_sip.h:203