| 1 |
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 |
|
| 5 |
#include "dfiledragclient.h" |
| 6 |
|
| 7 |
#include <QDialog> |
| 8 |
#include <QLabel> |
| 9 |
#include <QLineEdit> |
| 10 |
#include <QFrame> |
| 11 |
#include <QDragEnterEvent> |
| 12 |
#include <QDragMoveEvent> |
| 13 |
#include <QDropEvent> |
| 14 |
#include <QApplication> |
| 15 |
#include <QDebug> |
| 16 |
#include <QProgressBar> |
| 17 |
#include <QHBoxLayout> |
| 18 |
#include <QVBoxLayout> |
| 19 |
#include <QTimer> |
| 20 |
|
| 21 |
#include <QDBusConnectionInterface> |
| 22 |
|
| 23 |
DGUI_USE_NAMESPACE |
| 24 |
|
| 25 |
static DFileDragClient *c; |
| 26 |
static QProgressBar *p; |
| 27 |
|
| 28 |
class DropArea : public QFrame |
| 29 |
{ |
| 30 |
Q_OBJECT |
| 31 |
public: |
| 32 |
explicit DropArea(QString s) |
| 33 |
: lb(new QLabel(s, this)) |
| 34 |
, le(new QLineEdit(this)) |
| 35 |
{ |
| 36 |
setLayout(new QVBoxLayout); |
| 37 |
setFrameShape(Shape::Box); |
| 38 |
layout()->addWidget(lb); |
| 39 |
layout()->addWidget(le); |
| 40 |
lb->setAlignment(Qt::AlignmentFlag::AlignCenter); |
| 41 |
le->setText("/tmp"); |
| 42 |
le->setPlaceholderText(QString("do not accept drop when edit is empty")); |
| 43 |
setAcceptDrops(true); |
| 44 |
} |
| 45 |
protected: |
| 46 |
void dragEnterEvent(QDragEnterEvent *e) |
| 47 |
{ |
| 48 |
if (DFileDragClient::checkMimeData(e->mimeData())) { |
| 49 |
if (le->text().isEmpty()) |
| 50 |
e->ignore(); // or e->setDropAction(Qt::IgnoreAction); |
| 51 |
else |
| 52 |
e->acceptProposedAction(); |
| 53 |
|
| 54 |
DFileDragClient::setTargetUrl(e->mimeData(), QUrl(le->text())); |
| 55 |
} |
| 56 |
} |
| 57 |
void dragMoveEvent(QDragMoveEvent *e) |
| 58 |
{ |
| 59 |
if (DFileDragClient::checkMimeData(e->mimeData())) { |
| 60 |
e->acceptProposedAction(); |
| 61 |
DFileDragClient::setTargetUrl(e->mimeData(), QUrl(le->text())); |
| 62 |
} |
| 63 |
} |
| 64 |
void dropEvent(QDropEvent *e) |
| 65 |
{ |
| 66 |
if (DFileDragClient::checkMimeData(e->mimeData())) { |
| 67 |
e->acceptProposedAction(); |
| 68 |
DFileDragClient::setTargetUrl(e->mimeData(), QUrl(le->text())); |
| 69 |
c = new DFileDragClient(e->mimeData()); |
| 70 |
connect(c, &DFileDragClient::progressChanged, p, &QProgressBar::setValue); |
| 71 |
} |
| 72 |
} |
| 73 |
private: |
| 74 |
QLabel *lb; |
| 75 |
QLineEdit *le; |
| 76 |
}; |
| 77 |
|
| 78 |
int main(int argc, char **argv) |
| 79 |
{ |
| 80 |
QApplication a(argc, argv); |
| 81 |
QDialog d; |
| 82 |
d.show(); |
| 83 |
|
| 84 |
QHBoxLayout *lo = new QHBoxLayout(); |
| 85 |
lo->addWidget(new DropArea("area 51")); |
| 86 |
lo->addWidget(new DropArea("area 61")); |
| 87 |
|
| 88 |
QScopedPointer<QProgressBar> pp (new QProgressBar()); |
| 89 |
p = pp.data(); |
| 90 |
p->setMinimum(0); |
| 91 |
p->setMaximum(100); |
| 92 |
|
| 93 |
QVBoxLayout *hlo = new QVBoxLayout(); |
| 94 |
d.setLayout(hlo); |
| 95 |
hlo->addLayout(lo); |
| 96 |
hlo->addWidget(p); |
| 97 |
|
| 98 |
a.exec(); |
| 99 |
} |
| 100 |
|
| 101 |
#include "dnd-test-client.moc" |