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

0001 /*
0002     SPDX-FileCopyrightText: 2021 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "reconciliationdelegate.h"
0007 
0008 // ----------------------------------------------------------------------------
0009 // QT Includes
0010 
0011 #include <QAbstractItemView>
0012 #include <QApplication>
0013 #include <QDate>
0014 #include <QPainter>
0015 #include <QScrollBar>
0016 #include <QSortFilterProxyModel>
0017 
0018 // ----------------------------------------------------------------------------
0019 // KDE Includes
0020 
0021 #include <KColorScheme>
0022 #include <KLocalizedString>
0023 
0024 // ----------------------------------------------------------------------------
0025 // Project Includes
0026 
0027 #include "journalmodel.h"
0028 #include "mymoneyfile.h"
0029 #include "mymoneyutils.h"
0030 
0031 class ReconciliationDelegate::Private
0032 {
0033 public:
0034     Private()
0035     {
0036     }
0037 
0038     ~Private()
0039     {
0040     }
0041 };
0042 
0043 ReconciliationDelegate::ReconciliationDelegate(QWidget* parent)
0044     : KMMStyledItemDelegate(parent)
0045     , d(new Private)
0046 {
0047 }
0048 
0049 ReconciliationDelegate::~ReconciliationDelegate()
0050 {
0051     delete d;
0052 }
0053 
0054 void ReconciliationDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
0055 {
0056     QStyleOptionViewItem opt = option;
0057     initStyleOption(&opt, index);
0058 
0059     // never change the background of the cell the mouse is hovering over
0060     opt.state &= ~QStyle::State_MouseOver;
0061 
0062     // never show the focus
0063     opt.state &= ~QStyle::State_HasFocus;
0064 
0065     // if selected, always show as active, so that the
0066     // background does not change when the editor is shown
0067     if (opt.state & QStyle::State_Selected) {
0068         opt.state |= QStyle::State_Active;
0069     }
0070     // never draw it as selected but always enabled
0071     opt.state &= ~QStyle::State_Selected;
0072     opt.state |= QStyle::State_Enabled;
0073 
0074     painter->save();
0075 
0076     QAbstractItemView* view = qobject_cast<QAbstractItemView*>(parent());
0077 
0078     // Background
0079     QStyle* style = opt.widget ? opt.widget->style() : QApplication::style();
0080     const int margin = style->pixelMetric(QStyle::PM_FocusFrameHMargin);
0081 
0082     const QRect textArea = QRect(opt.rect.x() + margin, opt.rect.y() + margin, opt.rect.width() - 2 * margin, opt.rect.height() - 2 * margin);
0083 
0084     const auto reconciliationDate = index.data(eMyMoney::Model::TransactionPostDateRole).toDate();
0085     const auto reconciliationBalanceValue = index.data(eMyMoney::Model::ReconciliationAmountRole).value<MyMoneyMoney>();
0086     const auto accountBalance = MyMoneyFile::instance()->balance(index.data(eMyMoney::Model::SplitAccountIdRole).toString(), reconciliationDate);
0087 
0088     KColorScheme::BackgroundRole role = (accountBalance == reconciliationBalanceValue) ? KColorScheme::PositiveBackground : KColorScheme::NegativeBackground;
0089 
0090     KColorScheme::adjustBackground(opt.palette, role, QPalette::Base, KColorScheme::View, KSharedConfigPtr());
0091     // opt.rect.setHeight(lineHeight);
0092     opt.backgroundBrush = opt.palette.base();
0093 
0094     opt.rect.setX(opt.rect.x() - 2);
0095     opt.rect.setWidth(opt.rect.width() + 5);
0096     style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
0097 
0098     painter->setFont(opt.font);
0099     switch (index.column()) {
0100     case JournalModel::Column::Detail:
0101         // adjust the rect to cover all columns
0102         if (view && view->viewport()) {
0103             opt.rect.setX(0);
0104             opt.rect.setWidth(view->viewport()->width());
0105         }
0106         painter->setPen(opt.palette.color(QPalette::Normal, QPalette::Text));
0107         painter->drawText(opt.rect, Qt::AlignCenter, i18nc("Ledger marker showing a reconciliation entry", "Reconciliation"));
0108         break;
0109     case JournalModel::Column::Date:
0110         painter->setPen(opt.palette.color(QPalette::Normal, QPalette::Text));
0111         painter->drawText(textArea, opt.displayAlignment, MyMoneyUtils::formatDate(reconciliationDate));
0112         break;
0113     case JournalModel::Column::Balance:
0114         painter->setPen(opt.palette.color(QPalette::Normal, QPalette::Text));
0115         painter->drawText(textArea, opt.displayAlignment, index.data(eMyMoney::Model::ReconciliationBalanceRole).toString());
0116         break;
0117     }
0118 
0119     painter->restore();
0120 }
0121 
0122 QSize ReconciliationDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
0123 {
0124     QStyleOptionViewItem opt = option;
0125     initStyleOption(&opt, index);
0126     QStyle* style = opt.widget ? opt.widget->style() : QApplication::style();
0127     const auto margin = style->pixelMetric(QStyle::PM_FocusFrameHMargin);
0128     const auto lineHeight = opt.fontMetrics.lineSpacing();
0129 
0130     return QSize(10, lineHeight + 2 * margin);
0131 }
0132 
0133 /**
0134  * This eventfilter seems to do nothing but it prevents that selecting a
0135  * different row with the mouse closes the editor
0136  */
0137 bool ReconciliationDelegate::eventFilter(QObject* o, QEvent* event)
0138 {
0139     return QAbstractItemDelegate::eventFilter(o, event);
0140 }