File indexing completed on 2024-05-19 05:06:57

0001 /*
0002     SPDX-FileCopyrightText: 2019 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 
0007 #include "delegateproxy.h"
0008 
0009 // ----------------------------------------------------------------------------
0010 // Qt Includes
0011 
0012 #include <QDebug>
0013 #include <QHash>
0014 #include <QSortFilterProxyModel>
0015 class QAbstractItemDelegate;
0016 class QAbstractItemModel;
0017 class QStyleOption;
0018 
0019 // ----------------------------------------------------------------------------
0020 // KDE Includes
0021 
0022 // ----------------------------------------------------------------------------
0023 // Project Includes
0024 
0025 #include "mymoneyenums.h"
0026 #include "mymoneyfile.h"
0027 
0028 class DelegateProxyPrivate
0029 {
0030     Q_DISABLE_COPY(DelegateProxyPrivate)
0031     Q_DECLARE_PUBLIC(DelegateProxy)
0032 
0033 public:
0034     explicit DelegateProxyPrivate(DelegateProxy *qq)
0035         : q_ptr(qq)
0036     {
0037     }
0038 
0039     const KMMStyledItemDelegate* findDelegate(const QModelIndex& idx) const
0040     {
0041         // create an index for column 0
0042         if (idx.isValid()) {
0043             return mapping.value(idx.data(eMyMoney::Model::DelegateRole).toInt(), nullptr);
0044         }
0045         return nullptr;
0046     }
0047 
0048     DelegateProxy* q_ptr;
0049     QHash<int, const KMMStyledItemDelegate*> mapping;
0050 };
0051 
0052 
0053 DelegateProxy::DelegateProxy(QObject* parent)
0054     : QStyledItemDelegate(parent)
0055     , d_ptr(new DelegateProxyPrivate(this))
0056 {
0057 }
0058 
0059 void DelegateProxy::addDelegate(eMyMoney::Delegates::Types role, KMMStyledItemDelegate* delegate)
0060 {
0061     Q_D(DelegateProxy);
0062     const auto roleAsInt = static_cast<int>(role);
0063 
0064     // in case we have a mapping for the model, we remove it
0065     if (d->mapping.contains(roleAsInt)) {
0066         d->mapping.remove(roleAsInt);
0067     }
0068     if (delegate) {
0069         d->mapping[roleAsInt] = delegate;
0070         connect(delegate, &QAbstractItemDelegate::commitData, this, &QAbstractItemDelegate::commitData, Qt::UniqueConnection);
0071         connect(delegate, &QAbstractItemDelegate::closeEditor, this, &QAbstractItemDelegate::closeEditor, Qt::UniqueConnection);
0072         connect(delegate, &QAbstractItemDelegate::sizeHintChanged, this, &QAbstractItemDelegate::sizeHintChanged, Qt::UniqueConnection);
0073     }
0074 }
0075 
0076 const QStyledItemDelegate * DelegateProxy::delegate(const QModelIndex& idx) const
0077 {
0078     Q_D(const DelegateProxy);
0079     if (idx.isValid()) {
0080         return d->mapping.value(idx.data(eMyMoney::Model::DelegateRole).toInt(), nullptr);
0081     }
0082     return nullptr;
0083 }
0084 
0085 void DelegateProxy::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
0086 {
0087     Q_D(const DelegateProxy);
0088     const auto delegate = d->findDelegate(index);
0089     if (delegate) {
0090         delegate->paint(painter, option, index);
0091     }
0092 }
0093 
0094 QSize DelegateProxy::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
0095 {
0096     Q_D(const DelegateProxy);
0097     const auto delegate = d->findDelegate(index);
0098     if (delegate) {
0099         return delegate->sizeHint(option, index);
0100     }
0101     return QSize();
0102 }
0103 
0104 QWidget* DelegateProxy::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
0105 {
0106     Q_D(const DelegateProxy);
0107     const auto delegate = d->findDelegate(index);
0108     if (delegate) {
0109         return delegate->createEditor(parent, option, index);
0110     }
0111     return nullptr;
0112 }
0113 
0114 void DelegateProxy::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const
0115 {
0116     Q_D(const DelegateProxy);
0117     const auto delegate = d->findDelegate(index);
0118     if (delegate) {
0119         delegate->updateEditorGeometry(editor, option, index);
0120     }
0121 }
0122 
0123 void DelegateProxy::setEditorData(QWidget* editWidget, const QModelIndex& index) const
0124 {
0125     Q_D(const DelegateProxy);
0126     const auto delegate = d->findDelegate(index);
0127     if (delegate) {
0128         delegate->setEditorData(editWidget, index);
0129     }
0130 }
0131 
0132 void DelegateProxy::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
0133 {
0134     Q_D(const DelegateProxy);
0135     const auto delegate = d->findDelegate(index);
0136     if (delegate) {
0137         delegate->setModelData(editor, model, index);
0138     }
0139 }
0140 
0141 void DelegateProxy::destroyEditor(QWidget* editor, const QModelIndex& index) const
0142 {
0143     Q_D(const DelegateProxy);
0144     const auto delegate = d->findDelegate(index);
0145     if (delegate) {
0146         delegate->destroyEditor(editor, index);
0147     }
0148 }
0149 
0150 bool DelegateProxy::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index)
0151 {
0152     Q_D(const DelegateProxy);
0153     auto delegate = const_cast<KMMStyledItemDelegate*>(d->findDelegate(index));
0154     if (delegate) {
0155         return delegate->editorEvent(event, model, option, index);
0156     }
0157     return false;
0158 }
0159 
0160 bool DelegateProxy::helpEvent(QHelpEvent* event, QAbstractItemView* view, const QStyleOptionViewItem& option, const QModelIndex& index)
0161 {
0162     Q_D(DelegateProxy);
0163     auto delegate = const_cast<KMMStyledItemDelegate*>(d->findDelegate(index));
0164     if (delegate) {
0165         return delegate->helpEvent(event, view, option, index);
0166     }
0167     return false;
0168 }
0169 
0170 bool DelegateProxy::eventFilter(QObject* watched, QEvent* event)
0171 {
0172     Q_D(DelegateProxy);
0173     bool rc = false;
0174     for (const auto& delegate : qAsConst(d->mapping)) {
0175         rc |= const_cast<KMMStyledItemDelegate*>(delegate)->eventFilter(watched, event);
0176         if (rc)
0177             break;
0178     }
0179     return rc;
0180 }