-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.cpp
More file actions
282 lines (243 loc) · 10.4 KB
/
Copy pathnode.cpp
File metadata and controls
282 lines (243 loc) · 10.4 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "node.h"
#include "./ui_node.h"
NODE::NODE(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::NODE)
{
ui->setupUi(this);
}
NODE::~NODE()
{
delete ui;
}
void NODE::on_pushButton_2_clicked()
{
// --- 1. Retrieve input texts from the text edits ---
QString keyText = ui->textEdit_4->toPlainText().trimmed();
QString valueText = ui->textEdit_3->toPlainText().trimmed();
// If either field is empty, do nothing.
if (keyText.isEmpty() || valueText.isEmpty())
return;
// --- 2. Retrieve placeholders and build a combined key string ---
// For example, if textEdit_4 has placeholder "Key:" and the user enters "Apple",
// then fullKey becomes "Key: Apple".
QString keyPlaceholder = ui->textEdit_4->placeholderText().trimmed();
QString valuePlaceholder = ui->textEdit_3->placeholderText().trimmed();
QString fullKey = keyPlaceholder + " " + keyText;
// --- 3. Convert valueText to an integer ---
bool conversionOk = false;
int inputValue = valueText.toInt(&conversionOk);
if (!conversionOk)
return; // Optionally show an error message
// --- 4. Check listWidget_2 (for adding/summing) ---
QListWidgetItem *itemList2 = nullptr;
int count2 = ui->listWidget_2->count();
for (int i = 0; i < count2; i++) {
QListWidgetItem *item = ui->listWidget_2->item(i);
// We assume the item's text is formatted as:
// "fullKey: valuePlaceholder <number>"
if (item->text().startsWith(fullKey + ":"))
{
itemList2 = item;
break;
}
}
if (itemList2) {
// Item exists in listWidget_2: add the input value.
int currentValue = itemList2->data(Qt::UserRole).toInt();
int newValue = currentValue + inputValue;
if (newValue > 0) {
itemList2->setData(Qt::UserRole, newValue);
itemList2->setText(fullKey + ": " + valuePlaceholder + " " + QString::number(newValue));
} else {
// If the accumulated value becomes zero or less, remove the item.
int row = ui->listWidget_2->row(itemList2);
delete ui->listWidget_2->takeItem(row);
}
return; // Done
}
// --- 5. If no matching item in listWidget_2, check listWidget_1 (for removal) ---
QListWidgetItem *itemList1 = nullptr;
int count1 = ui->listWidget_1->count();
for (int i = 0; i < count1; i++) {
QListWidgetItem *item = ui->listWidget_1->item(i);
if (item->text().startsWith(fullKey + ":"))
{
itemList1 = item;
break;
}
}
if (itemList1) {
// Item exists in listWidget_1: subtract (remove) the input value.
int currentValue = itemList1->data(Qt::UserRole).toInt();
int newValue = currentValue - inputValue;
if (newValue > 0) {
itemList1->setData(Qt::UserRole, newValue);
itemList1->setText(fullKey + ": " + valuePlaceholder + " " + QString::number(newValue));
} else {
int row = ui->listWidget_1->row(itemList1);
delete ui->listWidget_1->takeItem(row);
}
return; // Done
}
// --- 6. If the key is not found in either list, create a new item in listWidget_2 ---
QListWidgetItem *newItem = new QListWidgetItem(
fullKey + ": " + valuePlaceholder + " " + QString::number(inputValue)
);
newItem->setData(Qt::UserRole, inputValue);
ui->listWidget_2->addItem(newItem);
}
void NODE::on_pushButton_clicked()
{
// --- 1. Retrieve input texts from the text edits ---
QString keyText = ui->textEdit_2->toPlainText().trimmed();
QString valueText = ui->textEdit_1->toPlainText().trimmed();
// If either field is empty, do nothing.
if (keyText.isEmpty() || valueText.isEmpty())
return;
// --- 2. Retrieve placeholders and build a combined key string ---
// For example, if textEdit_2 has placeholder "Key:" and the user enters "Apple",
// then fullKey becomes "Key: Apple".
QString keyPlaceholder = ui->textEdit_2->placeholderText().trimmed();
QString valuePlaceholder = ui->textEdit_1->placeholderText().trimmed();
QString fullKey = keyPlaceholder + " " + keyText;
// --- 3. Convert valueText to an integer ---
bool conversionOk = false;
int inputValue = valueText.toInt(&conversionOk);
if (!conversionOk)
return; // Optionally show an error message
// --- 4. Check listWidget_1 (for adding/summing) ---
QListWidgetItem *itemList1 = nullptr;
int count2 = ui->listWidget_1->count();
for (int i = 0; i < count2; i++) {
QListWidgetItem *item = ui->listWidget_1->item(i);
// We assume the item's text is formatted as:
// "fullKey: valuePlaceholder <number>"
if (item->text().startsWith(fullKey + ":"))
{
itemList1 = item;
break;
}
}
if (itemList1) {
// Item exists in listWidget_1: add the input value.
int currentValue = itemList1->data(Qt::UserRole).toInt();
int newValue = currentValue + inputValue;
if (newValue > 0) {
itemList1->setData(Qt::UserRole, newValue);
itemList1->setText(fullKey + ": " + valuePlaceholder + " " + QString::number(newValue));
} else {
// If the accumulated value becomes zero or less, remove the item.
int row = ui->listWidget_1->row(itemList1);
delete ui->listWidget_1->takeItem(row);
}
return; // Done
}
// --- 5. If no matching item in listWidget_2, check listWidget_1 (for removal) ---
QListWidgetItem *itemList2 = nullptr;
int count1 = ui->listWidget_2->count();
for (int i = 0; i < count1; i++) {
QListWidgetItem *item = ui->listWidget_2->item(i);
if (item->text().startsWith(fullKey + ":"))
{
itemList2 = item;
break;
}
}
if (itemList2) {
// Item exists in listWidget_2: subtract (remove) the input value.
int currentValue = itemList2->data(Qt::UserRole).toInt();
int newValue = currentValue - inputValue;
if (newValue > 0) {
itemList2->setData(Qt::UserRole, newValue);
itemList2->setText(fullKey + ": " + valuePlaceholder + " " + QString::number(newValue));
} else {
int row = ui->listWidget_2->row(itemList2);
delete ui->listWidget_2->takeItem(row);
}
return; // Done
}
// --- 6. If the key is not found in either list, create a new item in listWidget_1 ---
QListWidgetItem *newItem = new QListWidgetItem(
fullKey + ": " + valuePlaceholder + " " + QString::number(inputValue)
);
newItem->setData(Qt::UserRole, inputValue);
ui->listWidget_1->addItem(newItem);
}
void NODE::on_pushButton_3_clicked()
{
// 1. Retrieve the sell amount from textEdit_5.
QString sellValueText = ui->textEdit_5->toPlainText().trimmed();
if (sellValueText.isEmpty())
return;
bool conversionOk = false;
int sellValue = sellValueText.toInt(&conversionOk);
if (!conversionOk)
return; // Optionally, display an error if conversion fails.
// 2. Check if there is at least one item in listWidget_1.
if (ui->listWidget_1->count() == 0)
return; // Nothing to sell from.
// 3. For this example, operate on the first item in listWidget_1.
QListWidgetItem *item = ui->listWidget_1->item(0);
// 4. Retrieve the current stored value (assumed stored in Qt::UserRole).
int currentValue = item->data(Qt::UserRole).toInt();
// 5. Subtract the sell value.
int newValue = currentValue - sellValue;
// 6. If the new value is greater than zero, update the item; otherwise, remove it.
if (newValue > 0) {
item->setData(Qt::UserRole, newValue);
// Update the displayed text.
// We assume that the text format is something like:
// "Key: <userKey>: Value: <number>"
// To update just the number, we can try to find the last space and replace the trailing number.
QString currentText = item->text();
int lastSpace = currentText.lastIndexOf(" ");
if (lastSpace != -1) {
QString prefix = currentText.left(lastSpace + 1); // includes the space
item->setText(prefix + QString::number(newValue));
} else {
// Fallback if the format isn't as expected.
item->setText(QString::number(newValue));
}
} else {
// If newValue is 0 or negative, remove the item from listWidget_1.
int row = ui->listWidget_1->row(item);
delete ui->listWidget_1->takeItem(row);
}
}
void NODE::on_pushButton_4_clicked()
{
// 1. Get the buy amount from textEdit_5.
QString buyAmountText = ui->textEdit_5->toPlainText().trimmed();
if (buyAmountText.isEmpty())
return;
bool conversionOk = false;
int buyAmount = buyAmountText.toInt(&conversionOk);
if (!conversionOk)
return; // Optionally, you might display an error message here.
// 2. Ensure there is at least one item in listWidget_2.
if (ui->listWidget_2->count() == 0)
return; // Nothing to subtract from.
// 3. For this example, operate on the first item in listWidget_2.
QListWidgetItem *item = ui->listWidget_2->item(0);
// 4. Retrieve the current stored value.
int currentValue = item->data(Qt::UserRole).toInt();
// Subtract the buy amount.
int newValue = currentValue - buyAmount;
// 5. If the new value is positive, update the item.
if (newValue > 0) {
item->setData(Qt::UserRole, newValue);
// Update the displayed text.
// Here we assume that the item’s text is in a format ending with the numeric value.
QString currentText = item->text();
int lastSpace = currentText.lastIndexOf(" ");
if (lastSpace != -1)
item->setText(currentText.left(lastSpace + 1) + QString::number(newValue));
else
item->setText(QString::number(newValue));
} else {
// If the new value is zero or negative, remove the item.
int row = ui->listWidget_2->row(item);
delete ui->listWidget_2->takeItem(row);
}
}