1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | #include "dragwidget.h"
|
---|
44 |
|
---|
45 | DragWidget::DragWidget(QWidget *parent)
|
---|
46 | : QFrame(parent)
|
---|
47 | {
|
---|
48 | setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
|
---|
49 | dragDropLabel = new QLabel("", this);
|
---|
50 | dragDropLabel->setAlignment(Qt::AlignHCenter);
|
---|
51 |
|
---|
52 | QHBoxLayout *layout = new QHBoxLayout(this);
|
---|
53 | layout->addStretch(0);
|
---|
54 | layout->addWidget(dragDropLabel);
|
---|
55 | layout->addStretch(0);
|
---|
56 |
|
---|
57 | setAcceptDrops(true);
|
---|
58 | }
|
---|
59 |
|
---|
60 | // Accept all actions, but deal with them separately later.
|
---|
61 | //! [0]
|
---|
62 | void DragWidget::dragEnterEvent(QDragEnterEvent *event)
|
---|
63 | {
|
---|
64 | event->acceptProposedAction();
|
---|
65 | }
|
---|
66 | //! [0]
|
---|
67 |
|
---|
68 | //! [1]
|
---|
69 | void DragWidget::dropEvent(QDropEvent *event)
|
---|
70 | {
|
---|
71 | if (event->source() == this && event->possibleActions() & Qt::MoveAction)
|
---|
72 | return;
|
---|
73 | //! [1]
|
---|
74 |
|
---|
75 | //! [2]
|
---|
76 | if (event->proposedAction() == Qt::MoveAction) {
|
---|
77 | event->acceptProposedAction();
|
---|
78 | // Process the data from the event.
|
---|
79 | //! [2]
|
---|
80 | emit dragResult(tr("The data was moved here."));
|
---|
81 | //! [3]
|
---|
82 | } else if (event->proposedAction() == Qt::CopyAction) {
|
---|
83 | event->acceptProposedAction();
|
---|
84 | // Process the data from the event.
|
---|
85 | //! [3]
|
---|
86 | emit dragResult(tr("The data was copied here."));
|
---|
87 | //! [4]
|
---|
88 | } else {
|
---|
89 | // Ignore the drop.
|
---|
90 | return;
|
---|
91 | }
|
---|
92 | //! [4]
|
---|
93 | // End of quote
|
---|
94 |
|
---|
95 | emit mimeTypes(event->mimeData()->formats());
|
---|
96 | setData(event->mimeData()->formats()[0],
|
---|
97 | event->mimeData()->data(event->mimeData()->formats()[0]));
|
---|
98 | //! [5]
|
---|
99 | }
|
---|
100 | //! [5]
|
---|
101 |
|
---|
102 | //! [6]
|
---|
103 | void DragWidget::mousePressEvent(QMouseEvent *event)
|
---|
104 | {
|
---|
105 | if (event->button() == Qt::LeftButton)
|
---|
106 | dragStartPosition = event->pos();
|
---|
107 | }
|
---|
108 | //! [6]
|
---|
109 |
|
---|
110 | //! [7]
|
---|
111 | void DragWidget::mouseMoveEvent(QMouseEvent *event)
|
---|
112 | {
|
---|
113 | if (!(event->buttons() & Qt::LeftButton))
|
---|
114 | return;
|
---|
115 | if ((event->pos() - dragStartPosition).manhattanLength()
|
---|
116 | < QApplication::startDragDistance())
|
---|
117 | return;
|
---|
118 |
|
---|
119 | QDrag *drag = new QDrag(this);
|
---|
120 | QMimeData *mimeData = new QMimeData;
|
---|
121 |
|
---|
122 | mimeData->setData(mimeType, data);
|
---|
123 | drag->setMimeData(mimeData);
|
---|
124 |
|
---|
125 | Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);
|
---|
126 | //! [7]
|
---|
127 |
|
---|
128 | switch (dropAction) {
|
---|
129 | case Qt::CopyAction:
|
---|
130 | emit dragResult(tr("The text was copied."));
|
---|
131 | break;
|
---|
132 | case Qt::MoveAction:
|
---|
133 | emit dragResult(tr("The text was moved."));
|
---|
134 | break;
|
---|
135 | default:
|
---|
136 | emit dragResult(tr("Unknown action."));
|
---|
137 | break;
|
---|
138 | }
|
---|
139 | //! [8]
|
---|
140 | }
|
---|
141 | //! [8]
|
---|
142 |
|
---|
143 | void DragWidget::setData(const QString &mimetype, const QByteArray &newData)
|
---|
144 | {
|
---|
145 | mimeType = mimetype;
|
---|
146 | data = QByteArray(newData);
|
---|
147 |
|
---|
148 | dragDropLabel->setText(tr("%1 bytes").arg(data.size()));
|
---|
149 |
|
---|
150 | QStringList formats;
|
---|
151 | formats << mimetype;
|
---|
152 | emit mimeTypes(formats);
|
---|
153 | }
|
---|