KOSMIndoorMap

roomsortfilterproxymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "roomsortfilterproxymodel.h"
7#include "roommodel.h"
8
9using namespace KOSMIndoorMap;
10
11RoomSortFilterProxyModel::RoomSortFilterProxyModel(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, &RoomSortFilterProxyModel::filterStringChanged, this, &QSortFilterProxyModel::invalidate);
22}
23
24RoomSortFilterProxyModel::~RoomSortFilterProxyModel() = default;
25
26bool RoomSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
27{
28 if (m_filter.isEmpty()) {
29 return true;
30 }
31
32 // TODO building name only makes sense if there is more than one
33 const auto idx = sourceModel()->index(source_row, 0, source_parent);
34 return filterMatches(idx.data(RoomModel::NameRole).toString())
35 || filterMatches(idx.data(RoomModel::NumberRole).toString())
36 || filterMatches(idx.data(RoomModel::TypeNameRole).toString())
37 || filterMatches(idx.data(RoomModel::BuildingNameRole).toString())
38 || filterMatches(idx.data(RoomModel::LevelLongNameRole).toString());
39}
40
41bool RoomSortFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
42{
43 const auto lhsBldg = source_left.data(RoomModel::BuildingNameRole).toString();
44 const auto rhsBldg = source_right.data(RoomModel::BuildingNameRole).toString();
45 if (lhsBldg == rhsBldg) {
46 const int lhsLvl = source_left.data(RoomModel::LevelRole).toInt() / 10;
47 const int rhsLvl = source_right.data(RoomModel::LevelRole).toInt() / 10;
48 if (lhsLvl == rhsLvl) {
49 auto lhsTitle = source_left.data(RoomModel::NumberRole).toString();
50 if (lhsTitle.isEmpty()) {
51 lhsTitle = source_left.data(RoomModel::NameRole).toString();
52 }
53 auto rhsTitle = source_right.data(RoomModel::NumberRole).toString();
54 if (rhsTitle.isEmpty()) {
55 rhsTitle = source_right.data(RoomModel::NameRole).toString();
56 }
57 return m_collator.compare(lhsTitle, rhsTitle) < 0;
58 }
59 return lhsLvl > rhsLvl;
60 }
61 return m_collator.compare(lhsBldg, rhsBldg) < 0;
62}
63
64bool RoomSortFilterProxyModel::filterMatches(const QString &s) const
65{
66 return s.contains(m_filter, Qt::CaseInsensitive); // TODO ignore diacritics
67}
68
69#include "moc_roomsortfilterproxymodel.cpp"
@ TypeNameRole
Type of the room as translated human readable text, if set.
Definition roommodel.h:39
@ NumberRole
room number, if set
Definition roommodel.h:36
@ LevelRole
numeric level for positioning rather than for display
Definition roommodel.h:37
@ NameRole
room name, if set
Definition roommodel.h:34
@ LevelLongNameRole
Name of the floor the room is on (long form, if available)
Definition roommodel.h:41
@ BuildingNameRole
Name of the building the room is in.
Definition roommodel.h:40
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 Fri May 17 2024 11:53:52 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.