From 18400bcf2b17f1d9b7871f4985b1bccbb3384cf5 Mon Sep 17 00:00:00 2001 From: "Maciej S. Szmigiero" Date: Sat, 21 Sep 2019 17:22:05 +0200 Subject: Restore directories-first order of passwords tree view on non-Mac platforms Since commit b4dc9e69e7c553 ("Auto update CHANGELOG and sorting of treeview") directories are no longer listed first in the tree view of passwords. This is because a simple sort of the tree view widget (as enabled by the aforementioned commit) does its work by sorting the backing StoreModel (QSortFilterProxyModel), which in turn does just a simple lexicographical order sort on the path of each its item. Before that commit the sort was done by QFileSystemModel via QFileSystemModelSorter, which always places its directories first on non-Mac platforms. Unfortunately, QFileSystemModelSorter is an internal Qt helper class, so we can't just use it directly, we need to open-code a bit of logic from QFileSystemModelSorter::compareNodes() into StoreModel::lessThan() to restore the old behavior. --- src/storemodel.cpp | 16 ++++++++++++++++ src/storemodel.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/src/storemodel.cpp b/src/storemodel.cpp index b6fef94c..06c3c036 100644 --- a/src/storemodel.cpp +++ b/src/storemodel.cpp @@ -256,3 +256,19 @@ bool StoreModel::dropMimeData(const QMimeData *data, Qt::DropAction action, } return true; } + +bool StoreModel::lessThan(const QModelIndex &source_left, + const QModelIndex &source_right) const { +/* matches logic in QFileSystemModelSorter::compareNodes() */ +#ifndef Q_OS_MAC + if (fs && (source_left.column() == 0 || source_left.column() == 1)) { + bool leftD = fs->isDir(source_left); + bool rightD = fs->isDir(source_right); + + if (leftD ^ rightD) + return leftD; + } +#endif + + return QSortFilterProxyModel::lessThan(source_left, source_right); +} diff --git a/src/storemodel.h b/src/storemodel.h index 66486b10..6c8053e9 100644 --- a/src/storemodel.h +++ b/src/storemodel.h @@ -23,6 +23,8 @@ public: bool ShowThis(const QModelIndex) const; void setModelAndStore(QFileSystemModel *sourceModel, QString passStore); QVariant data(const QModelIndex &index, int role) const; + bool lessThan(const QModelIndex &source_left, + const QModelIndex &source_right) const override; // QAbstractItemModel interface public: -- cgit v1.2.3