2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2011-05-07 20:57:49 +00:00
|
|
|
#include <QtWidgets>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "dragwidget.h"
|
|
|
|
|
|
|
|
//! [0]
|
|
|
|
DragWidget::DragWidget(QWidget *parent)
|
|
|
|
: QFrame(parent)
|
|
|
|
{
|
|
|
|
setMinimumSize(200, 200);
|
|
|
|
setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
|
|
|
|
setAcceptDrops(true);
|
|
|
|
|
|
|
|
QLabel *boatIcon = new QLabel(this);
|
|
|
|
boatIcon->setPixmap(QPixmap(":/images/boat.png"));
|
2011-04-27 17:16:41 +00:00
|
|
|
boatIcon->move(10, 10);
|
2011-04-27 10:05:43 +00:00
|
|
|
boatIcon->show();
|
|
|
|
boatIcon->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
|
|
|
|
QLabel *carIcon = new QLabel(this);
|
|
|
|
carIcon->setPixmap(QPixmap(":/images/car.png"));
|
2011-04-27 17:16:41 +00:00
|
|
|
carIcon->move(100, 10);
|
2011-04-27 10:05:43 +00:00
|
|
|
carIcon->show();
|
|
|
|
carIcon->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
|
|
|
|
QLabel *houseIcon = new QLabel(this);
|
|
|
|
houseIcon->setPixmap(QPixmap(":/images/house.png"));
|
2011-04-27 17:16:41 +00:00
|
|
|
houseIcon->move(10, 80);
|
2011-04-27 10:05:43 +00:00
|
|
|
houseIcon->show();
|
|
|
|
houseIcon->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
}
|
|
|
|
//! [0]
|
|
|
|
|
|
|
|
void DragWidget::dragEnterEvent(QDragEnterEvent *event)
|
|
|
|
{
|
|
|
|
if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
|
|
|
|
if (event->source() == this) {
|
|
|
|
event->setDropAction(Qt::MoveAction);
|
|
|
|
event->accept();
|
|
|
|
} else {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DragWidget::dragMoveEvent(QDragMoveEvent *event)
|
|
|
|
{
|
|
|
|
if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
|
|
|
|
if (event->source() == this) {
|
|
|
|
event->setDropAction(Qt::MoveAction);
|
|
|
|
event->accept();
|
|
|
|
} else {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DragWidget::dropEvent(QDropEvent *event)
|
|
|
|
{
|
|
|
|
if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
|
|
|
|
QByteArray itemData = event->mimeData()->data("application/x-dnditemdata");
|
|
|
|
QDataStream dataStream(&itemData, QIODevice::ReadOnly);
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QPixmap pixmap;
|
|
|
|
QPoint offset;
|
|
|
|
dataStream >> pixmap >> offset;
|
|
|
|
|
|
|
|
QLabel *newIcon = new QLabel(this);
|
|
|
|
newIcon->setPixmap(pixmap);
|
2020-06-04 16:07:06 +00:00
|
|
|
newIcon->move(event->position().toPoint() - offset);
|
2011-04-27 10:05:43 +00:00
|
|
|
newIcon->show();
|
|
|
|
newIcon->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
|
|
|
|
if (event->source() == this) {
|
|
|
|
event->setDropAction(Qt::MoveAction);
|
|
|
|
event->accept();
|
|
|
|
} else {
|
|
|
|
event->acceptProposedAction();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
event->ignore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [1]
|
|
|
|
void DragWidget::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
2020-06-04 16:07:06 +00:00
|
|
|
QLabel *child = static_cast<QLabel*>(childAt(event->position().toPoint()));
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!child)
|
|
|
|
return;
|
|
|
|
|
2023-02-20 09:06:46 +00:00
|
|
|
QPixmap pixmap = child->pixmap();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QByteArray itemData;
|
|
|
|
QDataStream dataStream(&itemData, QIODevice::WriteOnly);
|
2020-06-04 16:07:06 +00:00
|
|
|
dataStream << pixmap << QPoint(event->position().toPoint() - child->pos());
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [1]
|
|
|
|
|
|
|
|
//! [2]
|
|
|
|
QMimeData *mimeData = new QMimeData;
|
|
|
|
mimeData->setData("application/x-dnditemdata", itemData);
|
|
|
|
//! [2]
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [3]
|
|
|
|
QDrag *drag = new QDrag(this);
|
|
|
|
drag->setMimeData(mimeData);
|
|
|
|
drag->setPixmap(pixmap);
|
2020-06-04 16:07:06 +00:00
|
|
|
drag->setHotSpot(event->position().toPoint() - child->pos());
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [3]
|
|
|
|
|
2024-11-28 08:14:49 +00:00
|
|
|
child->setParent(nullptr);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-11-23 13:27:50 +00:00
|
|
|
if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
|
2024-11-28 08:14:49 +00:00
|
|
|
delete child;
|
2012-11-23 13:27:50 +00:00
|
|
|
} else {
|
2024-11-28 08:14:49 +00:00
|
|
|
child->setParent(this);
|
|
|
|
child->move(event->position().toPoint() - drag->hotSpot());
|
2011-04-27 10:05:43 +00:00
|
|
|
child->show();
|
|
|
|
}
|
|
|
|
}
|