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
48
49
DynamicTreeModel::DynamicTreeModel(QObject *parent)
50
  : QAbstractItemModel(parent),
51
    nextId(1)
52
{
53
}
54
55
QModelIndex DynamicTreeModel::index(int row, int column, const QModelIndex &parent) const
56
{
57
//   if (column != 0)
58
//     return QModelIndex();
59
60
61
  if ( column < 0 || row < 0 )
62
    return QModelIndex();
63
64
  QList<QList<qint64> > childIdColumns = m_childItems.value(parent.internalId());
65
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());
72
  }
73
74
  if (childIdColumns.size() == 0)
75
    return QModelIndex();
76
77
  if (column >= childIdColumns.size())
78
    return QModelIndex();
79
80
  QList<qint64> rowIds = childIdColumns.at(column);
81
82
  if ( row >= rowIds.size())
83
    return QModelIndex();
84
85
  qint64 id = rowIds.at(row);
86
87
  return createIndex(row, column, reinterpret_cast<void *>(id));
88
89
}
90
91
qint64 DynamicTreeModel::findParentId(qint64 searchId) const
92
{
93
  if (searchId <= 0)
94
    return -1;
95
96
  QHashIterator<qint64, QList<QList<qint64> > > i(m_childItems);
97
  while (i.hasNext())
98
  {
99
    i.next();
100
    QListIterator<QList<qint64> > j(i.value());
101
    while (j.hasNext())
102
    {
103
      QList<qint64> l = j.next();
104
      if (l.contains(searchId))
105
      {
106
        return i.key();
107
      }
108
    }
109
  }
110
  return -1;
111
}
112
113
QModelIndex DynamicTreeModel::parent(const QModelIndex &index) const
114
{
115
  if (!index.isValid())
116
    return QModelIndex();
117
118
  qint64 searchId = index.internalId();
119
  qint64 parentId = findParentId(searchId);
120
  // Will never happen for valid index, but what the hey...
121
  if (parentId <= 0)
122
    return QModelIndex();
123
124
  qint64 grandParentId = findParentId(parentId);
125
  if (grandParentId < 0)
126
    grandParentId = 0;
127
128
  int column = 0;
129
  QList<qint64> childList = m_childItems.value(grandParentId).at(column);
130
131
  int row = childList.indexOf(parentId);
132
133
  return createIndex(row, column, reinterpret_cast<void *>(parentId));
134
135
}
136
137
int DynamicTreeModel::rowCount(const QModelIndex &index ) const
138
{
139
  QList<QList<qint64> > cols = m_childItems.value(index.internalId());
140
141
  if (cols.size() == 0 )
142
    return 0;
143
144
  if (index.column() > 0)
145
    return 0;
146
147
  return cols.at(0).size();
148
}
149
150
int DynamicTreeModel::columnCount(const QModelIndex &index ) const
151
{
152
//   Q_UNUSED(index);
153
  return m_childItems.value(index.internalId()).size();
154
}
155
156
QVariant DynamicTreeModel::data(const QModelIndex &index, int role) const
157
{
158
  if (!index.isValid())
159
    return QVariant();
160
161
  if (Qt::DisplayRole == role)
162
  {
163
    return m_items.value(index.internalId());
164
  }
165
  return QVariant();
166
}
167
168
void DynamicTreeModel::clear()
169
{
170
  beginResetModel();
171
  m_items.clear();
172
  m_childItems.clear();
173
  nextId = 1;
174
  endResetModel();
175
}
176
177
178
ModelChangeCommand::ModelChangeCommand( DynamicTreeModel *model, QObject *parent )
179
    : QObject(parent), m_model(model), m_numCols(1), m_startRow(-1), m_endRow(-1)
180
{
181
182
}
183
184
QModelIndex ModelChangeCommand::findIndex(QList<int> rows)
185
{
186
  const int col = 0;
187
  QModelIndex parent = QModelIndex();
188
  QListIterator<int> i(rows);
189
  while (i.hasNext())
190
  {
191
    parent = m_model->index(i.next(), col, parent);
192
    Q_ASSERT(parent.isValid());
193
  }
194
  return parent;
195
}
196
197
ModelInsertCommand::ModelInsertCommand(DynamicTreeModel *model, QObject *parent )
198
    : ModelChangeCommand(model, parent)
