~ubuntu-branches/ubuntu/karmic/kdepim/karmic-backports

« back to all changes in this revision

Viewing changes to akonadi/akonadi_next/tests/dynamictreemodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Mangold
  • Date: 2009-07-10 06:34:50 UTC
  • mfrom: (1.1.40 upstream)
  • Revision ID: james.westby@ubuntu.com-20090710063450-neojgew2fh0n3y0u
Tags: 4:4.2.96-0ubuntu1
* New upstream release
* Bump kde build-deps to 4.2.96

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    Copyright (c) 2009 Stephen Kelly <steveire@gmail.com>
3
 
 
4
 
    This library is free software; you can redistribute it and/or modify it
5
 
    under the terms of the GNU Library General Public License as published by
6
 
    the Free Software Foundation; either version 2 of the License, or (at your
7
 
    option) any later version.
8
 
 
9
 
    This library is distributed in the hope that it will be useful, but WITHOUT
10
 
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
12
 
    License for more details.
13
 
 
14
 
    You should have received a copy of the GNU Library General Public License
15
 
    along with this library; see the file COPYING.LIB.  If not, write to the
16
 
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17
 
    02110-1301, USA.
18
 
*/
19
 
 
20
 
#include "dynamictreemodel.h"
21
 
 
22
 
#include <QHash>
23
 
#include <QList>
24
 
#include <QTimer>
25
 
 
26
 
#include <QDebug>
27
 
 
28
 
#include <kdebug.h>
29
 
 
30
 
DynamicTreeModel::DynamicTreeModel(QObject *parent)
31
 
  : AbstractItemModel(parent),
32
 
    nextId(1)
33
 
{
34
 
}
35
 
 
36
 
QModelIndex DynamicTreeModel::index(int row, int column, const QModelIndex &parent) const
37
 
{
38
 
//   if (column != 0)
39
 
//     return QModelIndex();
40
 
 
41
 
 
42
 
  if ( column < 0 || row < 0 )
43
 
    return QModelIndex();
44
 
 
45
 
  QList<QList<qint64> > childIdColumns = m_childItems.value(parent.internalId());
46
 
 
47
 
 
48
 
  if (childIdColumns.size() == 0)
49
 
    return QModelIndex();
50
 
 
51
 
  if (column >= childIdColumns.size())
52
 
    return QModelIndex();
53
 
 
54
 
  QList<qint64> rowIds = childIdColumns.at(column);
55
 
 
56
 
  if ( row >= rowIds.size())
57
 
    return QModelIndex();
58
 
 
59
 
  qint64 id = rowIds.at(row);
60
 
 
61
 
  return createIndex(row, column, reinterpret_cast<void *>(id));
62
 
 
63
 
}
64
 
 
65
 
qint64 DynamicTreeModel::findParentId(qint64 searchId) const
66
 
{
67
 
  if (searchId <= 0)
68
 
    return -1;
69
 
 
70
 
  QHashIterator<qint64, QList<QList<qint64> > > i(m_childItems);
71
 
  while (i.hasNext())
72
 
  {
73
 
    i.next();
74
 
    QListIterator<QList<qint64> > j(i.value());
75
 
    while (j.hasNext())
76
 
    {
77
 
      QList<qint64> l = j.next();
78
 
      if (l.contains(searchId))
79
 
      {
80
 
        return i.key();
81
 
      }
82
 
    }
83
 
  }
84
 
  return -1;
85
 
}
86
 
 
87
 
QModelIndex DynamicTreeModel::parent(const QModelIndex &index) const
88
 
{
89
 
  if (!index.isValid())
90
 
    return QModelIndex();
91
 
 
92
 
  qint64 searchId = index.internalId();
93
 
  qint64 parentId = findParentId(searchId);
94
 
  // Will never happen for valid index, but what the hey...
95
 
  if (parentId <= 0)
96
 
    return QModelIndex();
97
 
 
98
 
  qint64 grandParentId = findParentId(parentId);
99
 
  if (grandParentId < 0)
100
 
    grandParentId = 0;
101
 
 
102
 
  int column = 0;
103
 
  QList<qint64> childList = m_childItems.value(grandParentId).at(column);
104
 
 
105
 
  int row = childList.indexOf(parentId);
106
 
 
107
 
  return createIndex(row, column, reinterpret_cast<void *>(parentId));
108
 
 
109
 
}
110
 
 
111
 
int DynamicTreeModel::rowCount(const QModelIndex &index ) const
112
 
{
113
 
  QList<QList<qint64> > cols = m_childItems.value(index.internalId());
114
 
 
115
 
  if (cols.size() == 0 )
116
 
    return 0;
117
 
 
118
 
  if (index.column() > 0)
119
 
    return 0;
120
 
 
121
 
  return cols.at(0).size();
122
 
}
123
 
 
124
 
