1
/****************************************************************************
3
** Copyright (C) 2009 Stephen Kelly <steveire@gmail.com>
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
7
** This file is part of the test suite of the Qt Toolkit.
9
** $QT_BEGIN_LICENSE:LGPL$
10
** No Commercial Usage
11
** This file contains pre-release code and may not be distributed.
12
** You may use this file in accordance with the terms and conditions
13
** contained in the Technology Preview License Agreement accompanying
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.
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.
28
** If you have questions regarding the use of this file, please contact
29
** Nokia at qt-info@nokia.com.
40
****************************************************************************/
42
#include "dynamictreemodel.h"
44
#include <QtCore/QHash>
45
#include <QtCore/QList>
46
#include <QtCore/QTimer>
49
DynamicTreeModel::DynamicTreeModel(QObject *parent)
50
: QAbstractItemModel(parent),
55
QModelIndex DynamicTreeModel::index(int row, int column, const QModelIndex &parent) const
58
// return QModelIndex();
61
if ( column < 0 || row < 0 )
64
QList<QList<qint64> > childIdColumns = m_childItems.value(parent.internalId());
66
const qint64 grandParent = findParentId(parent.internalId());
67
if (grandParent >= 0) {
68
QList<QList<qint64> > parentTable = m_childItems.value(grandParent);
69
Q_ASSERT(parent.column() < parentTable.size());
70
QList<qint64> parentSiblings = parentTable.at(parent.column());
71
Q_ASSERT(parent.row() < parentSiblings.size());
74
if (childIdColumns.size() == 0)
77
if (column >= childIdColumns.size())
80
QList<qint64> rowIds = childIdColumns.at(column);
82
if ( row >= rowIds.size())
85
qint64 id = rowIds.at(row);
87
return createIndex(row, column, reinterpret_cast<void *>(id));
91
qint64 DynamicTreeModel::findParentId(qint64 searchId) const
96
QHashIterator<qint64, QList<QList<qint64> > > i(m_childItems);
100
QListIterator<QList<qint64> > j(i.value());
103
QList<qint64> l = j.next();
104
if (l.contains(searchId))
113
QModelIndex DynamicTreeModel::parent(const QModelIndex &index) const
115
if (!index.isValid())
116
return QModelIndex();
118
qint64 searchId = index.internalId();
119
qint64 parentId = findParentId(searchId);
120
// Will never happen for valid index, but what the hey...
122
return QModelIndex();
124
qint64 grandParentId = findParentId(parentId);
125
if (grandParentId < 0)
129
QList<qint64> childList = m_childItems.value(grandParentId).at(column);
131
int row = childList.indexOf(parentId);
133
return createIndex(row, column, reinterpret_cast<void *>(parentId));
137
int DynamicTreeModel::rowCount(const QModelIndex &index ) const
139
QList<QList<qint64> > cols = m_childItems.value(index.internalId());
141
if (cols.size() == 0 )
144
if (index.column() > 0)
147
return cols.at(0).size();
150
int DynamicTreeModel::columnCount(const QModelIndex &index ) const
153
return m_childItems.value(index.internalId()).size();
156
QVariant DynamicTreeModel::data(const QModelIndex &index, int role) const
158
if (!index.isValid())
161
if (Qt::DisplayRole == role)
163
return m_items.value(index.internalId());
168
void DynamicTreeModel::clear()
172
m_childItems.clear();
178
ModelChangeCommand::ModelChangeCommand( DynamicTreeModel *model, QObject *parent )
179
: QObject(parent), m_model(model), m_numCols(1), m_startRow(-1), m_endRow(-1)
184
QModelIndex ModelChangeCommand::findIndex(QList<int> rows)
187
QModelIndex parent = QModelIndex();
188
QListIterator<int> i(rows);
191
parent = m_model->index(i.next(), col, parent);
192
Q_ASSERT(parent.isValid());
197
ModelInsertCommand::ModelInsertCommand(DynamicTreeModel *model, QObject *parent )
198
: ModelChangeCommand(model, parent)
203
void ModelInsertCommand::doCommand()
205
QModelIndex parent = findIndex(m_rowNumbers);
206
m_model->beginInsertRows(parent, m_startRow, m_endRow);
207
qint64 parentId = parent.internalId();
208
for (int row = m_startRow; row <= m_endRow; row++)
210
for(int col = 0; col < m_numCols; col++ )
212
if (m_model->m_childItems[parentId].size() <= col)
214
m_model->m_childItems[parentId].append(QList<qint64>());
216
// QString name = QUuid::createUuid().toString();
217
qint64 id = m_model->newId();
218
QString name = QString::number(id);
220
m_model->m_items.insert(id, name);
221
m_model->m_childItems[parentId][col].insert(row, id);
225
m_model->endInsertRows();
229
ModelMoveCommand::ModelMoveCommand(DynamicTreeModel *model, QObject *parent)
230
: ModelChangeCommand(model, parent)
234
bool ModelMoveCommand::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow)
236
return m_model->beginMoveRows(srcParent, srcStart, srcEnd, destParent, destRow);
239
void ModelMoveCommand::doCommand()
241
QModelIndex srcParent = findIndex(m_rowNumbers);
242
QModelIndex destParent = findIndex(m_destRowNumbers);
244
if (!emitPreSignal(srcParent, m_startRow, m_endRow, destParent, m_destRow))
249
for (int column = 0; column < m_numCols; ++column)
251
QList<qint64> l = m_model->m_childItems.value(srcParent.internalId())[column].mid(m_startRow, m_endRow - m_startRow + 1 );
253
for (int i = m_startRow; i <= m_endRow ; i++)
255
m_model->m_childItems[srcParent.internalId()][column].removeAt(m_startRow);
258
if (m_destRow < m_startRow)
262
if (srcParent == destParent)
263
d = m_destRow - (m_endRow - m_startRow + 1);
265
d = m_destRow - (m_endRow - m_startRow) + 1;
268
foreach(const qint64 id, l)
270
m_model->m_childItems[destParent.internalId()][column].insert(d++, id);
277
void ModelMoveCommand::emitPostSignal()
279
m_model->endMoveRows();
282
ModelResetCommand::ModelResetCommand(DynamicTreeModel* model, QObject* parent)
283
: ModelMoveCommand(model, parent)
288
ModelResetCommand::~ModelResetCommand()
293
bool ModelResetCommand::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow)
298
Q_UNUSED(destParent);
304
void ModelResetCommand::emitPostSignal()
309
ModelResetCommandFixed::ModelResetCommandFixed(DynamicTreeModel* model, QObject* parent)
310
: ModelMoveCommand(model, parent)
315
ModelResetCommandFixed::~ModelResetCommandFixed()
320
bool ModelResetCommandFixed::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow)
325
Q_UNUSED(destParent);
328
m_model->beginResetModel();
332
void ModelResetCommandFixed::emitPostSignal()
334
m_model->endResetModel();