| 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 |
//! [0] |
| 9 |
DragWidget::DragWidget(QWidget *parent) |
| 10 |
: QFrame(parent) |
| 11 |
{ |
| 12 |
setMinimumSize(200, 200); |
| 13 |
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel); |
| 14 |
setAcceptDrops(true); |
| 15 |
|
| 16 |
QLabel *boatIcon = new QLabel(this); |
| 17 |
boatIcon->setPixmap(QPixmap(":/images/boat.png")); |
| 18 |
boatIcon->move(10, 10); |
| 19 |
boatIcon->show(); |
| 20 |
boatIcon->setAttribute(Qt::WA_DeleteOnClose); |
| 21 |
|
| 22 |
QLabel *carIcon = new QLabel(this); |
| 23 |
carIcon->setPixmap(QPixmap(":/images/car.png")); |
| 24 |
carIcon->move(100, 10); |
| 25 |
carIcon->show(); |
| 26 |
carIcon->setAttribute(Qt::WA_DeleteOnClose); |
| 27 |
|
| 28 |
QLabel *houseIcon = new QLabel(this); |
| 29 |
houseIcon->setPixmap(QPixmap(":/images/house.png")); |
| 30 |
houseIcon->move(10, 80); |
| 31 |
houseIcon->show(); |
| 32 |
houseIcon->setAttribute(Qt::WA_DeleteOnClose); |
| 33 |
} |
| 34 |
//! [0] |
| 35 |
|
| 36 |
void DragWidget::dragEnterEvent(QDragEnterEvent *event) |
| 37 |
{ |
| 38 |
if (event->mimeData()->hasFormat("application/x-dnditemdata")) { |
| 39 |
if (event->source() == this) { |
| 40 |
event->setDropAction(Qt::MoveAction); |
| 41 |
event->accept(); |
| 42 |
} else { |
| 43 |
event->acceptProposedAction(); |
| 44 |
} |
| 45 |
} else { |
| 46 |
event->ignore(); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
void DragWidget::dragMoveEvent(QDragMoveEvent *event) |
| 51 |
{ |
| 52 |
if (event->mimeData()->hasFormat("application/x-dnditemdata")) { |
| 53 |
if (event->source() == this) { |
| 54 |
event->setDropAction(Qt::MoveAction); |
| 55 |
event->accept(); |
| 56 |
} else { |
| 57 |
event->acceptProposedAction(); |
| 58 |
} |
| 59 |
} else { |
| 60 |
event->ignore(); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
void DragWidget::dropEvent(QDropEvent *event) |
| 65 |
{ |
| 66 |
if (event->mimeData()->hasFormat("application/x-dnditemdata")) { |
| 67 |
QByteArray itemData = event->mimeData()->data("application/x-dnditemdata"); |
| 68 |
QDataStream dataStream(&itemData, QIODevice::ReadOnly); |
| 69 |
|
| 70 |
QPixmap pixmap; |
| 71 |
QPoint offset; |
| 72 |
dataStream >> pixmap >> offset; |
| 73 |
|
| 74 |
QLabel *newIcon = new QLabel(this); |
| 75 |
newIcon->setPixmap(pixmap); |
| 76 |
newIcon->move(event->position().toPoint() - offset); |
| 77 |
newIcon->show(); |
| 78 |
newIcon->setAttribute(Qt::WA_DeleteOnClose); |
| 79 |
|
| 80 |
if (event->source() == this) { |
| 81 |
event->setDropAction(Qt::MoveAction); |
| 82 |
event->accept(); |
| 83 |
} else { |
| 84 |
event->acceptProposedAction(); |
| 85 |
} |
| 86 |
} else { |
| 87 |
event->ignore(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
//! [1] |
| 92 |
void DragWidget::mousePressEvent(QMouseEvent *event) |
| 93 |
{ |
| 94 |
QLabel *child = static_cast<QLabel*>(childAt(event->position().toPoint())); |
| 95 |
if (!child) |
| 96 |
return; |
| 97 |
|
| 98 |
QPixmap pixmap = child->pixmap(); |
| 99 |
|
| 100 |
QByteArray itemData; |
| 101 |
QDataStream dataStream(&itemData, QIODevice::WriteOnly); |
| 102 |
dataStream << pixmap << QPoint(event->position().toPoint() - child->pos()); |
| 103 |
//! [1] |
| 104 |
|
| 105 |
//! [2] |
| 106 |
QMimeData *mimeData = new QMimeData; |
| 107 |
mimeData->setData("application/x-dnditemdata", itemData); |
| 108 |
//! [2] |
| 109 |
|
| 110 |
//! [3] |
| 111 |
QDrag *drag = new QDrag(this); |
| 112 |
drag->setMimeData(mimeData); |
| 113 |
drag->setPixmap(pixmap); |
| 114 |
drag->setHotSpot(event->position().toPoint() - child->pos()); |
| 115 |
//! [3] |
| 116 |
|
| 117 |
child->setParent(nullptr); |
| 118 |
|
| 119 |
if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) { |
| 120 |
delete child; |
| 121 |
} else { |
| 122 |
child->setParent(this); |
| 123 |
child->move(event->position().toPoint() - drag->hotSpot()); |
| 124 |
child->show(); |
| 125 |
} |
| 126 |
} |