File indexing completed on 2024-05-05 05:01:26

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         if (!source_left.data(SpaceChildrenModel::OrderRole).toString().isEmpty() && !source_right.data(SpaceChildrenModel::OrderRole).toString().isEmpty()) {
0031             return QString::compare(source_left.data(SpaceChildrenModel::OrderRole).toString(), source_right.data(SpaceChildrenModel::OrderRole).toString())
0032                 < 0;
0033         }
0034         return source_left.data(SpaceChildrenModel::ChildTimestampRole).toDateTime() > source_right.data(SpaceChildrenModel::ChildTimestampRole).toDateTime();
0035     }
0036     if (source_left.data(SpaceChildrenModel::IsSpaceRole).toBool()) {
0037         return true;
0038     } else if (source_right.data(SpaceChildrenModel::IsSpaceRole).toBool()) {
0039         return false;
0040     }
0041     if (!source_left.data(SpaceChildrenModel::OrderRole).toString().isEmpty() && !source_right.data(SpaceChildrenModel::OrderRole).toString().isEmpty()) {
0042         return QString::compare(source_left.data(SpaceChildrenModel::OrderRole).toString(), source_right.data(SpaceChildrenModel::OrderRole).toString()) < 0;
0043     }
0044     if (!source_left.data(SpaceChildrenModel::OrderRole).toString().isEmpty()) {
0045         return true;
0046     } else if (!source_right.data(SpaceChildrenModel::OrderRole).toString().isEmpty()) {
0047         return false;
0048     }
0049     return source_left.data(SpaceChildrenModel::ChildTimestampRole).toDateTime() > source_right.data(SpaceChildrenModel::ChildTimestampRole).toDateTime();
0050 }
0051 
0052 bool SpaceChildSortFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0053 {
0054     QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
0055     if (auto sourceModel = static_cast<SpaceChildrenModel *>(this->sourceModel())) {
0056         bool isReplaced = sourceModel->isRoomReplaced(index.data(SpaceChildrenModel::RoomIDRole).toString());
0057         bool acceptRoom = index.data(SpaceChildrenModel::DisplayNameRole).toString().contains(m_filterText, Qt::CaseInsensitive);
0058         return !isReplaced && acceptRoom;
0059     }
0060     return true;
0061 }
0062 
0063 #include "moc_spacechildsortfiltermodel.cpp"