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

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 4.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/****************************************************************************
43**
44** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
45** Contact: Qt Software Information ([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
56DragDropModel::DragDropModel(const QStringList &strings, QObject *parent)
57 : TreeModel(strings, parent)
58{
59}
60
61bool 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
109Qt::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
119QMimeData *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 (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
137QStringList DragDropModel::mimeTypes() const
138{
139 QStringList types;
140 types << "text/plain";
141 return types;
142}
143
144Qt::DropActions DragDropModel::supportedDropActions() const
145{
146 return Qt::CopyAction | Qt::MoveAction;
147}
Note: See TracBrowser for help on using the repository browser.