-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvehicle_select.cpp
362 lines (318 loc) · 15.4 KB
/
vehicle_select.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "vehicle_select.h"
#include "ui_vehicle_select.h"
VehicleSelect::VehicleSelect(FileActions::ConfigValuesStructure *configValues, QWidget *parent)
: QDialog(parent)
, ui(new Ui::VehicleSelectWindow)
, configValues(configValues)
{
ui->setupUi(this);
ui->select_button->setEnabled(false);
ui->car_make_tree_widget->setHeaderLabel("Manufacturer");
ui->car_model_tree_widget->setHeaderLabel("Model");
QStringList car_version_tree_widget_headers = {"Version","Type","kW","HP","Fuel","Year","ECU","Mode","CHK","RD","WR","Protocol"};
ui->car_version_tree_widget->setHeaderLabels(car_version_tree_widget_headers);
ui->car_version_tree_widget->setColumnWidth(0, 150);
ui->car_version_tree_widget->setColumnWidth(1, 75);
ui->car_version_tree_widget->setColumnWidth(2, 50);
ui->car_version_tree_widget->setColumnWidth(3, 50);
ui->car_version_tree_widget->setColumnWidth(4, 50);
ui->car_version_tree_widget->setColumnWidth(5, 50);
ui->car_version_tree_widget->setColumnWidth(6, 150);
ui->car_version_tree_widget->setColumnWidth(7, 50);
ui->car_version_tree_widget->setColumnWidth(8, 40);
ui->car_version_tree_widget->setColumnWidth(9, 40);
ui->car_version_tree_widget->setColumnWidth(10, 40);
ui->car_version_tree_widget->setColumnWidth(11, 225);
int width = 0;
for (int i = 0; i < ui->car_version_tree_widget->columnCount(); i++)
width += ui->car_version_tree_widget->columnWidth(i);
//qDebug() << "Full width =" << width;
ui->car_version_tree_widget->setMinimumWidth(width);
int height = width / 4 * 2.5 + 18;
this->setFixedHeight(height);
font = ui->car_make_tree_widget->font();
font.setPointSize(header_font_size);
font.setBold(header_font_bold);
font.setFamily(header_font_family);
ui->car_make_tree_widget->setFont(font);
ui->car_model_tree_widget->setFont(font);
ui->car_version_tree_widget->setFont(font);
int index = configValues->flash_protocol_selected_id.toInt();
QStringList car_makes;
QStringList car_makes_sorted;
bool car_make_changed_saved = false;
for (int i = 0; i < configValues->flash_protocol_id.length(); i++)
{
if (!car_makes.contains(configValues->flash_protocol_make.at(i)))
{
//qDebug() << "Make found:" << configValues->flash_protocol_make.at(i);
car_makes.append(configValues->flash_protocol_make.at(i));
}
}
car_makes_sorted = car_makes;
sort(car_makes_sorted.begin(), car_makes_sorted.end(), less<QString>());
for (int i = 0; i < car_makes_sorted.length(); i++)
{
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setFont(0, font);
item->setText(0, car_makes_sorted.at(i));
item->setFirstColumnSpanned(true);
ui->car_make_tree_widget->addTopLevelItem(item);
if (car_makes_sorted.at(i) == configValues->flash_protocol_selected_make)
{
car_make_changed_saved = true;
ui->car_make_tree_widget->setCurrentItem(item);
}
}
if (!car_make_changed_saved)
{
qDebug() << "Car make changed to first item, no make selected previously";
QTreeWidgetItem *item = ui->car_model_tree_widget->topLevelItem(0);
ui->car_model_tree_widget->setCurrentItem(item);
}
connect(ui->car_make_tree_widget, &QTreeWidget::itemSelectionChanged, this, &VehicleSelect::car_make_treewidget_item_selected);
connect(ui->car_model_tree_widget, &QTreeWidget::itemSelectionChanged, this, &VehicleSelect::car_model_treewidget_item_selected);
connect(ui->car_version_tree_widget, &QTreeWidget::itemSelectionChanged, this, &VehicleSelect::car_version_treewidget_item_selected);
connect(ui->car_version_tree_widget, &QTreeWidget::itemDoubleClicked, this, &VehicleSelect::car_model_selected);
connect(ui->cancel_button, &QPushButton::clicked, this, &QDialog::close);
connect(ui->select_button, &QPushButton::clicked, this, &VehicleSelect::car_model_selected);
const QModelIndex make_index = ui->car_make_tree_widget->selectionModel()->currentIndex();
ui->car_make_tree_widget->setCurrentIndex(make_index);
emit ui->car_make_tree_widget->itemSelectionChanged();
//this->adjustSize();
/*
const QModelIndex model_index = ui->car_model_tree_widget->selectionModel()->currentIndex();
ui->car_model_tree_widget->setCurrentIndex(model_index);
emit ui->car_model_tree_widget->itemSelectionChanged();
const QModelIndex version_index = ui->car_version_tree_widget->selectionModel()->currentIndex();
ui->car_version_tree_widget->setCurrentIndex(version_index);
emit ui->car_version_tree_widget->itemSelectionChanged();
*/
}
VehicleSelect::~VehicleSelect()
{
delete(ui);
}
void VehicleSelect::car_model_selected()
{
configValues->flash_protocol_selected_id = flash_protocol_id;
configValues->flash_protocol_selected_make = flash_protocol_make;
configValues->flash_protocol_selected_model = flash_protocol_model;
configValues->flash_protocol_selected_version = flash_protocol_version;
configValues->flash_protocol_selected_protocol_name = flash_protocol_family;
configValues->flash_protocol_selected_description = flash_protocol_description;
configValues->flash_protocol_selected_log_protocol = configValues->flash_protocol_log_protocol.at(configValues->flash_protocol_selected_id.toInt());
configValues->flash_protocol_selected_mcu = configValues->flash_protocol_mcu.at(configValues->flash_protocol_selected_id.toInt());
configValues->flash_protocol_selected_checksum = configValues->flash_protocol_checksum.at(configValues->flash_protocol_selected_id.toInt());
qDebug() << "Selected MCU:" << configValues->flash_protocol_selected_mcu;
accept();
close();
}
void VehicleSelect::car_make_treewidget_item_selected()
{
qDebug() << "Car make selection changed";
QTreeWidgetItem *item = ui->car_make_tree_widget->selectedItems().at(0);
if (item)
{
QTreeWidgetItem *item = ui->car_make_tree_widget->selectedItems().at(0);
QString selected_text = item->text(0);
//qDebug() << "Check models for manufacturer:" << selected_text;
QString car_make = selected_text;
flash_protocol_id.clear();
flash_protocol_make = car_make;
flash_protocol_model.clear();
flash_protocol_version.clear();
QStringList car_models;
QStringList car_models_sorted;
bool car_model_changed_saved = false;
// To delete treewidget items, disconnect itemSelectionChanged() signal first
disconnect(ui->car_model_tree_widget, &QTreeWidget::itemSelectionChanged, 0, 0);
qDebug() << "Delete car_model_tree_widget items";
int item_count = ui->car_model_tree_widget->topLevelItemCount();
for (int i = 0; i < item_count; i++)
{
QTreeWidgetItem *item = ui->car_model_tree_widget->topLevelItem(0);
delete item;
}
// Connect itemSelectionChanged() signal again
connect(ui->car_model_tree_widget, &QTreeWidget::itemSelectionChanged, this, &VehicleSelect::car_model_treewidget_item_selected);
qDebug() << "Add models data based on selected make";
for (int i = 0; i < configValues->flash_protocol_id.length(); i++)
{
if (!car_models.contains(configValues->flash_protocol_model.at(i)) &&
configValues->flash_protocol_make.at(i) == car_make &&
!configValues->flash_protocol_model.at(i).isEmpty())
{
//qDebug() << "Model found:" << configValues->flash_protocol_model.at(i);
car_models.append(configValues->flash_protocol_model.at(i));
}
}
qDebug() << "Sort models data items alphabetically";
car_models_sorted = car_models;
sort(car_models_sorted.begin(), car_models_sorted.end(), less<QString>());
qDebug() << "Add models data items to select";
for (int i = 0; i < car_models_sorted.length(); i++)
{
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0, car_models_sorted.at(i));
item->setFirstColumnSpanned(true);
ui->car_model_tree_widget->addTopLevelItem(item);
if (car_models_sorted.at(i) == configValues->flash_protocol_selected_model)
{
qDebug() << "Car model changed to saved model";
car_model_changed_saved = true;
ui->car_model_tree_widget->setCurrentItem(item);
}
}
if (!car_model_changed_saved)
{
qDebug() << "Car model changed to first item, no model selected previously";
QTreeWidgetItem *item = ui->car_model_tree_widget->topLevelItem(0);
ui->car_model_tree_widget->setCurrentItem(item);
}
}
qDebug() << "Car make selection applied";
}
void VehicleSelect::car_model_treewidget_item_selected()
{
qDebug() << "Car model selection changed";
QTreeWidgetItem *item = ui->car_model_tree_widget->selectedItems().at(0);
if (item)
{
QTreeWidgetItem *item = ui->car_model_tree_widget->selectedItems().at(0);
QString selected_text = item->text(0);
//qDebug() << "Check versions for model:" << selected_text;
QString car_model = selected_text;
QStringList car_versions;
QStringList car_versions_sorted;
bool car_version_changed_saved = false;
flash_protocol_id.clear();
flash_protocol_model = car_model;
flash_protocol_version.clear();
QStringList id;
QStringList version;
QStringList type;
QStringList kw;
QStringList hp;
QStringList fuel;
QStringList year;
QStringList ecu;
QStringList mcu;
QStringList mode;
QStringList checksum;
QStringList read;
QStringList write;
QStringList family;
QStringList description;
// To delete treewidget items, disconnect itemSelectionChanged() signal first
disconnect(ui->car_version_tree_widget, &QTreeWidget::itemSelectionChanged, 0, 0);
qDebug() << "Delete car_version_tree_widget items";
int item_count = ui->car_version_tree_widget->topLevelItemCount();
for (int i = 0; i < item_count; i++)
{
QTreeWidgetItem *item = ui->car_version_tree_widget->topLevelItem(0);
delete item;
}
// Connect itemSelectionChanged() signal again
connect(ui->car_version_tree_widget, &QTreeWidget::itemSelectionChanged, this, &VehicleSelect::car_version_treewidget_item_selected);
qDebug() << "Add versions data based on selected model";
for (int i = 0; i < configValues->flash_protocol_id.length(); i++)
{
if (configValues->flash_protocol_model.at(i) == car_model && configValues->flash_protocol_make.at(i) == flash_protocol_make)
{
//qDebug() << "Model found:" << configValues->flash_protocol_model.at(i);
id.append(configValues->flash_protocol_id.at(i));
version.append(configValues->flash_protocol_version.at(i));
type.append(configValues->flash_protocol_type.at(i));
kw.append(configValues->flash_protocol_kw.at(i));
hp.append(configValues->flash_protocol_hp.at(i));
fuel.append(configValues->flash_protocol_fuel.at(i));
year.append(configValues->flash_protocol_year.at(i));
ecu.append(configValues->flash_protocol_ecu.at(i));
mcu.append(configValues->flash_protocol_mcu.at(i));
mode.append(configValues->flash_protocol_mode.at(i));
checksum.append(configValues->flash_protocol_checksum.at(i));
read.append(configValues->flash_protocol_read.at(i));
write.append(configValues->flash_protocol_write.at(i));
family.append(configValues->flash_protocol_protocol_name.at(i));
description.append(configValues->flash_protocol_description.at(i));
}
}
qDebug() << "Add versions data items to select";
for (int i = 0; i < version.length(); i++)
{
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0, version.at(i));
item->setText(1, type.at(i));
item->setText(2, kw.at(i));
item->setText(3, hp.at(i));
item->setText(4, fuel.at(i));
item->setText(5, year.at(i));
item->setText(6, ecu.at(i));
item->setText(7, mode.at(i));
if (checksum.at(i) == "yes") {
item->setCheckState(8, Qt::Checked);
item->setForeground(8, Qt::darkGreen);
item->setToolTip(8, "Checksum calculation supported");
}
else if (checksum.at(i) == "no") {
item->setCheckState(8, Qt::Unchecked);
item->setForeground(8, Qt::gray);
item->setToolTip(8, "ROM has no checksum");
}
else if (checksum.at(i) == "n/a") {
item->setCheckState(8, Qt::Checked);
item->setForeground(8, Qt::red);
item->setToolTip(8, "Checksum calculation NOT supported yet");
}
if (read.at(i) == "yes")
item->setCheckState(9, Qt::Checked);
else
item->setCheckState(9, Qt::Unchecked);
if (write.at(i) == "yes")
item->setCheckState(10, Qt::Checked);
else
item->setCheckState(10, Qt::Unchecked);
item->setText(11, family.at(i));
item->setToolTip(11, family.at(i));
item->setText(12, id.at(i));
item->setText(13, description.at(i));
//topLevelCarVersionTreeItem->setFirstColumnSpanned(true);
ui->car_version_tree_widget->addTopLevelItem(item);
qDebug() << "Check if car version selected";
if (id.at(i) == configValues->flash_protocol_selected_id)
{
qDebug() << "Car version changed to saved model";
car_version_changed_saved = true;
ui->car_version_tree_widget->setCurrentItem(item);
}
}
if (!car_version_changed_saved)
{
qDebug() << "Car version changed to first item, no version selected previously";
QTreeWidgetItem *item = ui->car_version_tree_widget->topLevelItem(0);
ui->car_version_tree_widget->setCurrentItem(item);
}
}
qDebug() << "Car model selection applied";
}
void VehicleSelect::car_version_treewidget_item_selected()
{
QTreeWidgetItem *item = ui->car_version_tree_widget->selectedItems().at(0);
if (item)
{
QTreeWidgetItem *item = ui->car_version_tree_widget->selectedItems().at(0);
QString selected_text = item->text(0);
//qDebug() << "Selected version for model" << flash_protocol_model << "is" << selected_text;
ui->select_button->setEnabled(true);
QString car_version = selected_text;
flash_protocol_version.clear();
flash_protocol_version = car_version;
flash_protocol_family = item->text(11);
flash_protocol_id = item->text(12);
flash_protocol_description = item->text(13);
//flash_protocol_id = selected_text;//car_version_treewidget_item->text(11);
//qDebug() << "Protocol ID:" << flash_protocol_id;
}
qDebug() << "Car version selection applied";
}