-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathprotocol_select.cpp
More file actions
147 lines (125 loc) · 5.57 KB
/
protocol_select.cpp
File metadata and controls
147 lines (125 loc) · 5.57 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
#include "protocol_select.h"
#include "ui_protocol_select.h"
ProtocolSelect::ProtocolSelect(FileActions::ConfigValuesStructure *configValues, QWidget *parent)
: QDialog(parent)
, ui(new Ui::ProtocolSelect)
, configValues(configValues)
{
ui->setupUi(this);
//ui->select_button->setEnabled(false);
QStringList tree_widget_headers = {"Model","Protocol"};
ui->treeWidget->setHeaderLabels(tree_widget_headers);
ui->treeWidget->setFont(font);
//QRect screenGeometry = this->geometry();
QCoreApplication::processEvents(QEventLoop::AllEvents, 10);
//ui->treeWidget->setColumnWidth(0, 150);
//ui->treeWidget->setColumnWidth(1, 75);
QStringList protocols;
QStringList protocols_sorted;
QStringList descriptions_sorted;
bool protocol_changed_saved = false;
QFontMetrics fm(font);
int text_width = 0;
int protocol_width = 0;
int description_width = 0;
for (int i = 0; i < configValues->flash_protocol_id.length(); i++)
{
if (!protocols.contains(configValues->flash_protocol_protocol_name.at(i)))
{
//qDebug() << "Make found:" << configValues->flash_protocol_make.at(i);
protocols.append(configValues->flash_protocol_protocol_name.at(i));
}
}
protocols_sorted = protocols;
sort(protocols_sorted.begin(), protocols_sorted.end(), less<QString>());
for (int i = 0; i < protocols_sorted.length(); i++)
{
for (int j = 0; j < configValues->flash_protocol_id.length(); j++)
{
if (protocols_sorted.at(i) == configValues->flash_protocol_protocol_name.at(j))
{
qDebug() << protocols_sorted.at(i) << configValues->flash_protocol_protocol_name.at(j);
descriptions_sorted.append(configValues->flash_protocol_description.at(j));
text_width = fm.horizontalAdvance(descriptions_sorted.at(i));
if (text_width > description_width)
description_width = text_width;
break;
}
}
text_width = fm.horizontalAdvance(protocols_sorted.at(i));
if (text_width > protocol_width)
protocol_width = text_width;
}
protocol_width += 20;
description_width += 20;
ui->treeWidget->setColumnWidth(0, protocol_width);
ui->treeWidget->setColumnWidth(1, description_width);
/*
QScreen *screen = QGuiApplication::primaryScreen();
QRect screenGeometry = screen->geometry();
if (this->width() < screenGeometry.width() && this->height() < screenGeometry.height())
this->showMaximized();
*/
ui->treeWidget->setFixedWidth(protocol_width + description_width + 20);
for (int i = 0; i < protocols_sorted.length(); i++)
{
QTreeWidgetItem *item = new QTreeWidgetItem();
item->setText(0, protocols_sorted.at(i));
item->setText(1, descriptions_sorted.at(i));
item->setFirstColumnSpanned(true);
ui->treeWidget->addTopLevelItem(item);
if (protocols_sorted.at(i) == configValues->flash_protocol_selected_protocol_name)
{
protocol_changed_saved = true;
ui->treeWidget->setCurrentItem(item);
}
}
if (!protocol_changed_saved)
{
qDebug() << "Protocol changed to first item, no protocol selected previously";
QTreeWidgetItem *item = ui->treeWidget->topLevelItem(0);
ui->treeWidget->setCurrentItem(item);
}
connect(ui->treeWidget, &QTreeWidget::itemSelectionChanged, this, &ProtocolSelect::protocol_treewidget_item_selected);
connect(ui->treeWidget, &QTreeWidget::doubleClicked, this, &ProtocolSelect::car_model_selected);
connect(ui->cancel_button, &QPushButton::clicked, this, &QDialog::close);
connect(ui->select_button, &QPushButton::clicked, this, &ProtocolSelect::car_model_selected);
}
ProtocolSelect::~ProtocolSelect()
{
delete(ui);
}
void ProtocolSelect::car_model_selected()
{
QString protocol_name = ui->treeWidget->selectedItems().at(0)->text(0);
qDebug() << "Selected protocol:" << protocol_name;
for (int i = 0; i < configValues->flash_protocol_id.length(); i++)
{
if (configValues->flash_protocol_protocol_name.at(i) == protocol_name)
{
configValues->flash_protocol_selected_id = configValues->flash_protocol_id.at(i);
configValues->flash_protocol_selected_make = configValues->flash_protocol_make.at(i);
configValues->flash_protocol_selected_model = configValues->flash_protocol_model.at(i);
configValues->flash_protocol_selected_version = configValues->flash_protocol_version.at(i);
configValues->flash_protocol_selected_protocol_name = configValues->flash_protocol_protocol_name.at(i);
configValues->flash_protocol_selected_description = ui->treeWidget->selectedItems().at(0)->text(1);
configValues->flash_protocol_selected_log_protocol = configValues->flash_protocol_log_protocol.at(i);
configValues->flash_protocol_selected_mcu = configValues->flash_protocol_mcu.at(i);
configValues->flash_protocol_selected_checksum = configValues->flash_protocol_checksum.at(i);
}
}
qDebug() << "Selected MCU:" << configValues->flash_protocol_selected_mcu;
accept();
close();
}
void ProtocolSelect::protocol_treewidget_item_selected()
{
QTreeWidgetItem *item = ui->treeWidget->selectedItems().at(0);
if (item)
{
QTreeWidgetItem *item = ui->treeWidget->selectedItems().at(0);
QString selected_text = item->text(0);
//ui->select_button->setEnabled(true);
}
qDebug() << "protocol selection applied";
}