gamecvaredit.cpp
1 //------------------------------------------------------------------------------
2 // gamecvaredit.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2021 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "gamecvaredit.h"
24 
26 
27 #include <QCheckBox>
28 #include <QHBoxLayout>
29 #include <QSpinBox>
30 #include <QLineEdit>
31 
32 #include <climits>
33 #include <memory>
34 
35 namespace
36 {
37 class Edit
38 {
39 public:
40  virtual ~Edit() = default;
41  virtual bool externalLabel() const { return true; }
42  virtual void setValue(const QVariant &value) = 0;
43  virtual QVariant value() const = 0;
44  virtual QWidget *widget() const = 0;
45 };
46 
47 class BoolEdit final : public Edit
48 {
49 public:
50  BoolEdit(const GameCVar &cvar, QWidget *parent)
51  : edit(new QCheckBox(parent))
52  {
53  edit->setText(cvar.name());
54  edit->setChecked(cvar.value().toBool());
55  }
56 
57  bool externalLabel() const override { return false; }
58 
59  void setValue(const QVariant &value) override
60  {
61  edit->setChecked(value.toBool());
62  }
63 
64  QVariant value() const override
65  {
66  return edit->isChecked();
67  }
68 
69  QWidget *widget() const override
70  {
71  return edit.get();
72  }
73 
74 private:
75  std::unique_ptr<QCheckBox> edit;
76 };
77 
78 class IntEdit final : public Edit
79 {
80 public:
81  IntEdit(const GameCVar &cvar, QWidget *parent)
82  : edit(new QSpinBox(parent))
83  {
84  edit->setMaximum(INT_MAX);
85  edit->setMinimum(INT_MIN);
86  edit->setCorrectionMode(QAbstractSpinBox::CorrectToNearestValue);
87  edit->setValue(cvar.value().toInt());
88  }
89 
90  void setValue(const QVariant &value) override
91  {
92  edit->setValue(value.toInt());
93  }
94 
95  QVariant value() const override
96  {
97  return edit->value();
98  }
99 
100  QWidget *widget() const override
101  {
102  return edit.get();
103  }
104 
105 private:
106  std::unique_ptr<QSpinBox> edit;
107 };
108 
109 class StringEdit final : public Edit
110 {
111 public:
112  StringEdit(const GameCVar &cvar, QWidget *parent)
113  : edit(new QLineEdit(parent))
114  {
115  edit->setText(cvar.value().toString());
116  }
117 
118  void setValue(const QVariant &value) override
119  {
120  edit->setText(value.toString());
121  }
122 
123  QVariant value() const override
124  {
125  return edit->text();
126  }
127 
128  QWidget *widget() const override
129  {
130  return edit.get();
131  }
132 
133 private:
134  std::unique_ptr<QLineEdit> edit;
135 };
136 }
137 DClass<GameCVarEdit>
138 {
139 public:
140  GameCVar cvar;
141  std::unique_ptr<Edit> edit;
142 };
143 
144 DPointeredNoCopy(GameCVarEdit)
145 
146 GameCVarEdit::GameCVarEdit(GameCVar cvar, QWidget *parent)
147  : QWidget(parent)
148 {
149  d->cvar = cvar;
150  auto layout = new QHBoxLayout(this);
151  layout->setContentsMargins(0, 0, 0, 0);
152 
153  switch (cvar.value().type())
154  {
155  case QMetaType::Bool:
156  d->edit.reset(new BoolEdit(cvar, this));
157  break;
158  case QMetaType::Int:
159  case QMetaType::UInt:
160  case QMetaType::LongLong:
161  case QMetaType::ULongLong:
162  d->edit.reset(new IntEdit(cvar, this));
163  break;
164  default:
165  d->edit.reset(new StringEdit(cvar, this));
166  break;
167  }
168  layout->addWidget(d->edit->widget());
169 }
170 
172 {
173  auto cvar = d->cvar;
174  cvar.setValue(value());
175  return cvar;
176 }
177 
179 {
180  return d->edit->externalLabel();
181 }
182 
183 void GameCVarEdit::setValue(const QVariant &value)
184 {
185  d->edit->setValue(value);
186 }
187 
188 QVariant GameCVarEdit::value() const
189 {
190  return d->edit->value();
191 }