Commit 62faad4a authored by Łukasz Wojniłowicz's avatar Łukasz Wojniłowicz
Browse files

Show/Hide zero-balance equities in account models

BUG:340158
FIXED-IN:5.0

Differential Revision: https://phabricator.kde.org/D5341

Signed-off-by: Łukasz Wojniłowicz's avatarŁukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
parent 1e6281a7
......@@ -29,6 +29,8 @@
// Project Includes
#include <kmymoneydateinput.h>
#include "models.h"
#include "accountsmodel.h"
KSettingsGeneral::KSettingsGeneral(QWidget* parent) :
KSettingsGeneralDecl(parent)
......@@ -41,6 +43,8 @@ KSettingsGeneral::KSettingsGeneral(QWidget* parent) :
// setup connections, so that changes by the user are forwarded to the (hidden) edit fields
connect(m_startDateEdit, SIGNAL(dateChanged(QDate)), kcfg_StartDate, SLOT(setDate(QDate)));
initialHideZeroBalanceEquities = kcfg_HideZeroBalanceEquities->isChecked();
}
KSettingsGeneral::~KSettingsGeneral()
......@@ -53,3 +57,27 @@ void KSettingsGeneral::slotLoadStartDate(const QDate&)
disconnect(kcfg_StartDate, SIGNAL(dateChanged(QDate)), this, SLOT(slotLoadStartDate(QDate)));
m_startDateEdit->setDate(kcfg_StartDate->date());
}
void KSettingsGeneral::slotUpdateEquitiesVisibility()
{
if (initialHideZeroBalanceEquities == kcfg_HideZeroBalanceEquities->isChecked()) // setting hasn't been changed, so return
return;
initialHideZeroBalanceEquities = kcfg_HideZeroBalanceEquities->isChecked();
AccountsModel* accountsModel = Models::instance()->accountsModel(); // items' model for accounts' page
InstitutionsModel* institutionsModel = Models::instance()->institutionsModel(); // items' model for institutions' page
MyMoneyFile *file = MyMoneyFile::instance();
QList<MyMoneyAccount> accountsList;
file->accountList(accountsList);
foreach (const auto account, accountsList) {
if (account.isInvest() && account.balance().isZero()) { // search only for zero balance stocks
if (initialHideZeroBalanceEquities) {
accountsModel->slotObjectRemoved(MyMoneyFile::notifyAccount, account.id()); // remove item from accounts' page
institutionsModel->slotObjectRemoved(MyMoneyFile::notifyAccount, account.id()); // remove item from institutions' page
} else {
accountsModel->slotObjectAdded(MyMoneyFile::notifyAccount, dynamic_cast<const MyMoneyObject* const>(&account)); // add item to accounts' page
institutionsModel->slotObjectAdded(MyMoneyFile::notifyAccount, dynamic_cast<const MyMoneyObject* const>(&account)); // add item to institutions' page
}
}
}
}
......@@ -39,7 +39,8 @@ public:
class KSettingsGeneral : public KSettingsGeneralDecl
{
Q_OBJECT
private:
bool initialHideZeroBalanceEquities;
public:
KSettingsGeneral(QWidget* parent = 0);
~KSettingsGeneral();
......@@ -47,6 +48,8 @@ public:
protected slots:
void slotLoadStartDate(const QDate&);
public slots:
void slotUpdateEquitiesVisibility();
};
#endif
......@@ -88,6 +88,7 @@ KSettingsKMyMoney::KSettingsKMyMoney(QWidget *parent, const QString &name, KCore
QAbstractButton* defaultButton = button(QDialogButtonBox::RestoreDefaults);
connect(this, &KConfigDialog::rejected, schedulesPage, &KSettingsSchedules::slotResetRegion);
connect(this, &KConfigDialog::rejected, iconsPage, &KSettingsIcons::slotResetTheme);
connect(this, &KConfigDialog::settingsChanged, generalPage, &KSettingsGeneral::slotUpdateEquitiesVisibility);
connect(this, &KConfigDialog::accepted, pluginsPage, &KPluginSelector::save);
connect(defaultButton, &QAbstractButton::clicked, pluginsPage, &KPluginSelector::defaults);
......
......@@ -532,9 +532,17 @@ void AccountsModel::load()
accountsItem->appendRow(item);
item->setColumnCount(columnCount());
item->setEditable(false);
if (acc.accountList().count() > 0) {
d->loadSubAccounts(this, item, favoriteAccountsItem, acc.accountList());
QStringList subaccountsList = acc.accountList();
// filter out stocks with zero balance if requested by user
for (QStringList::Iterator it = subaccountsList.begin(); it != subaccountsList.end();) {
const MyMoneyAccount& subAcc = d->m_file->account(*it);
if (subAcc.isInvest() && KMyMoneyGlobalSettings::hideZeroBalanceEquities() && subAcc.balance().isZero())
it = subaccountsList.erase(it);
else
++it;
}
if (!subaccountsList.isEmpty())
d->loadSubAccounts(this, item, favoriteAccountsItem, subaccountsList);
d->setAccountData(this, item->index(), acc);
// set the account data after the children have been loaded
if (acc.value("PreferredAccount") == "Yes") {
......@@ -915,15 +923,22 @@ void InstitutionsModel::load()
MyMoneyInstitution none;
none.setName(i18n("Accounts with no institution assigned"));
institutionList.append(none);
foreach (const MyMoneyInstitution &institution, institutionList) {
foreach (const auto institution, institutionList)
static_cast<InstitutionsPrivate *>(d)->addInstitutionItem(this, institution);
QList<MyMoneyAccount> accountsList;
QList<MyMoneyAccount> stocksList;
d->m_file->accountList(accountsList);
foreach (const auto account, accountsList) { // create items for all the accounts...
if (account.isInvest()) // ...but wait with stocks until investment accounts appear
stocksList.append(account);
else
static_cast<InstitutionsPrivate *>(d)->loadInstitution(this, account);
}
// create items for all the accounts
QList<MyMoneyAccount> list;
d->m_file->accountList(list);
for (QList<MyMoneyAccount>::ConstIterator it_l = list.constBegin(); it_l != list.constEnd(); ++it_l) {
static_cast<InstitutionsPrivate *>(d)->loadInstitution(this, *it_l);
foreach (const auto stock, stocksList) {
if (!(KMyMoneyGlobalSettings::hideZeroBalanceEquities() && stock.balance().isZero()))
static_cast<InstitutionsPrivate *>(d)->loadInstitution(this, stock);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment