File indexing completed on 2024-05-12 16:42:03

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kcategoryreassigndlg.h"
0008 
0009 // ----------------------------------------------------------------------------
0010 // QT Includes
0011 
0012 #include <QPushButton>
0013 
0014 // ----------------------------------------------------------------------------
0015 // KDE Includes
0016 
0017 #include <KMessageBox>
0018 #include <kguiutils.h>
0019 #include <KLocalizedString>
0020 
0021 // ----------------------------------------------------------------------------
0022 // Project Includes
0023 
0024 #include "ui_kcategoryreassigndlg.h"
0025 
0026 #include "mymoneyfile.h"
0027 #include "mymoneyaccount.h"
0028 #include "kmymoneycategory.h"
0029 #include "kmymoneyaccountselector.h"
0030 #include "mymoneyenums.h"
0031 
0032 KCategoryReassignDlg::KCategoryReassignDlg(QWidget* parent) :
0033     QDialog(parent),
0034     ui(new Ui::KCategoryReassignDlg)
0035 {
0036     ui->setupUi(this);
0037     auto mandatory = new KMandatoryFieldGroup(this);
0038     mandatory->add(ui->m_category);
0039     mandatory->setOkButton(ui->buttonBox->button(QDialogButtonBox::Ok));
0040 }
0041 
0042 KCategoryReassignDlg::~KCategoryReassignDlg()
0043 {
0044     delete ui;
0045 }
0046 
0047 QString KCategoryReassignDlg::show(const MyMoneyAccount& category)
0048 {
0049     if (category.id().isEmpty())
0050         return QString(); // no payee available? nothing can be selected...
0051 
0052     AccountSet set;
0053     set.addAccountGroup(eMyMoney::Account::Type::Income);
0054     set.addAccountGroup(eMyMoney::Account::Type::Expense);
0055     set.load(ui->m_category->selector());
0056 
0057     // remove the category we are about to delete
0058     ui->m_category->selector()->removeItem(category.id());
0059 
0060     // make sure the available categories have the same currency
0061     QStringList list;
0062     QStringList::const_iterator it_a;
0063     ui->m_category->selector()->itemList(list);
0064     for (it_a = list.constBegin(); it_a != list.constEnd(); ++it_a) {
0065         MyMoneyAccount acc = MyMoneyFile::instance()->account(*it_a);
0066         if (acc.currencyId() != category.currencyId())
0067             ui->m_category->selector()->removeItem(*it_a);
0068     }
0069 
0070     // reload the list
0071     ui->m_category->selector()->itemList(list);
0072 
0073     // if there is no category for reassignment left, we bail out
0074     if (list.isEmpty()) {
0075         KMessageBox::sorry(this, QString("<qt>") + i18n("At least one transaction/schedule still references the category <b>%1</b>.  However, at least one category with the same currency must exist so that the transactions/schedules can be reassigned.", category.name()) + QString("</qt>"));
0076         return QString();
0077     }
0078 
0079     // execute dialog and if aborted, return empty string
0080     if (this->exec() == QDialog::Rejected)
0081         return QString();
0082 
0083     // otherwise return index of selected payee
0084     return ui->m_category->selectedItem();
0085 }
0086 
0087 
0088 void KCategoryReassignDlg::accept()
0089 {
0090     // force update of payeeCombo
0091     ui->buttonBox->button(QDialogButtonBox::Ok)->setFocus();
0092 
0093     if (ui->m_category->selectedItem().isEmpty())
0094         KMessageBox::information(this, i18n("This dialog does not allow new categories to be created. Please pick a category from the list."), i18n("Category creation"));
0095     else
0096         QDialog::accept();
0097 }