-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathqefidpeditorview.cpp
More file actions
290 lines (254 loc) · 11.3 KB
/
Copy pathqefidpeditorview.cpp
File metadata and controls
290 lines (254 loc) · 11.3 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
283
284
285
286
287
288
289
290
#include "qefidpeditorview.h"
#include "helpers.h"
#include <QMap>
#include <QVariant>
QEFIDPEditorView::QEFIDPEditorView(QEFIDevicePath *dp, QWidget *parent)
: QWidget(parent)
{
m_dpTypeSelected = -1;
m_dpSubtypeSelected = -1;
m_topLevelLayout = new QFormLayout(this);
m_topLevelLayout->setFieldGrowthPolicy(QFormLayout::ExpandingFieldsGrow);
m_topLevelLayout->setLabelAlignment(Qt::AlignRight | Qt::AlignVCenter);
m_topLevelLayout->setHorizontalSpacing(10);
m_topLevelLayout->setVerticalSpacing(8);
m_dpTypeSelector = new QComboBox(this);
m_dpTypeSelector->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
m_dpTypeSelector->addItem(convert_device_path_type_to_name(QEFIDevicePathType::DP_Hardware), QVariant(QEFIDevicePathType::DP_Hardware));
m_dpTypeSelector->addItem(convert_device_path_type_to_name(QEFIDevicePathType::DP_ACPI), QVariant(QEFIDevicePathType::DP_ACPI));
m_dpTypeSelector->addItem(convert_device_path_type_to_name(QEFIDevicePathType::DP_Message), QVariant(QEFIDevicePathType::DP_Message));
m_dpTypeSelector->addItem(convert_device_path_type_to_name(QEFIDevicePathType::DP_Media), QVariant(QEFIDevicePathType::DP_Media));
m_dpTypeSelector->addItem(convert_device_path_type_to_name(QEFIDevicePathType::DP_BIOSBoot), QVariant(QEFIDevicePathType::DP_BIOSBoot));
if (dp != nullptr) {
m_dpTypeSelected = m_dpTypeSelector->findData(QVariant(dp->type()));
}
m_dpTypeSelector->setCurrentIndex(m_dpTypeSelected);
connect(m_dpTypeSelector, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &QEFIDPEditorView::dpTypeComboBoxCurrentIndexChanged);
m_dpSubtypeSelector = new QComboBox(this);
m_dpSubtypeSelector->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
// m_dpSubtypeSelector->setPlaceholderText(tr("Device Path subtype"));
if (dp != nullptr) {
QList<quint8> subtypes = enum_device_path_subtype(dp->type());
for (const auto &subtype : std::as_const(subtypes)) {
m_dpSubtypeSelector->addItem(convert_device_path_subtype_to_name(dp->type(), subtype), QVariant(subtype));
}
m_dpSubtypeSelected = m_dpSubtypeSelector->findData(QVariant(dp->subType()));
}
m_dpSubtypeSelector->setCurrentIndex(m_dpSubtypeSelected);
connect(m_dpSubtypeSelector, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &QEFIDPEditorView::dpSubtypeComboBoxCurrentIndexChanged);
m_topLevelLayout->addRow("Device Path type:", m_dpTypeSelector);
m_topLevelLayout->addRow("Device Path subtype:", m_dpSubtypeSelector);
// Add edit view
m_currentWidgets = constructDPEditView(dp);
for (const auto &w : std::as_const(m_currentWidgets)) {
m_topLevelLayout->addRow(w.first, w.second);
}
setLayout(m_topLevelLayout);
}
QEFIDPEditorView::~QEFIDPEditorView()
{
if (m_topLevelLayout != nullptr) {
m_topLevelLayout->deleteLater();
m_topLevelLayout = nullptr;
}
}
#include <QDebug>
QVariant retrieveDPEditComponent(enum QEFIDPEditType type, QWidget *widget)
{
QVariant v;
switch (type) {
case QEFIDPEditType::EditType_Text:
case QEFIDPEditType::EditType_Path: {
QLineEdit *edit = dynamic_cast<QLineEdit *>(widget);
if (edit != nullptr) {
v = QVariant(edit->text());
}
} break;
case QEFIDPEditType::EditType_HexData: {
QLineEdit *edit = dynamic_cast<QLineEdit *>(widget);
if (edit != nullptr) {
v = QVariant(QByteArray::fromHex(edit->text().toLatin1()));
}
} break;
case QEFIDPEditType::EditType_UUID: {
QLineEdit *edit = dynamic_cast<QLineEdit *>(widget);
if (edit != nullptr) {
v = QVariant(QUuid(edit->text()));
}
} break;
case QEFIDPEditType::EditType_Number:
case QEFIDPEditType::EditType_HexNumber: {
QSpinBox *edit = dynamic_cast<QSpinBox *>(widget);
if (edit != nullptr) {
v = QVariant(edit->value());
}
} break;
case QEFIDPEditType::EditType_Enum: {
QComboBox *edit = dynamic_cast<QComboBox *>(widget);
if (edit != nullptr) {
v = edit->currentData();
}
} break;
default: {
QLineEdit *edit = dynamic_cast<QLineEdit *>(widget);
if (edit != nullptr) {
v = QVariant(edit->text());
}
} break;
}
qDebug() << v;
return v;
}
QEFIDevicePath *QEFIDPEditorView::getDevicePath()
{
// Get type and subtype
int type = (m_dpTypeSelector != nullptr ? m_dpTypeSelector->itemData(m_dpTypeSelected).toInt() : 0);
int subtype = (m_dpSubtypeSelector != nullptr ? m_dpSubtypeSelector->itemData(m_dpSubtypeSelected).toInt() : 0);
if (type == 0 || subtype == 0)
return nullptr;
// Collect values from widgets
QMap<QString, QVariant> values;
for (const auto &w : std::as_const(m_currentWidgets)) {
// Get the field type from the dummy device path
QEFIDevicePath *dummy = create_dummy_device_path((QEFIDevicePathType)type, (quint8)subtype);
if (dummy == nullptr)
continue;
QList<QPair<QString, enum QEFIDPEditType>> fieldTypes = convert_device_path_types(dummy);
delete dummy;
// Find the edit type for this field
enum QEFIDPEditType editType = QEFIDPEditType::EditType_Text;
for (const auto &ft : std::as_const(fieldTypes)) {
if (ft.first == w.first) {
editType = ft.second;
break;
}
}
QVariant data = retrieveDPEditComponent(editType, w.second);
if (!data.isNull()) {
values[w.first] = data;
}
}
// Create device path from values
return create_device_path_from_values((QEFIDevicePathType)type, (quint8)subtype, values);
}
void QEFIDPEditorView::dpTypeComboBoxCurrentIndexChanged(int index)
{
if (m_dpSubtypeSelector != nullptr && m_dpTypeSelected != index) {
// Change the subtype
m_dpSubtypeSelector->clear();
int type = (m_dpTypeSelector != nullptr ? m_dpTypeSelector->itemData(index).toInt() : 0);
QList<quint8> subtypes = enum_device_path_subtype((enum QEFIDevicePathType)type);
for (const auto &subtype : std::as_const(subtypes)) {
m_dpSubtypeSelector->addItem(convert_device_path_subtype_to_name((enum QEFIDevicePathType)type, subtype), QVariant(subtype));
}
// If we have subtypes, select the first one and create fields
if (!subtypes.isEmpty()) {
m_dpSubtypeSelector->setCurrentIndex(0);
// Manually trigger field creation since setCurrentIndex might not emit signal if already at 0
int subtype = m_dpSubtypeSelector->itemData(0).toInt();
QList<QPair<QString, QWidget *>> widgets = constructDPEditView((QEFIDevicePathType)type, (quint8)subtype & 0xFF);
while (m_topLevelLayout->rowCount() > 2)
m_topLevelLayout->removeRow(2);
m_currentWidgets = widgets;
for (const auto &w : std::as_const(m_currentWidgets)) {
m_topLevelLayout->addRow(w.first, w.second);
}
m_dpSubtypeSelected = 0;
}
}
m_dpTypeSelected = index;
}
void QEFIDPEditorView::dpSubtypeComboBoxCurrentIndexChanged(int index)
{
if (m_dpTypeSelector != nullptr && m_dpSubtypeSelected != index) {
// Change the page
int type = (m_dpTypeSelector != nullptr ? m_dpTypeSelector->itemData(m_dpTypeSelected).toInt() : 0);
int subtype = (m_dpTypeSelector != nullptr ? m_dpSubtypeSelector->itemData(index).toInt() : 0);
QList<QPair<QString, QWidget *>> widgets = constructDPEditView((QEFIDevicePathType)type, (quint8)subtype & 0xFF);
while (m_topLevelLayout->rowCount() > 2)
m_topLevelLayout->removeRow(2);
m_currentWidgets = widgets;
for (const auto &w : std::as_const(m_currentWidgets)) {
m_topLevelLayout->addRow(w.first, w.second);
}
}
m_dpSubtypeSelected = index;
}
QList<QPair<QString, QWidget *>> QEFIDPEditorView::constructDPEditView(QEFIDevicePath *dp)
{
if (dp == nullptr)
return {};
enum QEFIDevicePathType type = dp->type();
quint8 subType = dp->subType();
QList<QPair<QString, QWidget *>> widgets = constructDPEditView(type, subType);
// Populate fields with existing values
QList<QPair<QString, enum QEFIDPEditType>> fieldTypes = convert_device_path_types(dp);
QMap<QString, QVariant> fieldValues;
// Extract values from device path (simplified - would need full implementation)
// For now, we'll just create empty widgets and let user fill them
return widgets;
}
QWidget *constructDPEditComponent(enum QEFIDPEditType type)
{
QWidget *w;
switch (type) {
case QEFIDPEditType::EditType_Text:
case QEFIDPEditType::EditType_Path: // TODO: Add a Browse button
case QEFIDPEditType::EditType_HexData:
case QEFIDPEditType::EditType_UUID: {
QLineEdit *edit = new QLineEdit;
w = (QWidget *)edit;
} break;
case QEFIDPEditType::EditType_Number:
case QEFIDPEditType::EditType_HexNumber: {
QSpinBox *edit = new QSpinBox;
edit->setRange(0, INT_MAX);
w = (QWidget *)edit;
} break;
case QEFIDPEditType::EditType_Enum: {
QComboBox *edit = new QComboBox;
w = (QWidget *)edit;
} break;
default:
w = (QWidget *)new QLineEdit;
break;
}
return w;
}
QList<QPair<QString, QWidget *>> QEFIDPEditorView::constructDPEditView(enum QEFIDevicePathType type, quint8 subType)
{
QList<QPair<QString, QWidget *>> widgets;
// Create a dummy device path to get field definitions
QEFIDevicePath *dummy = create_dummy_device_path(type, subType);
if (dummy == nullptr)
return widgets;
QList<QPair<QString, enum QEFIDPEditType>> fieldTypes = convert_device_path_types(dummy);
for (const auto &t : std::as_const(fieldTypes)) {
QWidget *w = constructDPEditComponent(t.second);
// Apply size policy to ensure widgets fill the available width
if (w != nullptr) {
w->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
}
widgets << QPair<QString, QWidget *>(t.first, w);
// Set up enum values for specific fields
if (type == QEFIDevicePathType::DP_Media && subType == QEFIDevicePathMediaSubType::MEDIA_HD) {
if (t.first == "Format") {
QComboBox *comboBox = dynamic_cast<QComboBox *>(w);
if (comboBox != nullptr) {
comboBox->addItem(tr("GPT"), QVariant(QEFIDevicePathMediaHD::QEFIDevicePathMediaHDFormat::GPT));
comboBox->addItem(tr("PCAT"), QVariant(QEFIDevicePathMediaHD::QEFIDevicePathMediaHDFormat::PCAT));
}
}
if (t.first == "Signature Type") {
QComboBox *comboBox = dynamic_cast<QComboBox *>(w);
if (comboBox != nullptr) {
comboBox->addItem(tr("None"), QVariant(QEFIDevicePathMediaHD::QEFIDevicePathMediaHDSignatureType::NONE));
comboBox->addItem(tr("MBR"), QVariant(QEFIDevicePathMediaHD::QEFIDevicePathMediaHDSignatureType::MBR));
comboBox->addItem(tr("GUID"), QVariant(QEFIDevicePathMediaHD::QEFIDevicePathMediaHDSignatureType::GUID));
}
}
}
}
delete dummy;
return widgets;
}