|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 |
|
44 #include "dragwidget.h" |
|
45 |
|
46 //! [0] |
|
47 DragWidget::DragWidget(QWidget *parent) |
|
48 : QFrame(parent) |
|
49 { |
|
50 setMinimumSize(200, 200); |
|
51 setFrameStyle(QFrame::Sunken | QFrame::StyledPanel); |
|
52 setAcceptDrops(true); |
|
53 |
|
54 QLabel *boatIcon = new QLabel(this); |
|
55 boatIcon->setPixmap(QPixmap(":/images/boat.png")); |
|
56 boatIcon->move(20, 20); |
|
57 boatIcon->show(); |
|
58 boatIcon->setAttribute(Qt::WA_DeleteOnClose); |
|
59 |
|
60 QLabel *carIcon = new QLabel(this); |
|
61 carIcon->setPixmap(QPixmap(":/images/car.png")); |
|
62 carIcon->move(120, 20); |
|
63 carIcon->show(); |
|
64 carIcon->setAttribute(Qt::WA_DeleteOnClose); |
|
65 |
|
66 QLabel *houseIcon = new QLabel(this); |
|
67 houseIcon->setPixmap(QPixmap(":/images/house.png")); |
|
68 houseIcon->move(20, 120); |
|
69 houseIcon->show(); |
|
70 houseIcon->setAttribute(Qt::WA_DeleteOnClose); |
|
71 } |
|
72 //! [0] |
|
73 |
|
74 void DragWidget::dragEnterEvent(QDragEnterEvent *event) |
|
75 { |
|
76 if (event->mimeData()->hasFormat("application/x-dnditemdata")) { |
|
77 if (event->source() == this) { |
|
78 event->setDropAction(Qt::MoveAction); |
|
79 event->accept(); |
|
80 } else { |
|
81 event->acceptProposedAction(); |
|
82 } |
|
83 } else { |
|
84 event->ignore(); |
|
85 } |
|
86 } |
|
87 |
|
88 void DragWidget::dragMoveEvent(QDragMoveEvent *event) |
|
89 { |
|
90 if (event->mimeData()->hasFormat("application/x-dnditemdata")) { |
|
91 if (event->source() == this) { |
|
92 event->setDropAction(Qt::MoveAction); |
|
93 event->accept(); |
|
94 } else { |
|
95 event->acceptProposedAction(); |
|
96 } |
|
97 } else { |
|
98 event->ignore(); |
|
99 } |
|
100 } |
|
101 |
|
102 void DragWidget::dropEvent(QDropEvent *event) |
|
103 { |
|
104 if (event->mimeData()->hasFormat("application/x-dnditemdata")) { |
|
105 QByteArray itemData = event->mimeData()->data("application/x-dnditemdata"); |
|
106 QDataStream dataStream(&itemData, QIODevice::ReadOnly); |
|
107 |
|
108 QPixmap pixmap; |
|
109 QPoint offset; |
|
110 dataStream >> pixmap >> offset; |
|
111 |
|
112 QLabel *newIcon = new QLabel(this); |
|
113 newIcon->setPixmap(pixmap); |
|
114 newIcon->move(event->pos() - offset); |
|
115 newIcon->show(); |
|
116 newIcon->setAttribute(Qt::WA_DeleteOnClose); |
|
117 |
|
118 if (event->source() == this) { |
|
119 event->setDropAction(Qt::MoveAction); |
|
120 event->accept(); |
|
121 } else { |
|
122 event->acceptProposedAction(); |
|
123 } |
|
124 } else { |
|
125 event->ignore(); |
|
126 } |
|
127 } |
|
128 |
|
129 //! [1] |
|
130 void DragWidget::mousePressEvent(QMouseEvent *event) |
|
131 { |
|
132 QLabel *child = static_cast<QLabel*>(childAt(event->pos())); |
|
133 if (!child) |
|
134 return; |
|
135 |
|
136 QPixmap pixmap = *child->pixmap(); |
|
137 |
|
138 QByteArray itemData; |
|
139 QDataStream dataStream(&itemData, QIODevice::WriteOnly); |
|
140 dataStream << pixmap << QPoint(event->pos() - child->pos()); |
|
141 //! [1] |
|
142 |
|
143 //! [2] |
|
144 QMimeData *mimeData = new QMimeData; |
|
145 mimeData->setData("application/x-dnditemdata", itemData); |
|
146 //! [2] |
|
147 |
|
148 //! [3] |
|
149 QDrag *drag = new QDrag(this); |
|
150 drag->setMimeData(mimeData); |
|
151 drag->setPixmap(pixmap); |
|
152 drag->setHotSpot(event->pos() - child->pos()); |
|
153 //! [3] |
|
154 |
|
155 QPixmap tempPixmap = pixmap; |
|
156 QPainter painter; |
|
157 painter.begin(&tempPixmap); |
|
158 painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127)); |
|
159 painter.end(); |
|
160 |
|
161 child->setPixmap(tempPixmap); |
|
162 |
|
163 if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) |
|
164 child->close(); |
|
165 else { |
|
166 child->show(); |
|
167 child->setPixmap(pixmap); |
|
168 } |
|
169 } |