1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2009 Stephen Kelly <steveire@gmail.com> |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #ifndef DYNAMICTREEMODEL_H |
30 | #define DYNAMICTREEMODEL_H |
31 | |
32 | #include <QtCore/QAbstractItemModel> |
33 | |
34 | #include <QtCore/QHash> |
35 | #include <QtCore/QList> |
36 | |
37 | class DynamicTreeModel : public QAbstractItemModel |
38 | { |
39 | Q_OBJECT |
40 | |
41 | public: |
42 | DynamicTreeModel(QObject *parent = 0); |
43 | |
44 | QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const; |
45 | QModelIndex parent(const QModelIndex &index) const; |
46 | int rowCount(const QModelIndex &index = QModelIndex()) const; |
47 | int columnCount(const QModelIndex &index = QModelIndex()) const; |
48 | |
49 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; |
50 | |
51 | void clear(); |
52 | |
53 | protected slots: |
54 | |
55 | /** |
56 | Finds the parent id of the string with id @p searchId. |
57 | |
58 | Returns -1 if not found. |
59 | */ |
60 | qint64 findParentId(qint64 searchId) const; |
61 | |
62 | private: |
63 | QHash<qint64, QString> m_items; |
64 | QHash<qint64, QList<QList<qint64> > > m_childItems; |
65 | qint64 nextId; |
66 | qint64 newId() |
67 | { |
68 | return nextId++; |
69 | } |
70 | |
71 | QModelIndex m_nextParentIndex; |
72 | int m_nextRow; |
73 | |
74 | int m_depth; |
75 | int maxDepth; |
76 | |
77 | friend class ModelInsertCommand; |
78 | friend class ModelMoveCommand; |
79 | friend class ModelResetCommand; |
80 | friend class ModelResetCommandFixed; |
81 | friend class ModelChangeChildrenLayoutsCommand; |
82 | }; |
83 | |
84 | class ModelChangeCommand : public QObject |
85 | { |
86 | Q_OBJECT |
87 | public: |
88 | |
89 | ModelChangeCommand(DynamicTreeModel *model, QObject *parent = 0); |
90 | |
91 | virtual ~ModelChangeCommand() |
92 | { |
93 | } |
94 | |
95 | void setAncestorRowNumbers(QList<int> rowNumbers) |
96 | { |
97 | m_rowNumbers = rowNumbers; |
98 | } |
99 | |
100 | QModelIndex findIndex(const QList<int> &rows) const; |
101 | |
102 | void setStartRow(int row) |
103 | { |
104 | m_startRow = row; |
105 | } |
106 | |
107 | void setEndRow(int row) |
108 | { |
109 | m_endRow = row; |
110 | } |
111 | |
112 | void setNumCols(int cols) |
113 | { |
114 | m_numCols = cols; |
115 | } |
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 | typedef QList<ModelChangeCommand *> ModelChangeCommandList; |
128 | |
129 | class ModelInsertCommand : public ModelChangeCommand |
130 | { |
131 | Q_OBJECT |
132 | |
133 | public: |
134 | |
135 | ModelInsertCommand(DynamicTreeModel *model, QObject *parent = 0); |
136 | virtual ~ModelInsertCommand() |
137 | { |
138 | } |
139 | |
140 | virtual void doCommand(); |
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 | } |
152 | |
153 | virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, |
154 | const QModelIndex &destParent, int destRow); |
155 | |
156 | virtual void doCommand(); |
157 | |
158 | virtual void emitPostSignal(); |
159 | |
160 | void setDestAncestors(QList<int> rows) |
161 | { |
162 | m_destRowNumbers = rows; |
163 | } |
164 | |
165 | void setDestRow(int row) |
166 | { |
167 | m_destRow = row; |
168 | } |
169 | |
170 | protected: |
171 | QList<int> m_destRowNumbers; |
172 | int m_destRow; |
173 | }; |
174 | |
175 | /** |
176 | A command which does a move and emits a reset signal. |
177 | */ |
178 | class ModelResetCommand : public ModelMoveCommand |
179 | { |
180 | Q_OBJECT |
181 | public: |
182 | ModelResetCommand(DynamicTreeModel *model, QObject *parent = 0); |
183 | |
184 | virtual ~ModelResetCommand(); |
185 | |
186 | virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, |
187 | const QModelIndex &destParent, int destRow); |
188 | virtual void emitPostSignal(); |
189 | }; |
190 | |
191 | /** |
192 | A command which does a move and emits a beginResetModel and endResetModel signals. |
193 | */ |
194 | class ModelResetCommandFixed : public ModelMoveCommand |
195 | { |
196 | Q_OBJECT |
197 | public: |
198 | ModelResetCommandFixed(DynamicTreeModel *model, QObject *parent = 0); |
199 | |
200 | virtual ~ModelResetCommandFixed(); |
201 | |
202 | virtual bool emitPreSignal(const QModelIndex &srcParent, int srcStart, int srcEnd, |
203 | const QModelIndex &destParent, int destRow); |
204 | virtual void emitPostSignal(); |
205 | }; |
206 | |
207 | class ModelChangeChildrenLayoutsCommand : public ModelChangeCommand |
208 | { |
209 | Q_OBJECT |
210 | public: |
211 | ModelChangeChildrenLayoutsCommand(DynamicTreeModel *model, QObject *parent); |
212 | |
213 | virtual ~ModelChangeChildrenLayoutsCommand() |
214 | { |
215 | } |
216 | |
217 | virtual void doCommand(); |
218 | |
219 | void setSecondAncestorRowNumbers(QList<int> rows) |
220 | { |
221 | m_secondRowNumbers = rows; |
222 | } |
223 | |
224 | protected: |
225 | QList<int> m_secondRowNumbers; |
226 | int m_destRow; |
227 | }; |
228 | |
229 | #endif |
230 | |