| 1 | // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-only |
| 4 | |
| 5 | #include "isoselectview.h" |
| 6 | #include "widgetutil.h" |
| 7 | #include "bminterface.h" |
| 8 | |
| 9 | #include <DLabel> |
| 10 | #include <DFileDialog> |
| 11 | #include <DApplicationHelper> |
| 12 | #include <DUtil> |
| 13 | #include <DFontSizeManager> |
| 14 | |
| 15 | #include <QStyle> |
| 16 | #include <QVBoxLayout> |
| 17 | #include <QStandardPaths> |
| 18 | #include <QDebug> |
| 19 | #include <QFontDatabase> |
| 20 | #include <QtConcurrent> |
| 21 | #include <QDBusInterface> |
| 22 | |
| 23 | DWIDGET_USE_NAMESPACE |
| 24 | |
| 25 | const QString s_linkTemplatelight = "<a href='%1' style='text-decoration: none; color: #0066EC;'>%2</a>"; |
| 26 | const QString s_linkTemplatedark = "<a href='%1' style='text-decoration: none; color: #0082FA;'>%2</a>"; |
| 27 | const QString s_stateTemplate = "<a style='text-decoration: none; color: #FF5A5A;'>%1</a>"; |
| 28 | |
| 29 | ThreadCheckFile::ThreadCheckFile() |
| 30 | { |
| 31 | restart = false; |
| 32 | m_file = ""; |
| 33 | } |
| 34 | |
| 35 | void ThreadCheckFile::setFile(QString file) |
| 36 | { |
| 37 | m_file = file; |
| 38 | } |
| 39 | |
| 40 | void ThreadCheckFile::setRestart() |
| 41 | { |
| 42 | restart = true; |
| 43 | } |
| 44 | |
| 45 | void ThreadCheckFile::run() |
| 46 | { |
| 47 | restart = true; |
| 48 | bool checkok = false; |
| 49 | while (restart) { |
| 50 | restart = false; |
| 51 | checkok = BMInterface::instance()->checkfile(m_file); |
| 52 | } |
| 53 | emit checkFileFinish(checkok); |
| 54 | } |
| 55 | |
| 56 | ISOSelectView::ISOSelectView(DWidget *parent) : DWidget(parent) |
| 57 | { |
| 58 | setObjectName("ISOSelectView"); |
| 59 | setAutoFillBackground(true); |
| 60 | |
| 61 | QVBoxLayout *mainLayout = new QVBoxLayout(this); |
| 62 | mainLayout->setContentsMargins(0, 1, 0, 0); |
| 63 | mainLayout->setSpacing(0); |
| 64 | |
| 65 | m_title = new DLabel(tr("Select an ISO image file")); |
| 66 | m_title->setAccessibleName("isoSelectWidget_titleLabel"); |
| 67 | DPalette pa = DApplicationHelper::instance()->palette(m_title); |
| 68 | QBrush brush = DApplicationHelper::instance()->palette(m_title).text(); |
| 69 | pa.setBrush(DPalette::Text, brush); |
| 70 | m_title->setPalette(pa); |
| 71 | m_title->setWordWrap(true); |
| 72 | m_title->setAlignment(Qt::AlignHCenter); |
| 73 | DFontSizeManager::instance()->bind(m_title, DFontSizeManager::T3); |
| 74 | |
| 75 | isoIcon = new DLabel(this); |
| 76 | isoIcon->setAccessibleName("isoPanels_isoIconLabel"); |
| 77 | isoIcon->setObjectName("ISOIcon"); |
| 78 | isoIcon->setFixedSize(96, 96); |
| 79 | isoIcon->setPixmap(WidgetUtil::getDpiPixmap(":/theme/light/image/media-optical-96px.svg", this)); |
| 80 | |
| 81 | growIcon = new DLabel(this); |
| 82 | growIcon->setAccessibleName("isoSelectWidget_growIconLabel"); |
| 83 | growIcon->setObjectName("GrowIcon"); |
| 84 | |
| 85 | growIcon->setFixedSize(220, 220); |
| 86 | growIcon->hide(); |
| 87 | |
| 88 | isoPanel = new DropFrame; |
| 89 | isoPanel->setAccessibleName("isoSelectWidget_isoPanel"); |
| 90 | isoPanel->setObjectName("IosPanel"); |
| 91 | isoPanel->setFixedSize(410, 300); |
| 92 | |
| 93 | QVBoxLayout *isoPanelLayout = new QVBoxLayout(isoPanel); |
| 94 | isoPanelLayout->setMargin(0); |
| 95 | |
| 96 | #ifdef Q_OS_WIN |
| 97 | m_fileLabel = new DLabel(""); |
| 98 | #else |
| 99 | m_fileLabel = new DLabel(tr("Drag an ISO image file here")); |
| 100 | m_fileLabel->setFixedWidth(400); |
| 101 | m_fileLabel->setAlignment(Qt::AlignHCenter); |
| 102 | |
| 103 | #endif |
| 104 | m_fileLabel->setAccessibleName("isoPanel_fileLabel"); |
| 105 | m_fileLabel->setObjectName("IsoFileName"); |
| 106 | DFontSizeManager::instance()->bind(m_fileLabel, DFontSizeManager::T8); |
| 107 | |
| 108 | #ifdef Q_OS_WIN |
| 109 | m_hits = new DLabel(""); |
| 110 | #else |
| 111 | m_hits = new DLabel(tr("OR")); |
| 112 | #endif |
| 113 | m_hits->setAccessibleName("isoPanel_hitsLabel"); |
| 114 | m_hits->setObjectName("IsoHits"); |
| 115 | DFontSizeManager::instance()->bind(m_hits, DFontSizeManager::T8); |
| 116 | |
| 117 | spliter = new DLabel; |
| 118 | spliter->setAccessibleName("isoPanel_spliter"); |
| 119 | spliter->setObjectName("IsoSpliter"); |
| 120 | spliter->setFixedSize(230, 1); |
| 121 | spliter->setAutoFillBackground(true); |
| 122 | |
| 123 | m_fileSelect = new DLabel(); |
| 124 | m_fileSelect->setAccessibleName("isoPanel_fileSelectLabel"); |
| 125 | m_fileSelect->setObjectName("IsoFileSelect"); |
| 126 | m_selectText = tr("Select an ISO image file"); |
| 127 | m_fileSelect->setContextMenuPolicy(Qt::NoContextMenu); |
| 128 | |
| 129 | DFontSizeManager::instance()->bind(m_fileSelect, DFontSizeManager::T8); |
| 130 | |
| 131 | isoPanelLayout->addStretch(); |
| 132 | isoPanelLayout->addWidget(isoIcon, 0, Qt::AlignHCenter); |
| 133 | isoPanelLayout->addSpacing(3); |
| 134 | isoPanelLayout->addWidget(m_fileLabel, 0, Qt::AlignHCenter); |
| 135 | isoPanelLayout->addWidget(m_hits, 0, Qt::AlignHCenter); |
| 136 | isoPanelLayout->addSpacing(14); |
| 137 | isoPanelLayout->addWidget(spliter, 0, Qt::AlignHCenter); |
| 138 | isoPanelLayout->addSpacing(7); |
| 139 | isoPanelLayout->addWidget(m_fileSelect, 0, Qt::AlignHCenter); |
| 140 | isoPanelLayout->addStretch(); |
| 141 | isoPanel->setLayout(isoPanelLayout); |
| 142 | |
| 143 | m_checkFile = new DLabel(); |
| 144 | m_checkFile->setAccessibleName("isoSelectWidget_checkFileLabel"); |
| 145 | m_checkFile->setObjectName("IsoFileSelect"); |
| 146 | pa = DApplicationHelper::instance()->palette(m_checkFile); |
| 147 | brush = DApplicationHelper::instance()->palette(m_checkFile).text(); |
| 148 | pa.setBrush(DPalette::Text, brush); |
| 149 | |
| 150 | DFontSizeManager::instance()->bind(m_checkFile, DFontSizeManager::T8); |
| 151 | m_nextSetp = new DPushButton(); |
| 152 | m_nextSetp->setAccessibleName("isoSelectWidget_nextSetpButton"); |
| 153 | m_nextSetp->setFocusPolicy(Qt::NoFocus); |
| 154 | m_nextSetp->setFixedSize(310, 36); |
| 155 | m_nextSetp->setObjectName("NextStepButton"); |
| 156 | m_nextSetp->setText(tr("Next")); |
| 157 | DFontSizeManager::instance()->bind(m_nextSetp, DFontSizeManager::T8); |
| 158 | m_nextSetp->setDisabled(true); |
| 159 | |
| 160 | mainLayout->addWidget(m_title); |
| 161 | mainLayout->addStretch(); |
| 162 | mainLayout->addWidget(isoPanel, 0, Qt::AlignCenter); |
| 163 | mainLayout->addSpacing(16); |
| 164 | mainLayout->addWidget(m_checkFile, 0, Qt::AlignCenter); |
| 165 | mainLayout->addStretch(); |
| 166 | mainLayout->addWidget(m_nextSetp, 0, Qt::AlignCenter|Qt::AlignBottom); |
| 167 | |
| 168 | slot_ThemeChange(); |
| 169 | connect(&t_checkfile, &ThreadCheckFile::checkFileFinish, |
| 170 | this, [ = ](bool result) { |
| 171 | m_checkFile->setText(""); |
| 172 | QString stateText = ""; |
| 173 | if (!result) |
| 174 | stateText = tr("Illegal ISO image file"); |
| 175 | m_nextSetp->setDisabled(false); |
| 176 | if ("" != stateText) { |
| 177 | isoIcon->setPixmap(WidgetUtil::getDpiPixmap(":/theme/light/image/disc_dark.svg", this)); |
| 178 | QString stateTemplateText = QString(s_stateTemplate).arg(stateText); |
| 179 | m_hits->setText(stateTemplateText); |
| 180 | m_nextSetp->setDisabled(true); |
| 181 | m_isoFilePath = ""; |
| 182 | int itemCount = isoPanelLayout->count(); |
| 183 | for (int i = (itemCount - 1); i >= 0; --i) { |
| 184 | //从末尾开始是因为你删除会影响布局的顺序。例如你删掉第一个,后面的会往前移,第二就变成第一个,然后这时你要是++i的话,就是删掉原来布局里的第三个,这第二个被跳过了。 |
| 185 | QLayoutItem *item = isoPanelLayout->takeAt(i); |
| 186 | if (item != nullptr) { |
| 187 | isoPanelLayout->removeWidget(item->widget()); |
| 188 | } |
| 189 | } |
| 190 | } else { |
| 191 | isoIcon->setPixmap(WidgetUtil::getDpiPixmap(":/theme/light/image/media-optical-96px.svg", this)); |
| 192 | int itemCount = isoPanelLayout->count(); |
| 193 | for (int i = (itemCount - 1); i >= 0; --i) { |
| 194 | //从末尾开始是因为你删除会影响布局的顺序。例如你删掉第一个,后面的会往前移,第二就变成第一个,然后这时你要是++i的话,就是删掉原来布局里的第三个,这第二个被跳过了。 |
| 195 | QLayoutItem *item = isoPanelLayout->takeAt(i); |
| 196 | if (item != nullptr) { |
| 197 | isoPanelLayout->removeWidget(item->widget()); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | spliter->hide(); |
| 202 | isoPanelLayout->addStretch(); |
| 203 | isoPanelLayout->addWidget(isoIcon, 0, Qt::AlignHCenter); |
| 204 | isoPanelLayout->addSpacing(2); |
| 205 | isoPanelLayout->addWidget(m_fileLabel, 0, Qt::AlignHCenter); |
| 206 | isoPanelLayout->addSpacing(9); |
| 207 | isoPanelLayout->addWidget(m_hits, 0, Qt::AlignHCenter); |
| 208 | isoPanelLayout->addSpacing(19); |
| 209 | isoPanelLayout->addWidget(m_fileSelect, 0, Qt::AlignHCenter); |
| 210 | isoPanelLayout->addStretch(); |
| 211 | m_selectText = tr("Reselect an ISO image file"); |
| 212 | slot_ThemeChange(); |
| 213 | this->activateWindow(); |
| 214 | }); |
| 215 | connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, |
| 216 | this, &ISOSelectView :: slot_ThemeChange); |
| 217 | |
| 218 | #ifdef Q_OS_WIN |
| 219 | spliter->hide(); |
| 220 | #endif |
| 221 | connect(isoPanel, &DropFrame::fileAboutAccept, this, [ = ]() { |
| 222 | growIcon->show(); |
| 223 | auto center = isoIcon->geometry().center(); |
| 224 | growIcon->move(center); |
| 225 | auto topleft = growIcon->mapFromGlobal(isoIcon->mapToGlobal(center)); |
| 226 | auto offset = 220 - 96; |
| 227 | topleft.setX(topleft.x() - offset / 2); |
| 228 | topleft.setY(topleft.y() - offset / 2); |
| 229 | growIcon->move(topleft); |
| 230 | growIcon->setFocusPolicy(Qt::NoFocus); |
| 231 | isoPanel->setProperty("active", true); |
| 232 | isoPanel->update(); |
| 233 | }); |
| 234 | connect(isoPanel, &DropFrame::fileCancel, this, [ = ]() { |
| 235 | growIcon->hide(); |
| 236 | isoPanel->setProperty("active", false); |
| 237 | isoPanel->update(); |
| 238 | }); |
| 239 | |
| 240 | connect(m_fileSelect, &DLabel::linkActivated, this, [ = ](const QString & /*link*/) { |
| 241 | DFileDialog fileDlg(this); |
| 242 | fileDlg.setViewMode(DFileDialog::Detail); |
| 243 | fileDlg.setFileMode(DFileDialog::ExistingFile); |
| 244 | fileDlg.setDirectory(QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first()); |
| 245 | fileDlg.setNameFilter("ISO (*.iso)"); |
| 246 | fileDlg.selectNameFilter("ISO (*.iso)"); |
| 247 | if (DFileDialog::Accepted == fileDlg.exec()) { |
| 248 | QString text = fileDlg.selectedFiles().first(); |
| 249 | onFileSelected(text); |
| 250 | } |
| 251 | }); |
| 252 | connect(m_nextSetp, &DPushButton::clicked, this, [=]{ |
| 253 | emit isoFileSelected(m_isoFilePath); |
| 254 | }); |
| 255 | connect(isoPanel, &DropFrame::fileDrop, this, &ISOSelectView::onFileSelected); |
| 256 | |
| 257 | } |
| 258 | |
| 259 | void ISOSelectView :: slot_ThemeChange() |
| 260 | { |
| 261 | DPalette pa; |
| 262 | DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType(); |
| 263 | if (themeType == DGuiApplicationHelper::LightType) { |
| 264 | pa = palette(); |
| 265 | pa.setColor(DPalette::Background, QColor(255, 255, 255)); |
| 266 | setPalette(pa); |
| 267 | pa = isoPanel->palette(); |
| 268 | pa.setColor(DPalette::Background, QColor(255, 255, 255, 13)); |
| 269 | isoPanel->setPalette(pa); |
| 270 | pa = m_fileLabel->palette(); |
| 271 | pa.setColor(DPalette::Text, QColor("#B1B1B1")); |
| 272 | m_fileLabel->setPalette(pa); |
| 273 | pa = m_hits->palette(); |
| 274 | pa.setColor(DPalette::WindowText, QColor("#424242")); |
| 275 | m_hits->setPalette(pa); |
| 276 | growIcon->setPixmap(WidgetUtil::getDpiPixmap(":/theme/light/image/disk_big.svg", this)); |
| 277 | spliter->setPixmap(WidgetUtil::getDpiPixmap(":/theme/light/image/dash_line.svg", this)); |
| 278 | QString linkText = QString(s_linkTemplatelight).arg(m_selectText).arg(m_selectText); |
| 279 | m_fileSelect->setText(linkText); |
| 280 | pa = m_checkFile->palette(); |
| 281 | pa.setColor(DPalette::WindowText, QColor("#414D68")); |
| 282 | m_checkFile->setPalette(pa); |
| 283 | } else if (themeType == DGuiApplicationHelper::DarkType) { |
| 284 | pa = palette(); |
| 285 | pa.setColor(DPalette::Background, QColor("#292929")); |
| 286 | setPalette(pa); |
| 287 | pa = isoPanel->palette(); |
| 288 | pa.setColor(DPalette::Background, QColor(0, 0, 0, 13)); |
| 289 | isoPanel->setPalette(pa); |
| 290 | pa = m_fileLabel->palette(); |
| 291 | pa.setColor(DPalette::Text, QColor("#6D7C88")); |
| 292 | m_fileLabel->setPalette(pa); |
| 293 | pa = m_hits->palette(); |
| 294 | pa.setColor(DPalette::WindowText, QColor("#E3E3E3")); |
| 295 | m_hits->setPalette(pa); |
| 296 | growIcon->setPixmap(WidgetUtil::getDpiPixmap(":/theme/dark/image/disc_bg.png", this)); |
| 297 | spliter->setPixmap(WidgetUtil::getDpiPixmap(":/theme/dark/image/dash_line_dark.svg", this)); |
| 298 | QString linkText = QString(s_linkTemplatedark).arg(m_selectText).arg(m_selectText); |
| 299 | m_fileSelect->setText(linkText); |
| 300 | pa = m_checkFile->palette(); |
| 301 | pa.setColor(DPalette::WindowText, QColor("#6D7C88")); |
| 302 | m_checkFile->setPalette(pa); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | bool ISOSelectView::eventFilter(QObject *obj, QEvent *event) |
| 307 | { |
| 308 | if (obj == m_fileLabel && m_fileLabel != nullptr) { |
| 309 | if (event->type() == QEvent::FontChange) { |
| 310 | QFileInfo info(m_isoFilePath); |
| 311 | QFontMetrics fontWidth(m_fileLabel->font()); |
| 312 | QString elidedText = fontWidth.elidedText(info.fileName(), Qt::ElideMiddle, m_fileLabel->width() - 20); |
| 313 | // 重新更新m_fileLabel的text和像素高度 |
| 314 | m_fileLabel->setText(elidedText); |
| 315 | m_fileLabel->setFixedHeight(fontWidth.boundingRect(elidedText).height()); |
| 316 | return true; |
| 317 | } else { |
| 318 | return false; |
| 319 | } |
| 320 | } else { |
| 321 | return ISOSelectView::eventFilter(obj, event); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | void ISOSelectView::onFileSelected(const QString &file) |
| 326 | { |
| 327 | QFileInfo info(file); |
| 328 | QFontMetrics fontWidth(m_fileLabel->font()); |
| 329 | QString elidedText = fontWidth.elidedText(info.fileName(), Qt::ElideMiddle, m_fileLabel->width() - 20); |
| 330 | |
| 331 | m_fileLabel->installEventFilter(this); |
| 332 | |
| 333 | m_fileLabel->setText(elidedText); |
| 334 | m_fileLabel->setToolTip(info.fileName()); |
| 335 | m_fileLabel->show(); |
| 336 | m_hits->setText(""); |
| 337 | m_selectText = ""; |
| 338 | m_checkFile->setText(tr("Detecting ISO file, please wait...")); |
| 339 | slot_ThemeChange(); |
| 340 | m_nextSetp->setDisabled(true); |
| 341 | m_isoFilePath = file; |
| 342 | |
| 343 | isoIcon->setPixmap(WidgetUtil::getDpiPixmap(":/theme/light/image/disc_dark.svg", this)); |
| 344 | spliter->hide(); |
| 345 | t_checkfile.setFile(file); |
| 346 | if (t_checkfile.isRunning()) { |
| 347 | t_checkfile.setRestart(); |
| 348 | } else { |
| 349 | t_checkfile.start(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void ISOSelectView::onFileVerfiyFinished(bool ok) |
| 354 | { |
| 355 | if (ok) { |
| 356 | } |
| 357 | } |