199
{
200
201
}
202
203
void ModelInsertCommand::doCommand()
204
{
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++)
209
  {
210
    for(int col = 0; col < m_numCols; col++ )
211
    {
212
      if (m_model->m_childItems[parentId].size() <= col)
213
      {
214
        m_model->m_childItems[parentId].append(QList<qint64>());
215
      }
216
//       QString name = QUuid::createUuid().toString();
217
      qint64 id = m_model->newId();
218
      QString name = QString::number(id);
219
220
      m_model->m_items.insert(id, name);
221
      m_model->m_childItems[parentId][col].insert(row, id);
222
223
    }
224
  }
225
  m_model->endInsertRows();
226
}
227
228
229
ModelMoveCommand::ModelMoveCommand(DynamicTreeModel *model, QObject *parent)
230
  : ModelChangeCommand(model, parent)
231
{
232
233
}
234
bool ModelMoveCommand::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow)
235
{
236
  return m_model->beginMoveRows(srcParent, srcStart, srcEnd, destParent, destRow);
237
}
238
239
void ModelMoveCommand::doCommand()
240
{
241
    QModelIndex srcParent = findIndex(m_rowNumbers);
242
    QModelIndex destParent = findIndex(m_destRowNumbers);
243
244
    if (!emitPreSignal(srcParent, m_startRow, m_endRow, destParent, m_destRow))
245
    {
246
        return;
247
    }
248
249
    for (int column = 0; column < m_numCols; ++column)
250
    {
251
        QList<qint64> l = m_model->m_childItems.value(srcParent.internalId())[column].mid(m_startRow, m_endRow - m_startRow + 1 );
252
253
        for (int i = m_startRow; i <= m_endRow ; i++)
254
        {
255
            m_model->m_childItems[srcParent.internalId()][column].removeAt(m_startRow);
256
        }
257
        int d;
258
        if (m_destRow < m_startRow)
259
            d = m_destRow;
260
        else
261
        {
262
            if (srcParent == destParent)
263
                d = m_destRow - (m_endRow - m_startRow + 1);
264
            else
265
                d = m_destRow - (m_endRow - m_startRow) + 1;
266
        }
267
268
        foreach(const qint64 id, l)
269
        {
270
            m_model->m_childItems[destParent.internalId()][column].insert(d++, id);
271
        }
272
    }
273
274
    emitPostSignal();
275
}
276
277
void ModelMoveCommand::emitPostSignal()
278
{
279
    m_model->endMoveRows();
280
}
281
282
ModelResetCommand::ModelResetCommand(DynamicTreeModel* model, QObject* parent)
283
  : ModelMoveCommand(model, parent)
284
{
285
286
}
287
288
ModelResetCommand::~ModelResetCommand()
289
{
290
291
}
292
293
bool ModelResetCommand::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow)
294
{
295
    Q_UNUSED(srcParent);
296
    Q_UNUSED(srcStart);
297
    Q_UNUSED(srcEnd);
298
    Q_UNUSED(destParent);
299
    Q_UNUSED(destRow);
300
301
    return true;
302
}
303
304
void ModelResetCommand::emitPostSignal()
305
{
306
    m_model->reset();
307
}
308
309
ModelResetCommandFixed::ModelResetCommandFixed(DynamicTreeModel* model, QObject* parent)
310
  : ModelMoveCommand(model, parent)
311
{
312
313
}
314
315
ModelResetCommandFixed::~ModelResetCommandFixed()
316
{
317
318
}
319
320
bool ModelResetCommandFixed::emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow)
321
{
322
    Q_UNUSED(srcParent);
323
    Q_UNUSED(srcStart);
324
    Q_UNUSED(srcEnd);
325
    Q_UNUSED(destParent);
326
    Q_UNUSED(destRow);
327
328
    m_model->beginResetModel();
329
    return true;
330
}
331
332
void ModelResetCommandFixed::emitPostSignal()
333
{
334
    m_model->endResetModel();
335
}