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

0001 /*
0002  *    SPDX-FileCopyrightText: 2024 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 <QComboBox>
0011 #include <QLineEdit>
0012 #include <QTimer>
0013 
0014 // ----------------------------------------------------------------------------
0015 // KDE Headers
0016 
0017 #include <KLocalizedString>
0018 
0019 // ----------------------------------------------------------------------------
0020 // Project Includes
0021 
0022 #include "kmymoneyutils.h"
0023 #include "ktagcontainer.h"
0024 #include "mymoneyenums.h"
0025 #include "mymoneyfile.h"
0026 #include "tagcreator.h"
0027 
0028 TagCreator::TagCreator(QObject* parent)
0029     : QObject(parent)
0030     , m_tagContainer(nullptr)
0031 {
0032 }
0033 
0034 void TagCreator::addButton(QAbstractButton* button)
0035 {
0036     m_buttons.append(button);
0037 }
0038 
0039 void TagCreator::setTagContainer(KTagContainer* tagContainer)
0040 {
0041     m_tagContainer = tagContainer;
0042 }
0043 
0044 void TagCreator::createTag()
0045 {
0046     // keep our own copy just in case it
0047     // gets overwritten in the combobox while we
0048     // are active
0049     m_name = m_tagContainer->tagCombo()->currentText();
0050 
0051     QTimer::singleShot(150, this, [&]() {
0052         // wait another round if any of the buttons is pressed
0053         if (std::any_of(m_buttons.constBegin(), m_buttons.constEnd(), [&](QAbstractButton* b) -> bool {
0054                 return b->isDown();
0055             })) {
0056             createTag();
0057             return;
0058         }
0059 
0060         qDebug() << "createTag" << m_name;
0061         QString tagId;
0062         bool ok;
0063 
0064         MyMoneyFileTransaction ft(i18nc("Undo action description", "Create tag"), false);
0065         std::tie(ok, tagId) = KMyMoneyUtils::newTag(m_name);
0066         if (!ok) {
0067             m_tagContainer->tagCombo()->clearEditText();
0068             m_tagContainer->tagCombo()->setCurrentIndex(-1);
0069             m_tagContainer->setFocus();
0070         } else {
0071             ft.commit();
0072             const auto index = m_tagContainer->tagCombo()->findData(tagId, eMyMoney::Model::IdRole);
0073             if (index != -1) {
0074                 m_tagContainer->tagCombo()->setCurrentIndex(-1);
0075                 m_tagContainer->addTagWidget(tagId);
0076                 auto widget = m_tagContainer->nextInFocusChain();
0077                 while ((widget == m_tagContainer->tagCombo()) || (widget == m_tagContainer->tagCombo()->lineEdit())) {
0078                     widget = widget->nextInFocusChain();
0079                 }
0080                 widget->setFocus();
0081             } else {
0082                 m_tagContainer->tagCombo()->clearEditText();
0083                 m_tagContainer->tagCombo()->setCurrentIndex(-1);
0084                 m_tagContainer->tagCombo()->setFocus();
0085             }
0086         }
0087 
0088         // suicide, we're done
0089         deleteLater();
0090     });
0091 }