[office/kmymoney] kmymoney: Added additional filter combo box to categories view

Thomas Baumgart null at kde.org
Sun Jan 23 15:03:11 GMT 2022


Git commit ffd85aa0577cb573b1fdf72bbe179365cb56df1c by Thomas Baumgart.
Committed on 23/01/2022 at 15:02.
Pushed by tbaumgart into branch 'master'.

Added additional filter combo box to categories view

This allows to filter for unused or closed categories.

GUI:

M  +88   -24   kmymoney/models/accountsproxymodel.cpp
M  +12   -0    kmymoney/models/accountsproxymodel.h
M  +7    -1    kmymoney/models/accountsproxymodel_p.h
M  +23   -2    kmymoney/views/kaccountsview.ui
M  +1    -0    kmymoney/views/kaccountsview_p.h
M  +3    -0    kmymoney/views/kcategoriesview.ui
M  +1    -0    kmymoney/views/kcategoriesview_p.h

https://invent.kde.org/office/kmymoney/commit/ffd85aa0577cb573b1fdf72bbe179365cb56df1c

diff --git a/kmymoney/models/accountsproxymodel.cpp b/kmymoney/models/accountsproxymodel.cpp
index 6b98f85f2..64b28b4db 100644
--- a/kmymoney/models/accountsproxymodel.cpp
+++ b/kmymoney/models/accountsproxymodel.cpp
@@ -14,12 +14,17 @@
 // ----------------------------------------------------------------------------
 // KDE Includes
 
+#include <KLocalizedString>
+
 // ----------------------------------------------------------------------------
 // Project Includes
 
+#include "accountsmodel.h"
+#include "journalmodel.h"
 #include "mymoneyenums.h"
+#include "mymoneyfile.h"
 #include "mymoneymoney.h"
-#include "accountsmodel.h"
+#include "schedulesjournalmodel.h"
 
 AccountsProxyModel::AccountsProxyModel(QObject *parent) :
     QSortFilterProxyModel(parent),
