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 | /****************************************************************************
|
---|
42 | **
|
---|
43 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
44 | ** All rights reserved.
|
---|
45 | ** Contact: Nokia Corporation ([email protected])
|
---|
46 | **
|
---|
47 | ** This file is part of an example program for Qt.
|
---|
48 | ** EDITIONS: NOLIMITS
|
---|
49 | **
|
---|
50 | ****************************************************************************/
|
---|
51 |
|
---|
52 | #include <QtGui>
|
---|
53 |
|
---|
54 | #include "dragdropmodel.h"
|
---|
55 |
|
---|
56 | DragDropModel::DragDropModel(const QStringList &strings, QObject *parent)
|
---|
57 | : TreeModel(strings, parent)
|
---|
58 | {
|
---|
59 | }
|
---|
60 |
|
---|
61 | bool DragDropModel::dropMimeData(const QMimeData *data,
|
---|
62 | Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
---|
63 | {
|
---|
64 | if (action == Qt::IgnoreAction)
|
---|
65 | return true;
|
---|
66 |
|
---|
67 | if (!data->hasFormat("text/plain"))
|
---|
68 | return false;
|
---|
69 |
|
---|
70 | int beginRow;
|
---|
71 |
|
---|
72 | if (row != -1)
|
---|
73 | beginRow = row;
|
---|
74 | else if (parent.isValid())
|
---|
75 | beginRow = 0;
|
---|
76 | else
|
---|
77 | beginRow = rowCount(QModelIndex());
|
---|
78 |
|
---|
79 | QByteArray encodedData = data->data("text/plain");
|
---|
80 | QDataStream stream(&encodedData, QIODevice::ReadOnly);
|
---|
81 | QHash<qint64, QMap<int,QHash<int,QString> > > newItems;
|
---|
82 |
|
---|
83 | while (!stream.atEnd()) {
|
---|
84 | qint64 id;
|
---|
85 | int row;
|
---|
86 | int column;
|
---|
87 | QString text;
|
---|
88 | stream >> id >> row >> column >> text;
|
---|
89 | newItems[id][row][column] = text;
|
---|
90 | }
|
---|
91 | int rows = newItems.count();
|
---|
92 |
|
---|
93 | insertRows(beginRow, rows, parent);
|
---|
94 | QMap<int,QHash<int,QString> > childItems;
|
---|
95 | foreach (childItems, newItems.values()) {
|
---|
96 | QHash<int,QString> rowItems;
|
---|
97 | foreach (rowItems, childItems.values()) {
|
---|
98 | foreach (int column, rowItems.keys()) {
|
---|
99 | QModelIndex idx = index(beginRow, column, parent);
|
---|
100 | setData(idx, rowItems[column]);
|
---|
101 | }
|
---|
102 | ++beginRow;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | return true;
|
---|
107 | }
|
---|
108 |
|
---|
109 | Qt::ItemFlags DragDropModel::flags(const QModelIndex &index) const
|
---|
110 | {
|
---|
111 | Qt::ItemFlags defaultFlags = TreeModel::flags(index);
|
---|
112 |
|
---|
113 | if (index.isValid())
|
---|
114 | return Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled | defaultFlags;
|
---|
115 | else
|
---|
116 | return Qt::ItemIsDropEnabled | defaultFlags;
|
---|
117 | }
|
---|
118 |
|
---|
119 | QMimeData *DragDropModel::mimeData(const QModelIndexList &indexes) const
|
---|
120 | {
|
---|
121 | QMimeData *mimeData = new QMimeData();
|
---|
122 | QByteArray encodedData;
|
---|
123 |
|
---|
124 | QDataStream stream(&encodedData, QIODevice::WriteOnly);
|
---|
125 |
|
---|
126 | foreach (const QModelIndex &index, indexes) {
|
---|
127 | if (index.isValid()) {
|
---|
128 | QString text = data(index, Qt::DisplayRole).toString();
|
---|
129 | stream << index.internalId() << index.row() << index.column() << text;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | mimeData->setData("text/plain", encodedData);
|
---|
134 | return mimeData;
|
---|
135 | }
|
---|
136 |
|
---|
137 | QStringList DragDropModel::mimeTypes() const
|
---|
138 | {
|
---|
139 | QStringList types;
|
---|
140 | types << "text/plain";
|
---|
141 | return types;
|
---|
142 | }
|
---|
143 |
|
---|
144 | Qt::DropActions DragDropModel::supportedDropActions() const
|
---|
145 | {
|
---|
146 | return Qt::CopyAction | Qt::MoveAction;
|
---|
147 | }
|
---|