QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgssettingsentryenumflag.h
Go to the documentation of this file.
1/***************************************************************************
2 qgssettingsentryenumflag.cpp
3 --------------------------------------
4 Date : February 2022
5 Copyright : (C) 2022 by Denis Rouzaud
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 QGSSETTINGSENTRYENUMFLAG_H
17#define QGSSETTINGSENTRYENUMFLAG_H
18
19#define SIP_NO_FILE
20
21#include "qgis.h"
22#include "qgssettingsentry.h"
23#include "qgslogger.h"
24
34template <typename T>
36{
37 public:
38
54 parent,
55 QMetaEnum::fromType<T>().isFlag() ? qgsFlagValueToKeys( defaultValue ) : qgsEnumValueToKey( defaultValue ),
57 options )
58 {
59 mMetaEnum = QMetaEnum::fromType<T>();
60 Q_ASSERT( mMetaEnum.isValid() );
61 if ( !mMetaEnum.isValid() )
62 QgsDebugError( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( this->key() ) );
63 }
64
77 QgsSettingsEntryEnumFlag( const QString &key, const QString &section, T defaultValue, const QString &description = QString(), Qgis::SettingsOptions options = Qgis::SettingsOptions() )
79 section,
80 QMetaEnum::fromType<T>().isFlag() ? qgsFlagValueToKeys( defaultValue ) : qgsEnumValueToKey( defaultValue ),
82 options )
83 {
84 mMetaEnum = QMetaEnum::fromType<T>();
85 Q_ASSERT( mMetaEnum.isValid() );
86 if ( !mMetaEnum.isValid() )
87 QgsDebugError( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( this->key() ) );
88 }
89
90 QVariant convertToVariant( const T &value ) const override
91 {
92 if ( mMetaEnum.isFlag() )
93 return qgsFlagValueToKeys( value );
94 else
95 return qgsEnumValueToKey( value );
96 }
97
98
102 T convertFromVariant( const QVariant &value ) const override
103 {
104 if ( !mMetaEnum.isValid() )
105 {
106 QgsDebugError( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( this->key() ) );
107 return T();
108 }
109
110 bool ok = false;
111 T enumFlagValue;
112 if ( !mMetaEnum.isFlag() )
113 enumFlagValue = qgsEnumKeyToValue( value.toString(), mDefaultValue, true, &ok );
114 else
115 enumFlagValue = qgsFlagKeysToValue( value.toString(), mDefaultValue, true, &ok );
116
117 if ( !ok )
118 {
119 QgsDebugError( QStringLiteral( "Invalid enum/flag key/s '%1' for settings key '%2'" ).arg( value.toString(), this->key() ) );
120 return T();
121 }
122
123 return enumFlagValue;
124 }
125
132 bool setValuePrivate( const T &value, const QStringList &dynamicKeyPartList ) const override
133 {
134 if ( !mMetaEnum.isValid() )
135 {
136 QgsDebugError( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( this->key( dynamicKeyPartList ) ) );
137 return false;
138 }
139
140 QVariant variantValue;
141 bool ok = true;
142
143 if ( this->options().testFlag( Qgis::SettingsOption::SaveEnumFlagAsInt ) )
144 {
145 variantValue = static_cast<int>( value );
146 }
147 else
148 {
149 if ( !mMetaEnum.isFlag() )
150 variantValue = qgsEnumValueToKey( value, &ok );
151 else
152 variantValue = qgsFlagValueToKeys( value, &ok );
153 }
154
155 if ( ok )
156 return this->setVariantValue( variantValue, dynamicKeyPartList );
157 else
158 return false;
159 }
160
161 virtual Qgis::SettingsType settingsType() const override
162 {
164 }
165
166 virtual QString typeId() const override
167 {
168 return QStringLiteral( "%1-%2" ).arg( this->QgsSettingsEntryBase::typeId(), QMetaEnum::fromType<T>().name() );
169 }
170
171 private:
172 T mDefaultValue;
173 QMetaEnum mMetaEnum;
174
175};
176
177#endif // QGSSETTINGSENTRYENUMFLAG_H
@ SaveEnumFlagAsInt
The enum/flag will be saved as an integer value instead of text.
SettingsType
Types of settings entries.
Definition qgis.h:599
@ EnumFlag
Enum or Flag.
QFlags< SettingsOption > SettingsOptions
Definition qgis.h:693
Base abstract class for settings entries with typed get and set methods.
T defaultValue() const
Returns settings default value.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
virtual QString typeId() const
Returns the id of the type of settings This can be re-implemented in a custom implementation of a set...
Qgis::SettingsOptions options() const
Returns the settings options.
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.
bool setVariantValue(const QVariant &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
QString name() const
Returns the name of the settings.
QString key(const QString &dynamicKeyPart=QString()) const
Returns settings entry key.
A template class for enum and flag settings entry.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
virtual QString typeId() const override
Returns the id of the type of settings This can be re-implemented in a custom implementation of a set...
QVariant convertToVariant(const T &value) const override
Converts the value to a variant.
bool setValuePrivate(const T &value, const QStringList &dynamicKeyPartList) const override
Set settings value.
QgsSettingsEntryEnumFlag(const QString &name, QgsSettingsTreeNode *parent, T defaultValue, const QString &description=QString(), Qgis::SettingsOptions options=Qgis::SettingsOptions())
Constructor for QgsSettingsEntryEnumFlagBase.
QgsSettingsEntryEnumFlag(const QString &key, const QString &section, T defaultValue, const QString &description=QString(), Qgis::SettingsOptions options=Qgis::SettingsOptions())
Constructor for QgsSettingsEntryEnumFlagBase.
T convertFromVariant(const QVariant &value) const override
Returns settings default value.
QgsSettingsTreeNode is a tree node for the settings tree to help organizing and introspecting the tre...
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
Definition qgis.h:6168
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition qgis.h:6149
QString qgsFlagValueToKeys(const T &value, bool *returnOk=nullptr)
Returns the value for the given keys of a flag.
Definition qgis.h:6207
T qgsFlagKeysToValue(const QString &keys, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given keys of a flag.
Definition qgis.h:6229
#define QgsDebugError(str)
Definition qgslogger.h:38