24 #include <QtGui/QItemSelection>
25 #include <QtCore/QStringList>
28 class KIdentityProxyModelPrivate
43 QList<QPersistentModelIndex> layoutChangePersistentIndexes;
44 QModelIndexList proxyIndexes;
46 void _k_sourceRowsAboutToBeInserted(const QModelIndex &parent,
int start,
int end);
47 void _k_sourceRowsInserted(const QModelIndex &parent,
int start,
int end);
48 void _k_sourceRowsAboutToBeRemoved(const QModelIndex &parent,
int start,
int end);
49 void _k_sourceRowsRemoved(const QModelIndex &parent,
int start,
int end);
50 void _k_sourceRowsAboutToBeMoved(const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd, const QModelIndex &destParent,
int dest);
51 void _k_sourceRowsMoved(const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd, const QModelIndex &destParent,
int dest);
53 void _k_sourceColumnsAboutToBeInserted(const QModelIndex &parent,
int start,
int end);
54 void _k_sourceColumnsInserted(const QModelIndex &parent,
int start,
int end);
55 void _k_sourceColumnsAboutToBeRemoved(const QModelIndex &parent,
int start,
int end);
56 void _k_sourceColumnsRemoved(const QModelIndex &parent,
int start,
int end);
57 void _k_sourceColumnsAboutToBeMoved(const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd, const QModelIndex &destParent,
int dest);
58 void _k_sourceColumnsMoved(const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd, const QModelIndex &destParent,
int dest);
60 void _k_sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
61 void _k_sourceHeaderDataChanged(Qt::Orientation orientation,
int first,
int last);
63 void _k_sourceLayoutAboutToBeChanged();
64 void _k_sourceLayoutChanged();
67 void _k_sourceModelAboutToBeReset();
68 void _k_sourceModelReset();
69 void _k_sourceModelDestroyed();
152 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
153 return sourceModel()->canFetchMore(
mapToSource(parent));
163 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
164 return sourceModel()->columnCount(
mapToSource(parent));
174 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
185 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
186 return sourceModel()->dropMimeData(data, action, row, column,
mapToSource(parent));
195 return QModelIndex();
196 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
197 if (row < 0 || column < 0 || !hasIndex(row, column, parent))
198 return QModelIndex();
199 const QModelIndex sourceParent =
mapToSource(parent);
200 const QModelIndex sourceIndex = sourceModel()->index(row, column, sourceParent);
201 Q_ASSERT(sourceIndex.isValid());
212 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
213 return sourceModel()->insertColumns(column, count,
mapToSource(parent));
223 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
224 return sourceModel()->insertRows(row, count,
mapToSource(parent));
232 if (!sourceModel() || !sourceIndex.isValid())
233 return QModelIndex();
235 Q_ASSERT(sourceIndex.model() == sourceModel());
236 return createIndex(sourceIndex.row(), sourceIndex.column(), sourceIndex.internalPointer());
244 QItemSelection proxySelection;
247 return proxySelection;
249 QItemSelection::const_iterator it = selection.constBegin();
250 const QItemSelection::const_iterator
end = selection.constEnd();
251 for ( ; it !=
end; ++it) {
252 Q_ASSERT(it->model() == sourceModel());
254 proxySelection.append(range);
257 return proxySelection;
265 QItemSelection sourceSelection;
268 return sourceSelection;
270 QItemSelection::const_iterator it = selection.constBegin();
271 const QItemSelection::const_iterator
end = selection.constEnd();
272 for ( ; it !=
end; ++it) {
273 Q_ASSERT(it->model() ==
this);
275 sourceSelection.append(range);
278 return sourceSelection;
281 struct SourceModelIndex
284 : r(_r), c(_c), p(_p), m(_m)
289 operator QModelIndex() {
return reinterpret_cast<QModelIndex&
>(*this); }
301 if (!sourceModel() || !proxyIndex.isValid())
302 return QModelIndex();
303 Q_ASSERT(proxyIndex.model() ==
this);
304 return SourceModelIndex(proxyIndex.row(), proxyIndex.column(), proxyIndex.internalPointer(), sourceModel());
312 Q_ASSERT(start.isValid() ? start.model() ==
this :
true);
314 return QModelIndexList();
316 const QModelIndexList sourceList = sourceModel()->match(
mapToSource(start), role, value, hits, flags);
317 QModelIndexList::const_iterator it = sourceList.constBegin();
318 const QModelIndexList::const_iterator
end = sourceList.constEnd();
319 QModelIndexList proxyList;
320 for ( ; it !=
end; ++it)
331 return sourceModel()->mimeTypes();
333 return QAbstractProxyModel::mimeTypes();
339 return QAbstractProxyModel::mimeData(indexes);
341 QModelIndexList proxyIndexes;
342 foreach(
const QModelIndex &
index, indexes)
345 return sourceModel()->mimeData(proxyIndexes);
355 return QModelIndex();
357 Q_ASSERT(child.isValid() ? child.model() ==
this :
true);
358 const QModelIndex sourceIndex =
mapToSource(child);
359 const QModelIndex sourceParent = sourceIndex.parent();
371 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
372 return sourceModel()->removeColumns(column, count,
mapToSource(parent));
383 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
384 return sourceModel()->removeRows(row, count,
mapToSource(parent));
394 Q_ASSERT(parent.isValid() ? parent.model() ==
this :
true);
395 return sourceModel()->rowCount(
mapToSource(parent));
406 disconnect(sourceModel, SIGNAL(rowsAboutToBeInserted(QModelIndex,
int,
int)),
407 this, SLOT(_k_sourceRowsAboutToBeInserted(QModelIndex,
int,
int)));
408 disconnect(sourceModel, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
409 this, SLOT(_k_sourceRowsInserted(QModelIndex,
int,
int)));
410 disconnect(sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,
int,
int)),
411 this, SLOT(_k_sourceRowsAboutToBeRemoved(QModelIndex,
int,
int)));
412 disconnect(sourceModel, SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
413 this, SLOT(_k_sourceRowsRemoved(QModelIndex,
int,
int)));
414 disconnect(sourceModel, SIGNAL(rowsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)),
415 this, SLOT(_k_sourceRowsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)));
416 disconnect(sourceModel, SIGNAL(rowsMoved(QModelIndex,
int,
int,QModelIndex,
int)),
417 this, SLOT(_k_sourceRowsMoved(QModelIndex,
int,
int,QModelIndex,
int)));
418 disconnect(sourceModel, SIGNAL(columnsAboutToBeInserted(QModelIndex,
int,
int)),
419 this, SLOT(_k_sourceColumnsAboutToBeInserted(QModelIndex,
int,
int)));
420 disconnect(sourceModel, SIGNAL(columnsInserted(QModelIndex,
int,
int)),
421 this, SLOT(_k_sourceColumnsInserted(QModelIndex,
int,
int)));
422 disconnect(sourceModel, SIGNAL(columnsAboutToBeRemoved(QModelIndex,
int,
int)),
423 this, SLOT(_k_sourceColumnsAboutToBeRemoved(QModelIndex,
int,
int)));
424 disconnect(sourceModel, SIGNAL(columnsRemoved(QModelIndex,
int,
int)),
425 this, SLOT(_k_sourceColumnsRemoved(QModelIndex,
int,
int)));
426 disconnect(sourceModel, SIGNAL(columnsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)),
427 this, SLOT(_k_sourceColumnsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)));
428 disconnect(sourceModel, SIGNAL(columnsMoved(QModelIndex,
int,
int,QModelIndex,
int)),
429 this, SLOT(_k_sourceColumnsMoved(QModelIndex,
int,
int,QModelIndex,
int)));
430 disconnect(sourceModel, SIGNAL(modelAboutToBeReset()),
431 this, SLOT(_k_sourceModelAboutToBeReset()));
434 disconnect(sourceModel, SIGNAL(modelReset()),
435 this, SLOT(_k_sourceModelReset()));
436 disconnect(sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
437 this, SLOT(_k_sourceDataChanged(QModelIndex,QModelIndex)));
438 disconnect(sourceModel, SIGNAL(headerDataChanged(Qt::Orientation,
int,
int)),
439 this, SLOT(_k_sourceHeaderDataChanged(Qt::Orientation,
int,
int)));
440 disconnect(sourceModel, SIGNAL(layoutAboutToBeChanged()),
441 this, SLOT(_k_sourceLayoutAboutToBeChanged()));
442 disconnect(sourceModel, SIGNAL(layoutChanged()),
443 this, SLOT(_k_sourceLayoutChanged()));
448 disconnect(sourceModel, SIGNAL(destroyed()),
449 this, SLOT(_k_sourceModelDestroyed()));
452 QAbstractProxyModel::setSourceModel(sourceModel);
455 connect(sourceModel, SIGNAL(rowsAboutToBeInserted(QModelIndex,
int,
int)),
456 SLOT(_k_sourceRowsAboutToBeInserted(QModelIndex,
int,
int)));
457 connect(sourceModel, SIGNAL(rowsInserted(QModelIndex,
int,
int)),
458 SLOT(_k_sourceRowsInserted(QModelIndex,
int,
int)));
459 connect(sourceModel, SIGNAL(rowsAboutToBeRemoved(QModelIndex,
int,
int)),
460 SLOT(_k_sourceRowsAboutToBeRemoved(QModelIndex,
int,
int)));
461 connect(sourceModel, SIGNAL(rowsRemoved(QModelIndex,
int,
int)),
462 SLOT(_k_sourceRowsRemoved(QModelIndex,
int,
int)));
463 connect(sourceModel, SIGNAL(rowsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)),
464 SLOT(_k_sourceRowsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)));
465 connect(sourceModel, SIGNAL(rowsMoved(QModelIndex,
int,
int,QModelIndex,
int)),
466 SLOT(_k_sourceRowsMoved(QModelIndex,
int,
int,QModelIndex,
int)));
467 connect(sourceModel, SIGNAL(columnsAboutToBeInserted(QModelIndex,
int,
int)),
468 SLOT(_k_sourceColumnsAboutToBeInserted(QModelIndex,
int,
int)));
469 connect(sourceModel, SIGNAL(columnsInserted(QModelIndex,
int,
int)),
470 SLOT(_k_sourceColumnsInserted(QModelIndex,
int,
int)));
471 connect(sourceModel, SIGNAL(columnsAboutToBeRemoved(QModelIndex,
int,
int)),
472 SLOT(_k_sourceColumnsAboutToBeRemoved(QModelIndex,
int,
int)));
473 connect(sourceModel, SIGNAL(columnsRemoved(QModelIndex,
int,
int)),
474 SLOT(_k_sourceColumnsRemoved(QModelIndex,
int,
int)));
475 connect(sourceModel, SIGNAL(columnsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)),
476 SLOT(_k_sourceColumnsAboutToBeMoved(QModelIndex,
int,
int,QModelIndex,
int)));
477 connect(sourceModel, SIGNAL(columnsMoved(QModelIndex,
int,
int,QModelIndex,
int)),
478 SLOT(_k_sourceColumnsMoved(QModelIndex,
int,
int,QModelIndex,
int)));
479 connect(sourceModel, SIGNAL(modelAboutToBeReset()),
480 SLOT(_k_sourceModelAboutToBeReset()));
483 connect(sourceModel, SIGNAL(modelReset()),
484 SLOT(_k_sourceModelReset()));
485 connect(sourceModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
486 SLOT(_k_sourceDataChanged(QModelIndex,QModelIndex)));
487 connect(sourceModel, SIGNAL(headerDataChanged(Qt::Orientation,
int,
int)),
488 SLOT(_k_sourceHeaderDataChanged(Qt::Orientation,
int,
int)));
489 connect(sourceModel, SIGNAL(layoutAboutToBeChanged()),
490 SLOT(_k_sourceLayoutAboutToBeChanged()));
491 connect(sourceModel, SIGNAL(layoutChanged()),
492 SLOT(_k_sourceLayoutChanged()));
498 connect(sourceModel, SIGNAL(destroyed()),
499 SLOT(_k_sourceModelDestroyed()));
508 return sourceModel()->supportedDropActions();
510 return QAbstractProxyModel::supportedDropActions();
513 void KIdentityProxyModelPrivate::_k_sourceColumnsAboutToBeInserted(
const QModelIndex &parent,
int start,
int end)
516 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
517 q->beginInsertColumns(q->mapFromSource(parent), start,
end);
520 void KIdentityProxyModelPrivate::_k_sourceColumnsAboutToBeMoved(
const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd,
const QModelIndex &destParent,
int dest)
523 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == q->sourceModel() :
true);
524 Q_ASSERT(destParent.isValid() ? destParent.model() == q->sourceModel() :
true);
525 q->beginMoveColumns(q->mapFromSource(sourceParent), sourceStart, sourceEnd, q->mapFromSource(destParent), dest);
528 void KIdentityProxyModelPrivate::_k_sourceColumnsAboutToBeRemoved(
const QModelIndex &parent,
int start,
int end)
531 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
532 q->beginRemoveColumns(q->mapFromSource(parent), start,
end);
535 void KIdentityProxyModelPrivate::_k_sourceColumnsInserted(
const QModelIndex &parent,
int start,
int end)
538 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
542 q->endInsertColumns();
545 void KIdentityProxyModelPrivate::_k_sourceColumnsMoved(const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd, const QModelIndex &destParent,
int dest)
548 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == q->sourceModel() :
true);
549 Q_ASSERT(destParent.isValid() ? destParent.model() == q->sourceModel() :
true);
550 Q_UNUSED(sourceParent)
551 Q_UNUSED(sourceStart)
558 void KIdentityProxyModelPrivate::_k_sourceColumnsRemoved(const QModelIndex &parent,
int start,
int end)
561 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
565 q->endRemoveColumns();
568 void KIdentityProxyModelPrivate::_k_sourceDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
571 Q_ASSERT(topLeft.model() == q->sourceModel());
572 Q_ASSERT(bottomRight.model() == q->sourceModel());
573 q->dataChanged(q->mapFromSource(topLeft), q->mapFromSource(bottomRight));
576 void KIdentityProxyModelPrivate::_k_sourceHeaderDataChanged(Qt::Orientation orientation,
int first,
int last)
579 q->headerDataChanged(orientation, first, last);
582 void KIdentityProxyModelPrivate::_k_sourceLayoutAboutToBeChanged()
589 q->layoutAboutToBeChanged();
591 Q_FOREACH(
const QModelIndex &proxyPersistentIndex, q->persistentIndexList()) {
592 Q_ASSERT(proxyPersistentIndex.isValid());
593 const QPersistentModelIndex srcPersistentIndex = q->mapToSource(proxyPersistentIndex);
594 if (!srcPersistentIndex.isValid())
596 proxyIndexes << proxyPersistentIndex;
597 layoutChangePersistentIndexes << srcPersistentIndex;
601 void KIdentityProxyModelPrivate::_k_sourceLayoutChanged()
608 for (
int i = 0; i < proxyIndexes.size(); ++i) {
609 const QModelIndex oldProxyIndex = proxyIndexes.at(i);
610 const QModelIndex newProxyIndex = q->mapFromSource(layoutChangePersistentIndexes.at(i));
611 if (oldProxyIndex != newProxyIndex)
612 q->changePersistentIndex(oldProxyIndex, newProxyIndex);
615 layoutChangePersistentIndexes.clear();
616 proxyIndexes.clear();
621 #if 0 // this code was for the stuff that never went into Qt-4.8. We are keeping it for the Qt5 QIPM sourceLayoutChanged(QModelIndex) future code.
622 void KIdentityProxyModelPrivate::_k_sourceChildrenLayoutsAboutToBeChanged(
const QModelIndex &parent1,
const QModelIndex &parent2)
625 Q_ASSERT(parent1.isValid() ? parent1.model() == q->sourceModel() :
true);
626 Q_ASSERT(parent2.isValid() ? parent2.model() == q->sourceModel() :
true);
629 ignoreNextLayoutAboutToBeChanged =
true;
631 const QModelIndex proxyParent1 = q->mapFromSource(parent1);
632 const QModelIndex proxyParent2 = q->mapFromSource(parent2);
634 emit q->layoutAboutToBeChanged();
636 if (q->persistentIndexList().isEmpty())
639 Q_FOREACH(
const QModelIndex &proxyPersistentIndex, q->persistentIndexList()) {
640 const QPersistentModelIndex srcPersistentIndex = q->mapToSource(proxyPersistentIndex);
641 Q_ASSERT(proxyPersistentIndex.isValid());
642 Q_ASSERT(srcPersistentIndex.isValid());
643 const QModelIndex idxParent = srcPersistentIndex.parent();
644 if (idxParent != parent1 && idxParent != parent2)
646 proxyIndexes << proxyPersistentIndex;
647 layoutChangePersistentIndexes << srcPersistentIndex;
651 void KIdentityProxyModelPrivate::_k_sourceChildrenLayoutsChanged(
const QModelIndex &parent1,
const QModelIndex &parent2)
654 Q_ASSERT(parent1.isValid() ? parent1.model() == q->sourceModel() :
true);
655 Q_ASSERT(parent2.isValid() ? parent2.model() == q->sourceModel() :
true);
657 ignoreNextLayoutChanged =
true;
659 QModelIndexList oldList, newList;
660 for(
int i = 0; i < layoutChangePersistentIndexes.size(); ++i) {
661 const QModelIndex srcIdx = layoutChangePersistentIndexes.at(i);
662 const QModelIndex oldProxyIdx = proxyIndexes.at(i);
663 oldList << oldProxyIdx;
664 newList << q->mapFromSource(srcIdx);
666 q->changePersistentIndexList(oldList, newList);
667 layoutChangePersistentIndexes.clear();
668 proxyIndexes.clear();
670 const QModelIndex proxyParent1 = q->mapFromSource(parent1);
671 const QModelIndex proxyParent2 = q->mapFromSource(parent2);
673 emit q->layoutChanged();
677 void KIdentityProxyModelPrivate::_k_sourceModelAboutToBeReset()
680 q->beginResetModel();
683 void KIdentityProxyModelPrivate::_k_sourceModelReset()
689 void KIdentityProxyModelPrivate::_k_sourceModelDestroyed()
695 void KIdentityProxyModelPrivate::_k_sourceRowsAboutToBeInserted(
const QModelIndex &parent,
int start,
int end)
698 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
699 q->beginInsertRows(q->mapFromSource(parent), start,
end);
702 void KIdentityProxyModelPrivate::_k_sourceRowsAboutToBeMoved(
const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd,
const QModelIndex &destParent,
int dest)
705 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == q->sourceModel() :
true);
706 Q_ASSERT(destParent.isValid() ? destParent.model() == q->sourceModel() :
true);
707 q->beginMoveRows(q->mapFromSource(sourceParent), sourceStart, sourceEnd, q->mapFromSource(destParent), dest);
710 void KIdentityProxyModelPrivate::_k_sourceRowsAboutToBeRemoved(
const QModelIndex &parent,
int start,
int end)
713 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
714 q->beginRemoveRows(q->mapFromSource(parent), start,
end);
717 void KIdentityProxyModelPrivate::_k_sourceRowsInserted(
const QModelIndex &parent,
int start,
int end)
720 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
727 void KIdentityProxyModelPrivate::_k_sourceRowsMoved(const QModelIndex &sourceParent,
int sourceStart,
int sourceEnd, const QModelIndex &destParent,
int dest)
730 Q_ASSERT(sourceParent.isValid() ? sourceParent.model() == q->sourceModel() :
true);
731 Q_ASSERT(destParent.isValid() ? destParent.model() == q->sourceModel() :
true);
732 Q_UNUSED(sourceParent)
733 Q_UNUSED(sourceStart)
740 void KIdentityProxyModelPrivate::_k_sourceRowsRemoved(const QModelIndex &parent,
int start,
int end)
743 Q_ASSERT(parent.isValid() ? parent.model() == q->sourceModel() :
true);
760 #include "kidentityproxymodel.moc"
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
The KIdentityProxyModel class proxies its source model unmodified.
bool insertColumns(int column, int count, const QModelIndex &parent=QModelIndex())
virtual void fetchMore(const QModelIndex &parent)
QItemSelection mapSelectionToSource(const QItemSelection &selection) const
virtual ~KIdentityProxyModel()
virtual QStringList mimeTypes() const
QModelIndex mapFromSource(const QModelIndex &sourceIndex) const
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
bool removeColumns(int column, int count, const QModelIndex &parent=QModelIndex())
void setSourceModel(QAbstractItemModel *sourceModel)
QModelIndex mapToSource(const QModelIndex &proxyIndex) const
virtual QMimeData * mimeData(const QModelIndexList &indexes) const
QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits=1, Qt::MatchFlags flags=Qt::MatchFlags(Qt::MatchStartsWith|Qt::MatchWrap)) const
KIdentityProxyModel(QObject *parent=0)
virtual bool canFetchMore(const QModelIndex &parent) const
int columnCount(const QModelIndex &parent=QModelIndex()) const
virtual Qt::DropActions supportedDropActions() const
KIdentityProxyModelPrivate *const d_ptr
QModelIndex parent(const QModelIndex &child) const
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
const KShortcut & end()
Goto end of the document.
QItemSelection mapSelectionFromSource(const QItemSelection &selection) const
int rowCount(const QModelIndex &parent=QModelIndex()) const
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex())