File indexing completed on 2024-05-12 09:02:07

0001 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include "spacechildsortfiltermodel.h"
0005 
0006 #include "spacechildrenmodel.h"
0007 
0008 SpaceChildSortFilterModel::SpaceChildSortFilterModel(QObject *parent)
0009     : QSortFilterProxyModel(parent)
0010 {
0011     setRecursiveFilteringEnabled(true);
0012     sort(0);
0013 }
0014 
0015 void SpaceChildSortFilterModel::setFilterText(const QString &filterText)
0016 {
0017     m_filterText = filterText;
0018     Q_EMIT filterTextChanged();
0019     invalidateFilter();
0020 }
0021 
0022 QString SpaceChildSortFilterModel::filterText() const
0023 {
0024     return m_filterText;
0025 }
0026 
0027 bool SpaceChildSortFilterModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
0028 {
0029     if (!source_left.data(SpaceChildrenModel::IsSpaceRole).toBool() && source_right.data(SpaceChildrenModel::IsSpaceRole).toBool()) {
0030         return false;
0031     }
0032     return true;
0033 }
0034 
0035 bool SpaceChildSortFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0036 {
0037     QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
0038     if (auto sourceModel = static_cast<SpaceChildrenModel *>(this->sourceModel())) {
0039         bool isReplaced = sourceModel->isRoomReplaced(index.data(SpaceChildrenModel::RoomIDRole).toString());
0040         bool acceptRoom = index.data(SpaceChildrenModel::DisplayNameRole).toString().contains(m_filterText, Qt::CaseInsensitive);
0041         return !isReplaced && acceptRoom;
0042     }
0043     return true;
0044 }
0045 
0046 #include "moc_spacechildsortfiltermodel.cpp"