File: dnd-test-client.cpp

package info (click to toggle)
dtkgui 5.7.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,340 kB
  • sloc: cpp: 19,512; ansic: 34; makefile: 15; sh: 15
file content (101 lines) | stat: -rw-r--r-- 2,599 bytes parent folder | download
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
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "dfiledragclient.h"

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QFrame>
#include <QDragEnterEvent>
#include <QDragMoveEvent>
#include <QDropEvent>
#include <QApplication>
#include <QDebug>
#include <QProgressBar>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTimer>

#include <QDBusConnectionInterface>

DGUI_USE_NAMESPACE

static DFileDragClient *c;
static QProgressBar *p;

class DropArea : public QFrame
{
    Q_OBJECT
public:
    explicit DropArea(QString s)
        : lb(new QLabel(s, this))
        , le(new QLineEdit(this))
    {
        setLayout(new QVBoxLayout);
        setFrameShape(Shape::Box);
        layout()->addWidget(lb);
        layout()->addWidget(le);
        lb->setAlignment(Qt::AlignmentFlag::AlignCenter);
        le->setText("/tmp");
        le->setPlaceholderText(QString("do not accept drop when edit is empty"));
        setAcceptDrops(true);
    }
protected:
    void dragEnterEvent(QDragEnterEvent *e)
    {
        if (DFileDragClient::checkMimeData(e->mimeData())) {
            if (le->text().isEmpty())
                e->ignore(); // or e->setDropAction(Qt::IgnoreAction);
            else
                e->acceptProposedAction();

            DFileDragClient::setTargetUrl(e->mimeData(), QUrl(le->text()));
        }
    }
    void dragMoveEvent(QDragMoveEvent *e)
    {
        if (DFileDragClient::checkMimeData(e->mimeData())) {
            e->acceptProposedAction();
            DFileDragClient::setTargetUrl(e->mimeData(), QUrl(le->text()));
        }
    }
    void dropEvent(QDropEvent *e)
    {
        if (DFileDragClient::checkMimeData(e->mimeData())) {
            e->acceptProposedAction();
            DFileDragClient::setTargetUrl(e->mimeData(), QUrl(le->text()));
            c = new DFileDragClient(e->mimeData());
            connect(c, &DFileDragClient::progressChanged, p, &QProgressBar::setValue);
        }
    }
private:
    QLabel *lb;
    QLineEdit *le;
};

int main(int argc, char **argv)
{
    QApplication a(argc, argv);
    QDialog d;
    d.show();

    QHBoxLayout *lo = new QHBoxLayout();
    lo->addWidget(new DropArea("area 51"));
    lo->addWidget(new DropArea("area 61"));

    QScopedPointer<QProgressBar> pp (new QProgressBar());
    p = pp.data();
    p->setMinimum(0);
    p->setMaximum(100);

    QVBoxLayout *hlo = new QVBoxLayout();
    d.setLayout(hlo);
    hlo->addLayout(lo);
    hlo->addWidget(p);

    a.exec();
}

#include "dnd-test-client.moc"