1 /***************************************************************************
2                          interestcategorywizardpage  -  description
3                             -------------------
4    begin                : Sun Jul 4 2010
5    copyright            : (C) 2010 by Fernando Vilas
6    email                : kmymoney-devel@kde.org
7 ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include "interestcategorywizardpage.h"
19 
20 // ----------------------------------------------------------------------------
21 // QT Includes
22 
23 #include <QPointer>
24 #include <QIcon>
25 
26 // ----------------------------------------------------------------------------
27 // KDE Includes
28 
29 #include <KLocalizedString>
30 #include <KMessageBox>
31 
32 // ----------------------------------------------------------------------------
33 // Project Includes
34 
35 #include "ui_interestcategorywizardpage.h"
36 
37 #include "knewaccountdlg.h"
38 #include "mymoneyfile.h"
39 #include "mymoneyaccount.h"
40 #include "icons/icons.h"
41 #include "mymoneyexception.h"
42 #include "mymoneyenums.h"
43 
44 using namespace Icons;
45 
InterestCategoryWizardPage(QWidget * parent)46 InterestCategoryWizardPage::InterestCategoryWizardPage(QWidget *parent)
47   : QWizardPage(parent),
48     ui(new Ui::InterestCategoryWizardPage)
49 {
50   ui->setupUi(this);
51   // Register the fields with the QWizard and connect the
52   // appropriate signals to update the "Next" button correctly
53   registerField("interestAccountEdit", ui->m_interestAccountEdit, "selectedItems");
54 
55   connect(ui->m_interestAccountEdit, &KMyMoneySelector::stateChanged, this, &QWizardPage::completeChanged);
56   ui->m_interestAccountEdit->removeButtons();
57 
58   // load button icons
59   KGuiItem createCategoryButtonItem(i18n("&Create..."),
60                                     Icons::get(Icon::DocumentNew),
61                                     i18n("Create a new category"),
62                                     i18n("Use this to open the new account editor"));
63   KGuiItem::assign(ui->m_createCategoryButton, createCategoryButtonItem);
64   connect(ui->m_createCategoryButton, &QAbstractButton::clicked, this, &InterestCategoryWizardPage::slotCreateCategory);
65 }
66 
~InterestCategoryWizardPage()67 InterestCategoryWizardPage::~InterestCategoryWizardPage()
68 {
69   delete ui;
70 }
71 
72 /**
73  * Update the "Next" button
74  */
isComplete() const75 bool InterestCategoryWizardPage::isComplete() const
76 {
77   return ui->m_interestAccountEdit->selectedItems().count() > 0;
78 }
79 
slotCreateCategory()80 void InterestCategoryWizardPage::slotCreateCategory()
81 {
82   MyMoneyAccount acc, base;
83   MyMoneyFile* file = MyMoneyFile::instance();
84 
85   if (field("borrowButton").toBool()) {
86     base = file->expense();
87     acc.setAccountType(eMyMoney::Account::Type::Expense);
88   } else {
89     base = file->income();
90     acc.setAccountType(eMyMoney::Account::Type::Income);
91   }
92   acc.setParentAccountId(base.id());
93 
94   QPointer<KNewAccountDlg> dlg = new KNewAccountDlg(acc, true, true, nullptr, QString());
95   if (dlg->exec() == QDialog::Accepted) {
96     acc = dlg->account();
97 
98     MyMoneyFileTransaction ft;
99     try {
100       QString id;
101       id = file->createCategory(base, acc.name());
102       if (id.isEmpty())
103         throw MYMONEYEXCEPTION_CSTRING("failure while creating the account hierarchy");
104 
105       ft.commit();
106 
107       ui->m_interestAccountEdit->setSelected(id);
108 
109     } catch (const MyMoneyException &e) {
110       KMessageBox::information(this, i18n("Unable to add account: %1", QString::fromLatin1(e.what())));
111     }
112   }
113   delete dlg;
114 }
115