| 1 | // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-only |
| 4 | |
| 5 | #include "dropframe.h" |
| 6 | |
| 7 | #include <DApplicationHelper> |
| 8 | |
| 9 | #include <QDropEvent> |
| 10 | #include <QMimeData> |
| 11 | #include <QFileInfo> |
| 12 | #include <QDebug> |
| 13 | #include <QPainter> |
| 14 | |
| 15 | static bool checkMimeData(const QMimeData *mimeData) |
| 16 | { |
| 17 | if (!mimeData->hasUrls()) { |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | QList<QUrl> urlList = mimeData->urls(); |
| 22 | if (1 != urlList.size()) { |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | QFileInfo info(urlList.first().toLocalFile()); |
| 27 | if ("iso" != info.suffix().toLower()) { |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | DropFrame::DropFrame(DWidget *parent) : DFrame(parent) |
| 35 | { |
| 36 | setAcceptDrops(true); |
| 37 | setFrameStyle(QFrame::NoFrame); |
| 38 | } |
| 39 | |
| 40 | void DropFrame::dragEnterEvent(QDragEnterEvent *event) |
| 41 | { |
| 42 | const QMimeData *mimeData = event->mimeData(); |
| 43 | if (!checkMimeData(mimeData)) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | event->acceptProposedAction(); |
| 48 | emit fileAboutAccept(); |
| 49 | } |
| 50 | |
| 51 | void DropFrame::dragLeaveEvent(QDragLeaveEvent *event) |
| 52 | { |
| 53 | DFrame::dragLeaveEvent(event); |
| 54 | emit fileCancel(); |
| 55 | } |
| 56 | |
| 57 | void DropFrame::dropEvent(QDropEvent *event) |
| 58 | { |
| 59 | const QMimeData *mimeData = event->mimeData(); |
| 60 | if (!checkMimeData(mimeData)) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | emit fileDrop(mimeData->urls().first().toLocalFile()); |
| 65 | emit fileCancel(); |
| 66 | } |
| 67 | |
| 68 | void DropFrame::paintEvent(QPaintEvent *e) |
| 69 | { |
| 70 | if (this->property("active").toBool()) { |
| 71 | QPixmap pixmap; |
| 72 | DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType(); |
| 73 | if (themeType == DGuiApplicationHelper::LightType) { |
| 74 | pixmap = QPixmap(":/theme/light/image/dash.svg").scaled(this->size()); |
| 75 | } else if (themeType == DGuiApplicationHelper::DarkType) { |
| 76 | pixmap = QPixmap(":/theme/dark/image/dash.svg").scaled(this->size()); |
| 77 | } |
| 78 | QPainter painter(this); |
| 79 | painter.drawPixmap(this->rect(), pixmap); |
| 80 | } |
| 81 | DFrame::paintEvent(e); |
| 82 | } |