|
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 "draglabel.h" |
|
45 #include "dragwidget.h" |
|
46 |
|
47 DragWidget::DragWidget(QWidget *parent) |
|
48 : QWidget(parent) |
|
49 { |
|
50 QFile dictionaryFile(":/dictionary/words.txt"); |
|
51 dictionaryFile.open(QIODevice::ReadOnly); |
|
52 QTextStream inputStream(&dictionaryFile); |
|
53 |
|
54 int x = 5; |
|
55 int y = 5; |
|
56 |
|
57 while (!inputStream.atEnd()) { |
|
58 QString word; |
|
59 inputStream >> word; |
|
60 if (!word.isEmpty()) { |
|
61 DragLabel *wordLabel = new DragLabel(word, this); |
|
62 wordLabel->move(x, y); |
|
63 wordLabel->show(); |
|
64 wordLabel->setAttribute(Qt::WA_DeleteOnClose); |
|
65 x += wordLabel->width() + 2; |
|
66 if (x >= 195) { |
|
67 x = 5; |
|
68 y += wordLabel->height() + 2; |
|
69 } |
|
70 } |
|
71 } |
|
72 |
|
73 QPalette newPalette = palette(); |
|
74 newPalette.setColor(QPalette::Window, Qt::white); |
|
75 setPalette(newPalette); |
|
76 |
|
77 setAcceptDrops(true); |
|
78 setMinimumSize(400, qMax(200, y)); |
|
79 setWindowTitle(tr("Draggable Text")); |
|
80 } |
|
81 |
|
82 void DragWidget::dragEnterEvent(QDragEnterEvent *event) |
|
83 { |
|
84 if (event->mimeData()->hasText()) { |
|
85 if (children().contains(event->source())) { |
|
86 event->setDropAction(Qt::MoveAction); |
|
87 event->accept(); |
|
88 } else { |
|
89 event->acceptProposedAction(); |
|
90 } |
|
91 } else { |
|
92 event->ignore(); |
|
93 } |
|
94 } |
|
95 |
|
96 void DragWidget::dropEvent(QDropEvent *event) |
|
97 { |
|
98 if (event->mimeData()->hasText()) { |
|
99 const QMimeData *mime = event->mimeData(); |
|
100 QStringList pieces = mime->text().split(QRegExp("\\s+"), |
|
101 QString::SkipEmptyParts); |
|
102 QPoint position = event->pos(); |
|
103 QPoint hotSpot; |
|
104 |
|
105 QList<QByteArray> hotSpotPos = mime->data("application/x-hotspot").split(' '); |
|
106 if (hotSpotPos.size() == 2) { |
|
107 hotSpot.setX(hotSpotPos.first().toInt()); |
|
108 hotSpot.setY(hotSpotPos.last().toInt()); |
|
109 } |
|
110 |
|
111 foreach (QString piece, pieces) { |
|
112 DragLabel *newLabel = new DragLabel(piece, this); |
|
113 newLabel->move(position - hotSpot); |
|
114 newLabel->show(); |
|
115 newLabel->setAttribute(Qt::WA_DeleteOnClose); |
|
116 |
|
117 position += QPoint(newLabel->width(), 0); |
|
118 } |
|
119 |
|
120 if (event->source() == this) { |
|
121 event->setDropAction(Qt::MoveAction); |
|
122 event->accept(); |
|
123 } else { |
|
124 event->acceptProposedAction(); |
|
125 } |
|
126 } else { |
|
127 event->ignore(); |
|
128 } |
|
129 foreach (QObject *child, children()) { |
|
130 if (child->inherits("QWidget")) { |
|
131 QWidget *widget = static_cast<QWidget *>(child); |
|
132 if (!widget->isVisible()) |
|
133 widget->deleteLater(); |
|
134 } |
|
135 } |
|
136 } |
|
137 |
|
138 void DragWidget::mousePressEvent(QMouseEvent *event) |
|
139 { |
|
140 QLabel *child = static_cast<QLabel*>(childAt(event->pos())); |
|
141 if (!child) |
|
142 return; |
|
143 |
|
144 QPoint hotSpot = event->pos() - child->pos(); |
|
145 |
|
146 QMimeData *mimeData = new QMimeData; |
|
147 mimeData->setText(child->text()); |
|
148 mimeData->setData("application/x-hotspot", |
|
149 QByteArray::number(hotSpot.x()) |
|
150 + " " + QByteArray::number(hotSpot.y())); |
|
151 |
|
152 QPixmap pixmap(child->size()); |
|
153 child->render(&pixmap); |
|
154 |
|
155 QDrag *drag = new QDrag(this); |
|
156 drag->setMimeData(mimeData); |
|
157 drag->setPixmap(pixmap); |
|
158 drag->setHotSpot(hotSpot); |
|
159 |
|
160 Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction); |
|
161 |
|
162 if (dropAction == Qt::MoveAction) |
|
163 child->close(); |
|
164 } |