diff options
Diffstat (limited to 'src/widgets/dialogs/qfilesystemmodel.cpp')
-rw-r--r-- | src/widgets/dialogs/qfilesystemmodel.cpp | 86 |
1 files changed, 34 insertions, 52 deletions
diff --git a/src/widgets/dialogs/qfilesystemmodel.cpp b/src/widgets/dialogs/qfilesystemmodel.cpp index 4859231d95..67af7f8107 100644 --- a/src/widgets/dialogs/qfilesystemmodel.cpp +++ b/src/widgets/dialogs/qfilesystemmodel.cpp @@ -243,7 +243,7 @@ QModelIndex QFileSystemModel::index(int row, int column, const QModelIndex &pare Q_ASSERT(parentNode); // now get the internal pointer for the index - QString childName = parentNode->visibleChildren[d->translateVisibleLocation(parentNode, row)]; + const QString &childName = parentNode->visibleChildren.at(d->translateVisibleLocation(parentNode, row)); const QFileSystemModelPrivate::QFileSystemNode *indexNode = parentNode->children.value(childName); Q_ASSERT(indexNode); @@ -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); } /*! @@ -375,7 +372,7 @@ QFileSystemModelPrivate::QFileSystemNode *QFileSystemModelPrivate::node(const QS #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) { - if (!pathElements.at(0).contains(QLatin1String(":"))) { + if (!pathElements.at(0).contains(QLatin1Char(':'))) { QString rootPath = QDir(longPath).rootPath(); pathElements.prepend(rootPath); } @@ -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)); } /*! @@ -643,7 +640,7 @@ int QFileSystemModel::rowCount(const QModelIndex &parent) const */ int QFileSystemModel::columnCount(const QModelIndex &parent) const { - return (parent.column() > 0) ? 0 : 4; + return (parent.column() > 0) ? 0 : QFileSystemModelPrivate::NumColumns; } /*! @@ -799,8 +796,7 @@ QString QFileSystemModelPrivate::name(const QModelIndex &index) const #endif !resolvedSymLinks.isEmpty() && dirNode->isSymLink(/* ignoreNtfsSymLinks = */ true)) { QString fullPath = QDir::fromNativeSeparators(filePath(index)); - if (resolvedSymLinks.contains(fullPath)) - return resolvedSymLinks[fullPath]; + return resolvedSymLinks.value(fullPath, dirNode->fileName); } return dirNode->fileName; } @@ -1115,10 +1111,10 @@ public: return false; } - bool operator()(const QPair<QFileSystemModelPrivate::QFileSystemNode*, int> &l, - const QPair<QFileSystemModelPrivate::QFileSystemNode*, int> &r) const + bool operator()(const QFileSystemModelPrivate::QFileSystemNode *l, + const QFileSystemModelPrivate::QFileSystemNode *r) const { - return compareNodes(l.first, r.first); + return compareNodes(l, r); } @@ -1138,16 +1134,14 @@ void QFileSystemModelPrivate::sortChildren(int column, const QModelIndex &parent if (indexNode->children.count() == 0) return; - QList<QPair<QFileSystemModelPrivate::QFileSystemNode*, int> > values; + QVector<QFileSystemModelPrivate::QFileSystemNode*> values; QHash<QString, QFileSystemNode *>::const_iterator iterator; - int i = 0; for(iterator = indexNode->children.constBegin() ; iterator != indexNode->children.constEnd() ; ++iterator) { if (filtersAcceptsNode(iterator.value())) { - values.append(QPair<QFileSystemModelPrivate::QFileSystemNode*, int>((iterator.value()), i)); + values.append(iterator.value()); } else { iterator.value()->isVisible = false; } - i++; } QFileSystemModelSorter ms(column); std::sort(values.begin(), values.end(), ms); @@ -1155,9 +1149,11 @@ void QFileSystemModelPrivate::sortChildren(int column, const QModelIndex &parent indexNode->visibleChildren.clear(); //No more dirty item we reset our internal dirty index indexNode->dirtyChildrenIndex = -1; - for (int i = 0; i < values.count(); ++i) { - indexNode->visibleChildren.append(values.at(i).first->fileName); - values.at(i).first->isVisible = true; + const int numValues = values.count(); + indexNode->visibleChildren.reserve(numValues); + for (int i = 0; i < numValues; ++i) { + indexNode->visibleChildren.append(values.at(i)->fileName); + values.at(i)->isVisible = true; } if (!disableRecursiveSort) { @@ -1183,8 +1179,11 @@ void QFileSystemModel::sort(int column, Qt::SortOrder order) emit layoutAboutToBeChanged(); QModelIndexList oldList = persistentIndexList(); QList<QPair<QFileSystemModelPrivate::QFileSystemNode*, int> > oldNodes; - for (int i = 0; i < oldList.count(); ++i) { - QPair<QFileSystemModelPrivate::QFileSystemNode*, int> pair(d->node(oldList.at(i)), oldList.at(i).column()); + const int nodeCount = oldList.count(); + oldNodes.reserve(nodeCount); + for (int i = 0; i < nodeCount; ++i) { + const QModelIndex &oldNode = oldList.at(i); + QPair<QFileSystemModelPrivate::QFileSystemNode*, int> pair(d->node(oldNode), oldNode.column()); oldNodes.append(pair); } @@ -1197,10 +1196,11 @@ void QFileSystemModel::sort(int column, Qt::SortOrder order) d->sortOrder = order; QModelIndexList newList; - for (int i = 0; i < oldNodes.count(); ++i) { - QModelIndex idx = d->index(oldNodes.at(i).first); - idx = idx.sibling(idx.row(), oldNodes.at(i).second); - newList.append(idx); + const int numOldNodes = oldNodes.size(); + newList.reserve(numOldNodes); + for (int i = 0; i < numOldNodes; ++i) { + const QPair<QFileSystemModelPrivate::QFileSystemNode*, int> &oldNode = oldNodes.at(i); + newList.append(d->index(oldNode.first, oldNode.second)); } changePersistentIndexList(oldList, newList); emit layoutChanged(); @@ -1648,7 +1648,9 @@ QStringList QFileSystemModel::nameFilters() const Q_D(const QFileSystemModel); QStringList filters; #ifndef QT_NO_REGEXP - for (int i = 0; i < d->nameFilters.size(); ++i) { + const int numNameFilters = d->nameFilters.size(); + filters.reserve(numNameFilters); + for (int i = 0; i < numNameFilters; ++i) { filters << d->nameFilters.at(i).pattern(); } #endif @@ -1765,26 +1767,6 @@ void QFileSystemModelPrivate::removeNode(QFileSystemModelPrivate::QFileSystemNod q->endRemoveRows(); } -/* - \internal - Helper functor used by addVisibleFiles() -*/ -class QFileSystemModelVisibleFinder -{ -public: - inline QFileSystemModelVisibleFinder(QFileSystemModelPrivate::QFileSystemNode *node, QFileSystemModelSorter *sorter) : parentNode(node), sorter(sorter) {} - - bool operator()(const QString &, QString r) const - { - return sorter->compareNodes(parentNode->children.value(name), parentNode->children.value(r)); - } - - QString name; -private: - QFileSystemModelPrivate::QFileSystemNode *parentNode; - QFileSystemModelSorter *sorter; -}; - /*! \internal @@ -1806,9 +1788,9 @@ void QFileSystemModelPrivate::addVisibleFiles(QFileSystemNode *parentNode, const parentNode->dirtyChildrenIndex = parentNode->visibleChildren.count(); for (int i = 0; i < newFiles.count(); ++i) { - parentNode->visibleChildren.append(newFiles.at(i)); - parentNode->children[newFiles.at(i)]->isVisible = true; - } + parentNode->visibleChildren.append(newFiles.at(i)); + parentNode->children.value(newFiles.at(i))->isVisible = true; + } if (!indexHidden) q->endInsertRows(); } @@ -1830,7 +1812,7 @@ void QFileSystemModelPrivate::removeVisibleFile(QFileSystemNode *parentNode, int if (!indexHidden) q->beginRemoveRows(parent, translateVisibleLocation(parentNode, vLocation), translateVisibleLocation(parentNode, vLocation)); - parentNode->children[parentNode->visibleChildren.at(vLocation)]->isVisible = false; + parentNode->children.value(parentNode->visibleChildren.at(vLocation))->isVisible = false; parentNode->visibleChildren.removeAt(vLocation); if (!indexHidden) q->endRemoveRows(); |