File indexing completed on 2024-05-12 05:07:50
0001 /* 0002 SPDX-FileCopyrightText: 2015-2019 Thomas Baumgart <tbaumgart@kde.org> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "specialdatedelegate.h" 0007 0008 // ---------------------------------------------------------------------------- 0009 // QT Includes 0010 0011 #include <QApplication> 0012 #include <QScrollBar> 0013 #include <QPainter> 0014 #include <QDate> 0015 #include <QSortFilterProxyModel> 0016 #include <QColor> 0017 #include <QDebug> 0018 0019 // ---------------------------------------------------------------------------- 0020 // KDE Includes 0021 0022 #include <KLocalizedString> 0023 #include <KColorScheme> 0024 0025 // ---------------------------------------------------------------------------- 0026 // Project Includes 0027 0028 #include "mymoneyfile.h" 0029 #include "ledgerview.h" 0030 #include "journalmodel.h" 0031 #include "payeesmodel.h" 0032 #include "newtransactioneditor.h" 0033 0034 QColor SpecialDateDelegate::m_separatorColor = QColor(0xff, 0xf2, 0x9b); 0035 0036 0037 0038 0039 class SpecialDateDelegate::Private 0040 { 0041 public: 0042 Private() 0043 : m_lineHeight(12) 0044 , m_margin(2) 0045 0046 {} 0047 0048 ~Private() 0049 { 0050 } 0051 0052 int m_lineHeight; 0053 int m_margin; 0054 }; 0055 0056 0057 SpecialDateDelegate::SpecialDateDelegate(LedgerView* parent) 0058 : KMMStyledItemDelegate(parent) 0059 , d(new Private) 0060 { 0061 } 0062 0063 SpecialDateDelegate::~SpecialDateDelegate() 0064 { 0065 delete d; 0066 } 0067 0068 void SpecialDateDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const 0069 { 0070 QStyleOptionViewItem opt = option; 0071 initStyleOption(&opt, index); 0072 0073 // never change the background of the cell the mouse is hovering over 0074 opt.state &= ~QStyle::State_MouseOver; 0075 0076 // never show the focus 0077 opt.state &= ~QStyle::State_HasFocus; 0078 0079 // always draw it as selected and enabled but inactive 0080 opt.state |= QStyle::State_Selected; 0081 opt.state |= QStyle::State_Enabled; 0082 opt.state &= ~QStyle::State_Active; 0083 0084 painter->save(); 0085 0086 QAbstractItemView* view = qobject_cast< QAbstractItemView* >(parent()); 0087 0088 // Background 0089 QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); 0090 0091 opt.rect.setX(opt.rect.x()-2); 0092 opt.rect.setWidth(opt.rect.width()+5); 0093 style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget); 0094 0095 switch (index.column()) { 0096 case JournalModel::Column::Detail: 0097 // adjust the rect to cover all columns 0098 if(view && view->viewport()) { 0099 opt.rect.setX(0); 0100 opt.rect.setWidth(view->viewport()->width()); 0101 } 0102 // always paint in bold 0103 auto font = painter->font(); 0104 font.setBold(true); 0105 painter->setFont(font); 0106 const auto idx = index.model()->index(index.row(), 0, index.parent()); 0107 QString txt = idx.data(eMyMoney::Model::TransactionPostDateRole).toDate().toString(Qt::ISODate); 0108 painter->setPen(opt.palette.color(QPalette::Normal, QPalette::Text)); 0109 painter->drawText(opt.rect, Qt::AlignCenter, idx.data(Qt::DisplayRole).toString()); 0110 break; 0111 } 0112 0113 painter->restore(); 0114 } 0115 0116 QSize SpecialDateDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const 0117 { 0118 QStyleOptionViewItem opt = option; 0119 initStyleOption(&opt, index); 0120 QStyle *style = opt.widget ? opt.widget->style() : QApplication::style(); 0121 d->m_margin = style->pixelMetric(QStyle::PM_FocusFrameHMargin); 0122 d->m_lineHeight = opt.fontMetrics.lineSpacing(); 0123 0124 QSize size(10, d->m_lineHeight + 2 * d->m_margin); 0125 0126 return size; 0127 } 0128 0129 /** 0130 * This eventfilter seems to do nothing but it prevents that selecting a 0131 * different row with the mouse closes the editor 0132 */ 0133 bool SpecialDateDelegate::eventFilter(QObject* o, QEvent* event) 0134 { 0135 return QAbstractItemDelegate::eventFilter(o, event); 0136 }