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