24 #include <QtGui/QItemSelection>
25 #include <QtCore/QStringList>
27 class KIdentityProxyModelPrivate
31 ignoreNextLayoutAboutToBeChanged(false),
32 ignoreNextLayoutChanged(false)
40 bool ignoreNextLayoutAboutToBeChanged;
41 bool ignoreNextLayoutChanged;
42 QList<QPersistentModelIndex> layoutChangePersistentIndexes;
43 QModelIndexList proxyIndexes;
45 void _k_sourceRowsAboutToBeInserted(const QModelIndex &parent,
int start,
int end);
46 void _k_sourceRowsInserted(const QModelIndex &parent,
int start,
int end);
47 void _k_sourceRowsAboutToBeRemoved(const QModelIndex &parent,
int start,
int end);
48 void _k_sourceRowsRemoved(const QModelIndex &parent,
int start,
int end);
49 void _k_sourceRowsAboutToBeMoved(const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd, const QModelIndex &destParent,
int dest);
50 void _k_sourceRowsMoved(const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd, const QModelIndex &destParent,
int dest);
52 void _k_sourceColumnsAboutToBeInserted(const QModelIndex &parent,
int start,
int end);
53 void _k_sourceColumnsInserted(const QModelIndex &parent,
int start,
int end);
54 void _k_sourceColumnsAboutToBeRemoved(const QModelIndex &parent,
int start,
int end);
55 void _k_sourceColumnsRemoved(const QModelIndex &parent,
int start,
int end);
56 void _k_sourceColumnsAboutToBeMoved(const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd, const QModelIndex &destParent,
int dest);
57 void _k_sourceColumnsMoved(const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd, const QModelIndex &destParent,
int dest);
59 void _k_sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
60 void _k_sourceHeaderDataChanged(Qt::Orientation orientation,
int first,
int last);
62 void _k_sourceLayoutAboutToBeChanged();
63 void _k_sourceLayoutChanged();
64 void _k_sourceChildrenLayoutsAboutToBeChanged(const QModelIndex &parent1, const QModelIndex &parent2);
65 void _k_sourceChildrenLayoutsChanged(const QModelIndex &parent1, const QModelIndex &parent2);
66 void _k_sourceModelAboutToBeReset();
67 void _k_sourceModelReset();
68 void _k_sourceModelDestroyed();
151 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
152 return sourceModel()->canFetchMore(
mapToSource(parent));
162 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
163 return sourceModel()->columnCount(
mapToSource(parent));
173 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
184 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
185 return sourceModel()->dropMimeData(data, action, row, column,
mapToSource(parent));
194 return QModelIndex();
195 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
196 if (row < 0 || column < 0 || !hasIndex(row, column, parent))
197 return QModelIndex();
198 const QModelIndex sourceParent =
mapToSource(parent);
199 const QModelIndex sourceIndex = sourceModel()->index(row, column, sourceParent);
200 Q_ASSERT(sourceIndex.isValid());
211 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
212 return sourceModel()->insertColumns(column, count,
mapToSource(parent));
222 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
223 return sourceModel()->insertRows(row, count,
mapToSource(parent));
231 if (!sourceModel() || !sourceIndex.isValid())
232 return QModelIndex();
234 Q_ASSERT(sourceIndex.model() == sourceModel());
235 return createIndex(sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer());
243 QItemSelection proxySelection;
246 return proxySelection;
248 QItemSelection::const_iterator it = selection.constBegin();
249 const QItemSelection::const_iterator
end = selection.constEnd();
250 for ( ; it !=
end; ++it) {
251 Q_ASSERT(it->model() == sourceModel());
253 proxySelection.append(range);
256 return proxySelection;
264 QItemSelection sourceSelection;
267 return sourceSelection;
269 QItemSelection::const_iterator it = selection.constBegin();
270 const QItemSelection::const_iterator
end = selection.constEnd();
271 for ( ; it !=
end; ++it) {
272 Q_ASSERT(it->model() ==
this);
274 sourceSelection.append(range);
277 return sourceSelection;
280 struct SourceModelIndex
283 : r(_r), c(_c), p(_p), m(_m)
288 operator QModelIndex() {
return reinterpret_cast<QModelIndex&
>(*this); }
300 if (!sourceModel() || !proxyIndex.isValid())
301 return QModelIndex();
302 Q_ASSERT(proxyIndex.model() ==
this);
303 return SourceModelIndex(proxyIndex.row(), proxyIndex.column(), proxyIndex.internalPointer(), sourceModel());
311 Q_ASSERT(start.isValid() ? start.model() ==
this :
true);
313 return QModelIndexList();
315 const QModelIndexList sourceList = sourceModel()->match(
mapToSource(start), role, value, hits, flags);
316 QModelIndexList::const_iterator it = sourceList.constBegin();
317 const QModelIndexList::const_iterator
end = sourceList.constEnd();
318 QModelIndexList proxyList;
319 for ( ; it !=
end; ++it)
330 return sourceModel()->mimeTypes();
340 QModelIndexList proxyIndexes;
341 foreach(
const QModelIndex &
index, indexes)
344 return sourceModel()->mimeData(proxyIndexes);
354 return QModelIndex();
356 Q_ASSERT(child.isValid() ? child.model() ==
this :
true);
357 const QModelIndex sourceIndex =
mapToSource(child);
358 const QModelIndex sourceParent = sourceIndex.parent();
370 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
371 return sourceModel()->removeColumns(column, count,
mapToSource(parent));
382 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
383 return sourceModel()->removeRows(row, count,
mapToSource(parent));
393 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
394 return sourceModel()->rowCount(
mapToSource(parent));
405 disconnect(sourceModel, SIGNAL(rowsAboutToBeInserted(QModelIndex,
int,
int)),
406 this, SLOT(_k_sourceRowsAboutToBeInserted(QModelIndex,
int,
int)));
407 disconnect(sourceModel, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
408 this, SLOT(_k_sourceRowsInserted(QModelIndex,
int,
int)));
409 disconnect(sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,
int,
int)),
410 this, SLOT(_k_sourceRowsAboutToBeRemoved(QModelIndex,
int,
int)));
411 disconnect(sourceModel, SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
412 this, SLOT(_k_sourceRowsRemoved(QModelIndex,
int,
int)));
413 disconnect(sourceModel, SIGNAL(rowsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)),
414 this, SLOT(_k_sourceRowsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)));
415 disconnect(sourceModel, SIGNAL(rowsMoved(QModelIndex,
int,
int,QModelIndex,
int)),
416 this, SLOT(_k_sourceRowsMoved(QModelIndex,
int,
int,QModelIndex,
int)));
417 disconnect(sourceModel, SIGNAL(columnsAboutToBeInserted(QModelIndex,
int,
int)),
418 this, SLOT(_k_sourceColumnsAboutToBeInserted(QModelIndex,
int,
int)));
419 disconnect(sourceModel, SIGNAL(columnsInserted(QModelIndex,
int,
int)),
420 this, SLOT(_k_sourceColumnsInserted(QModelIndex,
int,
int)));
421 disconnect(sourceModel, SIGNAL(columnsAboutToBeRemoved(QModelIndex,
int,
int)),
422 this, SLOT(_k_sourceColumnsAboutToBeRemoved(QModelIndex,
int,
int)));
423 disconnect(sourceModel, SIGNAL(columnsRemoved(QModelIndex,
int,
int)),
424 this, SLOT(_k_sourceColumnsRemoved(QModelIndex,
int,
int)));
425 disconnect(sourceModel, SIGNAL(columnsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)),
426 this, SLOT(_k_sourceColumnsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)));
427 disconnect(sourceModel, SIGNAL(columnsMoved(QModelIndex,
int,
int,QModelIndex,
int)),
428 this, SLOT(_k_sourceColumnsMoved(QModelIndex,
int,
int,QModelIndex,
int)));
429 disconnect(sourceModel, SIGNAL(modelAboutToBeReset()),
430 this, SLOT(_k_sourceModelAboutToBeReset()));
433 disconnect(sourceModel, SIGNAL(modelReset()),
434 this, SLOT(_k_sourceModelReset()));
435 disconnect(sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
436 this, SLOT(_k_sourceDataChanged(QModelIndex,QModelIndex)));
437 disconnect(sourceModel, SIGNAL(headerDataChanged(Qt::Orientation,
int,
int)),
438 this, SLOT(_k_sourceHeaderDataChanged(Qt::Orientation,
int,
int)));
439 disconnect(sourceModel, SIGNAL(layoutAboutToBeChanged()),
440 this, SLOT(_k_sourceLayoutAboutToBeChanged()));
441 disconnect(sourceModel, SIGNAL(layoutChanged()),
442 this, SLOT(_k_sourceLayoutChanged()));
447 disconnect(sourceModel, SIGNAL(destroyed()),
448 this, SLOT(_k_sourceModelDestroyed()));
454 connect(sourceModel, SIGNAL(rowsAboutToBeInserted(QModelIndex,
int,
int)),
455 SLOT(_k_sourceRowsAboutToBeInserted(QModelIndex,
int,
int)));
456 connect(sourceModel, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
457 SLOT(_k_sourceRowsInserted(QModelIndex,
int,
int)));
458 connect(sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,
int,
int)),
459 SLOT(_k_sourceRowsAboutToBeRemoved(QModelIndex,
int,
int)));
460 connect(sourceModel, SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
461 SLOT(_k_sourceRowsRemoved(QModelIndex,
int,
int)));
462 connect(sourceModel, SIGNAL(rowsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)),
463 SLOT(_k_sourceRowsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)));
464 connect(sourceModel, SIGNAL(rowsMoved(QModelIndex,
int,
int,QModelIndex,
int)),
465 SLOT(_k_sourceRowsMoved(QModelIndex,
int,
int,QModelIndex,
int)));
466 connect(sourceModel, SIGNAL(columnsAboutToBeInserted(QModelIndex,
int,
int)),
467 SLOT(_k_sourceColumnsAboutToBeInserted(QModelIndex,
int,
int)));
468 connect(sourceModel, SIGNAL(columnsInserted(QModelIndex,
int,
int)),
469 SLOT(_k_sourceColumnsInserted(QModelIndex,
int,
int)));
470 connect(sourceModel, SIGNAL(columnsAboutToBeRemoved(QModelIndex,
int,
int)),
471 SLOT(_k_sourceColumnsAboutToBeRemoved(QModelIndex,
int,
int)));
472 connect(sourceModel, SIGNAL(columnsRemoved(QModelIndex,
int,
int)),
473 SLOT(_k_sourceColumnsRemoved(QModelIndex,
int,
int)));
474 connect(sourceModel, SIGNAL(columnsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)),
475 SLOT(_k_sourceColumnsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)));
476 connect(sourceModel, SIGNAL(columnsMoved(QModelIndex,
int,
int,QModelIndex,
int)),
477 SLOT(_k_sourceColumnsMoved(QModelIndex,
int,
int,QModelIndex,
int)));
478 connect(sourceModel, SIGNAL(modelAboutToBeReset()),
479 SLOT(_k_sourceModelAboutToBeReset()));
482 connect(sourceModel, SIGNAL(modelReset()),
483 SLOT(_k_sourceModelReset()));
484 connect(sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
485 SLOT(_k_sourceDataChanged(QModelIndex,QModelIndex)));
486 connect(sourceModel, SIGNAL(headerDataChanged(Qt::Orientation,
int,
int)),
487 SLOT(_k_sourceHeaderDataChanged(Qt::Orientation,
int,
int)));
488 connect(sourceModel, SIGNAL(layoutAboutToBeChanged()),
489 SLOT(_k_sourceLayoutAboutToBeChanged()));
490 connect(sourceModel, SIGNAL(layoutChanged()),
491 SLOT(_k_sourceLayoutChanged()));
497 connect(sourceModel, SIGNAL(destroyed()),
498 SLOT(_k_sourceModelDestroyed()));
507 return sourceModel()->supportedDropActions();
512 void KIdentityProxyModelPrivate::_k_sourceColumnsAboutToBeInserted(
const QModelIndex &parent,
int start,
int end)
515 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
516 q->beginInsertColumns(q->mapFromSource(parent), start,
end);
519 void KIdentityProxyModelPrivate::_k_sourceColumnsAboutToBeMoved(
const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd,
const QModelIndex &destParent,
int dest)
522 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == q->sourceModel() :
true);
523 Q_ASSERT(destParent.isValid() ? destParent.model() == q->sourceModel() :
true);
524 q->beginMoveColumns(q->mapFromSource(sourceParent), sourceStart, sourceEnd, q->mapFromSource(destParent), dest);
527 void KIdentityProxyModelPrivate::_k_sourceColumnsAboutToBeRemoved(
const QModelIndex &parent,
int start,
int end)
530 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
531 q->beginRemoveColumns(q->mapFromSource(parent), start,
end);
534 void KIdentityProxyModelPrivate::_k_sourceColumnsInserted(
const QModelIndex &parent,
int start,
int end)
537 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
541 q->endInsertColumns();
544 void KIdentityProxyModelPrivate::_k_sourceColumnsMoved(const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd, const QModelIndex &destParent,
int dest)
547 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == q->sourceModel() :
true);
548 Q_ASSERT(destParent.isValid() ? destParent.model() == q->sourceModel() :
true);
549 Q_UNUSED(sourceParent)
550 Q_UNUSED(sourceStart)
557 void KIdentityProxyModelPrivate::_k_sourceColumnsRemoved(const QModelIndex &parent,
int start,
int end)
560 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
564 q->endRemoveColumns();
567 void KIdentityProxyModelPrivate::_k_sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
570 Q_ASSERT(topLeft.model() == q->sourceModel());
571 Q_ASSERT(bottomRight.model() == q->sourceModel());
572 q->dataChanged(q->mapFromSource(topLeft), q->mapFromSource(bottomRight));
575 void KIdentityProxyModelPrivate::_k_sourceHeaderDataChanged(Qt::Orientation orientation,
int first,
int last)
578 q->headerDataChanged(orientation, first, last);
581 void KIdentityProxyModelPrivate::_k_sourceLayoutAboutToBeChanged()
583 if (ignoreNextLayoutAboutToBeChanged)
588 q->layoutAboutToBeChanged();
590 Q_FOREACH(
const QPersistentModelIndex &proxyPersistentIndex, q->persistentIndexList()) {
591 proxyIndexes << proxyPersistentIndex;
592 Q_ASSERT(proxyPersistentIndex.isValid());
593 const QPersistentModelIndex srcPersistentIndex = q->mapToSource(proxyPersistentIndex);
594 Q_ASSERT(srcPersistentIndex.isValid());
595 layoutChangePersistentIndexes << srcPersistentIndex;
599 void KIdentityProxyModelPrivate::_k_sourceLayoutChanged()
601 if (ignoreNextLayoutChanged)
610 const int columnCount = q->sourceModel()->columnCount();
611 for (
int i = 0; i < proxyIndexes.size(); ++i) {
612 const QModelIndex oldProxyIndex = proxyIndexes.at(i);
613 if (oldProxyIndex.column() < columnCount)
614 q->changePersistentIndex(oldProxyIndex, q->mapFromSource(layoutChangePersistentIndexes.at(i)));
617 layoutChangePersistentIndexes.clear();
618 proxyIndexes.clear();
624 void KIdentityProxyModelPrivate::_k_sourceChildrenLayoutsAboutToBeChanged(
const QModelIndex &parent1,
const QModelIndex &parent2)
627 Q_ASSERT(parent1.isValid() ? parent1.model() == q->sourceModel() :
true);
628 Q_ASSERT(parent2.isValid() ? parent2.model() == q->sourceModel() :
true);
631 ignoreNextLayoutAboutToBeChanged =
true;
633 const QModelIndex proxyParent1 = q->mapFromSource(parent1);
634 const QModelIndex proxyParent2 = q->mapFromSource(parent2);
636 emit q->layoutAboutToBeChanged();
638 if (q->persistentIndexList().isEmpty())
641 Q_FOREACH(
const QPersistentModelIndex &proxyPersistentIndex, q->persistentIndexList()) {
642 const QPersistentModelIndex srcPersistentIndex = q->mapToSource(proxyPersistentIndex);
643 Q_ASSERT(proxyPersistentIndex.isValid());
644 Q_ASSERT(srcPersistentIndex.isValid());
645 const QModelIndex idxParent = srcPersistentIndex.parent();
646 if (idxParent != parent1 && idxParent != parent2)
648 proxyIndexes << proxyPersistentIndex;
649 layoutChangePersistentIndexes << srcPersistentIndex;
653 void KIdentityProxyModelPrivate::_k_sourceChildrenLayoutsChanged(
const QModelIndex &parent1,
const QModelIndex &parent2)
656 Q_ASSERT(parent1.isValid() ? parent1.model() == q->sourceModel() :
true);
657 Q_ASSERT(parent2.isValid() ? parent2.model() == q->sourceModel() :
true);
659 ignoreNextLayoutChanged =
true;
661 QModelIndexList oldList, newList;
662 for(
int i = 0; i < layoutChangePersistentIndexes.size(); ++i) {
663 const QModelIndex srcIdx = layoutChangePersistentIndexes.at(i);
664 const QModelIndex oldProxyIdx = proxyIndexes.at(i);
665 oldList << oldProxyIdx;
666 newList << q->mapFromSource(srcIdx);
668 q->changePersistentIndexList(oldList, newList);
669 layoutChangePersistentIndexes.clear();
670 proxyIndexes.clear();
672 const QModelIndex proxyParent1 = q->mapFromSource(parent1);
673 const QModelIndex proxyParent2 = q->mapFromSource(parent2);
675 emit q->layoutChanged();
678 void KIdentityProxyModelPrivate::_k_sourceModelAboutToBeReset()
681 q->beginResetModel();
684 void KIdentityProxyModelPrivate::_k_sourceModelReset()
690 void KIdentityProxyModelPrivate::_k_sourceModelDestroyed()
696 void KIdentityProxyModelPrivate::_k_sourceRowsAboutToBeInserted(
const QModelIndex &parent,
int start,
int end)
699 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
700 q->beginInsertRows(q->mapFromSource(parent), start,
end);
703 void KIdentityProxyModelPrivate::_k_sourceRowsAboutToBeMoved(
const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd,
const QModelIndex &destParent,
int dest)
706 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == q->sourceModel() :
true);
707 Q_ASSERT(destParent.isValid() ? destParent.model() == q->sourceModel() :
true);
708 q->beginMoveRows(q->mapFromSource(sourceParent), sourceStart, sourceEnd, q->mapFromSource(destParent), dest);
711 void KIdentityProxyModelPrivate::_k_sourceRowsAboutToBeRemoved(
const QModelIndex &parent,
int start,
int end)
714 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
715 q->beginRemoveRows(q->mapFromSource(parent), start,
end);
718 void KIdentityProxyModelPrivate::_k_sourceRowsInserted(
const QModelIndex &parent,
int start,
int end)
721 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
728 void KIdentityProxyModelPrivate::_k_sourceRowsMoved(const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd, const QModelIndex &destParent,
int dest)
731 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == q->sourceModel() :
true);
732 Q_ASSERT(destParent.isValid() ? destParent.model() == q->sourceModel() :
true);
733 Q_UNUSED(sourceParent)
734 Q_UNUSED(sourceStart)
741 void KIdentityProxyModelPrivate::_k_sourceRowsRemoved(const QModelIndex &parent,
int start,
int end)
744 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
761 #include "kidentityproxymodel.moc"