QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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
52 QgsSettingsEntryEnumFlag( const QString &name, QgsSettingsTreeNode *parent, T defaultValue, const QString &description = QString(), Qgis::SettingsOptions options = Qgis::SettingsOptions() )
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 QgsDebugMsg( 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 QVariant::fromValue( defaultValue ),
82 options )
83 {
84 mMetaEnum = QMetaEnum::fromType<T>();
85 Q_ASSERT( mMetaEnum.isValid() );
86 if ( !mMetaEnum.isValid() )
87 QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( this->key() ) );
88 }
89
90 QVariant convertToVariant( 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 QgsDebugMsg( 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 QgsDebugMsg( 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( T value, const QStringList &dynamicKeyPartList ) const override
133 {
134 if ( !mMetaEnum.isValid() )
135 {
136 QgsDebugMsg( 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->setVariantValuePrivate( variantValue, dynamicKeyPartList );
157 else
158 return false;
159 }
160
161 virtual Qgis::SettingsType settingsType() const override
162 {
164 }
165
166 private:
167 T mDefaultValue;
168 QMetaEnum mMetaEnum;
169
170};
171
172#endif // QGSSETTINGSENTRYENUMFLAG_H
SettingsType
Types of settings entries.
Definition: qgis.h:345
@ EnumFlag
Enum or Flag.
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.
QString name() const
Returns the name of the settings.
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.
Base abstract class for settings entry which are passed by value.
T defaultValue() const
Returns settings default value.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
A template class for enum and flag settings entry.
bool setValuePrivate(T value, const QStringList &dynamicKeyPartList) const override
Set settings value.
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
QVariant convertToVariant(T value) const override
Converts the value to a variant.
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:3732
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition: qgis.h:3713
QString qgsFlagValueToKeys(const T &value, bool *returnOk=nullptr)
Returns the value for the given keys of a flag.
Definition: qgis.h:3771
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:3793
#define QgsDebugMsg(str)
Definition: qgslogger.h:38