File indexing completed on 2024-05-19 05:06:49

0001 /*
0002  *    SPDX-FileCopyrightText: 2022 Thomas Baumgart <tbaumgart@kde.org>
0003  *    SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 
0006 // ----------------------------------------------------------------------------
0007 // QT Includes
0008 
0009 #include <QAbstractButton>
0010 #include <QTimer>
0011 
0012 // ----------------------------------------------------------------------------
0013 // KDE Headers
0014 
0015 #include <KLocalizedString>
0016 
0017 // ----------------------------------------------------------------------------
0018 // Project Includes
0019 
0020 #include "accountcreator.h"
0021 #include "kmymoneyaccountcombo.h"
0022 #include "knewaccountdlg.h"
0023 #include "mymoneyaccount.h"
0024 #include "mymoneyfile.h"
0025 
0026 AccountCreator::AccountCreator(QObject* parent)
0027     : QObject(parent)
0028     , m_comboBox(nullptr)
0029     , m_accountType(eMyMoney::Account::Type::Unknown)
0030 {
0031 }
0032 
0033 void AccountCreator::addButton(QAbstractButton* button)
0034 {
0035     m_buttons.append(button);
0036 }
0037 
0038 void AccountCreator::setAccountType(eMyMoney::Account::Type type)
0039 {
0040     m_accountType = type;
0041 }
0042 
0043 void AccountCreator::setComboBox(KMyMoneyAccountCombo* cb)
0044 {
0045     m_comboBox = cb;
0046 }
0047 
0048 void AccountCreator::createAccount()
0049 {
0050     QTimer::singleShot(150, this, [&]() {
0051         // wait another round if any of the buttons is pressed
0052         if (std::any_of(m_buttons.constBegin(), m_buttons.constEnd(), [&](QAbstractButton* b) -> bool {
0053                 return b->isDown();
0054             })) {
0055             createAccount();
0056             return;
0057         }
0058 
0059         MyMoneyAccount parent;
0060         MyMoneyAccount account;
0061 
0062         account.setName(m_comboBox->currentText());
0063 
0064         if (m_accountType == eMyMoney::Account::Type::Asset) {
0065             parent = MyMoneyFile::instance()->asset();
0066         } else if (m_accountType == eMyMoney::Account::Type::Liability) {
0067             parent = MyMoneyFile::instance()->liability();
0068         } else if (m_accountType == eMyMoney::Account::Type::Expense) {
0069             parent = MyMoneyFile::instance()->expense();
0070         } else if (m_accountType == eMyMoney::Account::Type::Income) {
0071             parent = MyMoneyFile::instance()->income();
0072         }
0073 
0074         const bool isAccount = (m_accountType == eMyMoney::Account::Type::Asset) || (m_accountType == eMyMoney::Account::Type::Liability);
0075         const auto creator = isAccount ? &KNewAccountDlg::newAccount : &KNewAccountDlg::newCategory;
0076         const QString undoAction = isAccount ? i18nc("Create undo action", "Create account") : i18nc("Create undo action", "Create category");
0077 
0078         MyMoneyFileTransaction ft(undoAction, false);
0079         creator(account, parent);
0080 
0081         if (account.id().isEmpty()) {
0082             m_comboBox->setSelected(QString());
0083             m_comboBox->clearSelection();
0084             m_comboBox->setFocus();
0085         } else {
0086             ft.commit();
0087             m_comboBox->setSelected(account.id());
0088             auto widget = m_comboBox->nextInFocusChain();
0089             widget->setFocus();
0090         }
0091 
0092         // suicide, we're done
0093         deleteLater();
0094     });
0095 }