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

0001 /*
0002     SPDX-FileCopyrightText: 2021 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "ktransactionselectdlg.h"
0007 
0008 // ----------------------------------------------------------------------------
0009 // QT Includes
0010 
0011 // ----------------------------------------------------------------------------
0012 // KDE Includes
0013 
0014 // ----------------------------------------------------------------------------
0015 // Project Includes
0016 
0017 #include "ui_ktransactionselectdlg.h"
0018 
0019 #include "icons.h"
0020 #include "journalmodel.h"
0021 #include "ledgerjournalidfilter.h"
0022 #include "mymoneyfile.h"
0023 
0024 class KTransactionSelectDlgPrivate
0025 {
0026     Q_DISABLE_COPY(KTransactionSelectDlgPrivate)
0027     Q_DECLARE_PUBLIC(KTransactionSelectDlg)
0028 public:
0029     explicit KTransactionSelectDlgPrivate(KTransactionSelectDlg* qq)
0030         : q_ptr(qq)
0031         , ui(new Ui::KTransactionSelectDlg)
0032         , filterModel(new LedgerJournalIdFilter(qq, QVector<QAbstractItemModel*>{}))
0033         , sortOrder(Qt::AscendingOrder)
0034     {
0035     }
0036 
0037     ~KTransactionSelectDlgPrivate()
0038     {
0039         delete ui;
0040     }
0041 
0042     KTransactionSelectDlg* q_ptr;
0043     Ui::KTransactionSelectDlg* ui;
0044     LedgerJournalIdFilter* filterModel;
0045     Qt::SortOrder sortOrder;
0046 };
0047 
0048 KTransactionSelectDlg::KTransactionSelectDlg(QWidget* parent)
0049     : QDialog(parent)
0050     , d_ptr(new KTransactionSelectDlgPrivate(this))
0051 {
0052     Q_D(KTransactionSelectDlg);
0053     d->ui->setupUi(this);
0054     d->ui->switchButton->hide();
0055 
0056     d->ui->label->setText(i18nc("@info:label Description of select transaction dialog", "Select the transaction to use as template."));
0057 
0058     d->filterModel->setSourceModel(MyMoneyFile::instance()->journalModel());
0059     d->ui->m_ledgerView->setModel(d->filterModel);
0060     // don't show sort indicator and don't allow sorting via header
0061     d->ui->m_ledgerView->horizontalHeader()->setSortIndicatorShown(false);
0062     d->ui->m_ledgerView->setSortingEnabled(false);
0063     // we don't allow editing in this dialog
0064     d->filterModel->setLedgerIsEditable(false);
0065 
0066     QVector<int> columns;
0067     columns = {
0068         JournalModel::Column::Number,
0069         JournalModel::Column::Account,
0070         JournalModel::Column::Security,
0071         JournalModel::Column::CostCenter,
0072         JournalModel::Column::Quantity,
0073         JournalModel::Column::Price,
0074         JournalModel::Column::Amount,
0075         JournalModel::Column::Value,
0076         JournalModel::Column::Balance,
0077     };
0078     d->ui->m_ledgerView->setColumnsHidden(columns);
0079     columns = {
0080         JournalModel::Column::Date,
0081         JournalModel::Column::Detail,
0082         JournalModel::Column::Reconciliation,
0083         JournalModel::Column::Payment,
0084         JournalModel::Column::Deposit,
0085     };
0086     d->ui->m_ledgerView->setColumnsShown(columns);
0087     d->filterModel->setSortRole(eMyMoney::Model::IdRole);
0088     d->filterModel->setSortLocaleAware(true);
0089     d->filterModel->sort(JournalModel::Column::Date, Qt::AscendingOrder);
0090     d->filterModel->setDynamicSortFilter(true);
0091 }
0092 
0093 KTransactionSelectDlg::~KTransactionSelectDlg()
0094 {
0095     Q_D(KTransactionSelectDlg);
0096     delete d;
0097 }
0098 
0099 void KTransactionSelectDlg::addTransaction(const QString& journalEntryId)
0100 {
0101     Q_D(KTransactionSelectDlg);
0102     d->filterModel->appendFilterFixedString(journalEntryId);
0103 }
0104 
0105 QString KTransactionSelectDlg::journalEntryId() const
0106 {
0107     Q_D(const KTransactionSelectDlg);
0108     const auto selection = d->ui->m_ledgerView->selectedJournalEntryIds();
0109     if (!selection.isEmpty()) {
0110         return selection.first();
0111     }
0112     return {};
0113 }
0114 
0115 LedgerView* KTransactionSelectDlg::ledgerView() const
0116 {
0117     Q_D(const KTransactionSelectDlg);
0118     return d->ui->m_ledgerView;
0119 }
0120 
0121 KTransactionMergeDlg::KTransactionMergeDlg(QWidget* parent)
0122     : KTransactionSelectDlg(parent)
0123 {
0124     Q_D(KTransactionSelectDlg);
0125     // setup descriptive texts
0126     setWindowTitle(i18n("Merge Transactions"));
0127     d->ui->label->setText(i18nc("@info:label Description of merge transaction dialog",
0128                                 "The two selected transactions to merge are shown. The one at "
0129                                 "the top and its values will be used in the resulting "
0130                                 "merged transaction.  If you want to use the other one "
0131                                 "press the exchange button on the right."));
0132 
0133     d->ui->switchButton->setIcon(Icons::get(Icons::Icon::ItemExchange));
0134     d->ui->switchButton->setToolTip(i18nc("@info:tooltip Exchange transactions about to merge", "Press to exchange the two transactions."));
0135     d->ui->switchButton->show();
0136 
0137     connect(d->ui->switchButton, &QAbstractButton::clicked, this, [&]() {
0138         Q_D(KTransactionSelectDlg);
0139         // swap order
0140         d->sortOrder = (d->sortOrder == Qt::DescendingOrder) ? Qt::AscendingOrder : Qt::DescendingOrder;
0141         d->filterModel->setSortRole(eMyMoney::Model::IdRole);
0142         d->filterModel->QSortFilterProxyModel::sort(0, d->sortOrder);
0143 
0144         // and reselect the first entry
0145         const auto idx = d->filterModel->index(0, 0);
0146         d->ui->m_ledgerView->setSelectedJournalEntries(QStringList(idx.data(eMyMoney::Model::IdRole).toString()));
0147     });
0148 }
0149 
0150 void KTransactionMergeDlg::addTransaction(const QString& journalEntryId)
0151 {
0152     Q_D(KTransactionSelectDlg);
0153 
0154     KTransactionSelectDlg::addTransaction(journalEntryId);
0155     d->filterModel->QSortFilterProxyModel::sort(0, d->sortOrder);
0156     const auto idx = d->filterModel->index(0, 0);
0157     d->ui->m_ledgerView->setSelectedJournalEntries(QStringList(idx.data(eMyMoney::Model::IdRole).toString()));
0158 }
0159 
0160 QString KTransactionMergeDlg::remainingTransactionId() const
0161 {
0162     Q_D(const KTransactionSelectDlg);
0163 
0164     if (d->filterModel->rowCount() == 2) {
0165         const auto idx = d->filterModel->index(0, 0);
0166         return idx.data(eMyMoney::Model::IdRole).toString();
0167     }
0168     return {};
0169 }
0170 
0171 QString KTransactionMergeDlg::mergedTransactionId() const
0172 {
0173     Q_D(const KTransactionSelectDlg);
0174 
0175     if (d->filterModel->rowCount() == 2) {
0176         const auto idx = d->filterModel->index(1, 0);
0177         return idx.data(eMyMoney::Model::IdRole).toString();
0178     }
0179     return {};
0180 }