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 #ifndef DELEGATEPROXY_H
0008 #define DELEGATEPROXY_H
0009 
0010 #include "kmm_models_export.h"
0011 
0012 // ----------------------------------------------------------------------------
0013 // Qt Includes
0014 
0015 #include <QObject>
0016 
0017 // ----------------------------------------------------------------------------
0018 // KDE Includes
0019 
0020 // ----------------------------------------------------------------------------
0021 // Project Includes
0022 
0023 #include "kmmstyleditemdelegate.h"
0024 #include "mymoneyenums.h"
0025 
0026 class DelegateProxyPrivate;
0027 
0028 /**
0029  * Delegate Proxy object
0030  *
0031  * The delegate proxy object allows to assign a specific delegate in a view
0032  * depending on the model where the item is located. This becomes handy in case
0033  * the items shown are combined using e.g. a QConcatenateTablesProxyModel and
0034  * each base model should have its own delegate.
0035  */
0036 class KMM_MODELS_EXPORT DelegateProxy : public QStyledItemDelegate
0037 {
0038     Q_OBJECT
0039     Q_DISABLE_COPY(DelegateProxy)
0040 public:
0041     /**
0042      * This method creates a new DelegateProxy as child of @a parent.
0043      */
0044     explicit DelegateProxy(QObject* parent = nullptr);
0045 
0046     /**
0047      * This method adds @a delegate as the delegate to be used when the item
0048      * to be shown/edited is located in a model that returns @a role when asked
0049      * for DelegateRole. The delegate is still owned by the caller. It is not
0050      * destroyed when the DelegateProxy is destroyed.
0051      */
0052     void addDelegate(eMyMoney::Delegates::Types role, KMMStyledItemDelegate* delegate);
0053 
0054     /**
0055      * Retrieve the delegate for objects stored in @a model. In case no
0056      * assignment exists, @c nullptr is returned.
0057      */
0058     const QStyledItemDelegate* delegate(const QModelIndex& idx) const;
0059 
0060     /**
0061      * This method returns a list of pointers to all registered delegates
0062      *
0063      * @return QList<QStyledItemDelegate> of all delegates
0064      */
0065     QList<QStyledItemDelegate*> delegateList() const;
0066 
0067 
0068     void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const final override;
0069     QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const final override;
0070     QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const final override;
0071     void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const final override;
0072     void setEditorData(QWidget* editWidget, const QModelIndex& index) const final override;
0073 
0074     void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const final override;
0075     bool helpEvent ( QHelpEvent* event, QAbstractItemView* view, const QStyleOptionViewItem& option, const QModelIndex& index ) override;
0076     bool editorEvent ( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index ) override;
0077     void destroyEditor ( QWidget* editor, const QModelIndex& index ) const override;
0078 
0079     bool eventFilter ( QObject* watched, QEvent* event ) override;
0080 
0081 private:
0082     DelegateProxyPrivate * const d_ptr;
0083     Q_DECLARE_PRIVATE(DelegateProxy);
0084 };
0085 
0086 #endif // DELEGATEPROXY_H