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

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