KOSMIndoorMap

amenitysortfilterproxymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2023 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "amenitysortfilterproxymodel.h"
7#include "amenitymodel.h"
8
9using namespace KOSMIndoorMap;
10
11AmenitySortFilterProxyModel::AmenitySortFilterProxyModel(QObject *parent)
12 : QSortFilterProxyModel(parent)
13 , m_collator(QLocale())
14{
15 setDynamicSortFilter(true);
16
17 m_collator.setCaseSensitivity(Qt::CaseInsensitive);
18 m_collator.setIgnorePunctuation(true);
19
20 connect(this, &QAbstractProxyModel::sourceModelChanged, this, [this]() { sort(0); });
21 connect(this, &AmenitySortFilterProxyModel::filterStringChanged, this, &QSortFilterProxyModel::invalidate);
22}
23
24AmenitySortFilterProxyModel::~AmenitySortFilterProxyModel() = default;
25
26
27bool AmenitySortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
28{
29 if (m_filter.isEmpty()) {
30 return true;
31 }
32
33 const auto idx = sourceModel()->index(source_row, 0, source_parent);
34 return filterMatches(idx.data(AmenityModel::NameRole).toString())
35 || filterMatches(idx.data(AmenityModel::TypeNameRole).toString())
36 || filterMatches(idx.data(AmenityModel::GroupNameRole).toString())
37 || filterMatches(idx.data(AmenityModel::FallbackNameRole).toString())
38 || filterMatches(idx.data(AmenityModel::CuisineRole).toString());
39}
40
41bool AmenitySortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
42{
43 const auto lhsGroup = source_left.data(AmenityModel::GroupRole).toInt();
44 const auto rhsGroup = source_right.data(AmenityModel::GroupRole).toInt();
45 if (lhsGroup == rhsGroup) {
46 auto lhsTitle = source_left.data(AmenityModel::NameRole).toString();
47 if (lhsTitle.isEmpty()) {
48 lhsTitle = source_left.data(AmenityModel::TypeNameRole).toString();
49 }
50 auto rhsTitle = source_right.data(AmenityModel::NameRole).toString();
51 if (rhsTitle.isEmpty()) {
52 rhsTitle = source_right.data(AmenityModel::TypeNameRole).toString();
53 }
54 return m_collator.compare(lhsTitle, rhsTitle) < 0;
55 }
56 return lhsGroup < rhsGroup;
57}
58
59bool AmenitySortFilterProxyModel::filterMatches(const QString &s) const
60{
61 return s.contains(m_filter, Qt::CaseInsensitive); // TODO ignore diacritics
62}
63
64#include "moc_amenitysortfilterproxymodel.cpp"
@ CuisineRole
details on entries in the FoodGroup
@ FallbackNameRole
Brand/operator/network name, better than nothing but not the first choice to display.
OSM-based multi-floor indoor maps for buildings.
int compare(QStringView s1, QStringView s2) const const
QVariant data(int role) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
CaseInsensitive
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
int toInt(bool *ok) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:14:31 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.