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 bool externalLabel() const { return true; }
41  virtual void setValue(const QVariant &value) = 0;
42  virtual QVariant value() const = 0;
43  virtual QWidget *widget() const = 0;
44 };
45 
46 class BoolEdit final : public Edit
47 {
48 public:
49  BoolEdit(const GameCVar &cvar, QWidget *parent)
50  : edit(new QCheckBox(parent))
51  {
52  edit->setText(cvar.name());
53  edit->setChecked(cvar.value().toBool());
54  }
55 
56  bool externalLabel() const override { return false; }
57 
58  void setValue(const QVariant &value) override
59  {
60  edit->setChecked(value.toBool());
61  }
62 
63  QVariant value() const override
64  {
65  return edit->isChecked();
66  }
67 
68  QWidget *widget() const override
69  {
70  return edit.get();
71  }
72 
73 private:
74  std::unique_ptr<QCheckBox> edit;
75 };
76 
77 class IntEdit final : public Edit
78 {
79 public:
80  IntEdit(const GameCVar &cvar, QWidget *parent)
81  : edit(new QSpinBox(parent))
82  {
83  edit->setMaximum(INT_MAX);
84  edit->setMinimum(INT_MIN);
85  edit->setCorrectionMode(QAbstractSpinBox::CorrectToNearestValue);
86  edit->setValue(cvar.value().toInt());
87  }
88 
89  void setValue(const QVariant &value) override
90  {
91  edit->setValue(value.toInt());
92  }
93 
94  QVariant value() const override
95  {
96  return edit->value();
97  }
98 
99  QWidget *widget() const override
100  {
101  return edit.get();
102  }
103 
104 private:
105  std::unique_ptr<QSpinBox> edit;
106 };
107 
108 class StringEdit final : public Edit
109 {
110 public:
111  StringEdit(const GameCVar &cvar, QWidget *parent)
112  : edit(new QLineEdit(parent))
113  {
114  edit->setText(cvar.value().toString());
115  }
116 
117  void setValue(const QVariant &value) override
118  {
119  edit->setText(value.toString());
120  }
121 
122  QVariant value() const override
123  {
124  return edit->text();
125  }
126 
127  QWidget *widget() const override
128  {
129  return edit.get();
130  }
131 
132 private:
133  std::unique_ptr<QLineEdit> edit;
134 };
135 }
136 DClass<GameCVarEdit>
137 {
138 public:
139  GameCVar cvar;
140  std::unique_ptr<Edit> edit;
141 };
142 
143 DPointeredNoCopy(GameCVarEdit)
144 
145 GameCVarEdit::GameCVarEdit(GameCVar cvar, QWidget *parent)
146  : QWidget(parent)
147 {
148  d->cvar = cvar;
149  auto layout = new QHBoxLayout(this);
150  layout->setContentsMargins(0, 0, 0, 0);
151 
152  switch (cvar.value().type())
153  {
154  case QMetaType::Bool:
155  d->edit.reset(new BoolEdit(cvar, this));
156  break;
157  case QMetaType::Int:
158  case QMetaType::UInt:
159  case QMetaType::LongLong:
160  case QMetaType::ULongLong:
161  d->edit.reset(new IntEdit(cvar, this));
162  break;
163  default:
164  d->edit.reset(new StringEdit(cvar, this));
165  break;
166  }
167  layout->addWidget(d->edit->widget());
168 }
169 
171 {
172  auto cvar = d->cvar;
173  cvar.setValue(value());
174  return cvar;
175 }
176 
178 {
179  return d->edit->externalLabel();
180 }
181 
182 void GameCVarEdit::setValue(const QVariant &value)
183 {
184  d->edit->setValue(value);
185 }
186 
187 QVariant GameCVarEdit::value() const
188 {
189  return d->edit->value();
190 }