QGIS API Documentation 3.38.0-Grenoble (exported)
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(),
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(),
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
336 void copyValueToKeyIfChanged( const QString &key, const QStringList &dynamicKeyPartList = QStringList() ) const;
337
342 QgsSettingsTreeNode *parent() const {return mParentTreeElement;}
343
345 virtual bool checkValueVariant( const QVariant &value ) const
346 {
347 Q_UNUSED( value )
348 return true;
349 }
350
356 bool hasChanged() const { return mHasChanged; }
357
358 private:
359 QString formerValuekey( const QStringList &dynamicKeyPartList ) const;
360
361 QString completeKeyPrivate( const QString &key, const QStringList &dynamicKeyPartList ) const;
362
363 QgsSettingsTreeNode *mParentTreeElement = nullptr;
364 QString mName;
365 QString mKey;
366 QVariant mDefaultValue;
367 QString mDescription;
368 Qgis::SettingsOptions mOptions;
369 mutable bool mHasChanged = false;
370};
371
381template<class T>
383{
384 public:
385
405
416 const QString &section,
417 const QVariant &defaultValue,
418 const QString &description = QString(),
421 {}
422
423
424 virtual Qgis::SettingsType settingsType() const override = 0;
425
431 T value( const QString &dynamicKeyPart = QString() ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPart ) );}
432
438 T value( const QStringList &dynamicKeyPartList ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPartList ) );}
439
440
442 inline T valueWithDefaultOverride( const T &defaultValueOverride, const QString &dynamicKeyPart = QString() ) const
443 {
444 return this->convertFromVariant( valueAsVariantWithDefaultOverride( convertToVariant( defaultValueOverride ), dynamicKeyPart ) );
445 }
446
448 inline T valueWithDefaultOverride( const T &defaultValueOverride, const QStringList &dynamicKeyPartList ) const
449 {
450 return this->convertFromVariant( valueAsVariantWithDefaultOverride( convertToVariant( defaultValueOverride ), dynamicKeyPartList ) );
451 }
452
459 bool setValue( const T &value, const QString &dynamicKeyPart = QString() ) const
460 {
461 return setValuePrivate( value, dynamicKeyPartToList( dynamicKeyPart ) );
462 }
463
470 bool setValue( const T &value, const QStringList &dynamicKeyPartList ) const
471 {
472 return setValuePrivate( value, dynamicKeyPartList );
473 }
474
477
482 T formerValue( const QString &dynamicKeyPart = QString() ) const {return convertFromVariant( formerValueAsVariant( dynamicKeyPart ) );}
483
488 T formerValue( const QStringList &dynamicKeyPartList ) const {return convertFromVariant( formerValueAsVariant( dynamicKeyPartList ) );}
489
490 bool checkValueVariant( const QVariant &value ) const override
491 {
493 }
494
496 virtual T convertFromVariant( const QVariant &value ) const = 0;
497
498 protected:
500 virtual bool setValuePrivate( const T &value, const QStringList &dynamicKeyPartList ) const
501 {
502 if ( checkValuePrivate( value ) )
503 return QgsSettingsEntryBase::setVariantValue( convertToVariant( value ), dynamicKeyPartList );
504 else
505 return false;
506 }
507
509 virtual QVariant convertToVariant( const T &value ) const
510 {
511 return QVariant::fromValue( value );
512 }
513
515 virtual bool checkValuePrivate( const T &value ) const
516 {
517 Q_UNUSED( value )
518 return true;
519 }
520};
521
522
523
524
525#endif // QGSSETTINGSENTRY_H
SettingsType
Types of settings entries.
Definition qgis.h:445
@ Custom
Custom implementation.
QFlags< SettingsOption > SettingsOptions
Definition qgis.h:539
SettingsOrigin
The setting origin describes where a setting is stored.
Definition qgis.h:3871
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.
bool hasChanged() const
Returns true if the setting was changed during the current QGIS session.
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:191
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_THROW(name,...)
Definition qgis_sip.h:203
#define SIP_END
Definition qgis_sip.h:208