File indexing completed on 2024-12-01 10:39:37

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "usersforroomfilterproxymodel.h"
0008 #include "usersforroommodel.h"
0009 
0010 UsersForRoomFilterProxyModel::UsersForRoomFilterProxyModel(QObject *parent)
0011     : QSortFilterProxyModel(parent)
0012 {
0013     setFilterCaseSensitivity(Qt::CaseInsensitive);
0014     // Filter on alias/username ?
0015     setFilterRole(UsersForRoomModel::UsersForRoomRoles::UserName);
0016     setSortRole(UsersForRoomModel::UsersForRoomRoles::UserName);
0017     sort(0);
0018 }
0019 
0020 UsersForRoomFilterProxyModel::~UsersForRoomFilterProxyModel() = default;
0021 
0022 void UsersForRoomFilterProxyModel::clearFilter()
0023 {
0024     setFilterFixedString({});
0025     mStatusType.clear();
0026 }
0027 
0028 void UsersForRoomFilterProxyModel::setFilterString(const QString &string)
0029 {
0030     setFilterFixedString(string);
0031 }
0032 
0033 bool UsersForRoomFilterProxyModel::hasFullList() const
0034 {
0035     return static_cast<UsersForRoomModel *>(sourceModel())->hasFullList();
0036 }
0037 
0038 bool UsersForRoomFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0039 {
0040     if (!sourceModel()) {
0041         return false;
0042     }
0043     if (left.isValid() && right.isValid()) {
0044         const QString leftString = sourceModel()->data(left, UsersForRoomModel::UserName).toString();
0045         const QString rightString = sourceModel()->data(right, UsersForRoomModel::UserName).toString();
0046         return QString::localeAwareCompare(leftString, rightString) < 0;
0047     } else {
0048         return false;
0049     }
0050 }
0051 
0052 void UsersForRoomFilterProxyModel::setStatusType(const QString &statusType)
0053 {
0054     if (mStatusType != statusType) {
0055         mStatusType = statusType;
0056         invalidateFilter();
0057     }
0058 }
0059 
0060 bool UsersForRoomFilterProxyModel::loadMoreUsersInProgress() const
0061 {
0062     return static_cast<UsersForRoomModel *>(sourceModel())->loadMoreUsersInProgress();
0063 }
0064 
0065 int UsersForRoomFilterProxyModel::total() const
0066 {
0067     return static_cast<UsersForRoomModel *>(sourceModel())->total();
0068 }
0069 
0070 int UsersForRoomFilterProxyModel::numberOfUsers() const
0071 {
0072     return static_cast<UsersForRoomModel *>(sourceModel())->usersCount();
0073 }
0074 
0075 bool UsersForRoomFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0076 {
0077     if (mStatusType.isEmpty()) {
0078         return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
0079     }
0080     const QModelIndex sourceIndex = sourceModel()->index(source_row, 0, source_parent);
0081     const QString statusType = sourceIndex.data(UsersForRoomModel::Status).toString();
0082     return (mStatusType == statusType) && QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
0083 }
0084 
0085 #include "moc_usersforroomfilterproxymodel.cpp"