File indexing completed on 2024-05-12 04:42:08
0001 /* 0002 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org> 0003 SPDX-License-Identifier: LGPL-2.0-or-later 0004 */ 0005 0006 #include "amenitysortfilterproxymodel.h" 0007 #include "amenitymodel.h" 0008 0009 using namespace KOSMIndoorMap; 0010 0011 AmenitySortFilterProxyModel::AmenitySortFilterProxyModel(QObject *parent) 0012 : QSortFilterProxyModel(parent) 0013 , m_collator(QLocale()) 0014 { 0015 setDynamicSortFilter(true); 0016 0017 m_collator.setCaseSensitivity(Qt::CaseInsensitive); 0018 m_collator.setIgnorePunctuation(true); 0019 0020 connect(this, &QAbstractProxyModel::sourceModelChanged, this, [this]() { sort(0); }); 0021 connect(this, &AmenitySortFilterProxyModel::filterStringChanged, this, &QSortFilterProxyModel::invalidate); 0022 } 0023 0024 AmenitySortFilterProxyModel::~AmenitySortFilterProxyModel() = default; 0025 0026 0027 bool AmenitySortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const 0028 { 0029 if (m_filter.isEmpty()) { 0030 return true; 0031 } 0032 0033 const auto idx = sourceModel()->index(source_row, 0, source_parent); 0034 return filterMatches(idx.data(AmenityModel::NameRole).toString()) 0035 || filterMatches(idx.data(AmenityModel::TypeNameRole).toString()) 0036 || filterMatches(idx.data(AmenityModel::GroupNameRole).toString()) 0037 || filterMatches(idx.data(AmenityModel::FallbackNameRole).toString()) 0038 || filterMatches(idx.data(AmenityModel::CuisineRole).toString()); 0039 } 0040 0041 bool AmenitySortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const 0042 { 0043 const auto lhsGroup = source_left.data(AmenityModel::GroupRole).toInt(); 0044 const auto rhsGroup = source_right.data(AmenityModel::GroupRole).toInt(); 0045 if (lhsGroup == rhsGroup) { 0046 auto lhsTitle = source_left.data(AmenityModel::NameRole).toString(); 0047 if (lhsTitle.isEmpty()) { 0048 lhsTitle = source_left.data(AmenityModel::TypeNameRole).toString(); 0049 } 0050 auto rhsTitle = source_right.data(AmenityModel::NameRole).toString(); 0051 if (rhsTitle.isEmpty()) { 0052 rhsTitle = source_right.data(AmenityModel::TypeNameRole).toString(); 0053 } 0054 return m_collator.compare(lhsTitle, rhsTitle) < 0; 0055 } 0056 return lhsGroup < rhsGroup; 0057 } 0058 0059 bool AmenitySortFilterProxyModel::filterMatches(const QString &s) const 0060 { 0061 return s.contains(m_filter, Qt::CaseInsensitive); // TODO ignore diacritics 0062 } 0063 0064 #include "moc_amenitysortfilterproxymodel.cpp"