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
#ifndef DYNAMICTREEMODEL_H
43
#define DYNAMICTREEMODEL_H
44
45
#include <QtCore/QAbstractItemModel>
46
47
#include <QtCore/QHash>
48
#include <QtCore/QList>
49
50
51
class DynamicTreeModel : public QAbstractItemModel
52
{
53
  Q_OBJECT
54
55
public:
56
  DynamicTreeModel(QObject *parent = 0);
57
58
  QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
59
  QModelIndex parent(const QModelIndex &index) const;
60
  int rowCount(const QModelIndex &index = QModelIndex()) const;
61
  int columnCount(const QModelIndex &index = QModelIndex()) const;
62
63
  QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
64
65
  void clear();
66
67
protected slots:
68
69
  /**
70
  Finds the parent id of the string with id @p searchId.
71
72
  Returns -1 if not found.
73
  */
74
  qint64 findParentId(qint64 searchId) const;
75
76
private:
77
  QHash<qint64, QString> m_items;
78
  QHash<qint64, QList<QList<qint64> > > m_childItems;
79
  qint64 nextId;
80
  qint64 newId() { return nextId++; };
81
82
  QModelIndex m_nextParentIndex;
83
  int m_nextRow;
84
85
  int m_depth;
86
  int maxDepth;
87
88
  friend class ModelInsertCommand;
89
  friend class ModelMoveCommand;
90
  friend class ModelResetCommand;
91
  friend class ModelResetCommandFixed;
92
93
};
94
95
96
class ModelChangeCommand : public QObject
97
{
98
  Q_OBJECT
99
public:
100
101
  ModelChangeCommand( DynamicTreeModel *model, QObject *parent = 0 );
102
103
  virtual ~ModelChangeCommand() {}
104
105
  void setAncestorRowNumbers(QList<int> rowNumbers) { m_rowNumbers = rowNumbers; }
106
107
  QModelIndex findIndex(QList<int> rows);
108
109
  void setStartRow(int row) { m_startRow = row; }
110
111
  void setEndRow(int row) { m_endRow = row; }
112
113
  void setNumCols(int cols) { m_numCols = cols; }
114
115
  virtual void doCommand() = 0;
116
117
protected:
118
  DynamicTreeModel* m_model;
119
  QList<int> m_rowNumbers;
120
  int m_numCols;
121
  int m_startRow;
122
  int m_endRow;
123
124
};
125
126
typedef QList<ModelChangeCommand*> ModelChangeCommandList;
127
128
class ModelInsertCommand : public ModelChangeCommand
129
{
130
  Q_OBJECT
131
132
public:
133
134
  ModelInsertCommand(DynamicTreeModel *model, QObject *parent = 0 );
135
  virtual ~ModelInsertCommand() {}
136
137
  virtual void doCommand();
138
};
139
140
141
class ModelMoveCommand : public ModelChangeCommand
142
{
143
  Q_OBJECT
144
public:
145
  ModelMoveCommand(DynamicTreeModel *model, QObject *parent);
146
147
  virtual ~ModelMoveCommand() {}
148
149
  virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow);
150
151
  virtual void doCommand();
152
153
  virtual void emitPostSignal();
154
155
  void setDestAncestors( QList<int> rows ) { m_destRowNumbers = rows; }
156
157
  void setDestRow(int row) { m_destRow = row; }
158
159
protected:
160
  QList<int> m_destRowNumbers;
161
  int m_destRow;
162
};
163
164
/**
165
  A command which does a move and emits a reset signal.
166
*/
167
class ModelResetCommand : public ModelMoveCommand
168
{
169
  Q_OBJECT
170
public:
171
  ModelResetCommand(DynamicTreeModel* model, QObject* parent = 0);
172
173
  virtual ~ModelResetCommand();
174
175
  virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow);
176
  virtual void emitPostSignal();
177
178
};
179
180
/**
181
  A command which does a move and emits a beginResetModel and endResetModel signals.
182
*/
183
class ModelResetCommandFixed : public ModelMoveCommand
184
{
185
  Q_OBJECT
186
public:
187
  ModelResetCommandFixed(DynamicTreeModel* model, QObject* parent = 0);
188
189
  virtual ~ModelResetCommandFixed();
190
191
  virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, const QModelIndex &destParent, int destRow);
192
  virtual void emitPostSignal();
193
194
};
195
196
197
#endif