File indexing completed on 2024-05-19 05:08:20

0001 /*
0002     SPDX-FileCopyrightText: 2021 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "ksearchtransactiondlg.h"
0007 
0008 // ----------------------------------------------------------------------------
0009 // QT Includes
0010 
0011 #include <QAction>
0012 #include <QKeyEvent>
0013 #include <QPointer>
0014 #include <QPushButton>
0015 
0016 // ----------------------------------------------------------------------------
0017 // KDE Includes
0018 
0019 #include <KGuiItem>
0020 #include <KLocalizedString>
0021 #include <KStandardGuiItem>
0022 
0023 // ----------------------------------------------------------------------------
0024 // Project Includes
0025 
0026 #include "journalmodel.h"
0027 #include "ktransactionfilter.h"
0028 #include "ledgerjournalidfilter.h"
0029 #include "menuenums.h"
0030 #include "mymoneyfile.h"
0031 #include "mymoneytransactionfilter.h"
0032 #include "selectedobjects.h"
0033 #include "specialdatesmodel.h"
0034 #include "ui_ksearchtransactiondlg.h"
0035 
0036 class KSearchTransactionDlgPrivate
0037 {
0038     Q_DISABLE_COPY(KSearchTransactionDlgPrivate)
0039     Q_DECLARE_PUBLIC(KSearchTransactionDlg)
0040 
0041 public:
0042     KSearchTransactionDlgPrivate(KSearchTransactionDlg* qq)
0043         : q_ptr(qq)
0044         , filterModel(new LedgerJournalIdFilter(qq, QVector<QAbstractItemModel*>{/*MyMoneyFile::instance()->specialDatesModel()*/}))
0045     {
0046     }
0047 
0048     void init()
0049     {
0050         Q_Q(KSearchTransactionDlg);
0051 
0052         ui.setupUi(q);
0053         filterTab = new KTransactionFilter(q);
0054         ui.m_tabWidget->insertTab(0, filterTab, i18nc("Criteria tab", "Criteria"));
0055 
0056         // disable access to result page until we have a result
0057         ui.m_tabWidget->setTabEnabled(ui.m_tabWidget->indexOf(ui.m_resultPage), false);
0058 
0059         // only allow searches when a selection has been made
0060         ui.buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
0061         ui.buttonBox->button(QDialogButtonBox::Apply)->setDefault(true);
0062         ui.buttonBox->button(QDialogButtonBox::Apply)->setAutoDefault(true);
0063         KGuiItem::assign(ui.buttonBox->button(QDialogButtonBox::Apply), KStandardGuiItem::find());
0064         ui.buttonBox->button(QDialogButtonBox::Apply)->setToolTip(i18nc("@info:tooltip for find transaction apply button", "Search transactions"));
0065         q->connect(filterTab, &KTransactionFilter::selectionNotEmpty, ui.buttonBox->button(QDialogButtonBox::Apply), &QWidget::setEnabled);
0066     }
0067 
0068     void search()
0069     {
0070         // perform the search only if the button is enabled
0071         if (!ui.buttonBox->button(QDialogButtonBox::Apply)->isEnabled())
0072             return;
0073 
0074         filterModel->setSourceModel(MyMoneyFile::instance()->journalModel());
0075         ui.m_ledgerView->setModel(filterModel);
0076 
0077         // setup the filter from the dialog widgets
0078         filterModel->setFilterRole(eMyMoney::Model::Roles::IdRole);
0079         const auto filter = filterTab->setupFilter();
0080         const auto list = MyMoneyFile::instance()->journalEntryIds(filter);
0081         filterModel->setFilterFixedStrings(list);
0082 
0083         QVector<int> columns;
0084         columns = {
0085             JournalModel::Column::Number,
0086             JournalModel::Column::Security,
0087             JournalModel::Column::CostCenter,
0088             JournalModel::Column::Quantity,
0089             JournalModel::Column::Price,
0090             JournalModel::Column::Amount,
0091             JournalModel::Column::Value,
0092             JournalModel::Column::Balance,
0093         };
0094         ui.m_ledgerView->setColumnsHidden(columns);
0095         columns = {
0096             JournalModel::Column::Date,
0097             JournalModel::Column::Account,
0098             JournalModel::Column::Detail,
0099             JournalModel::Column::Reconciliation,
0100             JournalModel::Column::Payment,
0101             JournalModel::Column::Deposit,
0102         };
0103         ui.m_ledgerView->setColumnsShown(columns);
0104 
0105         ui.m_tabWidget->setTabEnabled(ui.m_tabWidget->indexOf(ui.m_resultPage), true);
0106         ui.m_tabWidget->setCurrentWidget(ui.m_resultPage);
0107         ui.m_ledgerView->setFocus();
0108 
0109         ui.m_foundText->setText(i18np("Found %1 matching splits", "Found %1 matching splits", filterModel->rowCount()));
0110     }
0111 
0112     void selectTransaction(const QModelIndex& idx)
0113     {
0114         Q_Q(KSearchTransactionDlg);
0115 
0116         const auto accountId = idx.data(eMyMoney::Model::JournalSplitAccountIdRole).toString();
0117         SelectedObjects selections;
0118         selections.setSelection(SelectedObjects::Account, accountId);
0119         selections.setSelection(SelectedObjects::JournalEntry, idx.data(eMyMoney::Model::IdRole).toString());
0120 
0121         // close myself and open the ledger of the account
0122         q->hide();
0123 
0124         Q_EMIT q->requestSelectionChange(selections);
0125         pActions[eMenu::Action::GoToAccount]->setData(accountId);
0126         pActions[eMenu::Action::GoToAccount]->trigger();
0127     }
0128 
0129     KSearchTransactionDlg* q_ptr;
0130     LedgerJournalIdFilter* filterModel;
0131     Ui_KSearchTransactionDlg ui;
0132     QPointer<KTransactionFilter> filterTab;
0133 };
0134 
0135 KSearchTransactionDlg::KSearchTransactionDlg(QWidget* parent)
0136     : QDialog(parent)
0137     , d_ptr(new KSearchTransactionDlgPrivate(this))
0138 {
0139     Q_D(KSearchTransactionDlg);
0140     d->init();
0141 
0142     connect(d->ui.buttonBox->button(QDialogButtonBox::Apply), &QAbstractButton::clicked, this, [&]() {
0143         Q_D(KSearchTransactionDlg);
0144         d->search();
0145     });
0146 
0147     connect(d->ui.buttonBox->button(QDialogButtonBox::Reset), &QAbstractButton::clicked, this, [&]() {
0148         Q_D(KSearchTransactionDlg);
0149         d->filterTab->slotReset();
0150     });
0151 
0152     connect(d->ui.buttonBox->button(QDialogButtonBox::Help), &QAbstractButton::clicked, this, [&]() {
0153         Q_D(KSearchTransactionDlg);
0154         if (d->ui.m_tabWidget->currentIndex() == 0) {
0155             d->filterTab->slotShowHelp();
0156         }
0157     });
0158 
0159     connect(d->ui.buttonBox->button(QDialogButtonBox::Close), &QAbstractButton::clicked, this, &QObject::deleteLater);
0160 
0161     d->filterTab->setFocus();
0162 
0163     // we don't allow editing here but double click selects the current
0164     // selected transaction in the ledger view
0165     d->ui.m_ledgerView->setEditTriggers(QAbstractItemView::NoEditTriggers);
0166     connect(d->ui.m_ledgerView, &QAbstractItemView::doubleClicked, this, [&](const QModelIndex& idx) {
0167         Q_D(KSearchTransactionDlg);
0168         d->selectTransaction(idx);
0169     });
0170 
0171     d->ui.m_ledgerView->installEventFilter(this);
0172 }
0173 
0174 KSearchTransactionDlg::~KSearchTransactionDlg()
0175 {
0176 }
0177 
0178 bool KSearchTransactionDlg::eventFilter(QObject* watched, QEvent* event)
0179 {
0180     Q_D(KSearchTransactionDlg);
0181     if (watched == d->ui.m_ledgerView) {
0182         if (event->type() == QEvent::KeyPress) {
0183             const auto kev = static_cast<QKeyEvent*>(event);
0184             if ((kev->key() == Qt::Key_Enter) || (kev->key() == Qt::Key_Return)) {
0185                 d->selectTransaction(d->ui.m_ledgerView->currentIndex());
0186             }
0187         }
0188     }
0189     return QDialog::eventFilter(watched, event);
0190 }