Debian Codegraph @

examples/widgets/draganddrop/draggabletext/dragwidget.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 #include <QtWidgets>
5
6 #include "dragwidget.h"
7
8 static QLabel *createDragLabel(const QString &text, QWidget *parent)
9 {
10 QLabel *label = new QLabel(text, parent);
11 label->setAutoFillBackground(true);
12 label->setFrameShape(QFrame::Panel);
13 label->setFrameShadow(QFrame::Raised);
14 return label;
15 }
16
17 static QString hotSpotMimeDataKey() { return QStringLiteral("application/x-hotspot"); }
18
19 DragWidget::DragWidget(QWidget *parent)
20 : QWidget(parent)
21 {
22 QFile dictionaryFile(QStringLiteral(":/dictionary/words.txt"));
23 if (!dictionaryFile.open(QIODevice::ReadOnly)) {
24 // This would be a build problem, as the dictionary is in the
25 // resource system.
26 qFatal("Could not open the dictionary file: %s",
27 qPrintable(dictionaryFile.errorString()));
28 }
29 QTextStream inputStream(&dictionaryFile);
30
31 int x = 5;
32 int y = 5;
33
34 while (!inputStream.atEnd()) {
35 QString word;
36 inputStream >> word;
37 if (!word.isEmpty()) {
38 QLabel *wordLabel = createDragLabel(word, this);
39 wordLabel->move(x, y);
40 wordLabel->show();
41 wordLabel->setAttribute(Qt::WA_DeleteOnClose);
42 x += wordLabel->width() + 2;
43 if (x >= 245) {
44 x = 5;
45 y += wordLabel->height() + 2;
46 }
47 }
48 }
49
50 setAcceptDrops(true);
51 setMinimumSize(400, qMax(200, y));
52 setWindowTitle(tr("Draggable Text"));
53 }
54
55 void DragWidget::dragEnterEvent(QDragEnterEvent *event)
56 {
57 if (event->mimeData()->hasText()) {
58 if (event->source() == this) {
59 event->setDropAction(Qt::MoveAction);
60 event->accept();
61 } else {
62 event->acceptProposedAction();
63 }
64 } else {
65 event->ignore();
66 }
67 }
68
69 void DragWidget::dropEvent(QDropEvent *event)
70 {
71 if (event->mimeData()->hasText()) {
72 const QMimeData *mime = event->mimeData();
73 QStringList pieces = mime->text().split(QRegularExpression(QStringLiteral("\\s+")),
74 Qt::SkipEmptyParts);
75 QPoint position = event->position().toPoint();
76 QPoint hotSpot;
77
78 QByteArrayList hotSpotPos = mime->data(hotSpotMimeDataKey()).split(' ');
79 if (hotSpotPos.size() == 2) {
80 hotSpot.setX(hotSpotPos.first().toInt());
81 hotSpot.setY(hotSpotPos.last().toInt());
82 }
83
84 for (const QString &piece : pieces) {
85 QLabel *newLabel = createDragLabel(piece, this);
86 newLabel->move(position - hotSpot);
87 newLabel->show();
88 newLabel->setAttribute(Qt::WA_DeleteOnClose);
89
90 position += QPoint(newLabel->width(), 0);
91 }
92
93 if (event->source() == this) {
94 event->setDropAction(Qt::MoveAction);
95 event->accept();
96 } else {
97 event->acceptProposedAction();
98 }
99 } else {
100 event->ignore();
101 }
102 for (QWidget *widget : findChildren<QWidget *>()) {
103 if (!widget->isVisible())
104 widget->deleteLater();
105 }
106 }
107
108 void DragWidget::mousePressEvent(QMouseEvent *event)
109 {
110 QLabel *child = qobject_cast<QLabel*>(childAt(event->position().toPoint()));
111 if (!child)
112 return;
113
114 QPoint hotSpot = event->position().toPoint() - child->pos();
115
116 QMimeData *mimeData = new QMimeData;
117 mimeData->setText(child->text());
118 mimeData->setData(hotSpotMimeDataKey(),
119 QByteArray::number(hotSpot.x()) + ' ' + QByteArray::number(hotSpot.y()));
120
121 qreal dpr = window()->windowHandle()->devicePixelRatio();
122 QPixmap pixmap(child->size() * dpr);
123 pixmap.setDevicePixelRatio(dpr);
124 child->render(&pixmap);
125
126 QDrag *drag = new QDrag(this);
127 drag->setMimeData(mimeData);
128 drag->setPixmap(pixmap);
129 drag->setHotSpot(hotSpot);
130
131 Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
132
133 if (dropAction == Qt::MoveAction)
134 child->close();
135 }