Debian Codegraph @

Dekko/utils/RowsJoinerProxy.cpp

raw symbols SCIP
1 /*
2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * [email protected] wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return Oleksandr Iakovliev
7 * ----------------------------------------------------------------------------
8 */
9
10 #include <QtCore>
11 #include "RowsJoinerProxy.h"
12
13 class RowsJoinerProxy::Private {
14 public:
15 RowsJoinerProxy * instance;
16 QList<QAbstractItemModel *> models;
17 int columnCount;
18 int rowCount;
19
20 struct Addr;
21 typedef QSharedPointer<Addr> AddrPtr;
22 typedef QList<AddrPtr> AddrRow;
23 typedef QList<AddrRow> AddrMatrix;
24
25 struct Addr {
26 Addr(const QAbstractItemModel * _m, void * _p)
27 :m(_m),p(_p),root(true) {}
28
29 const QAbstractItemModel * m;
30 void * p;
31
32 bool root;
33 AddrMatrix down;
34 };
35
36 AddrMatrix tops;
37
38 int topRowShift(const QAbstractItemModel *) const;
39 void checkExpand(AddrMatrix &, int row, int col);
40 AddrPtr initAddrFromSource(QModelIndex source);
41 };
42
43 RowsJoinerProxy::RowsJoinerProxy(QObject * parent)
44 :QAbstractItemModel(parent)
45 {
46 d = new Private;
47 d->instance = this;
48 d->columnCount = -1;
49 d->rowCount = -1;
50 }
51
52 RowsJoinerProxy::~RowsJoinerProxy() {
53 while(d->models.size())
54 removeSourceModel(d->models.back());
55 delete d;
56 }
57
58 QList<QAbstractItemModel *> RowsJoinerProxy::models() const { return d->models; }
59 int RowsJoinerProxy::indexOf(QAbstractItemModel * m) const { return d->models.indexOf(m); }
60
61 void RowsJoinerProxy::insertSourceModel(QAbstractItemModel * m, int idx)
62 {
63 if (!m)
64 return;
65
66 beginResetModel();
67 if (idx < 0)
68 d->models.append(m);
69 else d->models.insert(idx, m);
70
71 connect(m, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)),
72 this, SLOT(s_rowsAboutToBeInserted(QModelIndex, int, int)));
73 connect(m, SIGNAL(rowsInserted(QModelIndex, int, int)),
74 this, SLOT(s_rowsInserted(QModelIndex, int, int)));
75
76 connect(m, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
77 this, SLOT(s_rowsAboutToBeRemoved(QModelIndex, int, int)));
78 connect(m, SIGNAL(rowsRemoved(QModelIndex, int, int)),
79 this, SLOT(s_rowsRemoved(QModelIndex, int, int)));
80
81 connect(m, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
82 this, SLOT(s_dataChanged(QModelIndex,QModelIndex)));
83 connect(m, SIGNAL(modelReset()),
84 this, SLOT(s_modelReset()));
85
86 connect(m, SIGNAL(layoutAboutToBeChanged()),
87 this, SIGNAL(layoutAboutToBeChanged()));
88 connect(m, SIGNAL(layoutChanged()),
89 this, SIGNAL(layoutChanged()));
90
91 connect(m, SIGNAL(destroyed(QObject *)),
92 this, SLOT(s_destroyed(QObject *)));
93
94 d->columnCount = -1;
95 d->rowCount = -1;
96 d->tops.clear();
97 endResetModel();
98 }
99
100 void RowsJoinerProxy::removeSourceModel(QAbstractItemModel * m)
101 {
102 if (!m)
103 return;
104
105 beginResetModel();
106 disconnect(m, SIGNAL(rowsAboutToBeInserted(QModelIndex, int, int)),
107 this, SLOT(s_rowsAboutToBeInserted(QModelIndex, int, int)));
108 disconnect(m, SIGNAL(rowsInserted(QModelIndex, int, int)),
109 this, SLOT(s_rowsInserted(QModelIndex, int, int)));
110
111 disconnect(m, SIGNAL(rowsAboutToBeRemoved(QModelIndex, int, int)),
112 this, SLOT(s_rowsAboutToBeRemoved(QModelIndex, int, int)));
113 disconnect(m, SIGNAL(rowsRemoved(QModelIndex, int, int)),
114 this, SLOT(s_rowsRemoved(QModelIndex, int, int)));
115
116 disconnect(m, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
117 this, SLOT(s_dataChanged(QModelIndex,QModelIndex)));
118 disconnect(m, SIGNAL(modelReset()),
119 this, SLOT(s_modelReset()));
120
121 disconnect(m, SIGNAL(layoutAboutToBeChanged()),
122 this, SIGNAL(layoutAboutToBeChanged()));
123 disconnect(m, SIGNAL(layoutChanged()),
124 this, SIGNAL(layoutChanged()));
125
126 disconnect(m, SIGNAL(destroyed(QObject *)),
127 this, SLOT(s_destroyed(QObject *)));
128
129 d->models.removeAll(m);
130 d->columnCount = -1;
131 d->rowCount = -1;
132 d->tops.clear();
133 endResetModel();
134 }
135
136 int RowsJoinerProxy::Private::topRowShift(const QAbstractItemModel * model) const
137 {
138 int row_shift = 0;
139 QAbstractItemModel * m = const_cast<QAbstractItemModel *>(model);
140 int model_idx = models.indexOf(m);
141 for (int idx =0; idx< model_idx; ++idx)
142 row_shift += models[idx]->rowCount();
143
144 return row_shift;
145 }
146
147 void RowsJoinerProxy::Private::checkExpand(RowsJoinerProxy::Private::AddrMatrix & m, int row, int col)
148 {
149 while (m.size() <= row) {
150 Private::AddrRow empty_row;
151 for(int i= 0; i<= col; ++i)
152 empty_row.append(Private::AddrPtr());
153 m.append(empty_row);
154 }
155
156 while (m[row].size() <= col) {
157 m[row].append(Private::AddrPtr());
158 }
159 }
160
161 RowsJoinerProxy::Private::AddrPtr RowsJoinerProxy::Private::initAddrFromSource(QModelIndex i)
162 {
163 if (!i.isValid())
164 return AddrPtr();
165
166 int row = i.row();
167 int col = i.column();
168
169 if (!i.parent().isValid()) {
170 row += topRowShift(i.model());
171 checkExpand(tops, row, col);
172 if (tops[row][col])
173 return tops[row][col];
174
175 AddrPtr addr(new Addr(i.model(), i.internalPointer()));
176 tops[row][col] = addr;
177 return addr;
178 }
179
180 AddrPtr p = initAddrFromSource(i.parent());
181 checkExpand(p->down, row, col);
182 if (p->down[row][col])
183 return p->down[row][col];
184
185 AddrPtr addr(new Addr(i.model(), i.internalPointer()));
186 p->down[row][col] = addr;
187 addr->root = false;
188 return addr;
189 }
190
191 /*! Mapping source model index into exact clone index of the proxy model,
192 except top-level indexes which are shifted for aappropriate rows.
193 Uses same internal id or internal pointer to use parent-children
194 relations logic of source models.
195 */
196 QModelIndex RowsJoinerProxy::mapFromSource(const QModelIndex & s) const
197 {
198 if (!s.isValid())
199 return QModelIndex();
200
201 Private::AddrPtr addr = d->initAddrFromSource(s);
202
203 int row_shift = 0;
204 if (!s.parent().isValid())
205 row_shift = d->topRowShift(s.model());
206
207 return createIndex(s.row() +row_shift, s.column(), addr.data());
208 }
209
210 /*! Mapping index of proxy into appropriate index of source model.
211 It creates exact clones of indexes with internal id or internal pointer
212 to use parent-children relations logic of source models.
213 */
214 QModelIndex RowsJoinerProxy::mapToSource(const QModelIndex & proxy) const
215 {
216 if (!proxy.isValid())
217 return QModelIndex();
218
219 Private::Addr * addr = reinterpret_cast<Private::Addr *>(proxy.internalPointer());
220 if (!addr) return QModelIndex();
221
222 QAbstractItemModel * m = const_cast<QAbstractItemModel *>(addr->m);
223 if (!m) return QModelIndex();
224
225 int row_shift = 0;
226 if (addr->root) {
227 row_shift = -d->topRowShift(addr->m);
228 if (proxy.column() >= m->columnCount())
229 return QModelIndex();
230 }
231
232 // safe trick to access createIndex: needed in vs2010
233 RowsJoinerProxy * trick = reinterpret_cast<RowsJoinerProxy *>(m);
234 return trick->createIndex(proxy.row() +row_shift, proxy.column(), addr->p);
235 }
236
237 /*! Creates exact clones of parents of source models, except
238 top level where indexes should be shifted to appropriate rows.
239 */
240 QModelIndex RowsJoinerProxy::parent(const QModelIndex & i) const
241 {
242 if (!i.isValid())
243 return QModelIndex();
244
245 QModelIndex si = mapToSource(i);
246 if (!si.isValid() || !si.model())
247 return QModelIndex();
248
249 QModelIndex psi = si.model()->parent(si);
250 if (!psi.isValid())
251 return QModelIndex();
252
253 QModelIndex pi = mapFromSource(psi);
254 return pi;
255 }
256
257 /*! Creates exact clones of indexes of source models, except
258 top level where indexes should be shifted to appropriate rows.
259 */
260 QModelIndex RowsJoinerProxy::index(int row, int column, const QModelIndex & p) const
261 {
262 if (!p.isValid()) { // top level, we need to create clone of source index with shift
263 int mod_idx = -1;
264 int top_row = row;
265 QAbstractItemModel * source_model = 0L;
266
267 for (int idx =0; idx< d->models.size(); ++idx) {
268 source_model = d->models[idx];
269 if (source_model->rowCount() > top_row) {
270 mod_idx = idx;
271 break;
272 }
273 top_row -= source_model->rowCount();
274 }
275
276 if (mod_idx <0) // can not create index out of rows
277 return QModelIndex();
278
279 if (!source_model)
280 return QModelIndex();
281
282 QModelIndex si = source_model->index(top_row, column, QModelIndex());
283 return mapFromSource(si);
284 }
285
286 QModelIndex sp = mapToSource(p);
287 if (!sp.isValid())
288 return QModelIndex();
289
290 QModelIndex si = sp.model()->index(row, column, sp);
291 return mapFromSource(si);
292 }
293
294 /*! Return sum of row counts for top level.
295 For nested levels it returns row count of appropriate
296 models. The top level count is cached.
297 */
298 int RowsJoinerProxy::rowCount(const QModelIndex & p) const
299 {
300 if (!p.isValid()) {
301 if (d->rowCount <0) {
302 d->rowCount =0;
303 foreach(QAbstractItemModel * m, d->models)
304 d->rowCount += m->rowCount();
305 }
306 return d->rowCount;
307 }
308
309 QModelIndex sp = mapToSource(p);
310 if (sp.isValid() && sp.model())
311 return sp.model()->rowCount(sp);
312
313 return 0;
314 }
315
316 /*! Return maximum of columns counts for top level.
317 For nested levels it returns column count of appropriate
318 models. The top level count is cached.
319 */
320 int RowsJoinerProxy::columnCount(const QModelIndex & p) const
321 {
322 if (!p.isValid()) {
323 if (d->columnCount <0) {
324 d->columnCount = 0;
325 foreach(QAbstractItemModel * m, d->models)
326 d->columnCount = qMax(d->columnCount, m->columnCount());
327 }
328 return d->columnCount;
329 }
330
331 QModelIndex sp = mapToSource(p);
332 if (sp.isValid() && sp.model())
333 return sp.model()->columnCount(sp);
334
335 return 0;
336 }
337
338 void RowsJoinerProxy::s_rowsAboutToBeInserted(QModelIndex sp, int from, int to)
339 {
340 QAbstractItemModel * m = dynamic_cast<QAbstractItemModel *>(sender());
341 if (!m) return;
342 int f = from;
343 int t = to;
344
345 if (!sp.isValid()) {
346 f += d->topRowShift(m);
347 t = f + (to-from);
348 }
349
350 beginInsertRows(mapFromSource(sp), f, t);
351
352 if (sp.isValid()) {
353 Private::AddrPtr addr = d->initAddrFromSource(sp);
354 Private::AddrRow row;
355 for(int i= 0; i< m->columnCount(sp); ++i)
356 row.append(Private::AddrPtr());
357 for(int i= f; i<= t; ++i)
358 addr->down.insert(f, row);
359 }
360 else {
361 Private::AddrRow row;
362 for(int i= 0; i< m->columnCount(sp); ++i)
363 row.append(Private::AddrPtr());
364 for(int i= f; i<= t; ++i)
365 d->tops.insert(f, row);
366 d->rowCount += t-f+1;
367 }
368 }
369
370 void RowsJoinerProxy::s_rowsInserted(QModelIndex, int, int) { endInsertRows(); }
371
372 void RowsJoinerProxy::s_rowsAboutToBeRemoved(QModelIndex sp, int from, int to)
373 {
374 QAbstractItemModel * m = dynamic_cast<QAbstractItemModel *>(sender());
375 if (!m) return;
376 int f = from;
377 int t = to;
378
379 if (!sp.isValid()) {
380 f += d->topRowShift(m);
381 t = f + (to-from);
382 }
383
384 beginRemoveRows(mapFromSource(sp), f, t);
385
386 if (sp.isValid()) {
387 Private::AddrPtr addr = d->initAddrFromSource(sp);
388 for(int i= f; i<= t; ++i) {
389 addr->down.removeAt(f);
390 }
391 }
392 else {
393 for(int i= f; i<= t; ++i)
394 d->tops.removeAt(f);
395 d->rowCount -= t-f+1;
396 }
397 }
398
399 void RowsJoinerProxy::s_rowsRemoved(QModelIndex, int, int) { endRemoveRows(); }
400
401 void RowsJoinerProxy::s_dataChanged(QModelIndex tl, QModelIndex br)
402 {
403 QModelIndex p_tl = mapFromSource(tl);
404 QModelIndex p_br = mapFromSource(br);
405 emit dataChanged(p_tl, p_br);
406 }
407
408 void RowsJoinerProxy::s_modelReset()
409 {
410 beginResetModel();
411 d->columnCount = -1;
412 d->rowCount = -1;
413 d->tops.clear();
414 endResetModel();
415 }
416
417 void RowsJoinerProxy::s_destroyed(QObject * obj)
418 {
419 foreach(QAbstractItemModel * m, d->models) {
420 if (m == obj) {
421 beginResetModel();
422 d->models.removeAll(m);
423 d->columnCount = -1;
424 d->rowCount = -1;
425 d->tops.clear();
426 endResetModel();
427 break;
428 }
429 }
430 }