| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include <QtWidgets> |
| 52 | |
| 53 | #include "dragwidget.h" |
| 54 | |
| 55 | //! [0] |
| 56 | DragWidget::DragWidget(QWidget *parent) |
| 57 | : QFrame(parent) |
| 58 | { |
| 59 | setMinimumSize(minw: 200, minh: 200); |
| 60 | setFrameStyle(QFrame::Sunken | QFrame::StyledPanel); |
| 61 | setAcceptDrops(true); |
| 62 | |
| 63 | QLabel *boatIcon = new QLabel(this); |
| 64 | boatIcon->setPixmap(QPixmap(":/images/boat.png" )); |
| 65 | boatIcon->move(ax: 10, ay: 10); |
| 66 | boatIcon->show(); |
| 67 | boatIcon->setAttribute(Qt::WA_DeleteOnClose); |
| 68 | |
| 69 | QLabel *carIcon = new QLabel(this); |
| 70 | carIcon->setPixmap(QPixmap(":/images/car.png" )); |
| 71 | carIcon->move(ax: 100, ay: 10); |
| 72 | carIcon->show(); |
| 73 | carIcon->setAttribute(Qt::WA_DeleteOnClose); |
| 74 | |
| 75 | QLabel *houseIcon = new QLabel(this); |
| 76 | houseIcon->setPixmap(QPixmap(":/images/house.png" )); |
| 77 | houseIcon->move(ax: 10, ay: 80); |
| 78 | houseIcon->show(); |
| 79 | houseIcon->setAttribute(Qt::WA_DeleteOnClose); |
| 80 | } |
| 81 | //! [0] |
| 82 | |
| 83 | void DragWidget::dragEnterEvent(QDragEnterEvent *event) |
| 84 | { |
| 85 | if (event->mimeData()->hasFormat(mimetype: "application/x-dnditemdata" )) { |
| 86 | if (event->source() == this) { |
| 87 | event->setDropAction(Qt::MoveAction); |
| 88 | event->accept(); |
| 89 | } else { |
| 90 | event->acceptProposedAction(); |
| 91 | } |
| 92 | } else { |
| 93 | event->ignore(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void DragWidget::dragMoveEvent(QDragMoveEvent *event) |
| 98 | { |
| 99 | if (event->mimeData()->hasFormat(mimetype: "application/x-dnditemdata" )) { |
| 100 | if (event->source() == this) { |
| 101 | event->setDropAction(Qt::MoveAction); |
| 102 | event->accept(); |
| 103 | } else { |
| 104 | event->acceptProposedAction(); |
| 105 | } |
| 106 | } else { |
| 107 | event->ignore(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void DragWidget::dropEvent(QDropEvent *event) |
| 112 | { |
| 113 | if (event->mimeData()->hasFormat(mimetype: "application/x-dnditemdata" )) { |
| 114 | QByteArray itemData = event->mimeData()->data(mimetype: "application/x-dnditemdata" ); |
| 115 | QDataStream dataStream(&itemData, QIODevice::ReadOnly); |
| 116 | |
| 117 | QPixmap pixmap; |
| 118 | QPoint offset; |
| 119 | dataStream >> pixmap >> offset; |
| 120 | |
| 121 | QLabel *newIcon = new QLabel(this); |
| 122 | newIcon->setPixmap(pixmap); |
| 123 | newIcon->move(event->pos() - offset); |
| 124 | newIcon->show(); |
| 125 | newIcon->setAttribute(Qt::WA_DeleteOnClose); |
| 126 | |
| 127 | if (event->source() == this) { |
| 128 | event->setDropAction(Qt::MoveAction); |
| 129 | event->accept(); |
| 130 | } else { |
| 131 | event->acceptProposedAction(); |
| 132 | } |
| 133 | } else { |
| 134 | event->ignore(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | //! [1] |
| 139 | void DragWidget::mousePressEvent(QMouseEvent *event) |
| 140 | { |
| 141 | QLabel *child = static_cast<QLabel*>(childAt(p: event->pos())); |
| 142 | if (!child) |
| 143 | return; |
| 144 | |
| 145 | QPixmap pixmap = child->pixmap(Qt::ReturnByValue); |
| 146 | |
| 147 | QByteArray itemData; |
| 148 | QDataStream dataStream(&itemData, QIODevice::WriteOnly); |
| 149 | dataStream << pixmap << QPoint(event->pos() - child->pos()); |
| 150 | //! [1] |
| 151 | |
| 152 | //! [2] |
| 153 | QMimeData *mimeData = new QMimeData; |
| 154 | mimeData->setData(mimetype: "application/x-dnditemdata" , data: itemData); |
| 155 | //! [2] |
| 156 | |
| 157 | //! [3] |
| 158 | QDrag *drag = new QDrag(this); |
| 159 | drag->setMimeData(mimeData); |
| 160 | drag->setPixmap(pixmap); |
| 161 | drag->setHotSpot(event->pos() - child->pos()); |
| 162 | //! [3] |
| 163 | |
| 164 | QPixmap tempPixmap = pixmap; |
| 165 | QPainter painter; |
| 166 | painter.begin(&tempPixmap); |
| 167 | painter.fillRect(pixmap.rect(), color: QColor(127, 127, 127, 127)); |
| 168 | painter.end(); |
| 169 | |
| 170 | child->setPixmap(tempPixmap); |
| 171 | |
| 172 | if (drag->exec(supportedActions: Qt::CopyAction | Qt::MoveAction, defaultAction: Qt::CopyAction) == Qt::MoveAction) { |
| 173 | child->close(); |
| 174 | } else { |
| 175 | child->show(); |
| 176 | child->setPixmap(pixmap); |
| 177 | } |
| 178 | } |
| 179 | |