int DynamicTreeModel::columnCount(const QModelIndex &index ) const
125
 
{
126
 
//   Q_UNUSED(index);
127
 
  return m_childItems.value(index.internalId()).size();
128
 
}
129
 
 
130
 
QVariant DynamicTreeModel::data(const QModelIndex &index, int role) const
131
 
{
132
 
  if (!index.isValid())
133
 
    return QVariant();
134
 
 
135
 
  if (Qt::DisplayRole == role)
136
 
  {
137
 
    return m_items.value(index.internalId());
138
 
  }
139
 
  return QVariant();
140
 
}
141
 
 
142
 
 
143
 
ModelChangeCommand::ModelChangeCommand( DynamicTreeModel *model, QObject *parent )
144
 
    : QObject(parent), m_model(model), m_numCols(1)
145
 
{
146
 
 
147
 
}
148
 
 
149
 
QModelIndex ModelChangeCommand::findIndex(QList<int> rows)
150
 
{
151
 
  const int col = 0;
152
 
  QModelIndex parent = QModelIndex();
153
 
  QListIterator<int> i(rows);
154
 
  while (i.hasNext())
155
 
  {
156
 
    parent = m_model->index(i.next(), col, parent);
157
 
    Q_ASSERT(parent.isValid());
158
 
  }
159
 
  return parent;
160
 
}
161
 
 
162
 
ModelInsertCommand::ModelInsertCommand(DynamicTreeModel *model, QObject *parent )
163
 
    : ModelChangeCommand(model, parent)
164
 
{
165
 
 
166
 
}
167
 
 
168
 
void ModelInsertCommand::doCommand()
169
 
{
170
 
  QModelIndex parent = findIndex(m_rowNumbers);
171
 
  m_model->beginInsertRows(parent, m_startRow, m_endRow);
172
 
  qint64 parentId = parent.internalId();
173
 
  for (int row = m_startRow; row <= m_endRow; row++)
174
 
  {
175
 
    for(int col = 0; col < m_numCols; col++ )
176
 
    {
177
 
      if (m_model->m_childItems[parentId].size() <= col)
178
 
      {
179
 
        m_model->m_childItems[parentId].append(QList<qint64>());
180
 
      }
181
 
//       QString name = QUuid::createUuid().toString();
182
 
      qint64 id = m_model->newId();
183
 
      QString name = QString::number(id);
184
 
 
185
 
      m_model->m_items.insert(id, name);
186
 
      m_model->m_childItems[parentId][col].insert(row, id);
187
 
 
188
 
    }
189
 
  }
190
 
  m_model->endInsertRows();
191
 
}
192
 
 
193
 
ModelInsertWithDescendantsCommand::ModelInsertWithDescendantsCommand(DynamicTreeModel *model, QObject *parent)
194
 
    : ModelInsertCommand(model, parent)
195
 
{
196
 
 
197
 
}
198
 
 
199
 
void ModelInsertWithDescendantsCommand::setNumDescendants(QList<QPair<int, int > > descs)
200
 
{
201
 
  m_descs = descs;
202
 
}
203
 
 
204
 
void ModelInsertWithDescendantsCommand::doCommand()
205
 
{
206
 
  QModelIndex idx = findIndex(m_rowNumbers);
207
 
 
208
 
  QPair<int, int> firstPair = m_descs.value(0);
209
 
  QModelIndex parent = m_model->index(firstPair.first, 0, idx);
210
 
  int row = m_model->rowCount(parent);
211
 
  m_model->beginInsertRows(parent, row, row + firstPair.second - 1);
212
 
 
213
 
  qint64 parentId = parent.internalId();
214
 
  QListIterator<QPair<int, int> > i(m_descs);
215
 
  while (i.hasNext())
216
 
  {
217
 
    QPair<int, int> pair = i.next();
218
 
    for(int col = 0; col < m_numCols; col++ )
219
 
    {
220
 
      if (m_model->m_childItems[parentId].size() <= col)
221
 
      {
222
 
        m_model->m_childItems[parentId].append(QList<qint64>());
223
 
      }
224
 
      for (int i = 0; i < pair.second; i++)
225
 
      {
226
 
        qint64 id = m_model->newId();
227
 
        QString name = QString::number(id);
228
 
 
229
 
        m_model->m_items.insert(id, name);
230
 
        m_model->m_childItems[parentId][col].append(id);
231
 
      }
232
 
    }
233
 
    parentId = m_model->m_childItems[parentId][0].at(pair.first);
234
 
  }
235
 
  m_model->endInsertRows();
236
 
}
237
 
 
238
 
 
239
 
