QFileSystemModel: avoid sibling() calls

...by allowing to pass the column to the Private::index() overloads.

Because Private::index() always returns an index in column 0,
callers that needed a different column used QModelIndex::sibling()
to adjust the column of the returned index. But that calls
QAIM::sibling(), which calls both QFSM::index() and ::parent().

Simply allowing to pass the column number instead of hard-coding
0 avoids that heavy detour.

Change-Id: I8895b3d102d576ba291333cf61075b7263f96b9d
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Reviewed-by: David Faure <david.faure@kdab.com>
bb10
Marc Mutz 2015-01-29 12:13:47 +01:00
parent ce7fb157f0
commit 648a496e5e
2 changed files with 6 additions and 11 deletions

View File

@ -259,10 +259,7 @@ QModelIndex QFileSystemModel::index(const QString &path, int column) const
{
Q_D(const QFileSystemModel);
QFileSystemModelPrivate::QFileSystemNode *node = d->node(path, false);
QModelIndex idx = d->index(node);
if (idx.column() != column)
idx = idx.sibling(idx.row(), column);
return idx;
return d->index(node, column);
}
/*!
@ -562,7 +559,7 @@ QModelIndex QFileSystemModel::parent(const QModelIndex &index) const
return the index for node
*/
QModelIndex QFileSystemModelPrivate::index(const QFileSystemModelPrivate::QFileSystemNode *node) const
QModelIndex QFileSystemModelPrivate::index(const QFileSystemModelPrivate::QFileSystemNode *node, int column) const
{
Q_Q(const QFileSystemModel);
QFileSystemModelPrivate::QFileSystemNode *parentNode = (node ? node->parent : 0);
@ -575,7 +572,7 @@ QModelIndex QFileSystemModelPrivate::index(const QFileSystemModelPrivate::QFileS
return QModelIndex();
int visualRow = translateVisibleLocation(parentNode, parentNode->visibleLocation(node->fileName));
return q->createIndex(visualRow, 0, const_cast<QFileSystemNode*>(node));
return q->createIndex(visualRow, column, const_cast<QFileSystemNode*>(node));
}
/*!
@ -1202,9 +1199,7 @@ void QFileSystemModel::sort(int column, Qt::SortOrder order)
QModelIndexList newList;
for (int i = 0; i < nodeCount; ++i) {
const QPair<QFileSystemModelPrivate::QFileSystemNode*, int> &oldNode = oldNodes.at(i);
QModelIndex idx = d->index(oldNode.first);
idx = idx.sibling(idx.row(), oldNode.second);
newList.append(idx);
newList.append(d->index(oldNode.first, oldNode.second));
}
changePersistentIndexList(oldList, newList);
emit layoutChanged();

View File

@ -222,8 +222,8 @@ public:
}
QFileSystemNode *node(const QModelIndex &index) const;
QFileSystemNode *node(const QString &path, bool fetch = true) const;
inline QModelIndex index(const QString &path) { return index(node(path)); }
QModelIndex index(const QFileSystemNode *node) const;
inline QModelIndex index(const QString &path, int column = 0) { return index(node(path), column); }
QModelIndex index(const QFileSystemNode *node, int column = 0) const;
bool filtersAcceptsNode(const QFileSystemNode *node) const;
bool passNameFilters(const QFileSystemNode *node) const;
void removeNode(QFileSystemNode *parentNode, const QString &name);