source: trunk/examples/draganddrop/fridgemagnets/dragwidget.cpp@ 468

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

examples/draganddrop: Be consistent and use children as drag sources everywhere. This in particular makes sure that the visual representation corresponds to the default drop action (Move) [vendor bug].

File size: 6.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "draglabel.h"
45#include "dragwidget.h"
46
47//! [0]
48DragWidget::DragWidget(QWidget *parent)
49 : QWidget(parent)
50{
51 QFile dictionaryFile(":/dictionary/words.txt");
52 dictionaryFile.open(QFile::ReadOnly);
53 QTextStream inputStream(&dictionaryFile);
54//! [0]
55
56//! [1]
57 int x = 5;
58 int y = 5;
59
60 while (!inputStream.atEnd()) {
61 QString word;
62 inputStream >> word;
63 if (!word.isEmpty()) {
64 DragLabel *wordLabel = new DragLabel(word, this);
65 wordLabel->move(x, y);
66 wordLabel->show();
67 wordLabel->setAttribute(Qt::WA_DeleteOnClose);
68 x += wordLabel->width() + 2;
69 if (x >= 245) {
70 x = 5;
71 y += wordLabel->height() + 2;
72 }
73 }
74 }
75//! [1]
76
77//! [2]
78 QPalette newPalette = palette();
79 newPalette.setColor(QPalette::Window, Qt::white);
80 setPalette(newPalette);
81
82 setMinimumSize(400, qMax(200, y));
83 setWindowTitle(tr("Fridge Magnets"));
84//! [2] //! [3]
85 setAcceptDrops(true);
86}
87//! [3]
88
89//! [4]
90void DragWidget::dragEnterEvent(QDragEnterEvent *event)
91{
92//! [4] //! [5]
93 if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
94 if (children().contains(event->source())) {
95 event->setDropAction(Qt::MoveAction);
96 event->accept();
97 } else {
98 event->acceptProposedAction();
99//! [5] //! [6]
100 }
101//! [6] //! [7]
102 } else if (event->mimeData()->hasText()) {
103 event->acceptProposedAction();
104 } else {
105 event->ignore();
106 }
107}
108//! [7]
109
110//! [8]
111void DragWidget::dragMoveEvent(QDragMoveEvent *event)
112{
113 if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
114 if (children().contains(event->source())) {
115 event->setDropAction(Qt::MoveAction);
116 event->accept();
117 } else {
118 event->acceptProposedAction();
119 }
120 } else if (event->mimeData()->hasText()) {
121 event->acceptProposedAction();
122 } else {
123 event->ignore();
124 }
125}
126//! [8]
127
128//! [9]
129void DragWidget::dropEvent(QDropEvent *event)
130{
131 if (event->mimeData()->hasFormat("application/x-fridgemagnet")) {
132 const QMimeData *mime = event->mimeData();
133//! [9] //! [10]
134 QByteArray itemData = mime->data("application/x-fridgemagnet");
135 QDataStream dataStream(&itemData, QIODevice::ReadOnly);
136
137 QString text;
138 QPoint offset;
139 dataStream >> text >> offset;
140//! [10]
141//! [11]
142 DragLabel *newLabel = new DragLabel(text, this);
143 newLabel->move(event->pos() - offset);
144 newLabel->show();
145 newLabel->setAttribute(Qt::WA_DeleteOnClose);
146
147 if (children().contains(event->source())) {
148 event->setDropAction(Qt::MoveAction);
149 event->accept();
150 } else {
151 event->acceptProposedAction();
152 }
153//! [11] //! [12]
154 } else if (event->mimeData()->hasText()) {
155 QStringList pieces = event->mimeData()->text().split(QRegExp("\\s+"),
156 QString::SkipEmptyParts);
157 QPoint position = event->pos();
158
159 foreach (QString piece, pieces) {
160 DragLabel *newLabel = new DragLabel(piece, this);
161 newLabel->move(position);
162 newLabel->show();
163 newLabel->setAttribute(Qt::WA_DeleteOnClose);
164
165 position += QPoint(newLabel->width(), 0);
166 }
167
168 event->acceptProposedAction();
169 } else {
170 event->ignore();
171 }
172}
173//! [12]
174
175//! [13]
176void DragWidget::mousePressEvent(QMouseEvent *event)
177{
178//! [13]
179//! [14]
180 DragLabel *child = static_cast<DragLabel*>(childAt(event->pos()));
181 if (!child)
182 return;
183
184 QPoint hotSpot = event->pos() - child->pos();
185
186 QByteArray itemData;
187 QDataStream dataStream(&itemData, QIODevice::WriteOnly);
188 dataStream << child->labelText() << QPoint(hotSpot);
189//! [14]
190
191//! [15]
192 QMimeData *mimeData = new QMimeData;
193 mimeData->setData("application/x-fridgemagnet", itemData);
194 mimeData->setText(child->labelText());
195//! [15]
196
197//! [16]
198 QDrag *drag = new QDrag(child);
199 drag->setMimeData(mimeData);
200 drag->setPixmap(*child->pixmap());
201 drag->setHotSpot(hotSpot);
202
203 child->hide();
204//! [16]
205