QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
119 virtual ~QgsSettingsEntryBase();
120
126 virtual QString typeId() const;
127
132 QString name() const {return mName;}
133
139 QString key( const QString &dynamicKeyPart = QString() ) const;
140
146 QString key( const QStringList &dynamicKeyPartList ) const;
147
157 bool keyIsValid( const QString &key ) const;
158
164 QString definitionKey() const;
165
169 bool hasDynamicKey() const;
170
175 Qgis::SettingsOptions options() const {return mOptions;}
176
182 bool exists( const QString &dynamicKeyPart = QString() ) const;
183
189 bool exists( const QStringList &dynamicKeyPartList ) const;
190
196 Qgis::SettingsOrigin origin( const QStringList &dynamicKeyPartList ) const;
197
203 void remove( const QString &dynamicKeyPart = QString() ) const;
204
210 void remove( const QStringList &dynamicKeyPartList ) const;
211
216 Q_DECL_DEPRECATED int section() const;
217
224 bool setVariantValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const;
225
232 bool setVariantValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const;
233
235 QVariant valueAsVariant( const QString &dynamicKeyPart = QString() ) const;
236
238 QVariant valueAsVariant( const QStringList &dynamicKeyPartList ) const;
239
244 QVariant valueAsVariantWithDefaultOverride( const QVariant &defaultValueOverride, const QString &dynamicKeyPart = QString() ) const;
245
253 QVariant valueAsVariantWithDefaultOverride( const QVariant &defaultValueOverride, const QStringList &dynamicKeyPartList ) const;
254
259 Q_DECL_DEPRECATED QVariant valueAsVariant( const QString &dynamicKeyPart, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const SIP_DEPRECATED;
260
265 Q_DECL_DEPRECATED QVariant valueAsVariant( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const SIP_DEPRECATED;
266
267
271 QVariant defaultValueAsVariant() const;
272
277 // This cannot be pure virtual otherwise SIP is failing
278
282 QString description() const;
283
289 QVariant formerValueAsVariant( const QString &dynamicKeyPart ) const;
290
296 QVariant formerValueAsVariant( const QStringList &dynamicKeyPartList ) const;
297
305 bool copyValueFromKey( const QString &key, bool removeSettingAtKey = false ) const {return copyValueFromKey( key, {}, removeSettingAtKey );}
306
315 bool copyValueFromKey( const QString &key, const QStringList &dynamicKeyPartList, bool removeSettingAtKey = false ) const;
316
323 void copyValueToKey( const QString &key, const QStringList &dynamicKeyPartList = QStringList() ) const;
324
333 void copyValueToKeyIfChanged( const QString &key, const QStringList &dynamicKeyPartList = QStringList() ) const;
334
339 QgsSettingsTreeNode *parent() const {return mParentTreeElement;}
340
342 virtual bool checkValueVariant( const QVariant &value ) const
343 {
344 Q_UNUSED( value )
345 return true;
346 }
347
353 bool hasChanged() const { return mHasChanged; }
354
355 private:
356 QString formerValuekey( const QStringList &dynamicKeyPartList ) const;
357
358 QString completeKeyPrivate( const QString &key, const QStringList &dynamicKeyPartList ) const;
359
360 QgsSettingsTreeNode *mParentTreeElement = nullptr;
361 QString mName;
362 QString mKey;
363 QVariant mDefaultValue;
364 QString mDescription;
365 Qgis::SettingsOptions mOptions;
366 mutable bool mHasChanged = false;
367};
368
378template<class T>
380{
381 public:
382
402
413 const QString &section,
414 const QVariant &defaultValue,
415 const QString &description = QString(),
418 {}
419
420
421 virtual Qgis::SettingsType settingsType() const override = 0;
422
428 T value( const QString &dynamicKeyPart = QString() ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPart ) );}
429
435 T value( const QStringList &dynamicKeyPartList ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPartList ) );}
436
437
439 inline T valueWithDefaultOverride( const T &defaultValueOverride, const QString &dynamicKeyPart = QString() ) const
440 {
441 return this->convertFromVariant( valueAsVariantWithDefaultOverride( convertToVariant( defaultValueOverride ), dynamicKeyPart ) );
442 }
443
445 inline T valueWithDefaultOverride( const T &defaultValueOverride, const QStringList &dynamicKeyPartList ) const
446 {
447 return this->convertFromVariant( valueAsVariantWithDefaultOverride( convertToVariant( defaultValueOverride ), dynamicKeyPartList ) );
448 }
449
456 bool setValue( const T &value, const QString &dynamicKeyPart = QString() ) const
457 {
458 return setValuePrivate( value, dynamicKeyPartToList( dynamicKeyPart ) );
459 }
460
467 bool setValue( const T &value, const QStringList &dynamicKeyPartList ) const
468 {
469 return setValuePrivate( value, dynamicKeyPartList );
470 }
471
474
479 T formerValue( const QString &dynamicKeyPart = QString() ) const {return convertFromVariant( formerValueAsVariant( dynamicKeyPart ) );}
480
485 T formerValue( const QStringList &dynamicKeyPartList ) const {return convertFromVariant( formerValueAsVariant( dynamicKeyPartList ) );}
486
487 bool checkValueVariant( const QVariant &value ) const override
488 {
490 }
491
493 virtual T convertFromVariant( const QVariant &value ) const = 0;
494
495 protected:
497 virtual bool setValuePrivate( const T &value, const QStringList &dynamicKeyPartList ) const
498 {
499 if ( checkValuePrivate( value ) )
500 return QgsSettingsEntryBase::setVariantValue( convertToVariant( value ), dynamicKeyPartList );
501 else
502 return false;
503 }
504
506 virtual QVariant convertToVariant( const T &value ) const
507 {
508 return QVariant::fromValue( value );
509 }
510
512 virtual bool checkValuePrivate( const T &value ) const
513 {
514 Q_UNUSED( value )
515 return true;
516 }
517};
518
519
520
521
522#endif // QGSSETTINGSENTRY_H
SettingsType
Types of settings entries.
Definition qgis.h:599
@ Custom
Custom implementation.
QFlags< SettingsOption > SettingsOptions
Definition qgis.h:693
SettingsOrigin
The setting origin describes where a setting is stored.
Definition qgis.h:4168
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