QGIS API Documentation  3.27.0-Master (bef583a8ef)
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
6  Email : [email protected]
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 
33 template <typename T>
35 {
36  public:
37 
49  QgsSettingsEntryEnumFlag( const QString &key, const QString &section, T defaultValue, const QString &description = QString(), Qgis::SettingsOptions options = Qgis::SettingsOptions() )
51  section,
52  QMetaEnum::fromType<T>().isFlag() ? qgsFlagValueToKeys( defaultValue ) : qgsEnumValueToKey( defaultValue ),
54  options )
55  {
56  mMetaEnum = QMetaEnum::fromType<T>();
57  Q_ASSERT( mMetaEnum.isValid() );
58  if ( !mMetaEnum.isValid() )
59  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( this->key() ) );
60  }
61 
65  T convertFromVariant( const QVariant &value ) const override
66  {
67  if ( !mMetaEnum.isValid() )
68  {
69  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( this->key() ) );
70  return T();
71  }
72 
73  bool ok = false;
74  T enumFlagValue;
75  if ( !mMetaEnum.isFlag() )
76  enumFlagValue = qgsEnumKeyToValue( value.toString(), mDefaultValue, true, &ok );
77  else
78  enumFlagValue = qgsFlagKeysToValue( value.toString(), mDefaultValue, true, &ok );
79 
80  if ( !ok )
81  {
82  QgsDebugMsg( QStringLiteral( "Invalid enum/flag key/s '%1' for settings key '%2'" ).arg( value.toString(), this->key() ) );
83  return T();
84  }
85 
86  return enumFlagValue;
87  }
88 
95  bool setValuePrivate( T value, const QStringList &dynamicKeyPartList ) const override
96  {
97  if ( !mMetaEnum.isValid() )
98  {
99  QgsDebugMsg( QStringLiteral( "Invalid metaenum. Enum/Flag probably misses Q_ENUM/Q_FLAG declaration. Settings key: '%1'" ).arg( this->key( dynamicKeyPartList ) ) );
100  return false;
101  }
102 
103  QVariant variantValue;
104  bool ok = true;
105 
106  if ( this->options().testFlag( Qgis::SettingsOption::SaveEnumFlagAsInt ) )
107  {
108  variantValue = static_cast<int>( value );
109  }
110  else
111  {
112  if ( !mMetaEnum.isFlag() )
113  variantValue = qgsEnumValueToKey( value, &ok );
114  else
115  variantValue = qgsFlagValueToKeys( value, &ok );
116  }
117 
118  if ( ok )
119  return this->setVariantValuePrivate( variantValue, dynamicKeyPartList );
120  else
121  return false;
122  }
123 
124  virtual Qgis::SettingsType settingsType() const override
125  {
127  }
128 
129  private:
130  T mDefaultValue;
131  QMetaEnum mMetaEnum;
132 
133 };
134 
135 #endif // QGSSETTINGSENTRYENUMFLAG_H
SettingsType
Types of settings entries.
Definition: qgis.h:231
@ EnumFlag
Enum or Flag.
Qgis::SettingsOptions options() const
Returns the settings options.
Q_DECL_DEPRECATED QgsSettings::Section section() const
Returns settings section.
QString description() const
Returns the settings entry description.
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.
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.
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:2454
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition: qgis.h:2435
QString qgsFlagValueToKeys(const T &value, bool *returnOk=nullptr)
Returns the value for the given keys of a flag.
Definition: qgis.h:2493
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:2515
#define QgsDebugMsg(str)
Definition: qgslogger.h:38