File indexing completed on 2024-02-18 03:58:08

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2007-2008 Rafael Fernández López <ereslibre@kde.org>
0004     SPDX-FileCopyrightText: 2008 Kevin Ottens <ervin@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KWIDGETITEMDELEGATE_H
0010 #define KWIDGETITEMDELEGATE_H
0011 
0012 #include <QAbstractItemDelegate>
0013 #include <QEvent>
0014 #include <QList>
0015 #include <QPersistentModelIndex>
0016 #include <memory>
0017 
0018 #include <kitemviews_export.h>
0019 
0020 class QObject;
0021 class QPainter;
0022 class QStyleOption;
0023 class QStyleOptionViewItem;
0024 class QAbstractItemView;
0025 class QItemSelection;
0026 
0027 class KWidgetItemDelegatePrivate;
0028 class KWidgetItemDelegatePool;
0029 
0030 /**
0031  * @class KWidgetItemDelegate kwidgetitemdelegate.h KWidgetItemDelegate
0032  *
0033  * This class allows to create item delegates embedding simple widgets to interact
0034  * with items. For instance you can add push buttons, line edits, etc. to your delegate
0035  * and use them to modify the state of your model.
0036  *
0037  * Porting from KF5 to KF6:
0038  *
0039  * The signature of the virtual method
0040  * KWidgetItemDelegate::updateItemWidgets(const QList<QWidget *>, const QStyleOptionViewItem &, const QPersistentModelIndex &) const
0041  * was changed to ;
0042  * KWidgetItemDelegate::updateItemWidgets(const QList<QWidget *> &, const QStyleOptionViewItem &, const QPersistentModelIndex &) const.
0043  *
0044  * @since 4.1
0045  */
0046 class KITEMVIEWS_EXPORT KWidgetItemDelegate : public QAbstractItemDelegate
0047 {
0048     Q_OBJECT
0049 
0050 public:
0051     /**
0052      * Creates a new ItemDelegate to be used with a given itemview.
0053      *
0054      * @param itemView the item view the new delegate will monitor
0055      * @param parent the parent of this delegate
0056      */
0057     explicit KWidgetItemDelegate(QAbstractItemView *itemView, QObject *parent = nullptr);
0058 
0059     /**
0060      * Destroys an ItemDelegate.
0061      */
0062     ~KWidgetItemDelegate() override;
0063 
0064     /**
0065      * Retrieves the item view this delegate is monitoring.
0066      *
0067      * @return the item view this delegate is monitoring
0068      */
0069     QAbstractItemView *itemView() const;
0070 
0071     /**
0072      * Retrieves the currently focused index. An invalid index if none is focused.
0073      *
0074      * @return the current focused index, or QPersistentModelIndex() if none is focused.
0075      */
0076     QPersistentModelIndex focusedIndex() const;
0077 
0078     /**
0079      * trigger a modelReset
0080      */
0081     void resetModel();
0082 
0083 protected:
0084     /**
0085      * Creates the list of widgets needed for an item.
0086      *
0087      * @note No initialization of the widgets is supposed to happen here.
0088      *       The widgets will be initialized based on needs for a given item.
0089      *
0090      * @note If you want to connect some widget signals to any slot, you should
0091      *       do it here.
0092      *
0093      * @param index the index to create widgets for
0094      *
0095      * @return the list of newly created widgets which will be used to interact with an item.
0096      * @see updateItemWidgets()
0097      */
0098     virtual QList<QWidget *> createItemWidgets(const QModelIndex &index) const = 0;
0099 
0100     /**
0101      * Updates a list of widgets for its use inside of the delegate (painting or
0102      * event handling).
0103      *
0104      * @note All the positioning and sizing should be done in item coordinates.
0105      *
0106      * @warning Do not make widget connections in here, since this method will
0107      * be called very regularly.
0108      *
0109      * @param widgets the widgets to update
0110      * @param option the current set of style options for the view.
0111      * @param index the model index of the item currently manipulated.
0112      */
0113     virtual void updateItemWidgets(const QList<QWidget *> &widgets, const QStyleOptionViewItem &option, const QPersistentModelIndex &index) const = 0;
0114 
0115     /**
0116      * Sets the list of event @p types that a @p widget will block.
0117      *
0118      * Blocked events are not passed to the view. This way you can prevent an item
0119      * from being selected when a button is clicked for instance.
0120      *
0121      * @param widget the widget which must block events
0122      * @param types the list of event types the widget must block
0123      */
0124     void setBlockedEventTypes(QWidget *widget, const QList<QEvent::Type> &types) const;
0125 
0126     /**
0127      * Retrieves the list of blocked event types for the given widget.
0128      *
0129      * @param widget the specified widget.
0130      *
0131      * @return the list of blocked event types, can be empty if no events are blocked.
0132      */
0133     QList<QEvent::Type> blockedEventTypes(QWidget *widget) const;
0134 
0135 private:
0136     //@cond PRIVATE
0137     friend class KWidgetItemDelegatePool;
0138     friend class KWidgetItemDelegateEventListener;
0139     std::unique_ptr<class KWidgetItemDelegatePrivate> const d;
0140 
0141     Q_PRIVATE_SLOT(d, void _k_slotRowsInserted(const QModelIndex &, int, int))
0142     Q_PRIVATE_SLOT(d, void _k_slotRowsAboutToBeRemoved(const QModelIndex &, int, int))
0143     Q_PRIVATE_SLOT(d, void _k_slotRowsRemoved(const QModelIndex &, int, int))
0144     Q_PRIVATE_SLOT(d, void _k_slotDataChanged(const QModelIndex &, const QModelIndex &))
0145     Q_PRIVATE_SLOT(d, void _k_slotLayoutChanged())
0146     Q_PRIVATE_SLOT(d, void _k_slotModelReset())
0147     Q_PRIVATE_SLOT(d, void _k_slotSelectionChanged(const QItemSelection &, const QItemSelection &))
0148     //@endcond
0149 };
0150 
0151 #endif