author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 28 Apr 2010 13:15:16 +0300 | |
branch | RCL_3 |
changeset 15 | b25b6dc3ff8b |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 "draglabel.h" |
|
45 |
#include "dragwidget.h" |
|
46 |
||
47 |
//! [0] |
|
48 |
DragWidget::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 |
#ifndef Q_WS_S60 |
|
79 |
//Fridge magnets is used for demoing Qt on S60 and themed backgrounds look better than white |
|
80 |
QPalette newPalette = palette(); |
|
81 |
newPalette.setColor(QPalette::Window, Qt::white); |
|
82 |
setPalette(newPalette); |
|
83 |
#endif |
|
84 |
||
85 |
setMinimumSize(400, qMax(200, y)); |
|
86 |
setWindowTitle(tr("Fridge Magnets")); |
|
87 |
//! [2] //! [3] |
|
88 |
setAcceptDrops(true); |
|
89 |
} |
|
90 |
//! [3] |
|
91 |
||
92 |
//! [4] |
|
93 |
void DragWidget::dragEnterEvent(QDragEnterEvent *event) |
|
94 |
{ |
|
95 |
//! [4] //! [5] |
|
96 |
if (event->mimeData()->hasFormat("application/x-fridgemagnet")) { |
|
97 |
if (children().contains(event->source())) { |
|
98 |
event->setDropAction(Qt::MoveAction); |
|
99 |
event->accept(); |
|
100 |
} else { |
|
101 |
event->acceptProposedAction(); |
|
102 |
//! [5] //! [6] |
|
103 |
} |
|
104 |
//! [6] //! [7] |
|
105 |
} else if (event->mimeData()->hasText()) { |
|
106 |
event->acceptProposedAction(); |
|
107 |
} else { |
|
108 |
event->ignore(); |
|
109 |
} |
|
110 |
} |
|
111 |
//! [7] |
|
112 |
||
113 |
//! [8] |
|
114 |
void DragWidget::dragMoveEvent(QDragMoveEvent *event) |
|
115 |
{ |
|
116 |
if (event->mimeData()->hasFormat("application/x-fridgemagnet")) { |
|
117 |
if (children().contains(event->source())) { |
|
118 |
event->setDropAction(Qt::MoveAction); |
|
119 |
event->accept(); |
|
120 |
} else { |
|
121 |
event->acceptProposedAction(); |
|
122 |
} |
|
123 |
} else if (event->mimeData()->hasText()) { |
|
124 |
event->acceptProposedAction(); |
|
125 |
} else { |
|
126 |
event->ignore(); |
|
127 |
} |
|
128 |
} |
|
129 |
//! [8] |
|
130 |
||
131 |
//! [9] |
|
132 |
void DragWidget::dropEvent(QDropEvent *event) |
|
133 |
{ |
|
134 |
if (event->mimeData()->hasFormat("application/x-fridgemagnet")) { |
|
135 |
const QMimeData *mime = event->mimeData(); |
|
136 |
//! [9] //! [10] |
|
137 |
QByteArray itemData = mime->data("application/x-fridgemagnet"); |
|
138 |
QDataStream dataStream(&itemData, QIODevice::ReadOnly); |
|
139 |
||
140 |
QString text; |
|
141 |
QPoint offset; |
|
142 |
dataStream >> text >> offset; |
|
143 |
//! [10] |
|
144 |
//! [11] |
|
145 |
DragLabel *newLabel = new DragLabel(text, this); |
|
146 |
newLabel->move(event->pos() - offset); |
|
147 |
newLabel->show(); |
|
148 |
newLabel->setAttribute(Qt::WA_DeleteOnClose); |
|
149 |
||
150 |
if (event->source() == this) { |
|
151 |
event->setDropAction(Qt::MoveAction); |
|
152 |
event->accept(); |
|
153 |
} else { |
|
154 |
event->acceptProposedAction(); |
|
155 |
} |
|
156 |
//! [11] //! [12] |
|
157 |
} else if (event->mimeData()->hasText()) { |
|
158 |
QStringList pieces = event->mimeData()->text().split(QRegExp("\\s+"), |
|
159 |
QString::SkipEmptyParts); |
|
160 |
QPoint position = event->pos(); |
|
161 |
||
162 |
foreach (QString piece, pieces) { |
|
163 |
DragLabel *newLabel = new DragLabel(piece, this); |
|
164 |
newLabel->move(position); |
|
165 |
newLabel->show(); |
|
166 |
newLabel->setAttribute(Qt::WA_DeleteOnClose); |
|
167 |
||
168 |
position += QPoint(newLabel->width(), 0); |
|
169 |
} |
|
170 |
||
171 |
event->acceptProposedAction(); |
|
172 |
} else { |
|
173 |
event->ignore(); |
|
174 |
} |
|
175 |
} |
|
176 |
//! [12] |
|
177 |
||
178 |
//! [13] |
|
179 |
void DragWidget::mousePressEvent(QMouseEvent *event) |
|
180 |
{ |
|
181 |
//! [13] |
|
182 |
//! [14] |
|
183 |
DragLabel *child = static_cast<DragLabel*>(childAt(event->pos())); |
|
184 |
if (!child) |
|
185 |
return; |
|
186 |
||
187 |
QPoint hotSpot = event->pos() - child->pos(); |
|
188 |
||
189 |
QByteArray itemData; |
|
190 |
QDataStream dataStream(&itemData, QIODevice::WriteOnly); |
|
191 |
dataStream << child->labelText() << QPoint(hotSpot); |
|
192 |
//! [14] |
|
193 |
||
194 |
//! [15] |
|
195 |
QMimeData *mimeData = new QMimeData; |
|
196 |
mimeData->setData("application/x-fridgemagnet", itemData); |
|
197 |
mimeData->setText(child->labelText()); |
|
198 |
//! [15] |
|
199 |
||
200 |
//! [16] |
|
201 |
QDrag *drag = new QDrag(this); |
|
202 |
drag->setMimeData(mimeData); |
|
203 |
drag->setPixmap(*child->pixmap()); |
|
204 |
drag->setHotSpot(hotSpot); |
|
205 |
||
206 |
child->hide(); |
|
207 |
//! [16] |
|
208 |
||
209 |
//! [17] |
|
210 |
if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction) |
|
211 |
child->close(); |
|
212 |
else |
|
213 |
child->show(); |
|
214 |
} |
|
215 |
//! [17] |