Debian Codegraph @

src/app/view/usbselectview.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: GPL-3.0-only
4
5 #include "usbselectview.h"
6 #include "widgetutil.h"
7 #include "deviceinfoitem.h"
8 #include "devicelistwidget.h"
9 #include <DiskUtil/DiskUtil.h>
10 #include <bminterface.h>
11
12
13 #include <DListWidget>
14 #include <DLabel>
15 #include <DCheckBox>
16 #include <DPalette>
17 #include <DCheckBox>
18 #include <DPushButton>
19 #include <DApplicationHelper>
20 #include <DFontSizeManager>
21 #include <DSuggestButton>
22 #include <DWarningButton>
23 #include <DDialog>
24
25 #include <QDebug>
26 #include <QVBoxLayout>
27 #include <QMessageBox>
28 #include <QIcon>
29 #include <QFontDatabase>
30
31 static QString usageString(quint32 usage, quint32 total)
32 {
33 if (total <= 0 || usage > total) {
34 return "0/0G";
35 }
36
37 if (total <= 1024) {
38 return QString("%1/%2M").arg(usage).arg(total);
39 }
40
41 return QString("%1/%2G")
42 .arg(QString::number(static_cast<double>(usage) / 1024, 'f', 2))
43 .arg(QString::number(static_cast<double>(total) / 1024, 'f', 2));
44 }
45
46
47 static int percent(quint32 usage, quint32 total)
48 {
49 if (total <= 0) {
50 return 0;
51 }
52
53 if (usage > total) {
54 return 100;
55 }
56
57 return static_cast<int>(usage * 100 / total);
58 }
59
60 bool caseInsensitiveLessThan(const DeviceInfo &dev1, const DeviceInfo &dev2)
61 {
62 return dev1.path < dev2.path;
63 }
64
65
66 UsbSelectView::UsbSelectView(DWidget *parent) : DWidget(parent)
67 {
68 setObjectName("UsbSelectView");
69 setAutoFillBackground(true);
70
71 QVBoxLayout *mainLayout = new QVBoxLayout(this);
72 mainLayout->setSpacing(0);
73 mainLayout->setContentsMargins(0, 1, 0, 0);
74
75 DLabel *m_title = new DLabel(tr("Select a partition"));
76 m_title->setAccessibleName("usbSelectWidget_titleLabel");
77 DPalette pa = DApplicationHelper::instance()->palette(m_title);
78 QBrush brush = DApplicationHelper::instance()->palette(m_title).text();
79 pa.setBrush(DPalette::Text, brush);
80 m_title->setPalette(pa);
81 m_title->setWordWrap(true);
82 DFontSizeManager::instance()->bind(m_title, DFontSizeManager::T3);
83
84 DFrame *usbDeviceListPanel = new DFrame;
85 usbDeviceListPanel->setAccessibleName("usbSelectWidget_usbDeviceListPanel");
86 usbDeviceListPanel->setObjectName("UsbDeviceListPanel");
87 usbDeviceListPanel->setFixedSize(410, 282);
88 usbDeviceListPanel->setFrameStyle(QFrame::NoFrame);
89 usbDeviceListPanel->setAutoFillBackground(true);
90
91 QVBoxLayout *usbPanelLayout = new QVBoxLayout(usbDeviceListPanel);
92 usbPanelLayout->setContentsMargins(10, 0, 10, 0);
93
94 QHBoxLayout *checkBoxLayout = new QHBoxLayout;
95 checkBoxLayout->setContentsMargins(0, 0, 0, 0);
96 checkBoxLayout->setSpacing(0);
97
98 DCheckBox *m_formatDiskCheck = new DCheckBox(this);
99 m_formatDiskCheck->setAccessibleName("usbDeviceListPanel_formatDiskCheck");
100 m_formatDiskCheck->setText(tr("Format the partition"));
101 m_formatDiskCheck->setObjectName("UsbFormatCheckBox");
102 m_formatDiskCheck->setFixedHeight(20);
103 m_formatDiskCheck->setFocusPolicy(Qt::NoFocus);
104 m_formatDiskCheck->hide();
105 m_formatDiskCheck->setDisabled(true);
106 QFont font;
107 font.setPixelSize(14);
108 font.setWeight(QFont::Medium);
109 m_formatDiskCheck->setFont(font);
110 checkBoxLayout->addStretch();
111 checkBoxLayout->addWidget(m_formatDiskCheck);
112 checkBoxLayout->addStretch();
113 checkBoxLayout->setAlignment(Qt::AlignCenter);
114
115 DeviceListWidget *m_deviceList = new DeviceListWidget;
116 m_deviceList->setAccessibleName("usbDeviceListPanel_deviceListWidget");
117 m_deviceList->setObjectName("UsbDeviceList");
118 m_deviceList->setFixedSize(400, 270);
119 m_deviceList->hide();
120
121 DeviceDelegate *m_devicedelegate = new DeviceDelegate(m_deviceList);
122 m_deviceList->setItemDelegate(m_devicedelegate);
123
124 DLabel *m_warningHint = new DLabel("");
125 m_warningHint->setAccessibleName("usbSelectWidget_warningHintLabel");
126 m_warningHint->setObjectName("WarningHint");
127 m_warningHint->setWordWrap(true);
128 font.setPixelSize(11);
129 font.setWeight(QFont::Normal);
130 m_warningHint->setFont(font);
131 m_warningHint->setAlignment(Qt::AlignCenter);
132 QHBoxLayout* pWarningLayout = new QHBoxLayout;
133 pWarningLayout->setContentsMargins(30, 0, 30, 0);
134 pWarningLayout->addWidget(m_warningHint);
135
136 DLabel *m_emptyHint = new DLabel(tr("No disk available"));
137 m_emptyHint->setAccessibleName("usbDeviceListPanel_emptyHintLabel");
138 m_emptyHint->setObjectName("EmptyHintTitle");
139 m_emptyHint->setFixedHeight(29);
140 m_emptyHint->setAlignment(Qt::AlignCenter);
141 DFontSizeManager::instance()->bind(m_emptyHint, DFontSizeManager::T4);
142 usbPanelLayout->addStretch();
143 usbPanelLayout->addWidget(m_emptyHint, 0, Qt::AlignCenter);
144 usbPanelLayout->addStretch();
145 usbPanelLayout->addWidget(m_deviceList, 0, Qt::AlignLeft);
146 usbPanelLayout->addSpacing(15);
147 usbPanelLayout->addLayout(checkBoxLayout);
148
149 m_start = new DSuggestButton;
150 m_start->setAccessibleName("usbSelectWidget_startButton");
151 m_start->setFocusPolicy(Qt::NoFocus);
152 m_start->setObjectName("StartMake");
153 m_start->setText(tr("Start"));
154 DFontSizeManager::instance()->bind(m_start, DFontSizeManager::T6);
155 m_start->setDisabled(true);
156 m_start->setFixedHeight(36);
157 DPushButton* pBackBtn = new DPushButton(tr("Back", "button"));
158 pBackBtn->setAccessibleName("usbSelectWidget_backBuuton");
159 pBackBtn->setFocusPolicy(Qt::NoFocus);
160 DFontSizeManager::instance()->bind(m_start, DFontSizeManager::T6);
161 pBackBtn->setDisabled(false);
162 pBackBtn->setFixedHeight(36);
163 QHBoxLayout* pHlayout = new QHBoxLayout;
164 pHlayout->setContentsMargins(20, 0, 20, 0);
165 pHlayout->setSpacing(10);
166 pHlayout->addWidget(pBackBtn);
167 pHlayout->addWidget(m_start);
168
169 mainLayout->addWidget(m_title, 0, Qt::AlignHCenter);
170 mainLayout->addStretch();
171 mainLayout->addWidget(usbDeviceListPanel, 0, Qt::AlignHCenter);
172 mainLayout->addSpacing(10);
173 mainLayout->addLayout(pWarningLayout);
174 mainLayout->addStretch();
175 mainLayout->addLayout(pHlayout);
176
177 connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged,
178 this, [ = ] {
179 DPalette pa;
180 DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
181 if (themeType == DGuiApplicationHelper::LightType)
182 {
183 pa = palette();
184 pa.setColor(DPalette::Background, QColor(255, 255, 255));
185 setPalette(pa);
186 pa = m_warningHint->palette();
187 pa.setColor(DPalette::WindowText, QColor("#FF5800"));
188 m_warningHint->setPalette(pa);
189 pa = m_emptyHint->palette();
190 pa.setColor(DPalette::Text, QColor(85, 85, 85, 102));
191 m_emptyHint->setPalette(pa);
192 }
193 else if (themeType == DGuiApplicationHelper::DarkType)
194 {
195 pa = palette();
196 pa.setColor(DPalette::Background, QColor("#292929"));
197 setPalette(pa);
198 pa = m_warningHint->palette();
199 pa.setColor(DPalette::WindowText, QColor("#9A2F2F"));
200 m_warningHint->setPalette(pa);
201 pa = m_emptyHint->palette();
202 pa.setColor(DPalette::Text, QColor(192, 198, 212, 102));
203 m_emptyHint->setPalette(pa);
204 }
205 });
206
207 emit DGuiApplicationHelper::instance()->themeTypeChanged(DGuiApplicationHelper::instance()->themeType());
208
209 auto handleFormat = [ = ](bool checked) {
210 if (!checked) {
211 m_warningHint->setText("");
212 return;
213 }
214 m_warningHint->setText(tr("Formatting will erase all data on the partition, but can increase the success rate, please confirm before proceeding"));
215 };
216 connect(m_formatDiskCheck, &DCheckBox::clicked, this, [ = ](bool checked) {
217 this->setProperty("user_format", checked);
218 handleFormat(checked);
219 });
220
221 connect(BMInterface::instance(), &BMInterface::deviceListChanged,
222 this, [ = ](const QList<DeviceInfo> &addlist, const QList<DeviceInfo>& dellist) {
223 bool hasPartitionSelected = false;
224
225 foreach (DeviceInfo info, dellist) {
226 for (int i = 0; i < this->m_mountDevs.count(); i++) {
227 DeviceInfo refInfo = this->m_mountDevs.at(i);
228 if (refInfo == info) {
229 this->m_mountDevs.removeAt(i);
230 }
231 }
232
233 //移除列表中包含选中项
234 if (info.path == this->property("last_path")&&!info.path.isEmpty()&&!addlist.contains(info)) {
235 m_formatDiskCheck->setDisabled(true);
236 m_formatDiskCheck->setChecked(false);
237 setProperty("user_format", false);
238 setProperty("last_path", "");
239 setProperty("last_fstype", "");
240 }
241 }
242
243 this->m_mountDevs += addlist;
244 qSort(m_mountDevs.begin(), m_mountDevs.end(), caseInsensitiveLessThan);
245 m_formatDiskCheck->setVisible(this->m_mountDevs.size());
246 m_emptyHint->setVisible(!this->m_mountDevs.size());
247 m_deviceList->setVisible(this->m_mountDevs.size());
248 m_warningHint->setVisible(this->m_mountDevs.size());
249 m_deviceList->clear();
250 QStringList strDevList;
251
252 foreach (const DeviceInfo &partition, this->m_mountDevs) {
253 QListWidgetItem *listItem = new QListWidgetItem;
254 DeviceInfoItem *infoItem = new DeviceInfoItem(
255 partition.label,
256 partition.path,
257 usageString(partition.used, partition.total),
258 percent(partition.used, partition.total));
259 infoItem->setNeedFormat(partition.needFormat);
260 listItem->setSizeHint(QSize(infoItem->size().width(), infoItem->size().height() + 10));
261 m_deviceList->addItem(listItem);
262 m_deviceList->setItemWidget(listItem, infoItem);
263 infoItem->setProperty("path", partition.path);
264 infoItem->setProperty("fstype", partition.fstype);
265
266 if (!strDevList.contains(partition.strDev)) {
267 strDevList.push_back(partition.strDev);
268 listItem->setData(Qt::UserRole, true);
269 infoItem->setEnabled(true);
270 }
271 else {
272 listItem->setData(Qt::UserRole, false);
273 infoItem->setEnabled(false);
274 }
275 if (partition.path == this->property("last_path").toString()) {
276 infoItem->setCheck(true);
277 m_deviceList->setCurrentItem(listItem);
278 hasPartitionSelected = true;
279 }
280 else {
281 infoItem->setCheck(false);
282 }
283 }
284
285 m_start->setDisabled(!hasPartitionSelected);
286
287 if (!hasPartitionSelected) {
288 this->setProperty("last_path", "");
289 }
290 // 当插U盘或者拔U盘时,重新记录前面被勾选上的item
291 m_previous = nullptr;
292 for (int i = 0; i < m_deviceList->count(); i++) {
293 DeviceInfoItem *infoItem = qobject_cast<DeviceInfoItem *>(m_deviceList->itemWidget(m_deviceList->item(i)));
294 if (infoItem && infoItem->isChecked()) {
295 m_previous = m_deviceList->item(i);
296 }
297 }
298 });
299
300 connect(m_deviceList, &DeviceListWidget::itemClicked,
301 this, [ = ](QListWidgetItem * current) {
302
303 if (current != nullptr) {
304 bool bFirst = current->data(Qt::UserRole).toBool();
305 if (!bFirst) {
306 return ;
307 }
308 }
309 DeviceInfoItem *infoItem;
310 if (m_previous != nullptr) {
311 infoItem = qobject_cast<DeviceInfoItem *>(m_deviceList->itemWidget(m_previous));
312 if (infoItem) {
313 infoItem->setCheck(false);
314 }
315 }
316
317
318 infoItem = qobject_cast<DeviceInfoItem *>(m_deviceList->itemWidget(current));
319
320 if (infoItem) {
321 if (infoItem->needFormat()) {
322 m_formatDiskCheck->setChecked(true);
323 m_formatDiskCheck->setDisabled(true);
324 handleFormat(true);
325 } else {
326 auto format = this->property("user_format").toBool();
327 m_formatDiskCheck->setChecked(format);
328 m_formatDiskCheck->setDisabled(false);
329 handleFormat(format);
330 }
331
332 infoItem->setCheck(true);
333 this->setProperty("last_path", infoItem->property("path").toString());
334 this->setProperty("last_fstype", infoItem->property("fstype").toString());
335 m_start->setDisabled(false);
336 }
337 m_previous = current;
338 });
339
340
341 connect(m_start, &DPushButton::clicked, this, [ = ] {
342 auto format = m_formatDiskCheck->isChecked();
343
344 QString usbMountPoint = XSys::DiskUtil::MountPoint(this->property("last_path").toString());
345 usbMountPoint += "/";
346 QString isoFilePath = this->property("isoFilePath").toString();
347 int ret = 1;
348 // 判断用户勾选格式化后选择的ISO镜像的位置是否就是用户制作启动盘的U盘里。
349 if (format && (usbMountPoint.length() > 1) && (usbMountPoint == isoFilePath.left(usbMountPoint.length()))) {
350 FormatDialog formatDialogSceneB(this);
351 formatDialogSceneB.initDialog(tr("Format Partition"),tr("You have selected the ISO image in this USB flash drive. Formatting it will erase all your files. Please reselect the image file or cancel the formatting."),
352 tr("OK", "button"),DDialog::ButtonType::ButtonWarning);
353 formatDialogSceneB.setContentsMargins(0, 0, 0, 0);
354 ret = formatDialogSceneB.exec();
355 } else if (format) {
356 FormatDialog formatDialogSceneA(this);
357 formatDialogSceneA.initDialog(tr("Format Partition"),tr("Formatting the partition will overwrite all data, please have a backup before proceeding."),
358 tr("Cancel", "button"),DDialog::ButtonType::ButtonNormal,tr("OK", "button"),DDialog::ButtonType::ButtonWarning);
359 formatDialogSceneA.setContentsMargins(0, 0, 0, 0);
360 ret = formatDialogSceneA.exec();
361 }
362 if (ret != 1) {
363 if (this->property("last_fstype").toString() == "vfat") {
364 m_formatDiskCheck->setChecked(false);
365 this->setProperty("user_format", false);
366 handleFormat(false);
367 }
368 return;
369 }
370
371 m_start->setEnabled(false);
372 #ifdef Q_OS_LINUX
373 if (!m_formatDiskCheck->isChecked() && "vfat" != this->property("last_fstype").toString())
374 {
375 emit finish(2, "install failed", tr("Disk Format Error: Please format the partition with FAT32"));
376 return;
377 }
378 #endif
379 QString path = this->property("last_path").toString();
380 qDebug() << "Select usb device" << path;
381 emit this->deviceSelected(path, m_formatDiskCheck->isChecked());
382 });
383
384 connect(pBackBtn, &DPushButton::clicked, this, [=]{
385 emit this->backToPrevUI();
386 });
387 }
388
389 void UsbSelectView::getIsoFileSelectedPath(QString isoPath)
390 {
391 this->setProperty("isoFilePath",isoPath);
392 }
393
394 void UsbSelectView::resetStartInstall()
395 {
396 m_start->setEnabled(true);
397 }