ModelRemoveCommand::ModelRemoveCommand(DynamicTreeModel *model, QObject *parent )
240
 
    : ModelChangeCommand(model, parent)
241
 
{
242
 
 
243
 
}
244
 
 
245
 
void ModelRemoveCommand::doCommand()
246
 
{
247
 
  QModelIndex parent = findIndex(m_rowNumbers);
248
 
  m_model->beginRemoveRows(parent, m_startRow, m_endRow);
249
 
  qint64 parentId = parent.internalId();
250
 
  for(int col = 0; col < m_numCols; col++ )
251
 
  {
252
 
    QList<qint64> childItems = m_model->m_childItems.value(parentId).value(col);
253
 
    for (int row = m_startRow; row <= m_endRow; row++)
254
 
    {
255
 
      qint64 item = childItems[row];
256
 
      purgeItem(item);
257
 
      m_model->m_childItems[parentId][col].removeOne(item);
258
 
    }
259
 
  }
260
 
  m_model->endRemoveRows();
261
 
}
262
 
 
263
 
void ModelRemoveCommand::purgeItem(qint64 parent)
264
 
{
265
 
  QList<QList<qint64> > childItemRows = m_model->m_childItems.value(parent);
266
 
 
267
 
  if (childItemRows.size() > 0)
268
 
  {
269
 
    for (int col = 0; col < m_numCols; col++)
270
 
    {
271
 
      QList<qint64> childItems = childItemRows[col];
272
 
      foreach(qint64 item, childItems)
273
 
      {
274
 
        purgeItem(item);
275
 
        m_model->m_childItems[parent][col].removeOne(item);
276
 
      }
277
 
    }
278
 
  }
279
 
  m_model->m_items.remove(parent);
280
 
}
281
 
 
282
 
 
283
 
ModelDataChangeCommand::ModelDataChangeCommand(DynamicTreeModel *model, QObject *parent)
284
 
  : ModelChangeCommand(model, parent), m_startColumn(0)
285
 
{
286
 
 
287
 
}
288
 
 
289
 
void ModelDataChangeCommand::doCommand()
290
 
{
291
 
  QModelIndex parent = findIndex(m_rowNumbers);
292
 
  QModelIndex topLeft = m_model->index(m_startRow, m_startColumn, parent);
293
 
  QModelIndex bottomRight = m_model->index(m_endRow, m_numCols - 1, parent);
294
 
 
295
 
  QList<QList<qint64> > childItems = m_model->m_childItems[parent.internalId()];
296
 
 
297
 
 
298
 
  for (int col = m_startColumn; col < m_startColumn + m_numCols; col++)
299
 
  {
300
 
    for (int row = m_startRow; row < m_endRow; row++ )
301
 
    {
302
 
      QString name = QUuid::createUuid().toString();
303
 
      m_model->m_items[childItems[col][row]] = name;
304
 
    }
305
 
  }
306
 
  m_model->dataChanged(topLeft, bottomRight);
307
 
}
308
 
 
309
 
 
310
 
ModelMoveCommand::ModelMoveCommand(DynamicTreeModel *model, QObject *parent)
311
 
  : ModelChangeCommand(model, parent)
312
 
{
313
 
 
314
 
}
315
 
 
316
 
void ModelMoveCommand::doCommand()
317
 
{
318
 
  QModelIndex srcParent = findIndex(m_rowNumbers);
319
 
  QModelIndex destParent = findIndex(m_destRowNumbers);
320
 
 
321
 
  m_model->beginMoveRows(srcParent, m_startRow, m_endRow, destParent, m_destRow);
322
 
 
323
 
  QList<qint64> l = m_model->m_childItems.value(srcParent.internalId())[0].mid(m_startRow, m_endRow - m_startRow + 1 );
324
 
 
325
 
  for (int i = m_startRow; i <= m_endRow ; i++)
326
 
  {
327
 
    m_model->m_childItems[srcParent.internalId()][0].removeAt(m_startRow);
328
 
  }
329
 
  int d;
330
 
  if (m_destRow < m_startRow)
331
 
    d = m_destRow;
332
 
  else
333
 
  {
334
 
    if (srcParent == destParent)
335
 
      d = m_destRow - (m_endRow - m_startRow + 1);
336
 
    else
337
 
      d = m_destRow - (m_endRow - m_startRow) + 1;
338
 
  }
339
 
 
340
 
  foreach(const qint64 id, l)
341
 
  {
342
 
    m_model->m_childItems[destParent.internalId()][0].insert(d++, id);
343
 
  }
344
 
 
345
 
  m_model->endMoveRows();
346
 
}
347