QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
41class CORE_EXPORT QgsSettingsEntryBase
42{
43
44#ifdef SIP_RUN
46 if ( dynamic_cast< QgsSettingsEntryVariant * >( sipCpp ) )
47 sipType = sipType_QgsSettingsEntryVariant;
48 else if ( dynamic_cast< QgsSettingsEntryString * >( sipCpp ) )
49 sipType = sipType_QgsSettingsEntryString;
50 else if ( dynamic_cast< QgsSettingsEntryStringList * >( sipCpp ) )
51 sipType = sipType_QgsSettingsEntryStringList;
52 else if ( dynamic_cast< QgsSettingsEntryVariantMap * >( sipCpp ) )
53 sipType = sipType_QgsSettingsEntryVariantMap;
54 else if ( dynamic_cast< QgsSettingsEntryBool * >( sipCpp ) )
55 sipType = sipType_QgsSettingsEntryBool;
56 else if ( dynamic_cast< QgsSettingsEntryInteger * >( sipCpp ) )
57 sipType = sipType_QgsSettingsEntryInteger;
58 else if ( dynamic_cast< QgsSettingsEntryDouble * >( sipCpp ) )
59 sipType = sipType_QgsSettingsEntryDouble;
60 else if ( dynamic_cast< QgsSettingsEntryColor * >( sipCpp ) )
61 sipType = sipType_QgsSettingsEntryColor;
62 else if ( dynamic_cast< QgsSettingsEntryBase * >( sipCpp ) )
63 sipType = sipType_QgsSettingsEntryBase;
64 else
65 sipType = NULL;
67#endif
68
69 public:
70
75 static QStringList dynamicKeyPartToList( const QString &dynamicKeyPart );
76
86 QgsSettingsEntryBase( const QString &key,
87 const QString &section,
88 const QVariant &defaultValue = QVariant(),
89 const QString &description = QString(),
90 Qgis::SettingsOptions options = Qgis::SettingsOptions() )
91 : mName( key )
92 , mKey( QStringLiteral( "%1/%2" ).arg( section, key ) )
93 , mDefaultValue( defaultValue )
94 , mDescription( description )
95 , mOptions( options )
96 {}
97
110 QgsSettingsEntryBase( const QString &name,
111 QgsSettingsTreeNode *parent,
112 const QVariant &defaultValue = QVariant(),
113 const QString &description = QString(),
114 Qgis::SettingsOptions options = Qgis::SettingsOptions() ) SIP_THROW( QgsSettingsException );
115
119 virtual ~QgsSettingsEntryBase();
120
125 QString name() const {return mName;}
126
132 QString key( const QString &dynamicKeyPart = QString() ) const;
133
139 QString key( const QStringList &dynamicKeyPartList ) const;
140
150 bool keyIsValid( const QString &key ) const;
151
157 QString definitionKey() const;
158
162 bool hasDynamicKey() const;
163
168 Qgis::SettingsOptions options() const {return mOptions;}
169
175 bool exists( const QString &dynamicKeyPart = QString() ) const;
176
182 bool exists( const QStringList &dynamicKeyPartList ) const;
183
189 Qgis::SettingsOrigin origin( const QStringList &dynamicKeyPartList ) const;
190
196 void remove( const QString &dynamicKeyPart = QString() ) const;
197
203 void remove( const QStringList &dynamicKeyPartList ) const;
204
209 Q_DECL_DEPRECATED int section() const;
210
218 Q_DECL_DEPRECATED virtual bool setVariantValue( const QVariant &value, const QString &dynamicKeyPart = QString() ) const SIP_DEPRECATED;
219
227 Q_DECL_DEPRECATED virtual bool setVariantValue( const QVariant &value, const QStringList &dynamicKeyPartList ) const SIP_DEPRECATED;
228
230 QVariant valueAsVariant( const QString &dynamicKeyPart = QString() ) const;
231
233 QVariant valueAsVariant( const QStringList &dynamicKeyPartList ) const;
234
239 QVariant valueAsVariantWithDefaultOverride( const QVariant &defaultValueOverride, const QString &dynamicKeyPart = QString() ) const;
240
248 QVariant valueAsVariantWithDefaultOverride( const QVariant &defaultValueOverride, const QStringList &dynamicKeyPartList ) const;
249
254 Q_DECL_DEPRECATED QVariant valueAsVariant( const QString &dynamicKeyPart, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const SIP_DEPRECATED;
255
260 Q_DECL_DEPRECATED QVariant valueAsVariant( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, const QVariant &defaultValueOverride ) const SIP_DEPRECATED;
261
262
266 QVariant defaultValueAsVariant() const;
267
271 virtual Qgis::SettingsType settingsType() const {return Qgis::SettingsType::Custom;}
272 // This cannot be pure virtual otherwise SIP is failing
273
277 QString description() const;
278
284 QVariant formerValueAsVariant( const QString &dynamicKeyPart ) const;
285
291 QVariant formerValueAsVariant( const QStringList &dynamicKeyPartList ) const;
292
300 bool copyValueFromKey( const QString &key, bool removeSettingAtKey = false ) const {return copyValueFromKey( key, {}, removeSettingAtKey );}
301
310 bool copyValueFromKey( const QString &key, const QStringList &dynamicKeyPartList, bool removeSettingAtKey = false ) const;
311
318 void copyValueToKey( const QString &key, const QStringList &dynamicKeyPartList = QStringList() ) const;
319
324 QgsSettingsTreeNode *parent() const {return mParentTreeElement;}
325
326 protected:
327
333 bool setVariantValuePrivate( const QVariant &value, const QStringList &dynamicKeyPartList = QStringList() ) const;
334
335 private:
336 QString formerValuekey( const QStringList &dynamicKeyPartList ) const;
337
338 QString completeKeyPrivate( const QString &key, const QStringList &dynamicKeyPartList ) const;
339
340 QgsSettingsTreeNode *mParentTreeElement = nullptr;
341 QString mName;
342 QString mKey;
343 QVariant mDefaultValue;
344 QString mDescription;
345 Qgis::SettingsOptions mOptions;
346};
347
348
359template<class T>
361{
362 public:
363
378 const T &defaultValue,
379 const QString &description = QString(),
380 Qgis::SettingsOptions options = Qgis::SettingsOptions() )
382 {}
383
394 const QString &section,
395 const T &defaultValue,
396 const QString &description = QString(),
397 Qgis::SettingsOptions options = Qgis::SettingsOptions() )
399 {}
400
401
402 virtual Qgis::SettingsType settingsType() const override = 0;
403
409 T value( const QString &dynamicKeyPart = QString() ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPart ) );}
410
416 T value( const QStringList &dynamicKeyPartList ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPartList ) );}
417
418
420 inline T valueWithDefaultOverride( const T &defaultValueOverride, const QString &dynamicKeyPart = QString() ) const
421 {
422 return this->convertFromVariant( valueAsVariantWithDefaultOverride( convertToVariant( defaultValueOverride ), dynamicKeyPart ) );
423 }
424
426 inline T valueWithDefaultOverride( const T &defaultValueOverride, const QStringList &dynamicKeyPartList ) const
427 {
428 return this->convertFromVariant( valueAsVariantWithDefaultOverride( convertToVariant( defaultValueOverride ), dynamicKeyPartList ) );
429 }
430
435 Q_DECL_DEPRECATED T value( const QString &dynamicKeyPart, bool useDefaultValueOverride, const T &defaultValueOverride ) const SIP_DEPRECATED
436 {
437 if ( useDefaultValueOverride )
438 return this->convertFromVariant( valueAsVariantWithDefaultOverride( defaultValueOverride, dynamicKeyPart ) );
439 else
440 return this->convertFromVariant( valueAsVariant( dynamicKeyPart ) );
441 }
442
447 Q_DECL_DEPRECATED T value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, const T &defaultValueOverride ) const SIP_DEPRECATED
448 {
449 if ( useDefaultValueOverride )
450 return this->convertFromVariant( valueAsVariantWithDefaultOverride( defaultValueOverride, dynamicKeyPartList ) );
451 else
452 return this->convertFromVariant( valueAsVariant( dynamicKeyPartList ) );
453 }
454
461 bool setValue( const T &value, const QString &dynamicKeyPart = QString() ) const
462 {
463 return setValuePrivate( value, dynamicKeyPartToList( dynamicKeyPart ) );
464 }
465
472 bool setValue( const T &value, const QStringList &dynamicKeyPartList ) const
473 {
474 return setValuePrivate( value, dynamicKeyPartList );
475 }
476
479
484 T formerValue( const QString &dynamicKeyPart = QString() ) const {return convertFromVariant( formerValueAsVariant( dynamicKeyPart ) );}
485
490 T formerValue( const QStringList &dynamicKeyPartList ) const {return convertFromVariant( formerValueAsVariant( dynamicKeyPartList ) );}
491
492 protected:
494 bool setValuePrivate( const T &value, const QStringList &dynamicKeyPartList ) const
495 {
496 if ( checkValue( value ) )
498 else
499 return false;
500 }
501
503 virtual T convertFromVariant( const QVariant &value ) const = 0;
504
506 virtual QVariant convertToVariant( const T &value ) const
507 {
508 return QVariant::fromValue( value );
509 }
510
512 virtual bool checkValue( const T &value ) const
513 {
514 Q_UNUSED( value )
515 return true;
516 }
517};
518
519
530template<class T>
532{
533 public:
534
545 QgsSettingsEntryByValue( const QString &key, QgsSettingsTreeNode *parent, QVariant defaultValue, const QString &description = QString(), Qgis::SettingsOptions options = Qgis::SettingsOptions() )
547 {}
548
558 QgsSettingsEntryByValue( const QString &key, const QString &section, QVariant defaultValue, const QString &description = QString(), Qgis::SettingsOptions options = Qgis::SettingsOptions() )
560 {}
561
562 virtual Qgis::SettingsType settingsType() const override = 0;
563
569 T value( const QString &dynamicKeyPart = QString() ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPart ) );}
570
576 T value( const QStringList &dynamicKeyPartList ) const { return this->convertFromVariant( valueAsVariant( dynamicKeyPartList ) );}
577
579 inline T valueWithDefaultOverride( T defaultValueOverride, const QString &dynamicKeyPart = QString() ) const
580 {
581 return this->convertFromVariant( valueAsVariantWithDefaultOverride( convertToVariant( defaultValueOverride ), dynamicKeyPart ) );
582 }
583
585 inline T valueWithDefaultOverride( T defaultValueOverride, const QStringList &dynamicKeyPartList ) const
586 {
587 return this->convertFromVariant( valueAsVariantWithDefaultOverride( convertToVariant( defaultValueOverride ), dynamicKeyPartList ) );
588 }
589
594 Q_DECL_DEPRECATED T value( const QString &dynamicKeyPart, bool useDefaultValueOverride, T defaultValueOverride ) const SIP_DEPRECATED
595 {
596 if ( useDefaultValueOverride )
597 return this->convertFromVariant( valueAsVariantWithDefaultOverride( defaultValueOverride, dynamicKeyPart ) );
598 else
599 return this->convertFromVariant( valueAsVariant( dynamicKeyPart ) );
600 }
601
606 Q_DECL_DEPRECATED T value( const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, T defaultValueOverride ) const SIP_DEPRECATED
607 {
608 if ( useDefaultValueOverride )
609 return this->convertFromVariant( valueAsVariantWithDefaultOverride( defaultValueOverride, dynamicKeyPartList ) );
610 else
611 return this->convertFromVariant( valueAsVariant( dynamicKeyPartList ) );
612 }
613
620 bool setValue( T value, const QString &dynamicKeyPart = QString() ) const
621 {
622 return setValuePrivate( value, dynamicKeyPartToList( dynamicKeyPart ) );
623 }
624
631 bool setValue( T value, const QStringList &dynamicKeyPartList ) const
632 {
633 return setValuePrivate( value, dynamicKeyPartList );
634 }
635
638
643 T formerValue( const QString &dynamicKeyPart = QString() ) const {return convertFromVariant( formerValueAsVariant( dynamicKeyPart ) );}
644
649 T formerValue( const QStringList &dynamicKeyPartList ) const {return convertFromVariant( formerValueAsVariant( dynamicKeyPartList ) );}
650
651 protected:
653 virtual bool setValuePrivate( T value, const QStringList &dynamicKeyPartList ) const
654 {
655 if ( checkValue( value ) )
657 else
658 return false;
659 }
660
662 virtual T convertFromVariant( const QVariant &value ) const = 0;
663
665 virtual QVariant convertToVariant( T value ) const
666 {
667 return QVariant::fromValue( value );
668 }
669
671 virtual bool checkValue( T value ) const
672 {
673 Q_UNUSED( value )
674 return true;
675 }
676};
677
678
679#endif // QGSSETTINGSENTRY_H
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:55
SettingsType
Types of settings entries.
Definition: qgis.h:345
@ Custom
Custom implementation.
SettingsOrigin
The setting origin describes where a setting is stored.
Definition: qgis.h:2702
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.
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.
bool setVariantValuePrivate(const QVariant &value, const QStringList &dynamicKeyPartList=QStringList()) const
Sets the settings value with a variant value.
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.
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.
Base abstract class for settings entry which are passed by reference.
Q_DECL_DEPRECATED T value(const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, const T &defaultValueOverride) const
Returns the settings value for the dynamicKeyPartList and with a defaultValueOverride.
virtual QVariant convertToVariant(const T &value) const
Converts the value to a variant.
T formerValue(const QString &dynamicKeyPart=QString()) const
Returns the former value.
T valueWithDefaultOverride(const T &defaultValueOverride, const QString &dynamicKeyPart=QString()) const
Returns the settings value with a defaultValueOverride and with an optional dynamicKeyPart.
QgsSettingsEntryByReference(const QString &name, QgsSettingsTreeNode *parent, const T &defaultValue, const QString &description=QString(), Qgis::SettingsOptions options=Qgis::SettingsOptions())
Constructor for QgsSettingsEntryByReference.
virtual Qgis::SettingsType settingsType() const override=0
Returns the settings entry type.
T formerValue(const QStringList &dynamicKeyPartList) const
Returns the former value Returns the current value (or default) if there is no former value.
QgsSettingsEntryByReference(const QString &key, const QString &section, const T &defaultValue, const QString &description=QString(), Qgis::SettingsOptions options=Qgis::SettingsOptions())
Constructor for QgsSettingsEntryByReference.
bool setValue(const T &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
Q_DECL_DEPRECATED T value(const QString &dynamicKeyPart, bool useDefaultValueOverride, const T &defaultValueOverride) const
Returns the settings value for the dynamicKeyPart and with a defaultValueOverride.
T defaultValue() const
Returns settings default value.
T value(const QStringList &dynamicKeyPartList) const
Returns settings value.
bool setValue(const T &value, const QStringList &dynamicKeyPartList) const
Set settings value.
virtual T convertFromVariant(const QVariant &value) const =0
Converts the variant value to the value type of the setting.
virtual bool checkValue(const T &value) const
Check if the value is valid.
bool setValuePrivate(const T &value, const QStringList &dynamicKeyPartList) const
Sets the settings value with an optional list of dynamic parts.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
T valueWithDefaultOverride(const T &defaultValueOverride, const QStringList &dynamicKeyPartList) const
Returns the settings value with a defaultValueOverride for the dynamicKeyPartList.
Base abstract class for settings entry which are passed by value.
T formerValue(const QString &dynamicKeyPart=QString()) const
Returns the former value Returns the current value (or default) if there is no former value.
virtual QVariant convertToVariant(T value) const
Converts the value to a variant.
QgsSettingsEntryByValue(const QString &key, QgsSettingsTreeNode *parent, QVariant defaultValue, const QString &description=QString(), Qgis::SettingsOptions options=Qgis::SettingsOptions())
Constructor for QgsSettingsEntryByValue.
virtual T convertFromVariant(const QVariant &value) const =0
Converts the variant value to the value type of the setting.
virtual bool setValuePrivate(T value, const QStringList &dynamicKeyPartList) const
Sets the settings value with an optional list of dynamic parts.
T valueWithDefaultOverride(T defaultValueOverride, const QString &dynamicKeyPart=QString()) const
Returns the settings value with a defaultValueOverride and with an optional dynamicKeyPart.
QgsSettingsEntryByValue(const QString &key, const QString &section, QVariant defaultValue, const QString &description=QString(), Qgis::SettingsOptions options=Qgis::SettingsOptions())
Constructor for QgsSettingsEntryByValue.
T value(const QStringList &dynamicKeyPartList) const
Returns settings value.
T defaultValue() const
Returns settings default value.
T valueWithDefaultOverride(T defaultValueOverride, const QStringList &dynamicKeyPartList) const
Returns the settings value with a defaultValueOverride for the dynamicKeyPartList.
virtual bool checkValue(T value) const
Check if the value is valid.
bool setValue(T value, const QStringList &dynamicKeyPartList) const
Set settings value.
bool setValue(T value, const QString &dynamicKeyPart=QString()) const
Set settings value.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
T formerValue(const QStringList &dynamicKeyPartList) const
Returns the former value Returns the current value (or default) if there is no former value.
virtual Qgis::SettingsType settingsType() const override=0
Returns the settings entry type.
Q_DECL_DEPRECATED T value(const QStringList &dynamicKeyPartList, bool useDefaultValueOverride, T defaultValueOverride) const
Returns the settings value for the dynamicKeyPartList and with a defaultValueOverride.
Q_DECL_DEPRECATED T value(const QString &dynamicKeyPart, bool useDefaultValueOverride, T defaultValueOverride) const
Returns the settings value for the dynamicKeyPart and with a defaultValueOverride.
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.
Definition: qgsexception.h:134
QgsSettingsTreeNode is a tree node for the settings tree to help organizing and introspecting the tre...
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:186
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define SIP_THROW(name,...)
Definition: qgis_sip.h:198
#define SIP_END
Definition: qgis_sip.h:203