File indexing completed on 2024-05-19 12:06:06
0001 // SPDX-FileCopyrightText: 2003 - 2010 Jesper K. Pedersen <blackie@kde.org> 0002 // SPDX-FileCopyrightText: 2023 Johannes Zarl-Zierl <johannes@zarl-zierl.at> 0003 // 0004 // SPDX-License-Identifier: GPL-2.0-or-later 0005 0006 #ifndef TREEFILTER_H 0007 #define TREEFILTER_H 0008 #include <QSortFilterProxyModel> 0009 #include <memory> 0010 0011 class QCollator; 0012 0013 namespace Browser 0014 { 0015 /** 0016 * \brief Filter proxy that keeps parent branches if child branches matches 0017 * Additionally, it supports changing the sort order from QString comparison to localized numeric sort (aka natural sort order). 0018 * 0019 * See \ref Browser for a detailed description of how this fits in with the rest of the classes in this module 0020 * 0021 * The QSortFilterProxyModel has one drawback that makes it inappropriate to 0022 * use for filtering in the browser, namely that it hides an item, if the item 0023 * doesn't match the filter criteria, even if child items actually do match 0024 * the filter criteria. This class overcomes this shortcoming. 0025 */ 0026 class TreeFilter : public QSortFilterProxyModel 0027 { 0028 public: 0029 explicit TreeFilter(QObject *parent = nullptr); 0030 void resetCache(); 0031 0032 /** 0033 * @brief setNaturalSortOrder sets the sort order to be natural or not. 0034 * "Natural" sort order sorts according to the current locale and properly sorts numeric values, so that e.g. 2 sorts before 10. 0035 * 0036 * Note: Natural sort order is more expensive than string comparison, so even if it defaults to being enabled, it needs to be user-configurable for people with large databases. 0037 * @param naturalSortOrder 0038 */ 0039 void setNaturalSortOrder(bool naturalSortOrder); 0040 0041 /** 0042 * @brief naturalSortOrder 0043 * @return \c true, if natural sort order is enabled, \c false otherwise. 0044 */ 0045 bool naturalSortOrder() const; 0046 0047 protected: 0048 bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override; 0049 /** 0050 * @brief lessThan overrides QSortFilterProxyModel::lessThan to enable "natural" sorting of numeric values. 0051 * @see setNaturalSort(bool) 0052 */ 0053 bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override; 0054 0055 mutable QMap<QModelIndex, bool> m_matchedMap; 0056 0057 private: 0058 bool m_naturalSortOrder = true; 0059 std::unique_ptr<QCollator> m_naturalCollator; 0060 }; 0061 } 0062 0063 #endif /* TREEFILTER_H */ 0064 0065 // vi:expandtab:tabstop=4 shiftwidth=4: