author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
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 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
||
43 |
#include "draglabel.h" |
|
44 |
#include "dragwidget.h" |
|
45 |
||
46 |
//! [0] |
|
47 |
DragWidget::DragWidget(QWidget *parent) |
|
48 |
: QWidget(parent) |
|
49 |
{ |
|
50 |
QFile dictionaryFile(":/dictionary/words.txt"); |
|
51 |
dictionaryFile.open(QFile::ReadOnly); |
|
52 |
QTextStream inputStream(&dictionaryFile); |
|
53 |
//! [0] |
|
54 |
||
55 |
//! [1] |
|
56 |
int x = 5; |
|
57 |
int y = 5; |
|
58 |
||
59 |
while (!inputStream.atEnd()) { |
|
60 |
QString word; |
|
61 |
inputStream >> word; |
|
62 |
if (!word.isEmpty()) { |
|
63 |
DragLabel *wordLabel = new DragLabel(word, this); |
|
64 |
wordLabel->move(x, y); |
|
65 |
wordLabel->show(); |
|
66 |
wordLabel->setAttribute(Qt::WA_DeleteOnClose); |
|
67 |
x += wordLabel->width() + 2; |
|
68 |
if (x >= 245) { |
|
69 |
x = 5; |
|
70 |
y += wordLabel->height() + 2; |
|
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
//! [1] |
|
75 |
||
76 |
//! [2] |
|
77 |
#ifndef Q_WS_S60 |
|
78 |
//Fridge magnets is used for demoing Qt on S60 and themed backgrounds look better than white |
|
79 |
QPalette newPalette = palette(); |
|
80 |
newPalette.setColor(QPalette::Window, Qt::white); |
|
81 |
setPalette(newPalette); |
|
82 |
#endif |
|
83 |
||
84 |
setMinimumSize(400, qMax(200, y)); |
|
85 |
setWindowTitle(tr("Fridge Magnets")); |
|
86 |
//! [2] //! [3] |
|
87 |
setAcceptDrops(true); |
|
88 |
} |
|
89 |
//! [3] |
|
90 |
||
91 |
//! [4] |
|
92 |
void DragWidget::dragEnterEvent(QDragEnterEvent *event) |
|
93 |
{ |
|
94 |
//! [4] //! [5] |
|
95 |
if (event->mimeData()->hasFormat("application/x-fridgemagnet")) { |
|
96 |
if (children().contains(event->source())) { |
|
97 |
event->setDropAction(Qt::MoveAction); |
|
98 |
event->accept(); |
|
99 |
} else { |
|
100 |
event->acceptProposedAction(); |
|
101 |
//! [5] //! [6] |
|
102 |
} |
|
103 |
//! [6] //! [7] |
|
104 |
} else if (event->mimeData()->hasText()) { |
|
105 |
event->acceptProposedAction(); |
|
106 |
} else { |
|
107 |
event->ignore(); |
|
108 |
} |
|
109 |
} |
|
110 |
//! [7] |
|
111 |
||
112 |
//! [8] |
|
113 |
void DragWidget::dragMoveEvent(QDragMoveEvent *event) |
|
114 |
{ |
|
115 |
if (event->mimeData()->hasFormat("application/x-fridgemagnet")) { |
|
116 |
if (children().contains(event->source())) { |
|
117 |
event->setDropAction(Qt::MoveAction); |
|
118 |
event->accept(); |
|
119 |
} else { |
|
120 |
event->acceptProposedAction(); |
|
121 |
} |
|
122 |
} else if (event->mimeData()->hasText()) { |
|
123 |
event->acceptProposedAction(); |
|
124 |
} else { |
|
125 |
event->ignore(); |
|
126 |
} |
|
127 |
} |
|
128 |
//! [8] |
|
129 |
||
130 |
//! [9] |
|
131 |
void DragWidget::dropEvent(QDropEvent *event) |
|
132 |
{ |
|
133 |
if (event->mimeData()->hasFormat("application/x-fridgemagnet")) { |
|
134 |
const QMimeData *mime = event->mimeData(); |
|
135 |
//! [9] //! [10] |
|
136 |
QByteArray itemData = mime->data("application/x-fridgemagnet"); |
|
137 |
QDataStream dataStream(&itemData, QIODevice::ReadOnly); |
|
138 |
||
139 |
QString text; |
|
140 |
QPoint offset; |
|
141 |
dataStream >> text >> offset; |
|
142 |
//! [10] |
|
143 |
//! [11] |
|
144 |
DragLabel *newLabel = new DragLabel(text, this); |
|
145 |
newLabel->move(event->pos() - offset); |
|
146 |
newLabel->show(); |
|
147 |
newLabel->setAttribute(Qt::WA_DeleteOnClose); |
|
148 |
||
149 |
if (event->source() == this) { |
|
150 |
event->setDropAction(Qt::MoveAction); |
|
151 |
event->accept(); |
|
152 |
} else { |
|
153 |
event->acceptProposedAction(); |
|
154 |
} |
|
155 |
//! [11] //! [12] |
|
156 |
} else if (event->mimeData()->hasText()) { |
|
157 |
QStringList pieces = event->mimeData()->text().split(QRegExp("\\s+"), |
|
158 |
QString::SkipEmptyParts); |
|
159 |
QPoint position = event->pos(); |
|
160 |
||
161 |
foreach (QString piece, pieces) { |
|
162 |
DragLabel *newLabel = new DragLabel(piece, this); |
|
163 |
newLabel->move(position); |
|
164 |
newLabel->show(); |
|
165 |
newLabel->setAttribute(Qt::WA_DeleteOnClose); |
|
166 |
||
167 |
position += QPoint(newLabel->width(), 0); |
|
168 |
} |
|
169 |
||
170 |
event->acceptProposedAction(); |
|
171 |
} else { |
|
172 |
event->ignore(); |
|
173 |
} |
|
174 |
} |
|
175 |
//! [12] |
|
176 |
||
177 |
//! [13] |
|
178 |
void DragWidget::mousePressEvent(QMouseEvent *event) |
|
179 |
{ |
|
180 |
//! [13] |
|
181 |
//! [14] |
|
182 |
DragLabel *child = static_cast<DragLabel*>(childAt(event->pos())); |
|
183 |
if (!child) |
|
184 |
return; |
|
185 |
||
186 |
QPoint hotSpot = event->pos() - child->pos(); |
|
187 |
||
188 |
QByteArray itemData; |
|
189 |
QDataStream dataStream(&itemData, QIODevice::WriteOnly); |
|
190 |
dataStream << child->labelText() << QPoint(hotSpot); |
|
191 |
//! [14] |
|
192 |
||
193 |
//! [15] |
|
194 |
QMimeData *mimeData = new QMimeData; |
|
195 |
mimeData->setData("application/x-fridgemagnet", itemData); |
|
196 |
mimeData->setText(child->labelText()); |
|
197 |
//! [15] |
|
198 |
||
199 |
//! [16] |
|
200 |
QDrag *drag = new QDrag(this); |
|
201 |
drag->setMimeData(mimeData); |
|
202 |
drag->setPixmap(*child->pixmap()); |
|
203 |
drag->setHotSpot(hotSpot); |
|
204 |
||
205 |
child->hide(); |
|
206 |
//! [16] |
|
207 |
||
208 |
//! [17] |
|
209 |
if (drag->exec(Qt::MoveAction | Qt::CopyAction, Qt::CopyAction) == Qt::MoveAction) |
|
210 |
child->close(); |
|
211 |
else |
|
212 |
child->show(); |
|
213 |
} |
|
214 |
//! [17] |