source: trunk/doc/src/snippets/qtreeview-dnd/dragdropmodel.cpp@ 624

Last change on this file since 624 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

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