1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 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:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
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 | ** 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 have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui>
|
---|
43 |
|
---|
44 | #include "dragwidget.h"
|
---|
45 |
|
---|
46 | DragWidget::DragWidget(QWidget *parent)
|
---|
47 | : QFrame(parent)
|
---|
48 | {
|
---|
49 | setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
|
---|
50 | dragDropLabel = new QLabel("", this);
|
---|
51 | dragDropLabel->setAlignment(Qt::AlignHCenter);
|
---|
52 |
|
---|
53 | QHBoxLayout *layout = new QHBoxLayout(this);
|
---|
54 | layout->addStretch(0);
|
---|
55 | layout->addWidget(dragDropLabel);
|
---|
56 | layout->addStretch(0);
|
---|
57 |
|
---|
58 | setAcceptDrops(true);
|
---|
59 | }
|
---|
60 |
|
---|
61 | // Accept all actions, but deal with them separately later.
|
---|
62 | //! [0]
|
---|
63 | void DragWidget::dragEnterEvent(QDragEnterEvent *event)
|
---|
64 | {
|
---|
65 | event->acceptProposedAction();
|
---|
66 | }
|
---|
67 | //! [0]
|
---|
68 |
|
---|
69 | //! [1]
|
---|
70 | void DragWidget::dropEvent(QDropEvent *event)
|
---|
71 | {
|
---|
72 | if (event->source() == this && event->possibleActions() & Qt::MoveAction)
|
---|
73 | return;
|
---|
74 | //! [1]
|
---|
75 |
|
---|
76 | //! [2]
|
---|
77 | if (event->proposedAction() == Qt::MoveAction) {
|
---|
78 | event->acceptProposedAction();
|
---|
79 | // Process the data from the event.
|
---|
80 | //! [2]
|
---|
81 | emit dragResult(tr("The data was moved here."));
|
---|
82 | //! [3]
|
---|
83 | } else if (event->proposedAction() == Qt::CopyAction) {
|
---|
84 | event->acceptProposedAction();
|
---|
85 | // Process the data from the event.
|
---|
86 | //! [3]
|
---|
87 | emit dragResult(tr("The data was copied here."));
|
---|
88 | //! [4]
|
---|
89 | } else {
|
---|
90 | // Ignore the drop.
|
---|
91 | return;
|
---|
92 | }
|
---|
93 | //! [4]
|
---|
94 | // End of quote
|
---|
95 |
|
---|
96 | emit mimeTypes(event->mimeData()->formats());
|
---|
97 | setData(event->mimeData()->formats()[0],
|
---|
98 | event->mimeData()->data(event->mimeData()->formats()[0]));
|
---|
99 | //! [5]
|
---|
100 | }
|
---|
101 | //! [5]
|
---|
102 |
|
---|
103 | //! [6]
|
---|
104 | void DragWidget::mousePressEvent(QMouseEvent *event)
|
---|
105 | {
|
---|
106 | if (event->button() == Qt::LeftButton)
|
---|
107 | dragStartPosition = event->pos();
|
---|
108 | }
|
---|
109 | //! [6]
|
---|
110 |
|
---|
111 | //! [7]
|
---|
112 | void DragWidget::mouseMoveEvent(QMouseEvent *event)
|
---|
113 | {
|
---|
114 | if (!(event->buttons() & Qt::LeftButton))
|
---|
115 | return;
|
---|
116 | if ((event->pos() - dragStartPosition).manhattanLength()
|
---|
117 | < QApplication::startDragDistance())
|
---|
118 | return;
|
---|
119 |
|
---|
120 | QDrag *drag = new QDrag(this);
|
---|
121 | QMimeData *mimeData = new QMimeData;
|
---|
122 |
|
---|
123 | mimeData->setData(mimeType, data);
|
---|
124 | drag->setMimeData(mimeData);
|
---|
125 |
|
---|
126 | Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);
|
---|
127 | //! [7]
|
---|
128 |
|
---|
129 | switch (dropAction) {
|
---|
130 | case Qt::CopyAction:
|
---|
131 | emit dragResult(tr("The text was copied."));
|
---|
132 | break;
|
---|
133 | case Qt::MoveAction:
|
---|
134 | emit dragResult(tr("The text was moved."));
|
---|
135 | break;
|
---|
136 | default:
|
---|
137 | emit dragResult(tr("Unknown action."));
|
---|
138 | break;
|
---|
139 | }
|
---|
140 | //! [8]
|
---|
141 | }
|
---|
142 | //! [8]
|
---|
143 |
|
---|
144 | void DragWidget::setData(const QString &mimetype, const QByteArray &newData)
|
---|
145 | {
|
---|
146 | mimeType = mimetype;
|
---|
147 | data = QByteArray(newData);
|
---|
148 |
|
---|
149 | dragDropLabel->setText(tr("%1 bytes").arg(data.size()));
|
---|
150 |
|
---|
151 | QStringList formats;
|
---|
152 | formats << mimetype;
|
---|
153 | emit mimeTypes(formats);
|
---|
154 | }
|
---|