source: trunk/examples/draganddrop/draggableicons/dragwidget.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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