File indexing completed on 2024-12-08 12:53:34

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 "rocketchataccountfilterproxymodel.h"
0008 #include "rocketchataccountmodel.h"
0009 
0010 RocketChatAccountFilterProxyModel::RocketChatAccountFilterProxyModel(QObject *parent)
0011     : QSortFilterProxyModel(parent)
0012 {
0013     setFilterCaseSensitivity(Qt::CaseInsensitive);
0014     setFilterRole(RocketChatAccountModel::Name);
0015     sort(0);
0016 }
0017 
0018 RocketChatAccountFilterProxyModel::~RocketChatAccountFilterProxyModel() = default;
0019 
0020 bool RocketChatAccountFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0021 {
0022     if (!sourceModel()) {
0023         return false;
0024     }
0025     if (!mAccountOrder.isEmpty()) {
0026         if (left.isValid() && right.isValid()) {
0027             const QString leftString = sourceModel()->data(left, RocketChatAccountModel::AccountName).toString();
0028             const QString rightString = sourceModel()->data(right, RocketChatAccountModel::AccountName).toString();
0029             const int leftAccountIndex = mAccountOrder.indexOf(leftString);
0030             const int rightAccountIndex = mAccountOrder.indexOf(rightString);
0031             if (leftAccountIndex < rightAccountIndex) {
0032                 return true;
0033             } else if (leftAccountIndex > rightAccountIndex) {
0034                 return false;
0035             }
0036         }
0037     }
0038     return QSortFilterProxyModel::lessThan(left, right);
0039 }
0040 
0041 QStringList RocketChatAccountFilterProxyModel::accountOrder() const
0042 {
0043     return mAccountOrder;
0044 }
0045 
0046 void RocketChatAccountFilterProxyModel::setAccountOrder(const QStringList &newAccountOrder)
0047 {
0048     if (mAccountOrder != newAccountOrder) {
0049         mAccountOrder = newAccountOrder;
0050         invalidate();
0051     }
0052 }
0053 
0054 #include "moc_rocketchataccountfilterproxymodel.cpp"