File indexing completed on 2024-05-12 05:06:06

0001 /*
0002     SPDX-FileCopyrightText: 2003-2017 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-FileCopyrightText: 2016-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kaccountselectdlg.h"
0008 
0009 // ----------------------------------------------------------------------------
0010 // QT Includes
0011 
0012 #include <QLabel>
0013 #include <QPushButton>
0014 #include <QIcon>
0015 
0016 // ----------------------------------------------------------------------------
0017 // KDE Includes
0018 
0019 #include <KGuiItem>
0020 #include <KStandardGuiItem>
0021 #include <KLocalizedString>
0022 
0023 // ----------------------------------------------------------------------------
0024 // Project Includes
0025 
0026 #include "ui_kaccountselectdlg.h"
0027 
0028 #include "mymoneyaccount.h"
0029 #include "mymoneyinstitution.h"
0030 #include "mymoneyfile.h"
0031 #include "kmymoneycategory.h"
0032 #include "kmymoneyaccountselector.h"
0033 #include "knewaccountdlg.h"
0034 #include "accountsmodel.h"
0035 
0036 #include "dialogenums.h"
0037 #include "icons/icons.h"
0038 #include "mymoneyenums.h"
0039 
0040 using namespace Icons;
0041 
0042 class KAccountSelectDlgPrivate
0043 {
0044     Q_DISABLE_COPY(KAccountSelectDlgPrivate)
0045 
0046 public:
0047     KAccountSelectDlgPrivate() :
0048         ui(new Ui::KAccountSelectDlg),
0049         m_mode(0),
0050         m_accountType(eDialogs::Category::none),
0051         m_aborted(false)
0052     {
0053     }
0054 
0055     ~KAccountSelectDlgPrivate()
0056     {
0057         delete ui;
0058     }
0059 
0060     Ui::KAccountSelectDlg *ui;
0061     QString                m_purpose;
0062     MyMoneyAccount         m_account;
0063     int                    m_mode;       // 0 - select or create, 1 - create only
0064     eDialogs::Category     m_accountType;
0065     bool                   m_aborted;
0066 };
0067 
0068 KAccountSelectDlg::KAccountSelectDlg(const eDialogs::Category accountType, const QString& purpose, QWidget *parent) :
0069     QDialog(parent),
0070     d_ptr(new KAccountSelectDlgPrivate)
0071 {
0072     Q_D(KAccountSelectDlg);
0073     d->ui->setupUi(this);
0074     d->m_purpose = purpose;
0075     d->m_accountType = accountType;
0076     // Hide the abort button. It needs to be shown on request by the caller
0077     // using showAbortButton()
0078     d->ui->m_kButtonAbort->hide();
0079 
0080     KGuiItem skipButtonItem(i18n("&Skip"),
0081                             Icons::get(Icon::SkipForward),
0082                             i18n("Skip this transaction"),
0083                             i18n("Use this to skip importing this transaction and proceed with the next one."));
0084     KGuiItem::assign(d->ui->m_qbuttonCancel, skipButtonItem);
0085 
0086     KGuiItem createButtenItem(i18n("&Create..."),
0087                               Icons::get(Icon::DocumentNew),
0088                               i18n("Create a new account/category"),
0089                               i18n("Use this to add a new account/category to the file"));
0090     KGuiItem::assign(d->ui->m_createButton, createButtenItem);
0091     KGuiItem::assign(d->ui->m_qbuttonOk, KStandardGuiItem::ok());
0092 
0093     KGuiItem abortButtenItem(i18n("&Abort"),
0094                              Icons::get(Icon::DialogCancel),
0095                              i18n("Abort the import operation and dismiss all changes"),
0096                              i18n("Use this to abort the import. Your financial data will be in the state before you started the QIF import."));
0097     KGuiItem::assign(d->ui->m_kButtonAbort, abortButtenItem);
0098 
0099     QVector<eMyMoney::Account::Type> accountTypes;
0100     if (d->m_accountType & eDialogs::Category::asset)
0101         accountTypes.append(eMyMoney::Account::Type::Asset);
0102     if (d->m_accountType & eDialogs::Category::liability)
0103         accountTypes.append(eMyMoney::Account::Type::Liability);
0104     if (d->m_accountType & eDialogs::Category::income)
0105         accountTypes.append(eMyMoney::Account::Type::Income);
0106     if (d->m_accountType & eDialogs::Category::expense)
0107         accountTypes.append(eMyMoney::Account::Type::Expense);
0108     if (d->m_accountType & eDialogs::Category::equity)
0109         accountTypes.append(eMyMoney::Account::Type::Equity);
0110     if (d->m_accountType & eDialogs::Category::checking)
0111         accountTypes.append(eMyMoney::Account::Type::Checkings);
0112     if (d->m_accountType & eDialogs::Category::savings)
0113         accountTypes.append(eMyMoney::Account::Type::Savings);
0114     if (d->m_accountType & eDialogs::Category::investment)
0115         accountTypes.append(eMyMoney::Account::Type::Investment);
0116     if (d->m_accountType & eDialogs::Category::creditCard)
0117         accountTypes.append(eMyMoney::Account::Type::CreditCard);
0118 
0119     auto filterProxyModel = new AccountNamesFilterProxyModel(this);
0120     filterProxyModel->setHideEquityAccounts(true);
0121     filterProxyModel->addAccountGroup(accountTypes);
0122 
0123     filterProxyModel->setSourceModel(MyMoneyFile::instance()->accountsModel());
0124     filterProxyModel->sort(AccountsModel::Column::AccountName);
0125 
0126     d->ui->m_accountSelector->setModel(filterProxyModel);
0127 
0128     connect(d->ui->m_createButton,  &QAbstractButton::clicked, this, &KAccountSelectDlg::slotCreateAccount);
0129     connect(d->ui->m_qbuttonOk,     &QAbstractButton::clicked, this, &QDialog::accept);
0130     connect(d->ui->m_qbuttonCancel, &QAbstractButton::clicked, this, &QDialog::reject);
0131     connect(d->ui->m_kButtonAbort,  &QAbstractButton::clicked, this, &KAccountSelectDlg::abort);
0132 }
0133 
0134 KAccountSelectDlg::~KAccountSelectDlg()
0135 {
0136     Q_D(KAccountSelectDlg);
0137     delete d;
0138 }
0139 
0140 void KAccountSelectDlg::setDescription(const QString& msg)
0141 {
0142     Q_D(KAccountSelectDlg);
0143     d->ui->m_descLabel->setText(msg);
0144 }
0145 
0146 void KAccountSelectDlg::setHeader(const QString& msg)
0147 {
0148     Q_D(KAccountSelectDlg);
0149     d->ui->m_headerLabel->setText(msg);
0150 }
0151 
0152 void KAccountSelectDlg::setAccount(const MyMoneyAccount& account, const QString& id)
0153 {
0154     Q_D(KAccountSelectDlg);
0155     d->m_account = account;
0156     d->ui->m_accountSelector->setSelected(id);
0157 }
0158 
0159 void KAccountSelectDlg::slotCreateAccount()
0160 {
0161     Q_D(KAccountSelectDlg);
0162     if (!((int)d->m_accountType & ((int)eDialogs::Category::expense | (int)eDialogs::Category::income))) {
0163         Q_EMIT createAccount(d->m_account);
0164         if (!d->m_account.id().isEmpty()) {
0165             d->ui->m_accountSelector->setSelected(d->m_account.id());
0166             accept();
0167         }
0168     } else {
0169         if (d->m_account.accountType() == eMyMoney::Account::Type::Expense)
0170             KNewAccountDlg::newCategory(d->m_account, MyMoneyFile::instance()->expense());
0171         else
0172             KNewAccountDlg::newCategory(d->m_account, MyMoneyFile::instance()->income());
0173         if (!d->m_account.id().isEmpty()) {
0174             d->ui->m_accountSelector->setSelected(d->m_account.id());
0175             accept();
0176         }
0177     }
0178 }
0179 
0180 void KAccountSelectDlg::abort()
0181 {
0182     Q_D(KAccountSelectDlg);
0183     d->m_aborted = true;
0184     reject();
0185 }
0186 
0187 void KAccountSelectDlg::setMode(const int mode)
0188 {
0189     Q_D(KAccountSelectDlg);
0190     d->m_mode = mode ? 1 : 0;
0191 }
0192 
0193 void KAccountSelectDlg::showAbortButton(const bool visible)
0194 {
0195     Q_D(KAccountSelectDlg);
0196     d->ui->m_kButtonAbort->setVisible(visible);
0197 }
0198 
0199 bool KAccountSelectDlg::aborted() const
0200 {
0201     Q_D(const KAccountSelectDlg);
0202     return d->m_aborted;
0203 }
0204 
0205 void KAccountSelectDlg::hideQifEntry()
0206 {
0207     Q_D(KAccountSelectDlg);
0208     d->ui->m_qifEntry->hide();
0209 }
0210 
0211 int KAccountSelectDlg::exec()
0212 {
0213     Q_D(KAccountSelectDlg);
0214     int rc = Rejected;
0215 
0216     if (d->m_mode == 1) {
0217         slotCreateAccount();
0218         rc = result();
0219     }
0220     if (rc != Accepted) {
0221         d->ui->m_createButton->setFocus();
0222         rc = QDialog::exec();
0223     }
0224     return rc;
0225 }
0226 
0227 QString KAccountSelectDlg::selectedAccount() const
0228 {
0229     Q_D(const KAccountSelectDlg);
0230 
0231     return d->ui->m_accountSelector->getSelected();
0232 }