-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequipmentwidget.cpp
More file actions
179 lines (154 loc) · 6.18 KB
/
equipmentwidget.cpp
File metadata and controls
179 lines (154 loc) · 6.18 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "equipmentwidget.h"
#include "database.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QHeaderView>
#include <QMessageBox>
#include <QLabel>
#include <QCompleter>
static void makeSearchable(QComboBox* combo) {
combo->setEditable(true);
combo->setInsertPolicy(QComboBox::NoInsert);
auto* c = new QCompleter(combo->model(), combo);
c->setFilterMode(Qt::MatchContains);
c->setCaseSensitivity(Qt::CaseInsensitive);
combo->setCompleter(c);
}
// ---- EquipmentDialog ----
EquipmentDialog::EquipmentDialog(QWidget* parent, int edit_id)
: QDialog(parent), m_edit_id(edit_id)
{
setWindowTitle(edit_id < 0 ? "New Equipment" : "Edit Equipment");
auto* form = new QFormLayout;
m_name = new QLineEdit;
form->addRow("Name:", m_name);
m_item = new QComboBox;
m_item->addItem("(no linked item)", -1);
for (const auto& it : Database::instance().getItems())
m_item->addItem(it.name, it.id);
makeSearchable(m_item);
form->addRow("Associated item:", m_item);
m_total_uses = new QSpinBox;
m_total_uses->setMinimum(1);
m_total_uses->setMaximum(999999);
m_total_uses->setValue(25);
form->addRow("Total uses before breaking:", m_total_uses);
auto* hint = new QLabel("<i>Regular anvil: 25 uses per stage (3 stages = 75 total)</i>");
form->addRow("", hint);
m_notes = new QTextEdit;
m_notes->setMaximumHeight(60);
form->addRow("Notes:", m_notes);
auto* btns = new QHBoxLayout;
auto* ok = new QPushButton("OK");
auto* can = new QPushButton("Cancel");
btns->addStretch();
btns->addWidget(ok);
btns->addWidget(can);
auto* layout = new QVBoxLayout(this);
layout->addLayout(form);
layout->addLayout(btns);
connect(ok, &QPushButton::clicked, this, &QDialog::accept);
connect(can, &QPushButton::clicked, this, &QDialog::reject);
if (edit_id >= 0) {
for (const auto& eq : Database::instance().getEquipmentTypes()) {
if (eq.id == edit_id) {
m_name->setText(eq.name);
int idx = m_item->findData(eq.item_id);
if (idx >= 0) m_item->setCurrentIndex(idx);
m_total_uses->setValue(eq.total_uses);
m_notes->setPlainText(eq.notes);
break;
}
}
}
}
EquipmentType EquipmentDialog::equipmentType() const {
EquipmentType eq;
eq.id = m_edit_id;
eq.name = m_name->text().trimmed();
eq.item_id = m_item->currentData().toInt();
eq.total_uses = m_total_uses->value();
eq.notes = m_notes->toPlainText().trimmed();
return eq;
}
// ---- EquipmentWidget ----
EquipmentWidget::EquipmentWidget(QWidget* parent) : QWidget(parent) {
auto* layout = new QVBoxLayout(this);
auto* btn_layout = new QHBoxLayout;
m_btn_add = new QPushButton("New Equipment");
m_btn_edit = new QPushButton("Edit");
m_btn_del = new QPushButton("Delete");
btn_layout->addWidget(m_btn_add);
btn_layout->addWidget(m_btn_edit);
btn_layout->addWidget(m_btn_del);
btn_layout->addStretch();
auto* info = new QLabel(
"Depreciation equipment: register items like anvils, tools, etc. "
"Depreciation is calculated as: (item price / total uses) × uses per session.");
info->setWordWrap(true);
layout->addWidget(info);
layout->addLayout(btn_layout);
m_table = new QTableWidget;
m_table->setColumnCount(5);
m_table->setHorizontalHeaderLabels({"ID", "Name", "Linked Item", "Total Uses", "Notes"});
m_table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
m_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_table->setAlternatingRowColors(true);
layout->addWidget(m_table);
connect(m_btn_add, &QPushButton::clicked, this, &EquipmentWidget::addEquipment);
connect(m_btn_edit, &QPushButton::clicked, this, &EquipmentWidget::editEquipment);
connect(m_btn_del, &QPushButton::clicked, this, &EquipmentWidget::deleteEquipment);
connect(m_table, &QTableWidget::doubleClicked, this, &EquipmentWidget::editEquipment);
refresh();
}
void EquipmentWidget::refresh() {
m_table->setRowCount(0);
auto items = Database::instance().getItems();
QMap<int,QString> item_names;
for (const auto& it : items) item_names[it.id] = it.name;
for (const auto& eq : Database::instance().getEquipmentTypes()) {
int row = m_table->rowCount();
m_table->insertRow(row);
m_table->setItem(row, 0, new QTableWidgetItem(QString::number(eq.id)));
m_table->setItem(row, 1, new QTableWidgetItem(eq.name));
m_table->setItem(row, 2, new QTableWidgetItem(eq.item_id >= 0 ? item_names.value(eq.item_id, "?") : "(no item)"));
m_table->setItem(row, 3, new QTableWidgetItem(QString::number(eq.total_uses)));
m_table->setItem(row, 4, new QTableWidgetItem(eq.notes));
}
}
void EquipmentWidget::addEquipment() {
EquipmentDialog dlg(this);
if (dlg.exec() != QDialog::Accepted) return;
auto eq = dlg.equipmentType();
if (eq.name.isEmpty()) { QMessageBox::warning(this, "Error", "Name is required."); return; }
if (Database::instance().addEquipmentType(eq) < 0) {
QMessageBox::critical(this, "Error", Database::instance().lastError());
return;
}
refresh();
}
void EquipmentWidget::editEquipment() {
int row = m_table->currentRow();
if (row < 0) return;
int id = m_table->item(row, 0)->text().toInt();
EquipmentDialog dlg(this, id);
if (dlg.exec() != QDialog::Accepted) return;
if (!Database::instance().updateEquipmentType(dlg.equipmentType())) {
QMessageBox::critical(this, "Error", Database::instance().lastError());
return;
}
refresh();
}
void EquipmentWidget::deleteEquipment() {
int row = m_table->currentRow();
if (row < 0) return;
int id = m_table->item(row, 0)->text().toInt();
if (QMessageBox::question(this, "Confirm", "Delete equipment?") != QMessageBox::Yes) return;
if (!Database::instance().deleteEquipmentType(id)) {
QMessageBox::critical(this, "Error", Database::instance().lastError());
return;
}
refresh();
}