QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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
QgsSettingsEntryBase::description
QString description() const
Returns the settings entry description.
Definition: qgssettingsentry.cpp:269
qgsEnumValueToKey
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
Definition: qgis.h:2440
qgsFlagKeysToValue
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:2520
qgis.h
QgsSettingsEntryEnumFlag::QgsSettingsEntryEnumFlag
QgsSettingsEntryEnumFlag(const QString &key, const QString &section, T defaultValue, const QString &description=QString(), Qgis::SettingsOptions options=Qgis::SettingsOptions())
Constructor for QgsSettingsEntryEnumFlagBase.
Definition: qgssettingsentryenumflag.h:49
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsSettingsEntryBase::options
Qgis::SettingsOptions options() const
Returns the settings options.
Definition: qgssettingsentry.h:202
Qgis::SettingsType
SettingsType
Types of settings entries.
Definition: qgis.h:230
QgsSettingsEntryBase::setVariantValuePrivate
bool setVariantValuePrivate(const QVariant &value, const QStringList &dynamicKeyPartList=QStringList()) const
Sets the settings value with a variant value.
Definition: qgssettingsentry.cpp:203
QgsSettingsEntryEnumFlag::convertFromVariant
T convertFromVariant(const QVariant &value) const override
Returns settings default value.
Definition: qgssettingsentryenumflag.h:65
qgsFlagValueToKeys
QString qgsFlagValueToKeys(const T &value, bool *returnOk=nullptr)
Returns the value for the given keys of a flag.
Definition: qgis.h:2498
QgsSettingsEntryBase::key
QString key(const QString &dynamicKeyPart=QString()) const
Returns settings entry key.
Definition: qgssettingsentry.cpp:109
Qgis::SettingsOption::SaveEnumFlagAsInt
@ SaveEnumFlagAsInt
qgssettingsentry.h
QgsSettingsEntryByValue
Base abstract class for settings entry which are passed by value.
Definition: qgssettingsentry.h:494
qgsEnumKeyToValue
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:2459
QgsSettingsEntryEnumFlag::setValuePrivate
bool setValuePrivate(T value, const QStringList &dynamicKeyPartList) const override
Set settings value.
Definition: qgssettingsentryenumflag.h:95
Qgis::SettingsType::EnumFlag
@ EnumFlag
Enum or Flag.
QgsSettingsEntryEnumFlag::settingsType
virtual Qgis::SettingsType settingsType() const override
Returns the settings entry type.
Definition: qgssettingsentryenumflag.h:124
QgsSettingsEntryByValue::defaultValue
T defaultValue() const
Returns settings default value.
Definition: qgssettingsentry.h:582
QgsSettingsEntryEnumFlag
A template class for enum and flag settings entry.
Definition: qgssettingsentryenumflag.h:34
QgsSettingsEntryBase::section
Q_DECL_DEPRECATED QgsSettings::Section section() const
Returns settings section.
Definition: qgssettingsentry.h:236
QgsSettingsEntryByValue::value
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
Definition: qgssettingsentry.h:520