| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 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 qt-sales@nokia.com. | 
|---|
| 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 | } | 
|---|