@@ -221,37 +226,63 @@ bool AccountsProxyModel::acceptSourceItem(const QModelIndex &source) const
         if (isValidAccountEntry) {
             const auto accountType = static_cast<eMyMoney::Account::Type>(accountTypeValue.toInt());
 
-            if (hideClosedAccounts() && sourceModel()->data(source, eMyMoney::Model::Roles::AccountIsClosedRole).toBool())
-                return false;
-
-            // we hide stock accounts if not in expert mode
-            // we hide equity accounts if not in expert mode
-            if (hideEquityAccounts()) {
-                if (accountType == eMyMoney::Account::Type::Equity)
+            switch (state()) {
+            case State::Any:
+                if (hideClosedAccounts() && sourceModel()->data(source, eMyMoney::Model::Roles::AccountIsClosedRole).toBool())
                     return false;
 
-                if (sourceModel()->data(source, eMyMoney::Model::Roles::AccountIsInvestRole).toBool())
-                    return false;
-            }
+                // we hide stock accounts if not in expert mode
+                // we hide equity accounts if not in expert mode
+                if (hideEquityAccounts()) {
+                    if (accountType == eMyMoney::Account::Type::Equity)
+                        return false;
 
-            // we hide unused income and expense accounts if the specific flag is set
-            if (hideUnusedIncomeExpenseAccounts()) {
-                if ((accountType == eMyMoney::Account::Type::Income) || (accountType == eMyMoney::Account::Type::Expense)) {
-                    const auto totalValue = sourceModel()->data(source, eMyMoney::Model::Roles::AccountTotalValueRole);
-                    if (totalValue.isValid() && totalValue.value<MyMoneyMoney>().isZero()) {
-                        emit unusedIncomeExpenseAccountHidden();
+                    if (sourceModel()->data(source, eMyMoney::Model::Roles::AccountIsInvestRole).toBool())
                         return false;
+                }
+
+                // we hide unused income and expense accounts if the specific flag is set
+                if (hideUnusedIncomeExpenseAccounts()) {
+                    if ((accountType == eMyMoney::Account::Type::Income) || (accountType == eMyMoney::Account::Type::Expense)) {
+                        const auto totalValue = sourceModel()->data(source, eMyMoney::Model::Roles::AccountTotalValueRole);
+                        if (totalValue.isValid() && totalValue.value<MyMoneyMoney>().isZero()) {
+                            emit unusedIncomeExpenseAccountHidden();
+                            return false;
+                        }
                     }
                 }
-            }
-            // we hide zero balance investment accounts
-            if (hideZeroBalancedEquityAccounts()) {
-                if (accountType == eMyMoney::Account::Type::Equity || sourceModel()->data(source, eMyMoney::Model::Roles::AccountIsInvestRole).toBool()) {
-                    const auto totalValue = sourceModel()->data(source, eMyMoney::Model::Roles::AccountTotalValueRole);
-                    if (totalValue.isValid() && totalValue.value<MyMoneyMoney>().isZero()) {
-                        return false;
+                // we hide zero balance investment accounts
+                if (hideZeroBalancedEquityAccounts()) {
+                    if (accountType == eMyMoney::Account::Type::Equity || sourceModel()->data(source, eMyMoney::Model::Roles::AccountIsInvestRole).toBool()) {
+                        const auto totalValue = sourceModel()->data(source, eMyMoney::Model::Roles::AccountTotalValueRole);
+                        if (totalValue.isValid() && totalValue.value<MyMoneyMoney>().isZero()) {
+                            return false;
+                        }
                     }
                 }
+                break;
+
+            case State::Unused: {
+                // if an account has sub-accounts it is still used
+                if (sourceModel()->rowCount(source) > 0) {
+                    return false;
+                }
+                const auto accountId = sourceModel()->data(source, eMyMoney::Model::IdRole).toString();
+                auto journalModel = MyMoneyFile::instance()->journalModel();
+                auto indexes = journalModel->match(journalModel->index(0, 0), eMyMoney::Model::JournalSplitAccountIdRole, accountId);
+                if (!indexes.isEmpty())
+                    return false;
+                journalModel = MyMoneyFile::instance()->schedulesJournalModel();
+                indexes = journalModel->match(journalModel->index(0, 0), eMyMoney::Model::JournalSplitAccountIdRole, accountId);
+                if (!indexes.isEmpty())
+                    return false;
+                break;
+            }
+            case State::Closed:
+                if (sourceModel()->data(source, eMyMoney::Model::AccountIsClosedRole).toBool() == false) {
+                    return false;
+                }
+                break;
             }
 
             if (d->m_typeList.contains(accountType)) {
@@ -458,3 +489,36 @@ QVector<eMyMoney::Account::Type> AccountsProxyModel::assetLiabilityEquityIncomeE
 {
     return assetLiabilityEquity() << incomeExpense();
 }
+
+void AccountsProxyModel::setState(AccountsProxyModel::State state)
+{
+    Q_D(AccountsProxyModel);
+    if (d->m_state != state) {
+        d->m_state = state;
+        invalidateFilter();
+    }
+}
+
+AccountsProxyModel::State AccountsProxyModel::state() const
+{
+    Q_D(const AccountsProxyModel);
+    return d->m_state;
+}
+
+void AccountsProxyModel::setFilterComboBox(QComboBox* comboBox)
+{
+    Q_D(AccountsProxyModel);
+    d->m_filterComboBox = comboBox;
+    d->m_filterComboBox->clear();
+    d->m_filterComboBox->insertItem(static_cast<int>(State::Any), i18nc("", "Any status"));
+    d->m_filterComboBox->insertItem(static_cast<int>(State::Unused), i18nc("", "Unused"));
+    d->m_filterComboBox->insertItem(static_cast<int>(State::Closed), i18nc("", "Closed"));
+
+    connect(comboBox, QOverload<int>::of(&QComboBox::activated), this, [&](int idx) {
+        setState(static_cast<AccountsProxyModel::State>(idx));
+    });
+    connect(comboBox, &QComboBox::destroyed, this, [&]() {
+        Q_D(AccountsProxyModel);
+        d->m_filterComboBox = nullptr;
+    });
+}
diff --git a/kmymoney/models/accountsproxymodel.h b/kmymoney/models/accountsproxymodel.h
index 2befab032..e635fdbbb 100644
--- a/kmymoney/models/accountsproxymodel.h
+++ b/kmymoney/models/accountsproxymodel.h
@@ -13,6 +13,7 @@
 // ----------------------------------------------------------------------------
 // QT Includes
 
+#include <QComboBox>
 #include <QSortFilterProxyModel>
 
 // ----------------------------------------------------------------------------
@@ -61,6 +62,12 @@ class KMM_MODELS_EXPORT AccountsProxyModel : public QSortFilterProxyModel
     Q_DISABLE_COPY(AccountsProxyModel)
 
 public:
+    enum State {
+        Any,
+        Closed,
+        Unused,
+    };
+
     explicit AccountsProxyModel(QObject *parent = nullptr);
     virtual ~AccountsProxyModel();
 
@@ -88,8 +95,13 @@ public:
     void setHideAllEntries(bool hideAllEntries);
     bool hideAllEntries() const;
 
+    void setState(State state);
+    State state() const;
+
     int visibleItems(bool includeBaseAccounts = false) const;
 
+    void setFilterComboBox(QComboBox* comboBox);
+
     void setNotSelectable(const QString& accountId);
 
     Qt::ItemFlags flags(const QModelIndex &index) const override;
diff --git a/kmymoney/models/accountsproxymodel_p.h b/kmymoney/models/accountsproxymodel_p.h
index 98bc21c7a..af01b48f8 100644
--- a/kmymoney/models/accountsproxymodel_p.h
+++ b/kmymoney/models/accountsproxymodel_p.h
@@ -11,6 +11,8 @@
 // ----------------------------------------------------------------------------
 // QT Includes
 
+class QComboBox;
+
 // ----------------------------------------------------------------------------
 // KDE Includes
 
@@ -25,13 +27,15 @@ class AccountsProxyModelPrivate
 
 public:
     AccountsProxyModelPrivate()
-        : m_hideClosedAccounts(true)
+        : m_filterComboBox(nullptr)
+        , m_hideClosedAccounts(true)
         , m_hideEquityAccounts(true)
         , m_hideZeroBalanceEquityAccounts(false)
         , m_hideUnusedIncomeExpenseAccounts(false)
         , m_haveHiddenUnusedIncomeExpenseAccounts(false)
         , m_hideFavoriteAccounts(true)
         , m_hideAllEntries(false)
+        , m_state(AccountsProxyModel::State::Any)
     {
     }
 
@@ -41,6 +45,7 @@ public:
 
     QList<eMyMoney::Account::Type> m_typeList;
     QString m_notSelectableId;
+    QComboBox* m_filterComboBox;
     bool m_hideClosedAccounts;
     bool m_hideEquityAccounts;
     bool m_hideZeroBalanceEquityAccounts;
@@ -48,6 +53,7 @@ public:
     bool m_haveHiddenUnusedIncomeExpenseAccounts;
     bool m_hideFavoriteAccounts;
     bool m_hideAllEntries;
+    AccountsProxyModel::State m_state;
 };
 
 #endif // ACCOUNTSPROXYMODEL_P_H
diff --git a/kmymoney/views/kaccountsview.ui b/kmymoney/views/kaccountsview.ui
index 245580a89..d9b6c1ac4 100644
--- a/kmymoney/views/kaccountsview.ui
+++ b/kmymoney/views/kaccountsview.ui
@@ -11,12 +11,30 @@
    </rect>
   </property>
   <layout class="QVBoxLayout">
-   <property name="margin">
+   <property name="leftMargin">
+    <number>0</number>
+   </property>
+   <property name="topMargin">
+    <number>0</number>
+   </property>
+   <property name="rightMargin">
+    <number>0</number>
+   </property>
+   <property name="bottomMargin">
     <number>0</number>
    </property>
    <item>
     <layout class="QVBoxLayout" name="verticalLayout">
-     <property name="margin">
+     <property name="leftMargin">
+      <number>0</number>
+     </property>
+     <property name="topMargin">
+      <number>0</number>
+     </property>
+     <property name="rightMargin">
+      <number>0</number>
+     </property>
+     <property name="bottomMargin">
       <number>0</number>
      </property>
      <item>
@@ -31,6 +49,9 @@
          </property>
         </widget>
        </item>
+       <item>
+        <widget class="QComboBox" name="m_filterBox"/>
+       </item>
        <item>
         <widget class="QPushButton" name="m_collapseButton">
          <property name="toolTip">
diff --git a/kmymoney/views/kaccountsview_p.h b/kmymoney/views/kaccountsview_p.h
index 8fa980075..9c88b9b2d 100644
--- a/kmymoney/views/kaccountsview_p.h
+++ b/kmymoney/views/kaccountsview_p.h
@@ -91,6 +91,7 @@ public:
 
         ui->m_accountTree->setModel(MyMoneyFile::instance()->accountsModel());
         m_proxyModel->addAccountGroup(AccountsProxyModel::assetLiabilityEquity());
+        m_proxyModel->setFilterComboBox(ui->m_filterBox);
 
         columnSelector->setModel(m_proxyModel);
         q->slotSettingsChanged();
diff --git a/kmymoney/views/kcategoriesview.ui b/kmymoney/views/kcategoriesview.ui
index b1886ce69..a4f60f8f6 100644
--- a/kmymoney/views/kcategoriesview.ui
+++ b/kmymoney/views/kcategoriesview.ui
@@ -35,6 +35,9 @@
        </property>
       </widget>
      </item>
+     <item>
+      <widget class="QComboBox" name="m_filterBox"/>
+     </item>
      <item>
       <widget class="QPushButton" name="m_collapseButton">
        <property name="toolTip">
diff --git a/kmymoney/views/kcategoriesview_p.h b/kmymoney/views/kcategoriesview_p.h
index bfe60ab45..296c1faf1 100644
--- a/kmymoney/views/kcategoriesview_p.h
+++ b/kmymoney/views/kcategoriesview_p.h
@@ -76,6 +76,7 @@ public:
 
         ui->m_accountTree->setModel(MyMoneyFile::instance()->accountsModel());
         m_proxyModel->addAccountGroup(AccountsProxyModel::incomeExpense());
+        m_proxyModel->setFilterComboBox(ui->m_filterBox);
 
         columnSelector->setModel(m_proxyModel);
         q->slotSettingsChanged();


More information about the kde-doc-english mailing list