File: dnd-test-server.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 (121 lines) | stat: -rw-r--r-- 2,610 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "dfiledragserver.h"
#include "dfiledrag.h"

#include <QDialog>
#include <QLabel>
#include <QTimer>
#include <QMimeData>
#include <QApplication>
#include <QMouseEvent>
#include <QVBoxLayout>
#include <QProgressBar>
#include <QDebug>
#include <QRandomGenerator>

DGUI_USE_NAMESPACE

static int p;
static DFileDragServer *s = nullptr;
static QLabel *lbr = nullptr;

class DraggableLabel : public QLabel
{
    Q_OBJECT
public:
    using QLabel::QLabel;

Q_SIGNALS:
    void dragFinished();

protected:
    void mousePressEvent(QMouseEvent *e)
    {
        if (e->button() == Qt::MouseButton::LeftButton) {
            dragpos = e->pos();
        }
    }

    void mouseMoveEvent(QMouseEvent *e)
    {
        if (!(e->buttons() & Qt::MouseButton::LeftButton) || s) {
            return;
        }
        if ((e->pos() - dragpos).manhattanLength() < QApplication::startDragDistance()) {
            return;
        }

        s = new DFileDragServer();
        DFileDrag *drag = new DFileDrag(this, s);
        QMimeData *m = new QMimeData();
        m->setText("your stuff here");
        drag->setMimeData(m);

        connect(drag, &DFileDrag::targetUrlChanged, [drag] {
            lbr->setText(drag->targetUrl().toString());
        });

        Qt::DropAction res = drag->exec(Qt::MoveAction);
        if (res!= Qt::IgnoreAction)
            Q_EMIT dragFinished();
        else {
            s->deleteLater();
            s = nullptr;
        }
    }

private:
    QPoint dragpos;
};

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

    DraggableLabel l("Drag me", &d);
    l.setAlignment(Qt::AlignCenter);

    QTimer t;
    t.setInterval(50);
    t.setSingleShot(false);

    QProgressBar pg;
    pg.setMinimum(0);
    pg.setMaximum(100);

    QLabel lb;
    lbr = &lb;
    lb.setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

    QObject::connect(&l, &DraggableLabel::dragFinished, [&t] {
        p = -1;
        t.start();
    });

    QObject::connect(&t, &QTimer::timeout, [&t, &pg] {
        if (QRandomGenerator::global()->generate() & 1) {
            s->setProgress(++p);
            pg.setValue(p);
            if(p == 100) {
                t.stop();
                s->deleteLater();
                s = nullptr;
            }
        }
    });

    QVBoxLayout lo;
    lo.addWidget(&l);
    lo.addWidget(&pg);
    lo.addWidget(&lb);
    d.setLayout(&lo);

    a.exec();
}

#include "dnd-test-server.moc"