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