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

0001 /*
0002     SPDX-FileCopyrightText: 2002-2018 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 "kfindtransactiondlg.h"
0008 #include "kfindtransactiondlg_p.h"
0009 
0010 // ----------------------------------------------------------------------------
0011 // QT Includes
0012 
0013 #include <QLabel>
0014 #include <QRadioButton>
0015 #include <QCheckBox>
0016 #include <QTabWidget>
0017 #include <QKeyEvent>
0018 #include <QList>
0019 #include <QEvent>
0020 #include <QPushButton>
0021 #include <QDialogButtonBox>
0022 
0023 // ----------------------------------------------------------------------------
0024 // KDE Includes
0025 
0026 #include <KLineEdit>
0027 #include <KComboBox>
0028 #include <KHelpClient>
0029 #include <KLocalizedString>
0030 
0031 // ----------------------------------------------------------------------------
0032 // Project Includes
0033 
0034 #include "mymoneysplit.h"
0035 #include "mymoneytransaction.h"
0036 #include "mymoneytransactionfilter.h"
0037 #include "kmymoneysettings.h"
0038 #include "register.h"
0039 #include "transaction.h"
0040 #include "daterangedlg.h"
0041 
0042 #include "ui_kfindtransactiondlg.h"
0043 #include "ui_ksortoptiondlg.h"
0044 
0045 KSortOptionDlg::KSortOptionDlg(QWidget *parent) :
0046     QDialog(parent),
0047     ui(new Ui::KSortOptionDlg)
0048 {
0049     ui->setupUi(this);
0050 }
0051 
0052 KSortOptionDlg::~KSortOptionDlg()
0053 {
0054     delete ui;
0055 }
0056 
0057 void KSortOptionDlg::setSortOption(const QString& option, const QString& def)
0058 {
0059     if (option.isEmpty()) {
0060         ui->m_sortOption->setSettings(def);
0061         ui->m_useDefault->setChecked(true);
0062     } else {
0063         ui->m_sortOption->setSettings(option);
0064         ui->m_useDefault->setChecked(false);
0065     }
0066 }
0067 
0068 QString KSortOptionDlg::sortOption() const
0069 {
0070     QString rc;
0071     if (!ui->m_useDefault->isChecked()) {
0072         rc = ui->m_sortOption->settings();
0073     }
0074     return rc;
0075 }
0076 
0077 void KSortOptionDlg::hideDefaultButton()
0078 {
0079     ui->m_useDefault->hide();
0080 }
0081 
0082 KFindTransactionDlg::KFindTransactionDlg(QWidget *parent, bool withEquityAccounts) :
0083     QDialog(parent),
0084     d_ptr(new KFindTransactionDlgPrivate(this))
0085 {
0086     Q_D(KFindTransactionDlg);
0087     d->init(withEquityAccounts);
0088 }
0089 
0090 KFindTransactionDlg::KFindTransactionDlg(KFindTransactionDlgPrivate &dd, QWidget *parent, bool withEquityAccounts) :
0091     QDialog(parent),
0092     d_ptr(&dd)
0093 {
0094     Q_D(KFindTransactionDlg);
0095     d->init(withEquityAccounts);
0096 }
0097 
0098 KFindTransactionDlg::~KFindTransactionDlg()
0099 {
0100     Q_D(KFindTransactionDlg);
0101     delete d;
0102 }
0103 
0104 void KFindTransactionDlg::slotReset()
0105 {
0106     Q_D(KFindTransactionDlg);
0107     d->m_tabFilters->slotReset();
0108 }
0109 
0110 void KFindTransactionDlg::slotSearch()
0111 {
0112     Q_D(KFindTransactionDlg);
0113     // perform the search only if the button is enabled
0114     if (!d->ui->buttonBox->button(QDialogButtonBox::Open)->isEnabled())
0115         return;
0116 
0117     // setup the filter from the dialog widgets
0118     d->m_filter = d->m_tabFilters->setupFilter();
0119 
0120     // filter is setup, now fill the register
0121     slotRefreshView();
0122 
0123     d->ui->m_register->setFocus();
0124 }
0125 
0126 void KFindTransactionDlg::slotRefreshView()
0127 {
0128     Q_D(KFindTransactionDlg);
0129     d->m_needReload = true;
0130     if (isVisible()) {
0131         d->loadView();
0132         d->m_needReload = false;
0133     }
0134 }
0135 
0136 void KFindTransactionDlg::showEvent(QShowEvent* event)
0137 {
0138     Q_D(KFindTransactionDlg);
0139     if (d->m_needReload) {
0140         d->loadView();
0141         d->m_needReload = false;
0142     }
0143     QDialog::showEvent(event);
0144 }
0145 
0146 void KFindTransactionDlg::slotRightSize()
0147 {
0148     Q_D(KFindTransactionDlg);
0149     d->ui->m_register->update();
0150 }
0151 
0152 void KFindTransactionDlg::resizeEvent(QResizeEvent* ev)
0153 {
0154     Q_D(KFindTransactionDlg);
0155     // Columns
0156     // 1 = Date
0157     // 2 = Account
0158     // 4 = Detail
0159     // 5 = C
0160     // 6 = Payment
0161     // 7 = Deposit
0162 
0163     // don't forget the resizer
0164     QDialog::resizeEvent(ev);
0165 
0166     if (!d->ui->m_register->isVisible())
0167         return;
0168 
0169     // resize the register
0170     int w = d->ui->m_register->contentsRect().width();
0171 
0172     int m_debitWidth = 80;
0173     int m_creditWidth = 80;
0174 
0175     d->ui->m_register->adjustColumn(1);
0176     d->ui->m_register->adjustColumn(2);
0177     d->ui->m_register->adjustColumn(5);
0178 
0179     d->ui->m_register->setColumnWidth(6, m_debitWidth);
0180     d->ui->m_register->setColumnWidth(7, m_creditWidth);
0181 
0182     for (auto i = 0; i < d->ui->m_register->columnCount(); ++i) {
0183         switch (i) {
0184         case 4:     // skip the one, we want to set
0185             break;
0186         default:
0187             w -= d->ui->m_register->columnWidth(i);
0188             break;
0189         }
0190     }
0191 
0192     d->ui->m_register->setColumnWidth(4, w);
0193 }
0194 
0195 void KFindTransactionDlg::slotSelectTransaction()
0196 {
0197     Q_D(KFindTransactionDlg);
0198     auto list = d->ui->m_register->selectedItems();
0199     if (!list.isEmpty()) {
0200         KMyMoneyRegister::Transaction* t = dynamic_cast<KMyMoneyRegister::Transaction*>(list[0]);
0201         if (t) {
0202             emit transactionSelected(t->split().accountId(), t->transaction().id());
0203             hide();
0204         }
0205     }
0206 }
0207 
0208 bool KFindTransactionDlg::eventFilter(QObject* o, QEvent* e)
0209 {
0210     Q_D(KFindTransactionDlg);
0211     auto rc = false;
0212 
0213     if (o->isWidgetType()) {
0214         if (e->type() == QEvent::KeyPress) {
0215             const QWidget* w = dynamic_cast<const QWidget*>(o);
0216             QKeyEvent *k = static_cast<QKeyEvent *>(e);
0217             if (w == d->ui->m_register) {
0218                 switch (k->key()) {
0219                 default:
0220                     break;
0221 
0222                 case Qt::Key_Return:
0223                 case Qt::Key_Enter:
0224                     rc = true;
0225                     slotSelectTransaction();
0226                     break;
0227                 }
0228             }
0229         }
0230     }
0231     return rc;
0232 }
0233 
0234 void KFindTransactionDlg::slotShowHelp()
0235 {
0236     Q_D(KFindTransactionDlg);
0237     if (d->ui->m_tabWidget->currentIndex() == 0)
0238         d->m_tabFilters->slotShowHelp();
0239 }
0240 
0241 void KFindTransactionDlg::slotSortOptions()
0242 {
0243     QPointer<KSortOptionDlg> dlg = new KSortOptionDlg(this);
0244 
0245     dlg->setSortOption(KMyMoneySettings::sortSearchView(), QString());
0246     dlg->hideDefaultButton();
0247 
0248     if (dlg->exec() == QDialog::Accepted) {
0249         QString sortOrder = dlg->sortOption();
0250         if (sortOrder != KMyMoneySettings::sortSearchView()) {
0251             KMyMoneySettings::setSortSearchView(sortOrder);
0252             slotRefreshView();
0253         }
0254     }
0255     delete dlg;
0256 }