Beispiel #1
0
QModelIndex FlatProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
    if (!proxyIndex.isValid())
        return QModelIndex();

    Q_ASSERT(proxyIndex.model() == this);
    Q_ASSERT(_rootSourceItem);

    int row = proxyIndex.row();
    QModelIndex sourceParent;
    SourceItem *sourceItem = _rootSourceItem->findChild(row);
    while (sourceItem) {
        if (sourceItem->pos() == row) {
            return sourceModel()->index(sourceItem->sourceRow(), proxyIndex.column(), sourceParent);
        }
        else {
            sourceParent = sourceModel()->index(sourceItem->sourceRow(), 0, sourceParent);
            sourceItem = sourceItem->findChild(row);
        }
    }

    qWarning() << "FlatProxyModel::mapToSource(): couldn't find source index for" << proxyIndex;
    Q_ASSERT(false);
    return QModelIndex(); // make compilers happy :)
}
Beispiel #2
0
QModelIndex FlatProxyModel::index(int row, int column, const QModelIndex &parent) const
{
    if (parent.isValid()) {
        qWarning() << "FlatProxyModel::index() called with valid parent:" << parent;
        return QModelIndex();
    }

    if (!_rootSourceItem) {
        qWarning() << "FlatProxyModel::index() while model has no root Item";
        return QModelIndex();
    }

    SourceItem *item = _rootSourceItem;
    while (item->pos() != row) {
        item = item->findChild(row);
        if (!item) {
            qWarning() << "FlatProxyModel::index() no such row:" << row;
            return QModelIndex();
        }
    }

    Q_ASSERT(item->pos() == row);
    return createIndex(row, column, item);
}