Debian Codegraph @

tests/manual/highdpi/kitchensink/dragwidget.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtWidgets>
5 #include "dragwidget.h"
6
7 class FramedLabel : public QLabel
8 {
9 public:
10 FramedLabel(const QString &text, QWidget *parent)
11 : QLabel(text, parent)
12 {
13 setAutoFillBackground(true);
14 setFrameShape(QFrame::Panel);
15 setFrameShadow(QFrame::Raised);
16 }
17 };
18
19 DragWidget::DragWidget(QString text, QWidget *parent)
20 : QWidget(parent)
21 {
22 int x = 5;
23 int y = 5;
24
25 bool createChildWindow = text.isEmpty(); // OK, yes this is a hack...
26 if (text.isEmpty())
27 text = "You can drag from this window and drop text here";
28
29 QStringList words = text.split(' ');
30 for (const QString &word : words) {
31 if (!word.isEmpty()) {
32 auto wordLabel = new FramedLabel(word, this);
33 wordLabel->move(x, y);
34 wordLabel->show();
35 x += wordLabel->width() + 2;
36 if (x >= 245) {
37 x = 5;
38 y += wordLabel->height() + 2;
39 }
40 }
41 }
42
43 /*
44 QPalette newPalette = palette();
45 newPalette.setColor(QPalette::Window, Qt::white);
46 setPalette(newPalette);
47 */
48
49 setAcceptDrops(true);
50 setMinimumSize(400, qMax(200, y));
51 setWindowTitle(tr("Draggable Text Window %1").arg(createChildWindow ? 1 : 2));
52 if (createChildWindow)
53 otherWindow = new DragWidget("Here is a second window that accepts drops");
54 }
55
56 void DragWidget::dragEnterEvent(QDragEnterEvent *event)
57 {
58 if (event->mimeData()->hasText()) {
59 if (event->source() == this) {
60 event->setDropAction(Qt::MoveAction);
61 event->accept();
62 } else {
63 event->acceptProposedAction();
64 }
65 } else {
66 event->ignore();
67 }
68 }
69
70 void DragWidget::dragMoveEvent(QDragMoveEvent * event)
71 {
72 dragPos = event->pos();
73 dragTimer.start(500, this);
74 update();
75 }
76
77 void DragWidget::dragLeaveEvent(QDragLeaveEvent *)
78 {
79 dragTimer.stop();
80 update();
81 }
82
83 void DragWidget::dropEvent(QDropEvent *event)
84 {
85 if (event->mimeData()->hasText()) {
86 const QMimeData *mime = event->mimeData();
87 const QStringList pieces = mime->text().split(QRegularExpression("\\s+"),
88 Qt::SkipEmptyParts);
89 QPoint position = event->pos();
90 QPoint hotSpot;
91
92 QList<QByteArray> hotSpotPos = mime->data("application/x-hotspot").split(' ');
93 if (hotSpotPos.size() == 2) {
94 hotSpot.setX(hotSpotPos.first().toInt());
95 hotSpot.setY(hotSpotPos.last().toInt());
96 }
97 dropPos = position - hotSpot;
98 dropTimer.start(500, this);
99 update();
100
101 for (const QString &piece : pieces) {
102 FramedLabel *newLabel = new FramedLabel(piece, this);
103 newLabel->move(position - hotSpot);
104 newLabel->show();
105
106 position += QPoint(newLabel->width(), 0);
107 }
108
109 if (event->source() == this) {
110 event->setDropAction(Qt::MoveAction);
111 event->accept();
112 } else {
113 event->acceptProposedAction();
114 }
115 } else {
116 event->ignore();
117 }
118 for (QObject *child : children()) {
119 if (child->isWidgetType()) {
120 auto widget = static_cast<QWidget *>(child);
121 if (!widget->isVisible())
122 widget->deleteLater();
123 }
124 }
125 }
126
127 void DragWidget::mousePressEvent(QMouseEvent *event)
128 {
129 QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
130 if (!child)
131 return;
132
133 QPoint hotSpot = event->pos() - child->pos();
134
135 QMimeData *mimeData = new QMimeData;
136 mimeData->setText(child->text());
137 mimeData->setData("application/x-hotspot",
138 QByteArray::number(hotSpot.x()) + ' ' + QByteArray::number(hotSpot.y()));
139
140 const qreal dpr = devicePixelRatio() > 1 && !(QGuiApplication::keyboardModifiers() & Qt::ShiftModifier)
141 ? devicePixelRatio() : 1;
142
143 QPixmap pixmap(child->size() * dpr);
144 pixmap.setDevicePixelRatio(dpr);
145 child->render(&pixmap);
146
147 auto drag = new QDrag(this);
148 drag->setMimeData(mimeData);
149 drag->setPixmap(pixmap);
150 drag->setHotSpot(hotSpot);
151
152 Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction);
153
154 if (dropAction == Qt::MoveAction)
155 child->close();
156 }
157
158 void DragWidget::timerEvent(QTimerEvent *e)
159 {
160 if (e->timerId() == dragTimer.timerId())
161 dragTimer.stop();
162 if (e->timerId() == dropTimer.timerId())
163 dropTimer.stop();
164 update();
165 }
166
167 void DragWidget::paintEvent(QPaintEvent *)
168 {
169 QPainter p(this);
170 p.fillRect(rect(), Qt::white);
171
172 if (dropTimer.isActive()) {
173 p.setBrush(Qt::red);
174 p.drawEllipse(dropPos, 50, 50);
175 }
176
177 if (dragTimer.isActive()) {
178 p.setPen(QPen(Qt::blue, 5));
179 QPoint p1 = (rect().topLeft()*3 + rect().bottomRight())/4;
180 QPoint p2 = (rect().topLeft() + rect().bottomRight()*3)/4;
181 p.drawLine(p1, dragPos);
182 p.drawLine(p2, dragPos);
183 }
184 }
185
186 void DragWidget::showEvent(QShowEvent *)
187 {
188 if (otherWindow)
189 otherWindow->show();
190 }
191
192 void DragWidget::hideEvent(QHideEvent *)
193 {
194 if (otherWindow)
195 otherWindow->hide();
196 }