Debian Codegraph @

tests/manual/examples/widgets/draganddrop/fridgemagnets/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 "draglabel.h"
5 #include "dragwidget.h"
6
7 #include <QtWidgets>
8
9 static inline QString fridgetMagnetsMimeType() { return QStringLiteral("application/x-fridgemagnet"); }
10
11 //! [0]
12 DragWidget::DragWidget(QWidget *parent)
13 : QWidget(parent)
14 {
15 QFile dictionaryFile(QStringLiteral(":/dictionary/words.txt"));
16 dictionaryFile.open(QFile::ReadOnly);
17 QTextStream inputStream(&dictionaryFile);
18 //! [0]
19
20 //! [1]
21 int x = 5;
22 int y = 5;
23
24 QString word;
25 while (inputStream >> word) {
26 if (!word.isEmpty()) {
27 DragLabel *wordLabel = new DragLabel(word, this);
28 wordLabel->move(x, y);
29 wordLabel->show();
30 wordLabel->setAttribute(Qt::WA_DeleteOnClose);
31 x += wordLabel->width() + 2;
32 if (x >= 245) {
33 x = 5;
34 y += wordLabel->height() + 2;
35 }
36 }
37 }
38 //! [1]
39
40 //! [2]
41 QPalette newPalette = palette();
42 newPalette.setColor(QPalette::Window, Qt::white);
43 setPalette(newPalette);
44
45 setMinimumSize(400, qMax(200, y));
46 setWindowTitle(tr("Fridge Magnets"));
47 //! [2] //! [3]
48 setAcceptDrops(true);
49 }
50 //! [3]
51
52 //! [4]
53 void DragWidget::dragEnterEvent(QDragEnterEvent *event)
54 {
55 //! [4] //! [5]
56 if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) {
57 if (children().contains(event->source())) {
58 event->setDropAction(Qt::MoveAction);
59 event->accept();
60 } else {
61 event->acceptProposedAction();
62 //! [5] //! [6]
63 }
64 //! [6] //! [7]
65 } else if (event->mimeData()->hasText()) {
66 event->acceptProposedAction();
67 } else {
68 event->ignore();
69 }
70 }
71 //! [7]
72
73 //! [8]
74 void DragWidget::dragMoveEvent(QDragMoveEvent *event)
75 {
76 if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) {
77 if (children().contains(event->source())) {
78 event->setDropAction(Qt::MoveAction);
79 event->accept();
80 } else {
81 event->acceptProposedAction();
82 }
83 } else if (event->mimeData()->hasText()) {
84 event->acceptProposedAction();
85 } else {
86 event->ignore();
87 }
88 }
89 //! [8]
90
91 //! [9]
92 void DragWidget::dropEvent(QDropEvent *event)
93 {
94 if (event->mimeData()->hasFormat(fridgetMagnetsMimeType())) {
95 const QMimeData *mime = event->mimeData();
96 //! [9] //! [10]
97 QByteArray itemData = mime->data(fridgetMagnetsMimeType());
98 QDataStream dataStream(&itemData, QIODevice::ReadOnly);
99
100 QString text;
101 QPoint offset;
102 dataStream >> text >> offset;
103 //! [10]
104 //! [11]
105 DragLabel *newLabel = new DragLabel(text, this);
106 newLabel->move(event->position().toPoint() - offset);
107 newLabel->show();
108 newLabel->setAttribute(Qt::WA_DeleteOnClose);
109
110 if (event->source() == this) {
111 event->setDropAction(Qt::MoveAction);
112 event->accept();
113 } else {
114 event->acceptProposedAction();
115 }
116 //! [11] //! [12]
117 } else if (event->mimeData()->hasText()) {
118 QStringList pieces = event->mimeData()->text().split(
119 QRegularExpression(QStringLiteral("\\s+")), Qt::SkipEmptyParts);
120 QPoint position = event->position().toPoint();
121
122 for (const QString &piece : pieces) {
123 DragLabel *newLabel = new DragLabel(piece, this);
124 newLabel->move(position);
125 newLabel->show();
126 newLabel->setAttribute(Qt::WA_DeleteOnClose);
127
128 position += QPoint(newLabel->width(), 0);
129 }
130
131 event->acceptProposedAction();
132 } else {
133 event->ignore();
134 }
135 }
136 //! [12]
137
138 //! [13]
139 void DragWidget::mousePressEvent(QMouseEvent *event)
140 {
141 //! [13]
142 //! [14]
143 DragLabel *child = static_cast<DragLabel*>(childAt(event->position().toPoint()));
144 if (!child)
145 return;
146
147 QPoint hotSpot = event->position().toPoint() - child->pos();
148
149 QByteArray itemData;
150 QDataStream dataStream(&itemData, QIODevice::WriteOnly);
151 dataStream << child->labelText() << QPoint(hotSpot);
152 //! [14]
153
154 //! [15]
155 QMimeData *mimeData = new QMimeData;
156 mimeData->setData(fridgetMagnetsMimeType(), itemData);
157 mimeData->setText(child->labelText());
158 //! [15]
159
160 //! [16]
161 QDrag *drag = new QDrag(this);
162 drag->setMimeData(mimeData);
163 drag->setPixmap(child->pixmap());
164 drag->setHotSpot(hotSpot);
165
166 child->hide();
167 //! [16]
168
169 //! [17]
170 if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction)
171 child->close();
172 else
173 child->show();
174 }
175 //! [17]