File indexing completed on 2024-05-12 05:06:14
0001 /* 0002 SPDX-FileCopyrightText: 2010-2014 Cristian Oneț <onet.cristian@gmail.com> 0003 SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com> 0004 SPDX-FileCopyrightText: 2019-2020 Thomas Baumgart <tbaumgart@kde.org> 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #ifndef ACCOUNTSPROXYMODEL_H 0009 #define ACCOUNTSPROXYMODEL_H 0010 0011 #include "kmm_models_export.h" 0012 0013 // ---------------------------------------------------------------------------- 0014 // QT Includes 0015 0016 #include <QComboBox> 0017 #include <QSortFilterProxyModel> 0018 0019 // ---------------------------------------------------------------------------- 0020 // KDE Includes 0021 0022 // ---------------------------------------------------------------------------- 0023 // Project Includes 0024 0025 #include "mymoneyenums.h" 0026 0027 /** 0028 * A proxy model to provide various sorting and filtering operations for @ref AccountsModel. 0029 * 0030 * Here is an example of how to use this class in combination with the @ref AccountsModel. 0031 * (in the example @a widget is a pointer to a model/view widget): 0032 * 0033 * @code 0034 * AccountsProxyModel *filterModel = new AccountsProxyModel(widget); 0035 * filterModel->addAccountGroup(AccountsProxyModel::assetLiabilityEquity()); 0036 * filterModel->setSourceModel(MyMoneyFile::instance()->accountsModel()); 0037 * filterModel->sort(0); 0038 * 0039 * widget->setModel(filterModel); 0040 * @endcode 0041 * 0042 * @see AccountsModel 0043 * 0044 * @author Cristian Onet 2010 0045 * @author Thomas Baumgart 2019 0046 * 0047 */ 0048 0049 namespace eMyMoney { 0050 namespace Account { 0051 enum class Type; 0052 } 0053 } 0054 namespace eAccountsModel { 0055 enum class Column; 0056 } 0057 0058 class AccountsProxyModelPrivate; 0059 class KMM_MODELS_EXPORT AccountsProxyModel : public QSortFilterProxyModel 0060 { 0061 Q_OBJECT 0062 Q_DISABLE_COPY(AccountsProxyModel) 0063 0064 public: 0065 enum State { 0066 Any, 0067 Closed, 0068 Unused, 0069 }; 0070 0071 explicit AccountsProxyModel(QObject *parent = nullptr); 0072 virtual ~AccountsProxyModel(); 0073 0074 void setSourceModel(QAbstractItemModel* model) override; 0075 0076 void addAccountType(eMyMoney::Account::Type type); 0077 void addAccountGroup(const QVector<eMyMoney::Account::Type> &groups); 0078 void removeAccountType(eMyMoney::Account::Type type); 0079 0080 void clear(); 0081 0082 void setHideClosedAccounts(bool hideClosedAccounts); 0083 bool hideClosedAccounts() const; 0084 0085 void setHideEquityAccounts(bool hideEquityAccounts); 0086 bool hideEquityAccounts() const; 0087 0088 void setHideZeroBalancedEquityAccounts(bool hideZeroBalancedEquityAccounts); 0089 bool hideZeroBalancedEquityAccounts() const; 0090 0091 void setHideZeroBalancedAccounts(bool hideZeroBalancedAccounts); 0092 bool hideZeroBalancedAccounts() const; 0093 0094 void setHideUnusedIncomeExpenseAccounts(bool hideUnusedIncomeExpenseAccounts); 0095 bool hideUnusedIncomeExpenseAccounts() const; 0096 0097 void setHideFavoriteAccounts(bool hideFavoriteAccounts); 0098 bool hideFavoriteAccounts() const; 0099 0100 void setSelectableAccountTypes(QSet<eMyMoney::Account::Type> selectableAccountTypes); 0101 0102 void setHideAllEntries(bool hideAllEntries); 0103 bool hideAllEntries() const; 0104 0105 void setShowAllEntries(bool hideAllEntries); 0106 bool showAllEntries() const; 0107 0108 void setState(State state); 0109 State state() const; 0110 0111 int visibleItems(bool includeBaseAccounts = false) const; 0112 0113 void setFilterComboBox(QComboBox* comboBox); 0114 0115 void setNotSelectable(const QString& accountId); 0116 0117 void setClosedSelectable(bool selectable); 0118 0119 Qt::ItemFlags flags(const QModelIndex &index) const override; 0120 0121 /** 0122 * This is a convenience method which returns a prefilled vector 0123 * to be used with addAccountGroup() for asset, liability and equity 0124 * accounts. 0125 */ 0126 static QVector<eMyMoney::Account::Type> assetLiabilityEquity(); 0127 0128 /** 0129 * This is a convenience method which returns a prefilled vector 0130 * to be used with addAccountGroup() for asset and liability 0131 * accounts. 0132 */ 0133 static QVector<eMyMoney::Account::Type> assetLiability(); 0134 0135 /** 0136 * This is a convenience method which returns a prefilled vector 0137 * to be used with accAccountGroup() for income and expense 0138 * accounts. 0139 */ 0140 static QVector<eMyMoney::Account::Type> incomeExpense(); 0141 0142 /** 0143 * This is a convenience method which returns a prefilled vector 0144 * to be used with accAccountGroup() for asset, liability, equity, income and expense 0145 * accounts. 0146 */ 0147 static QVector<eMyMoney::Account::Type> assetLiabilityEquityIncomeExpense(); 0148 0149 protected: 0150 const QScopedPointer<AccountsProxyModelPrivate> d_ptr; 0151 AccountsProxyModel(AccountsProxyModelPrivate &dd, QObject *parent); 0152 0153 bool lessThan(const QModelIndex &left, const QModelIndex &right) const override; 0154 bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override; 0155 bool acceptSourceItem(const QModelIndex &source) const; 0156 0157 bool filterAcceptsRowOrChildRows(int source_row, const QModelIndex &source_parent) const; 0158 0159 int visibleItems(const QModelIndex& index) const; 0160 0161 Q_SIGNALS: 0162 void unusedIncomeExpenseAccountHidden(); 0163 0164 private: 0165 Q_DECLARE_PRIVATE(AccountsProxyModel) 0166 0167 }; 0168 0169 #undef QSortFilterProxyModel 0170 0171 /** 0172 * A proxy model used to filter all the data from the core accounts model leaving 0173 * only the name of the accounts so this model can be used in the account 0174 * completion combo. 0175 * 0176 * It shows only the first column (account name) and makes top level items non-selectable. 0177 * 0178 * @see AccountsModel 0179 * @see AccountsProxyModel 0180 * 0181 * @author Cristian Onet 2010 0182 * @author Christian David 0183 */ 0184 0185 template <class baseProxyModel> 0186 class AccountNamesFilterProxyModelTpl : public baseProxyModel 0187 { 0188 public: 0189 explicit AccountNamesFilterProxyModelTpl(QObject *parent = 0) 0190 : baseProxyModel(parent) 0191 {} 0192 0193 /** 0194 * Top items are not selectable because they are not real accounts but are only used for grouping. 0195 */ 0196 virtual Qt::ItemFlags flags(const QModelIndex &idx) const override 0197 { 0198 if (!idx.parent().isValid()) 0199 return baseProxyModel::flags(idx) & ~Qt::ItemIsSelectable; 0200 return baseProxyModel::flags(idx); 0201 } 0202 0203 /** 0204 * The Qt::EditRole returns the full account name including parent account name 0205 */ 0206 virtual QVariant data(const QModelIndex& idx, int role) const override 0207 { 0208 if (role == Qt::EditRole) { 0209 return baseProxyModel::data(idx, eMyMoney::Model::AccountFullNameRole); 0210 } 0211 return baseProxyModel::data(idx, role); 0212 } 0213 0214 protected: 0215 /** 0216 * Filter all but the first column. 0217 */ 0218 bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const override 0219 { 0220 Q_UNUSED(source_parent) 0221 if (source_column == 0) 0222 return true; 0223 return false; 0224 } 0225 }; 0226 0227 /** 0228 * @brief "typedef" for AccountNamesFilterProxyModelTpl<AccountsFilterProxyModel> 0229 * 0230 * To create valid Qt moc data this class inherits the template and uses Q_OBJECT. 0231 * Simply using a typedef like 0232 * 0233 * @code 0234 * typedef AccountNamesFilterProxyModelTpl<AccountsFilterProxyModel> AccountNamesFilterProxyModel; 0235 * @endcode 0236 * 0237 * does not work, because at some point the code needs to use qobject_cast<> to promote a 0238 * returned QSortFilterProxyModel pointer to an AccountNamesFilterProxyModel which is 0239 * only possible with Q_OBJECT being in place. 0240 */ 0241 class KMM_MODELS_EXPORT AccountNamesFilterProxyModel : public AccountNamesFilterProxyModelTpl<AccountsProxyModel> 0242 { 0243 Q_OBJECT 0244 public: 0245 explicit AccountNamesFilterProxyModel(QObject* parent = 0) 0246 : AccountNamesFilterProxyModelTpl< AccountsProxyModel >(parent) {} 0247 }; 0248 0249 0250 #endif