This repository has been archived by the owner on Apr 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CVarDialog.cpp
78 lines (68 loc) · 2.38 KB
/
CVarDialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "CVarDialog.hpp"
#include "ui_CVarDialog.h"
#include <utility>
enum class CVarType {
String,
Boolean,
};
struct CVarItem {
QString m_name;
CVarType m_type;
QVariant m_defaultValue;
CVarItem(QString name, CVarType type, QVariant defaultValue)
: m_name(std::move(name)), m_type(type), m_defaultValue(std::move(defaultValue)) {}
};
static std::array cvarList{
CVarItem{QStringLiteral("tweak.game.FieldOfView"), CVarType::String, 55},
CVarItem{QStringLiteral("debugOverlay.playerInfo"), CVarType::Boolean, false},
CVarItem{QStringLiteral("debugOverlay.areaInfo"), CVarType::Boolean, false},
// TODO expand
};
CVarDialog::CVarDialog(QWidget* parent) : QDialog(parent), m_ui(std::make_unique<Ui::CVarDialog>()) {
m_ui->setupUi(this);
QStringList list;
for (const auto& item : cvarList) {
list << item.m_name;
}
m_model.setStringList(list);
m_ui->cvarList->setModel(&m_model);
connect(m_ui->cvarList->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this,
SLOT(handleSelectionChanged(QItemSelection)));
}
CVarDialog::~CVarDialog() = default;
void CVarDialog::on_buttonBox_accepted() {
const QModelIndexList& list = m_ui->cvarList->selectionModel()->selectedIndexes();
if (list.isEmpty()) {
reject();
} else {
accept();
}
}
void CVarDialog::on_buttonBox_rejected() { reject(); }
void CVarDialog::handleSelectionChanged(const QItemSelection& selection) {
const QModelIndexList& list = selection.indexes();
if (list.isEmpty()) {
return;
}
const auto item = cvarList[(*list.begin()).row()];
m_ui->valueStack->setCurrentIndex(static_cast<int>(item.m_type));
if (item.m_type == CVarType::String) {
m_ui->stringValueField->setText(item.m_defaultValue.toString());
} else if (item.m_type == CVarType::Boolean) {
m_ui->booleanValueField->setChecked(item.m_defaultValue.toBool());
}
}
QString CVarDialog::textValue() {
const QModelIndexList& list = m_ui->cvarList->selectionModel()->selectedIndexes();
if (list.isEmpty()) {
return QStringLiteral("");
}
const auto item = cvarList[(*list.begin()).row()];
QVariant value;
if (item.m_type == CVarType::String) {
value = m_ui->stringValueField->text();
} else if (item.m_type == CVarType::Boolean) {
value = m_ui->booleanValueField->isChecked();
}
return QStringLiteral("+") + item.m_name + QStringLiteral("=") + value.toString();
}