HAL
settings_item_dropdown.cpp
Go to the documentation of this file.
4 //#include <QDebug>
5 
6 namespace hal
7 {
8  SettingsItemDropdown::SettingsItemDropdown(const QString& label, const QString& tag, int defVal, const QString& cat, const QString& desc, bool isGlobal)
9  {
10  mLabel = label;
11  mTag = tag;
12  mValue = defVal;
13  mDefaultValue = defVal;
14  mCategory = cat;
15  mDescription = desc;
17 
19  }
20 
22  {
23  return mValue;
24  }
25 
27  {
28  return mDefaultValue;
29  }
30 
32  {
33  QString s = dv.toString();
34 
35  if (s.isEmpty()) return;
36  int newDefaultValue = valueFromString(s);
37 
38  if(mDefaultValue == newDefaultValue)
39  return;
40 
41  bool hasDefaultValue = (mValue == mDefaultValue);
42  mDefaultValue = newDefaultValue;
43 
44  if (hasDefaultValue) setValue(mDefaultValue);
45  }
46 
48  {
49  int newValue = v.toInt();
50 
51  if (newValue == mValue)
52  return;
53 
54  mValue = newValue;
55 
57  Q_EMIT intChanged(v.toInt());
58  }
59 
61  {
63  return retval;
64  }
65 
67  {
68  if (mValueNames.size() > mValue)
69  return mValueNames.at(mValue);
70 
71  return mValue;
72  }
73 
75  {
76  QString s = val.toString();
77 
78  if (s.isEmpty()) return;
79  mValue = valueFromString(s);
80  }
81 
82  int SettingsItemDropdown::valueFromString(const QString &s) const
83  {
84  if (s.at(0).isDigit())
85  return s.toInt();
86 
87  for (int i=0; i<mValueNames.size(); i++)
88  {
89  if (s == mValueNames.at(i))
90  return i;
91  }
92 
93  // case insensitive values for backward compatibility
94  for (int i=0; i<mValueNames.size(); i++)
95  {
96  if (s.trimmed().toLower() == mValueNames.at(i).trimmed().toLower())
97  return i;
98  }
99 
100 // qDebug() << "SettingsItemDropdown: key not supported" << tag() << s << mValueNames;
101  return -1; // enum name not found
102  }
103 
104  void SettingsItemDropdown::reloadSettings()
105  {
107  restoreFromSettings(SettingsManager::instance()->settingsValue(this->tag()));
108  }
109 }
virtual void setValue(const QVariant &v) override
virtual void setDefaultValue(const QVariant &dv) override
SettingsItemDropdown(const QString &label, const QString &tag, int defVal, const QString &cat=QString(), const QString &desc=QString(), bool isGlobal=true)
virtual SettingsWidget * editWidget(QWidget *parent=nullptr) override
virtual QVariant value() const override
void intChanged(int value)
virtual QVariant defaultValue() const override
virtual void restoreFromSettings(const QVariant &val) override
virtual QVariant persistToSettings() const override
virtual QString tag() const
virtual QString label() const
virtual bool isGlobal() const
void registerSetting(SettingsItem *item)
static SettingsManager * instance()
A SettingsWidget representing a dropdown menu.
The base class for all specific SettingsWidgets.
bool isDigit() const const
const T & at(int i) const const
int size() const const
Q_EMITQ_EMIT
QObject * parent() const const
const QChar at(int position) const const
bool isEmpty() const const
int toInt(bool *ok, int base) const const
QString toLower() const const
QString trimmed() const const
int toInt(bool *ok) const const
QString toString() const const