File indexing completed on 2024-05-19 05:06:57

0001 /*
0002     SPDX-FileCopyrightText: 2019 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "institutionsproxymodel.h"
0007 
0008 // ----------------------------------------------------------------------------
0009 // QT Includes
0010 
0011 // ----------------------------------------------------------------------------
0012 // KDE Includes
0013 
0014 // ----------------------------------------------------------------------------
0015 // Project Includes
0016 
0017 #include "mymoneyenums.h"
0018 #include "mymoneyinstitution.h"
0019 #include "mymoneyaccount.h"
0020 #include "mymoneymoney.h"
0021 #include "accountsmodel.h"
0022 
0023 InstitutionsProxyModel::InstitutionsProxyModel(QObject *parent)
0024     : AccountsProxyModel(parent)
0025 {
0026     setDynamicSortFilter(true);
0027     setSortLocaleAware(true);
0028     setFilterCaseSensitivity(Qt::CaseInsensitive);
0029 }
0030 
0031 InstitutionsProxyModel::~InstitutionsProxyModel()
0032 {
0033 }
0034 
0035 /**
0036   * This function was re-implemented so we could have a special display order (favorites first)
0037   */
0038 bool InstitutionsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0039 {
0040     if (!left.isValid() || !right.isValid())
0041         return false;
0042     // different sorting based on the column which is being sorted
0043     switch (left.column()) {
0044     // for the accounts column sort based on the DisplayOrderRole
0045     case AccountsModel::Column::AccountName: {
0046         const auto leftData = sourceModel()->data(left, eMyMoney::Model::Roles::AccountDisplayOrderRole);
0047         const auto rightData = sourceModel()->data(right, eMyMoney::Model::Roles::AccountDisplayOrderRole);
0048 
0049         if (leftData.toInt() == rightData.toInt()) {
0050             // sort items of the same display order alphabetically
0051             // we bypass the base class here on purpose
0052             return QSortFilterProxyModel::lessThan(left, right); // clazy:exclude=skipped-base-method
0053         }
0054         return leftData.toInt() < rightData.toInt();
0055     }
0056     // the balance and total value columns are sorted based on the value of the account
0057     case AccountsModel::Column::Balance:
0058     case AccountsModel::Column::TotalPostedValue: {
0059         const auto leftData = sourceModel()->data(sourceModel()->index(left.row(), AccountsModel::Column::AccountName, left.parent()), eMyMoney::Model::Roles::AccountTotalValueRole);
0060         const auto rightData = sourceModel()->data(sourceModel()->index(right.row(), AccountsModel::Column::AccountName, right.parent()), eMyMoney::Model::Roles::AccountTotalValueRole);
0061         return leftData.value<MyMoneyMoney>() < rightData.value<MyMoneyMoney>();
0062     }
0063     default:
0064         break;
0065     }
0066 
0067     // we bypass the base class here on purpose
0068     return QSortFilterProxyModel::lessThan(left, right); // clazy:exclude=skipped-base-method
0069 }
0070 
0071 /**
0072   * This function was re-implemented to consider all the filtering aspects that we need in the application.
0073   */
0074 bool InstitutionsProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0075 {
0076     if (source_parent.isValid()) {
0077         // if the entry has a valid parent it is an account
0078         return AccountsProxyModel::filterAcceptsRow(source_row, source_parent);
0079     } else {
0080         return filterAcceptsRowOrChildRows(source_row, source_parent);
0081     }
0082 }