1
/****************************************************************************
2
**
3
** Copyright (C) 2009 Stephen Kelly <steveire@gmail.com>
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the test suite of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#include "dynamictreemodel.h"
43
44
#include <QtCore/QHash>
45
#include <QtCore/QList>
46
#include <QtCore/QTimer>
47
#include <QtCore/QDebug>
48
49
50
DynamicTreeModel::DynamicTreeModel(QObject *parent)
51
  : QAbstractItemModel(parent),
52
    nextId(1)
53
{
54
}
55
56
QModelIndex DynamicTreeModel::index(int row, int column, const QModelIndex &parent) const
57
{
58
//   if (column != 0)
59
//     return QModelIndex();
60
61
62
  if ( column < 0 || row < 0 )
63
    return QModelIndex();
64
65
  QList<QList<qint64> > childIdColumns = m_childItems.value(parent.internalId());
66
67
  const qint64 grandParent = findParentId(parent.internalId());
68
  if (grandParent >= 0) {
69
    QList<QList<qint64> > parentTable = m_childItems.value(grandParent);
70
    if (parent.column() >= parentTable.size())
71
        qFatal("%s: parent.column() must be less than parentTable.size()", Q_FUNC_INFO);
72
    QList<qint64> parentSiblings = parentTable.at(parent.column());
73
    if (parent.row() >= parentSiblings.size())
74
        qFatal("%s: parent.row() must be less than parentSiblings.size()", Q_FUNC_INFO);
75
  }
76
77
  if (childIdColumns.size() == 0)
78
    return QModelIndex();
79
80
  if (column >= childIdColumns.size())
81
    return QModelIndex();
82
83
  QList<qint64> rowIds = childIdColumns.at(column);
84
85
  if ( row >= rowIds.size())
86
    return QModelIndex();
87
88
  qint64 id = rowIds.at(row);
89
90
  return createIndex(row, column, reinterpret_cast<void *>(id));
91
92
}
93
94
qint64 DynamicTreeModel::findParentId(qint64 searchId) const
95
{
96
  if (searchId <= 0)
97
    return -1;
98
99
  QHashIterator<qint64, QList<QList<qint64> > > i(m_childItems);
100
  while (i.hasNext())
101
  {
102
    i.next();
103
    QListIterator<QList<qint64> > j(i.value());
104
    while (j.hasNext())
105
    {
106
      QList<qint64> l = j.next();
107
      if (l.contains(searchId))
108
      {
109
        return i.key();
110
      }
111
    }
112
  }
113
  return -1;
114
}
115
116
QModelIndex DynamicTreeModel::parent(const QModelIndex &index) const
117
{
118
  if (!index.isValid())
119
    return QModelIndex();
120
121
  qint64 searchId = index.internalId();
122
  qint64 parentId = findParentId(searchId);
123
  // Will never happen for valid index, but what the hey...
124
  if (parentId <= 0)
125
    return QModelIndex();
126
127
  qint64 grandParentId = findParentId(parentId);
128
  if (grandParentId < 0)
129
    grandParentId = 0;
130
131
  int column = 0;
132
  QList<qint64> childList = m_childItems.value(grandParentId).at(column);
133
134
  int row = childList.indexOf(parentId);
135
136
  return createIndex(row, column, reinterpret_cast<void *>(parentId));
137
138
}
139
140
int DynamicTreeModel::rowCount(const QModelIndex &index ) const
141
{
142
  QList<QList<qint64> > cols = m_childItems.value(index.internalId());
143
144
  if (cols.size() == 0 )
145
    return 0;
146
147
  if (index.column() > 0)
148
    return 0;
149
150
  return cols.at(0).size();
151
}
152
153
int DynamicTreeModel::columnCount(const QModelIndex &index ) const
154
{
155
//   Q_UNUSED(index);
156
  return m_childItems.value(index.internalId()).size();
157
}
158
159
QVariant DynamicTreeModel::data(const QModelIndex &index, int role) const
160
{
161
  if (!index.isValid())
162
    return QVariant();
163
164
  if (Qt::DisplayRole == role)
165
  {
166
    return m_items.value(index.internalId());
167
  }
168
  return QVariant();
169
}
170
171
void DynamicTreeModel::clear()
172
{
173
  beginResetModel();
174
  m_items.clear();
175
  m_childItems.clear();
176
  nextId = 1;
177
  endResetModel();
178
}
179
180
181
ModelChangeCommand::ModelChangeCommand( DynamicTreeModel *model, QObject *parent )
182
    : QObject(parent), m_model(model), m_numCols(1), m_startRow(-1), m_endRow(-1)
183
{
184
185
}
186
187
QModelIndex ModelChangeCommand::findIndex(QList<int> rows)
188
{
189
  const int col = 0;
190
  QModelIndex parent = QModelIndex();
191
  QListIterator<int> i(rows);
192
  while (i.hasNext())
193
  {
194
    parent = m_model->index(i.next(), col, parent);
195
    if (!parent.isValid())
196
        qFatal("%s: parent must be valid", Q_FUNC_INFO);
197
  }
198
  return parent;
199
}
200
201
ModelInsertCommand::ModelInsertCommand(DynamicTreeModel *model, QObject *parent )
202
    : ModelChangeCommand(model, parent)
203
{
204
205
}
206
207
void ModelInsertCommand::doCommand()
208
{
209
  QModelIndex parent = findIndex(m_rowNumbers);
210
  m_model->beginInsertRows(parent, m_startRow, m_endRow);
211
  qint64 parentId = parent.internalId();
212
  for (int row = m_startRow; row <= m_endRow; row++)
213
  {
214
    for(int col = 0; col < m_numCols; col++ )
215
    {
216
      if (m_model->m_childItems[parentId].size() <= col)
217
      {
218
        m_model->m_childItems[parentId].append(QList<qint64>());
219
      }
220
//       QString name = QUuid::createUuid().toString();
221
      qint64 id = m_model->newId();
222
      QString name = QString::number(id);
223
224
      m_model->m_items.insert(id, name);
225
      m_model->m_childItems[parentId][col].insert(row, id);
226
227
    }
228
  }
229
  m_model->endInsertRows();
230
}
231
232
233
ModelMoveCommand::ModelMoveCommand(DynamicTreeModel *model, QObject *parent)
234
  : ModelChangeCommand(model, parent)
235
{
236
237
}
238
bool ModelMoveCommand::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow)
239
{
240
  return m_model->beginMoveRows(srcParent, srcStart, srcEnd, destParent, destRow);
241
}
242
243
void ModelMoveCommand::doCommand()
244
{
245
    QModelIndex srcParent = findIndex(m_rowNumbers);
246
    QModelIndex destParent = findIndex(m_destRowNumbers);
247
248
    if (!emitPreSignal(srcParent, m_startRow, m_endRow, destParent, m_destRow))
249
    {
250
        return;
251
    }
252
253
    for (int column = 0; column < m_numCols; ++column)
254
    {
255
        QList<qint64> l = m_model->m_childItems.value(srcParent.internalId())[column].mid(m_startRow, m_endRow - m_startRow + 1 );
256
257
        for (int i = m_startRow; i <= m_endRow ; i++)
258
        {
259
            m_model->m_childItems[srcParent.internalId()][column].removeAt(m_startRow);
260
        }
261
        int d;
262
        if (m_destRow < m_startRow)
263
            d = m_destRow;
264
        else
265
        {
266
            if (srcParent == destParent)
267
                d = m_destRow - (m_endRow - m_startRow + 1);
268
            else
269
                d = m_destRow - (m_endRow - m_startRow) + 1;
270
        }
271
272
        foreach(const qint64 id, l)
273
        {
274
            m_model->m_childItems[destParent.internalId()][column].insert(d++, id);
275
        }
276
    }
277
278
    emitPostSignal();
279
}
280
281
void ModelMoveCommand::emitPostSignal()
282
{
283
    m_model->endMoveRows();
284
}
285
286
ModelResetCommand::ModelResetCommand(DynamicTreeModel* model, QObject* parent)
287
  : ModelMoveCommand(model, parent)
288
{
289
290
}
291
292
ModelResetCommand::~ModelResetCommand()
293
{
294
295
}
296
297
bool ModelResetCommand::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow)
298
{
299
    Q_UNUSED(srcParent);
300
    Q_UNUSED(srcStart);
301
    Q_UNUSED(srcEnd);
302
    Q_UNUSED(destParent);
303
    Q_UNUSED(destRow);
304
305
    return true;
306
}
307
308
void ModelResetCommand::emitPostSignal()
309
{
310
    m_model->reset();
311
}
312
313
ModelResetCommandFixed::ModelResetCommandFixed(DynamicTreeModel* model, QObject* parent)
314
  : ModelMoveCommand(model, parent)
315
{
316
317
}
318
319
ModelResetCommandFixed::~ModelResetCommandFixed()
320
{
321
322
}
323
324
bool ModelResetCommandFixed::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow)
325
{
326
    Q_UNUSED(srcParent);
327
    Q_UNUSED(srcStart);
328
    Q_UNUSED(srcEnd);
329
    Q_UNUSED(destParent);
330
    Q_UNUSED(destRow);
331
332
    m_model->beginResetModel();
333
    return true;
334
}
335
336
void ModelResetCommandFixed::emitPostSignal()
337
{
338
    m_model->endResetModel();
339
}