1 /*
2  * Copyright 2005-2008  Thomas Baumgart <tbaumgart@kde.org>
3  * Copyright 2017-2018  Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "ksettingsgeneral.h"
20 
21 // ----------------------------------------------------------------------------
22 // QT Includes
23 
24 #include <QFileDialog>
25 
26 // ----------------------------------------------------------------------------
27 // KDE Includes
28 
29 // ----------------------------------------------------------------------------
30 // Project Includes
31 
32 #include "ui_ksettingsgeneral.h"
33 
34 #include "kmymoneydateinput.h"
35 #include "models.h"
36 #include "accountsmodel.h"
37 #include "mymoneymoney.h"
38 #include "mymoneyfile.h"
39 #include "mymoneyaccount.h"
40 #include "mymoneyenums.h"
41 
42 class KSettingsGeneralPrivate
43 {
44   Q_DISABLE_COPY(KSettingsGeneralPrivate)
45 
46 public:
KSettingsGeneralPrivate()47   KSettingsGeneralPrivate() :
48     ui(new Ui::KSettingsGeneral),
49     initialHideZeroBalanceEquities(false)
50   {
51   }
52 
~KSettingsGeneralPrivate()53   ~KSettingsGeneralPrivate()
54   {
55     delete ui;
56   }
57 
58   Ui::KSettingsGeneral *ui;
59   bool initialHideZeroBalanceEquities;
60 };
61 
KSettingsGeneral(QWidget * parent)62 KSettingsGeneral::KSettingsGeneral(QWidget* parent) :
63   QWidget(parent),
64   d_ptr(new KSettingsGeneralPrivate)
65 {
66   Q_D(KSettingsGeneral);
67   d->ui->setupUi(this);
68   // hide the internally used date field
69   d->ui->kcfg_StartDate->hide();
70 
71   // setup connections, so that the sort options get loaded once the edit fields are filled
72   connect(d->ui->kcfg_StartDate, &QDateTimeEdit::dateChanged, this, &KSettingsGeneral::slotLoadStartDate);
73 
74   // setup connections, so that changes by the user are forwarded to the (hidden) edit fields
75   connect(d->ui->m_startDateEdit, &KMyMoneyDateInput::dateChanged, d->ui->kcfg_StartDate, &QDateTimeEdit::setDate);
76 
77   connect(d->ui->choosePath, &QAbstractButton::pressed, this, &KSettingsGeneral::slotChooseLogPath);
78   d->initialHideZeroBalanceEquities = d->ui->kcfg_HideZeroBalanceEquities->isChecked();
79 }
80 
~KSettingsGeneral()81 KSettingsGeneral::~KSettingsGeneral()
82 {
83   Q_D(KSettingsGeneral);
84   delete d;
85 }
86 
slotChooseLogPath()87 void KSettingsGeneral::slotChooseLogPath()
88 {
89   Q_D(KSettingsGeneral);
90   QString filePath = QFileDialog::getExistingDirectory(this, i18n("Choose file path"), QDir::homePath());
91   d->ui->kcfg_logPath->setText(filePath);
92   slotUpdateLogTypes();
93 }
94 
slotLoadStartDate(const QDate &)95 void KSettingsGeneral::slotLoadStartDate(const QDate&)
96 {
97   Q_D(KSettingsGeneral);
98   // only need this once
99   disconnect(d->ui->kcfg_StartDate, &QDateTimeEdit::dateChanged, this, &KSettingsGeneral::slotLoadStartDate);
100   d->ui->m_startDateEdit->setDate(d->ui->kcfg_StartDate->date());
101 }
102 
slotUpdateLogTypes()103 void KSettingsGeneral::slotUpdateLogTypes()
104 {
105   Q_D(KSettingsGeneral);
106   bool enable = d->ui->kcfg_logPath->text().isEmpty() ? false : true;
107   d->ui->kcfg_logImportedStatements->setEnabled(enable);
108   d->ui->kcfg_logOfxTransactions->setEnabled(enable);
109   if (!enable)
110   {
111     d->ui->kcfg_logImportedStatements->setChecked(enable);
112     d->ui->kcfg_logOfxTransactions->setChecked(enable);
113   }
114 }
115 
showEvent(QShowEvent * event)116 void KSettingsGeneral::showEvent(QShowEvent *event)
117 {
118   Q_UNUSED(event)
119   QWidget::showEvent(event);
120   slotUpdateLogTypes();
121 }
122 
slotUpdateEquitiesVisibility()123 void KSettingsGeneral::slotUpdateEquitiesVisibility()
124 {
125   Q_D(KSettingsGeneral);
126   if (d->initialHideZeroBalanceEquities == d->ui->kcfg_HideZeroBalanceEquities->isChecked())      // setting hasn't been changed, so return
127     return;
128   d->initialHideZeroBalanceEquities = d->ui->kcfg_HideZeroBalanceEquities->isChecked();
129   AccountsModel* accountsModel = Models::instance()->accountsModel();                   // items' model for accounts' page
130   InstitutionsModel* institutionsModel = Models::instance()->institutionsModel();       // items' model for institutions' page
131   auto file = MyMoneyFile::instance();
132   QList<MyMoneyAccount> accountsList;
133   file->accountList(accountsList);
134 
135   foreach (const auto account, accountsList) {
136     if (account.isInvest() && account.balance().isZero()) {                             // search only for zero balance stocks
137       if (d->initialHideZeroBalanceEquities) {
138         accountsModel->slotObjectRemoved(eMyMoney::File::Object::Account, account.id());     // remove item from accounts' page
139         institutionsModel->slotObjectRemoved(eMyMoney::File::Object::Account, account.id()); // remove item from institutions' page
140       } else {
141         accountsModel->slotObjectAdded(eMyMoney::File::Object::Account, account.id());     // add item to accounts' page
142         institutionsModel->slotObjectAdded(eMyMoney::File::Object::Account, account.id()); // add item to institutions' page
143       }
144     }
145   }
146 }
147