-
Notifications
You must be signed in to change notification settings - Fork 0
/
qmultiproxymodel.cpp
707 lines (614 loc) · 25.2 KB
/
qmultiproxymodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
#include "qmultiproxymodel.h"
#include <QDebug>
#include <QItemSelection>
class QMultiProxyModelPrivate
{
QMultiProxyModel *const q_ptr;
Q_DECLARE_PUBLIC(QMultiProxyModel)
QList<QAbstractItemModel *> m_sourceModels;
#if QT_VERSION >= 0x050000
QHash<int, QByteArray> m_rolenames;
#endif
QMultiProxyModelPrivate(QMultiProxyModel *qptr);
void updateRolenames();
int offsetForModel(const QAbstractItemModel *) const;
const QAbstractItemModel * sourceModelByProxyRow(int row) const;
public /* slots */:
void _q_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end);
void _q_rowsInserted(const QModelIndex &parent, int start, int end);
void _q_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
void _q_rowsRemoved(const QModelIndex &parent, int start, int end);
void _q_rowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest);
void _q_rowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest);
void _q_columnsAboutToBeInserted(const QModelIndex &parent, int start, int end);
void _q_columnsInserted(const QModelIndex &parent, int start, int end);
void _q_columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
void _q_columnsRemoved(const QModelIndex &parent, int start, int end);
void _q_columnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest);
void _q_columnsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest);
void _q_modelAboutToBeReset();
void _q_modelReset();
void _q_headerDataChanged(Qt::Orientation orientation, int first, int last);
#if QT_VERSION < 0x050000
void _q_layoutAboutToBeChanged();
void _q_layoutChanged();
void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
#else
void _q_layoutAboutToBeChanged(const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint);
void _q_layoutChanged(const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint);
void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
#endif
};
QMultiProxyModelPrivate::QMultiProxyModelPrivate(QMultiProxyModel *qptr) : q_ptr(qptr)
{
}
int QMultiProxyModelPrivate::offsetForModel(const QAbstractItemModel *sourceModel) const
{
int offset = 0;
foreach (QAbstractItemModel *model, m_sourceModels) {
if (sourceModel == model) {
return offset;
}
offset += model->rowCount();
}
return -1;
}
const QAbstractItemModel *QMultiProxyModelPrivate::sourceModelByProxyRow(int row) const
{
int offset = 0;
foreach (const QAbstractItemModel *model, m_sourceModels) {
if ((model->rowCount() + offset) > row) {
return model;
}
offset += model->rowCount();
}
return 0;
}
/*!
* \todo In Qt5 we have no possibility to notify viewers about update of roleNames if the viewer has been using the proxy model.
* May be we can resetModel, but it's overhead.
* Anyway it's an expansion of functionality and not necessary now.
*/
void QMultiProxyModelPrivate::updateRolenames()
{
#if QT_VERSION < 0x050000
Q_Q(QMultiProxyModel);
QHash<int,QByteArray> allRoleNames = q->roleNames();
bool changed = false;
#else
QHash<int,QByteArray> &allRoleNames = m_rolenames;
#endif
foreach (QAbstractItemModel *model, m_sourceModels) {
const QHash<int,QByteArray> &modelRN = model->roleNames();
foreach (int role, modelRN.uniqueKeys()) {
if (!allRoleNames.contains(role) || allRoleNames.value(role) != modelRN.value(role) ) {
#if QT_VERSION < 0x050000
changed = true;
#endif
allRoleNames.insertMulti(role, modelRN.value(role));
}
}
}
#if QT_VERSION < 0x050000
if (changed) {
q->setRoleNames(allRoleNames);
}
#endif
}
void QMultiProxyModelPrivate::_q_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
{
Q_Q(QMultiProxyModel);
QAbstractItemModel *srcModel = qobject_cast<QAbstractItemModel*>(q->sender());
Q_ASSERT(srcModel);
Q_ASSERT(parent.isValid() ? parent.model() == srcModel : true);
int offset = offsetForModel(srcModel);
q->beginInsertRows(parent, offset+start, offset+end);
}
void QMultiProxyModelPrivate::_q_rowsInserted(const QModelIndex &parent, int start, int end)
{
Q_UNUSED(start)
Q_UNUSED(end)
Q_Q(QMultiProxyModel);
updateRolenames();
QAbstractItemModel *srcModel = qobject_cast<QAbstractItemModel*>(q->sender());
Q_ASSERT(srcModel);
Q_ASSERT(parent.isValid() ? parent.model() == srcModel : true);
q->endInsertRows();
}
void QMultiProxyModelPrivate::_q_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
{
Q_Q(QMultiProxyModel);
QAbstractItemModel *srcModel = qobject_cast<QAbstractItemModel*>(q->sender());
Q_ASSERT(srcModel);
Q_ASSERT(parent.isValid() ? parent.model() == srcModel : true);
int offset = offsetForModel(srcModel);
q->beginRemoveRows(q->mapFromSource(parent), offset+start, offset+end);
}
void QMultiProxyModelPrivate::_q_rowsRemoved(const QModelIndex &parent, int start, int end)
{
Q_UNUSED(parent)
Q_UNUSED(start)
Q_UNUSED(end)
Q_Q(QMultiProxyModel);
q->endRemoveRows();
}
void QMultiProxyModelPrivate::_q_rowsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest)
{
Q_Q(QMultiProxyModel);
QAbstractItemModel *srcModel = qobject_cast<QAbstractItemModel*>(q->sender());
Q_ASSERT(srcModel);
Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == srcModel : true);
Q_ASSERT(destParent.isValid() ? destParent.model() == srcModel : true);
int offset = offsetForModel(srcModel);
q->beginMoveRows(q->mapFromSource(sourceParent), offset+sourceStart, offset+sourceEnd, q->mapFromSource(destParent), offset+dest);
}
void QMultiProxyModelPrivate::_q_rowsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest)
{
Q_UNUSED(sourceStart)
Q_UNUSED(sourceEnd)
Q_UNUSED(dest)
Q_Q(QMultiProxyModel);
QAbstractItemModel *srcModel = qobject_cast<QAbstractItemModel*>(q->sender());
Q_ASSERT(srcModel);
Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == srcModel : true);
Q_ASSERT(destParent.isValid() ? destParent.model() == srcModel : true);
q->endMoveRows();
}
void QMultiProxyModelPrivate::_q_columnsAboutToBeInserted(const QModelIndex &parent, int start, int end)
{
Q_Q(QMultiProxyModel);
QAbstractItemModel *srcModel = qobject_cast<QAbstractItemModel*>(q->sender());
Q_ASSERT(srcModel);
Q_ASSERT(parent.isValid() ? parent.model() == srcModel : true);
q->beginInsertColumns(q->mapFromSource(parent), start, end);
}
void QMultiProxyModelPrivate::_q_columnsInserted(const QModelIndex &parent, int start, int end)
{
Q_UNUSED(start)
Q_UNUSED(end)
Q_Q(QMultiProxyModel);
QAbstractItemModel *srcModel = qobject_cast<QAbstractItemModel*>(q->sender());
Q_ASSERT(srcModel);
Q_ASSERT(parent.isValid() ? parent.model() == srcModel : true);
updateRolenames();
q->endInsertColumns();
}
void QMultiProxyModelPrivate::_q_columnsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
{
Q_Q(QMultiProxyModel);
QAbstractItemModel *srcModel = qobject_cast<QAbstractItemModel*>(q->sender());
Q_ASSERT(srcModel);
Q_ASSERT(parent.isValid() ? parent.model() == srcModel : true);
q->beginRemoveColumns(q->mapFromSource(parent), start, end);
}
void QMultiProxyModelPrivate::_q_columnsRemoved(const QModelIndex &parent, int start, int end)
{
Q_UNUSED(start)
Q_UNUSED(end)
Q_Q(QMultiProxyModel);
QAbstractItemModel *srcModel = qobject_cast<QAbstractItemModel*>(q->sender());
Q_ASSERT(srcModel);
Q_ASSERT(parent.isValid() ? parent.model() == srcModel : true);
q->endRemoveColumns();
}
void QMultiProxyModelPrivate::_q_columnsAboutToBeMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest)
{
Q_Q(QMultiProxyModel);
QAbstractItemModel *srcModel = qobject_cast<QAbstractItemModel*>(q->sender());
Q_ASSERT(srcModel);
Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == srcModel : true);
Q_ASSERT(destParent.isValid() ? destParent.model() == srcModel : true);
q->beginMoveColumns(q->mapFromSource(sourceParent), sourceStart, sourceEnd, q->mapFromSource(destParent), dest);
}
void QMultiProxyModelPrivate::_q_columnsMoved(const QModelIndex &sourceParent, int sourceStart, int sourceEnd, const QModelIndex &destParent, int dest)
{
Q_UNUSED(sourceStart)
Q_UNUSED(sourceEnd)
Q_UNUSED(dest)
Q_Q(QMultiProxyModel);
QAbstractItemModel *srcModel = qobject_cast<QAbstractItemModel*>(q->sender());
Q_ASSERT(srcModel);
Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == srcModel : true);
Q_ASSERT(destParent.isValid() ? destParent.model() == srcModel : true);
q->endMoveColumns();
}
void QMultiProxyModelPrivate::_q_modelAboutToBeReset()
{
Q_Q(QMultiProxyModel);
emit q->beginResetModel();
}
void QMultiProxyModelPrivate::_q_modelReset()
{
Q_Q(QMultiProxyModel);
emit q->endResetModel();
}
/*!
* \todo I have no definite idea what to do if we have several models with different headers.
* Perhaps we need to merge headers.
*/
void QMultiProxyModelPrivate::_q_headerDataChanged(Qt::Orientation orientation, int first, int last)
{
Q_Q(QMultiProxyModel);
emit q->headerDataChanged(orientation, first, last);
}
#if QT_VERSION < 0x050000
void QMultiProxyModelPrivate::_q_layoutAboutToBeChanged()
{
Q_Q(QMultiProxyModel);
emit q->layoutAboutToBeChanged();
}
#else
void QMultiProxyModelPrivate::_q_layoutAboutToBeChanged(const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint)
{
Q_UNUSED(sourceParents)
Q_UNUSED(hint)
Q_Q(QMultiProxyModel);
emit q->layoutAboutToBeChanged();
}
#endif
#if QT_VERSION < 0x050000
void QMultiProxyModelPrivate::_q_layoutChanged()
{
Q_Q(QMultiProxyModel);
emit q->layoutChanged();
}
#else
void QMultiProxyModelPrivate::_q_layoutChanged(const QList<QPersistentModelIndex> &sourceParents, QAbstractItemModel::LayoutChangeHint hint)
{
Q_UNUSED(sourceParents)
Q_UNUSED(hint)
Q_Q(QMultiProxyModel);
emit q->layoutChanged();
}
#endif
#if QT_VERSION < 0x050000
void QMultiProxyModelPrivate::_q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
Q_Q(QMultiProxyModel);
Q_ASSERT(topLeft.isValid() ? topLeft.model() != q : true);
Q_ASSERT(bottomRight.isValid() ? bottomRight.model() != q : true);
emit q->dataChanged(q->mapFromSource(topLeft), q->mapFromSource(bottomRight));
}
#else
void QMultiProxyModelPrivate::_q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles)
{
Q_Q(QMultiProxyModel);
Q_ASSERT(topLeft.isValid() ? topLeft.model() != q : true);
Q_ASSERT(bottomRight.isValid() ? bottomRight.model() != q : true);
emit q->dataChanged(q->mapFromSource(topLeft), q->mapFromSource(bottomRight), roles);
}
#endif
/*!
\class QMultiProxyModel
\brief The QMultiProxyModel class provides several item models as one model.
\ingroup model-view
\note If the source model is deleted or no source model is specified, the
proxy model operates on a empty placeholder model.
\sa QSortFilterProxyModel, QAbstractItemModel, {Model/View Programming}
*/
/*!
* \brief Constructs a proxy model with the given parent.
*/
QMultiProxyModel::QMultiProxyModel(QObject *parent) :
QAbstractProxyModel(parent),
d_ptr(new QMultiProxyModelPrivate(this))
{
}
/*!
* \brief Destroys the proxy model.
*/
QMultiProxyModel::~QMultiProxyModel()
{
}
/*!
* \brief Returns the models that contains the data that is available through the proxy model.
* \return List of models
*/
QList<QAbstractItemModel *> QMultiProxyModel::sourceModels()
{
Q_D(QMultiProxyModel);
return d->m_sourceModels;
}
/*!
* \brief Adds the given source model into list for processing by the proxy model.
* \note Source models will be provided in the order they were added.
* \param model
* \return Returns false if the given model is NULL or already contained in the model's list; otherwise returns true.
*/
bool QMultiProxyModel::addSourceModel(QAbstractItemModel *model)
{
Q_D(QMultiProxyModel);
if (!model || d->m_sourceModels.contains(model)) {
return false;
}
beginResetModel();
d->m_sourceModels.append(model);
d->updateRolenames();
connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)));
connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
SLOT(_q_rowsInserted(QModelIndex,int,int)));
connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
SLOT(_q_rowsRemoved(QModelIndex,int,int)));
connect(model, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
SLOT(_q_rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
connect(model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
SLOT(_q_rowsMoved(QModelIndex,int,int,QModelIndex,int)));
connect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)));
connect(model, SIGNAL(columnsInserted(QModelIndex,int,int)),
SLOT(_q_columnsInserted(QModelIndex,int,int)));
connect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
SLOT(_q_columnsRemoved(QModelIndex,int,int)));
connect(model, SIGNAL(columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
SLOT(_q_columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
connect(model, SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)),
SLOT(_q_columnsMoved(QModelIndex,int,int,QModelIndex,int)));
connect(model, SIGNAL(modelAboutToBeReset()),
SLOT(_q_modelAboutToBeReset()));
connect(model, SIGNAL(modelReset()),
SLOT(_q_modelReset()));
connect(model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
SLOT(_q_headerDataChanged(Qt::Orientation,int,int)));
#if QT_VERSION < 0x050000
connect(model, SIGNAL(layoutAboutToBeChanged()),
SLOT(_q_layoutAboutToBeChanged()));
connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
connect(model, SIGNAL(layoutChanged()),
SLOT(_q_layoutChanged()));
#else
connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),
SLOT(_q_dataChanged(QModelIndex,QModelIndex,QVector<int>)));
connect(model, SIGNAL(layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
SLOT(_q_layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)));
connect(model, SIGNAL(layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
SLOT(_q_layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)));
#endif
endResetModel();
return true;
}
/*!
* \brief Removes the given source model from the model's list.
* \note The proxy model will be reseted.
* \param model
* \return Returns true if the proxy model contains an occurrence of the given model; otherwise returns false.
*/
bool QMultiProxyModel::removeSourceModel(QAbstractItemModel *model)
{
Q_D(QMultiProxyModel);
if (d->m_sourceModels.contains(model)) {
beginResetModel();
if (model) {
disconnect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int)));
disconnect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
this, SLOT(_q_rowsInserted(QModelIndex,int,int)));
disconnect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int)));
disconnect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
this, SLOT(_q_rowsRemoved(QModelIndex,int,int)));
disconnect(model, SIGNAL(rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
this, SLOT(_q_rowsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
disconnect(model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)),
this, SLOT(_q_rowsMoved(QModelIndex,int,int,QModelIndex,int)));
disconnect(model, SIGNAL(columnsAboutToBeInserted(QModelIndex,int,int)),
this, SLOT(_q_columnsAboutToBeInserted(QModelIndex,int,int)));
disconnect(model, SIGNAL(columnsInserted(QModelIndex,int,int)),
this, SLOT(_q_columnsInserted(QModelIndex,int,int)));
disconnect(model, SIGNAL(columnsAboutToBeRemoved(QModelIndex,int,int)),
this, SLOT(_q_columnsAboutToBeRemoved(QModelIndex,int,int)));
disconnect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)),
this, SLOT(_q_columnsRemoved(QModelIndex,int,int)));
disconnect(model, SIGNAL(columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)),
this, SLOT(_q_columnsAboutToBeMoved(QModelIndex,int,int,QModelIndex,int)));
disconnect(model, SIGNAL(columnsMoved(QModelIndex,int,int,QModelIndex,int)),
this, SLOT(_q_columnsMoved(QModelIndex,int,int,QModelIndex,int)));
disconnect(model, SIGNAL(modelAboutToBeReset()),
this, SLOT(_q_modelAboutToBeReset()));
disconnect(model, SIGNAL(modelReset()),
this, SLOT(_q_modelReset()));
disconnect(model, SIGNAL(headerDataChanged(Qt::Orientation,int,int)),
this, SLOT(_q_headerDataChanged(Qt::Orientation,int,int)));
#if QT_VERSION < 0x050000
disconnect(model, SIGNAL(layoutAboutToBeChanged()),
this, SLOT(_q_layoutAboutToBeChanged()));
disconnect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
this, SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
disconnect(model, SIGNAL(layoutChanged()),
this, SLOT(_q_layoutChanged()));
#else
disconnect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),
this, SLOT(_q_dataChanged(QModelIndex,QModelIndex,QVector<int>)));
disconnect(model, SIGNAL(layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
this, SLOT(_q_layoutAboutToBeChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)));
disconnect(model, SIGNAL(layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)),
this, SLOT(_q_layoutChanged(QList<QPersistentModelIndex>,QAbstractItemModel::LayoutChangeHint)));
#endif
}
d->m_sourceModels.removeAll(model);
endResetModel();
} else {
return false;
}
return true;
}
/*!
* \brief Clear the model's list.
* \sa removeSourceModel()
* \todo Don't reset the proxy model every iteration
*/
void QMultiProxyModel::clearSourceModelsList()
{
Q_D(QMultiProxyModel);
foreach (QAbstractItemModel *model, d->m_sourceModels) {
removeSourceModel(model);
}
}
/*!
* \param model
* \return Returns true if the proxy model contains an occurrence of the given model; otherwise returns false.
*/
bool QMultiProxyModel::containsSourceModel(QAbstractItemModel *model)
{
Q_D(QMultiProxyModel);
return d->m_sourceModels.contains(model);
}
/*!
* \brief reimplemented QAbstractProxyModel::data
*/
QVariant QMultiProxyModel::data(const QModelIndex &proxyIndex, int role) const
{
Q_D(const QMultiProxyModel);
const QAbstractItemModel *model = d->sourceModelByProxyRow(proxyIndex.row());
if (!model) {
return QVariant();
}
return model->data(mapToSource(proxyIndex), role);
}
/*!
* \brief reimplemented QAbstractProxyModel::mapToSource
*/
QModelIndex QMultiProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
if (!proxyIndex.isValid())
return QModelIndex();
Q_ASSERT(proxyIndex.model() == this);
Q_D(const QMultiProxyModel);
const QAbstractItemModel *model = d->sourceModelByProxyRow(proxyIndex.row());
if (model) {
int newRow = proxyIndex.row() - d->offsetForModel(model);
return model->index(newRow, proxyIndex.column());
}
return QModelIndex();
}
/*!
* \brief reimplemented QAbstractProxyModel::mapFromSource
*/
QModelIndex QMultiProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
{
if (!sourceIndex.isValid())
return QModelIndex();
Q_ASSERT(sourceIndex.model() != this);
Q_D(const QMultiProxyModel);
int row = d->offsetForModel(sourceIndex.model()) + sourceIndex.row();
return createIndex(row, sourceIndex.column());
}
/*! \todo Check ranges for multi selection.
* If \a it and \a end from different models we need to return several ranges for every model.
* Otherwise returns how it made now.
*/
/*!
* \brief reimplemented QAbstractProxyModel::mapSelectionToSource
*/
QItemSelection QMultiProxyModel::mapSelectionToSource(const QItemSelection &selection) const
{
QItemSelection sourceSelection;
QItemSelection::const_iterator it = selection.constBegin();
const QItemSelection::const_iterator end = selection.constEnd();
for ( ; it != end; ++it) {
const QItemSelectionRange range(mapToSource(it->topLeft()), mapToSource(it->bottomRight()));
sourceSelection.append(range);
}
return sourceSelection;
}
/*!
* \brief reimplemented QAbstractProxyModel::mapSelectionFromSource
*/
QItemSelection QMultiProxyModel::mapSelectionFromSource(const QItemSelection &selection) const
{
QItemSelection proxySelection;
QItemSelection::const_iterator it = selection.constBegin();
const QItemSelection::const_iterator end = selection.constEnd();
for ( ; it != end; ++it) {
const QItemSelectionRange range(mapFromSource(it->topLeft()), mapFromSource(it->bottomRight()));
proxySelection.append(range);
}
return proxySelection;
}
/*!
* \brief reimplemented QAbstractProxyModel::columnCount
*/
int QMultiProxyModel::columnCount(const QModelIndex &parent) const
{
Q_ASSERT(parent.isValid() ? parent.model() == this : true);
Q_D(const QMultiProxyModel);
const QAbstractItemModel *model = d->sourceModelByProxyRow(parent.row());
if (!model) {
return 0;
}
return model->columnCount(mapToSource(parent));
}
/*!
* \brief reimplemented QAbstractProxyModel::index
*/
QModelIndex QMultiProxyModel::index(int row, int column, const QModelIndex &parent) const
{
Q_UNUSED(parent)
return createIndex(row, column);
}
/*!
* \brief reimplemented QAbstractProxyModel::parent
*/
QModelIndex QMultiProxyModel::parent(const QModelIndex &child) const
{
Q_ASSERT(child.isValid() ? child.model() == this : true);
const QModelIndex sourceIndex = mapToSource(child);
const QModelIndex sourceParent = sourceIndex.parent();
return mapFromSource(sourceParent);
}
/*!
* \brief reimplemented QAbstractProxyModel::rowCount
*/
int QMultiProxyModel::rowCount(const QModelIndex &parent) const
{
Q_ASSERT(parent.isValid() ? parent.model() == this : true);
Q_D(const QMultiProxyModel);
int rowCountSum = 0;
foreach (QAbstractItemModel *model, d->m_sourceModels) {
rowCountSum += model->rowCount(parent);
}
return rowCountSum;
}
/*!
* \brief reimplemented QAbstractProxyModel::flags
*/
Qt::ItemFlags QMultiProxyModel::flags(const QModelIndex &index) const{
Q_D(const QMultiProxyModel);
const QAbstractItemModel *model = d->sourceModelByProxyRow(index.row());
if (model) {
return model->flags(mapToSource(this->index(index.row(), index.column())));
}
return QAbstractProxyModel::flags(index);
}
/*!
* \brief reimplemented QAbstractProxyModel::buddy
*/
QModelIndex QMultiProxyModel::buddy(const QModelIndex &index) const{
Q_D(const QMultiProxyModel);
QModelIndex source_index = mapToSource(index);
QModelIndex source_buddy;
const QAbstractItemModel *model = d->sourceModelByProxyRow(index.row());
if (model) {
source_buddy = model->buddy(source_index);
}
if (source_index == source_buddy) {
return index;
}
return mapFromSource(source_buddy);
}
#if QT_VERSION >= 0x050000
/*!
* \brief overriden QAbstractProxyModel::roleNames
*/
QHash<int, QByteArray> QMultiProxyModel::roleNames() const
{
Q_D(const QMultiProxyModel);
return d->m_rolenames;
}
#endif
#include "moc_qmultiproxymodel.cpp"