1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2007-2008 Rafael Fernández López <ereslibre@kde.org>
4 SPDX-FileCopyrightText: 2008 Kevin Ottens <ervin@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#ifndef KWIDGETITEMDELEGATE_H
10#define KWIDGETITEMDELEGATE_H
11
12#include <QAbstractItemDelegate>
13#include <QEvent>
14#include <QList>
15#include <QPersistentModelIndex>
16#include <memory>
17
18#include <kitemviews_export.h>
19
20class QObject;
21class QPainter;
22class QStyleOption;
23class QStyleOptionViewItem;
24class QAbstractItemView;
25class QItemSelection;
26
27class KWidgetItemDelegatePrivate;
28class KWidgetItemDelegatePool;
29
30/*!
31 * \class KWidgetItemDelegate
32 * \inmodule KItemViews
33 *
34 * \brief This class allows to create item delegates embedding simple widgets to interact
35 * with items.
36 *
37 * For instance you can add push buttons, line edits, etc. to your delegate
38 * and use them to modify the state of your model.
39 *
40 * Porting from KF5 to KF6:
41 *
42 * The signature of the virtual method
43 * KWidgetItemDelegate::updateItemWidgets(const QList<QWidget *>, const QStyleOptionViewItem &, const QPersistentModelIndex &) const
44 * was changed to ;
45 * KWidgetItemDelegate::updateItemWidgets(const QList<QWidget *> &, const QStyleOptionViewItem &, const QPersistentModelIndex &) const.
46 *
47 * \since 4.1
48 */
49class KITEMVIEWS_EXPORT KWidgetItemDelegate : public QAbstractItemDelegate
50{
51 Q_OBJECT
52
53public:
54 /*!
55 * Creates a new ItemDelegate to be used with a given itemview.
56 *
57 * \a itemView the item view the new delegate will monitor
58 *
59 * \a parent the parent of this delegate
60 */
61 explicit KWidgetItemDelegate(QAbstractItemView *itemView, QObject *parent = nullptr);
62
63 ~KWidgetItemDelegate() override;
64
65 /*!
66 * Retrieves the item view this delegate is monitoring.
67 */
68 QAbstractItemView *itemView() const;
69
70 /*!
71 * Retrieves the currently focused index. An invalid index if none is focused.
72 */
73 QPersistentModelIndex focusedIndex() const;
74
75 /*!
76 * trigger a modelReset
77 */
78 void resetModel();
79
80protected:
81 /*!
82 * Creates the list of widgets needed for an item.
83 *
84 * \note No initialization of the widgets is supposed to happen here.
85 * The widgets will be initialized based on needs for a given item.
86 *
87 * \note If you want to connect some widget signals to any slot, you should
88 * do it here.
89 *
90 * \a index the index to create widgets for
91 *
92 * Returns the list of newly created widgets which will be used to interact with an item.
93 * \sa updateItemWidgets()
94 */
95 virtual QList<QWidget *> createItemWidgets(const QModelIndex &index) const = 0;
96
97 /*!
98 * Updates a list of widgets for its use inside of the delegate (painting or
99 * event handling).
100 *
101 * \note All the positioning and sizing should be done in item coordinates.
102 *
103 * \warning Do not make widget connections in here, since this method will
104 * be called very regularly.
105 *
106 * \a widgets the widgets to update
107 *
108 * \a option the current set of style options for the view.
109 *
110 * \a index the model index of the item currently manipulated.
111 */
112 virtual void updateItemWidgets(const QList<QWidget *> &widgets, const QStyleOptionViewItem &option, const QPersistentModelIndex &index) const = 0;
113
114 /*!
115 * Sets the list of event \a types that a \a widget will block.
116 *
117 * Blocked events are not passed to the view. This way you can prevent an item
118 * from being selected when a button is clicked for instance.
119 *
120 * \a widget the widget which must block events
121 *
122 * \a types the list of event types the widget must block
123 */
124 void setBlockedEventTypes(QWidget *widget, const QList<QEvent::Type> &types) const;
125
126 /*!
127 * Retrieves the list of blocked event types for the given widget.
128 *
129 * \a widget the specified widget.
130 *
131 * Returns the list of blocked event types, can be empty if no events are blocked.
132 */
133 QList<QEvent::Type> blockedEventTypes(QWidget *widget) const;
134
135private:
136 friend class KWidgetItemDelegatePool;
137 friend class KWidgetItemDelegateEventListener;
138 std::unique_ptr<class KWidgetItemDelegatePrivate> const d;
139
140 Q_PRIVATE_SLOT(d, void _k_slotRowsInserted(const QModelIndex &, int, int))
141 Q_PRIVATE_SLOT(d, void _k_slotRowsAboutToBeRemoved(const QModelIndex &, int, int))
142 Q_PRIVATE_SLOT(d, void _k_slotRowsRemoved(const QModelIndex &, int, int))
143 Q_PRIVATE_SLOT(d, void _k_slotDataChanged(const QModelIndex &, const QModelIndex &))
144 Q_PRIVATE_SLOT(d, void _k_slotLayoutChanged())
145 Q_PRIVATE_SLOT(d, void _k_slotModelReset())
146 Q_PRIVATE_SLOT(d, void _k_slotSelectionChanged(const QItemSelection &, const QItemSelection &))
147};
148
149#endif
150

source code of kitemviews/src/kwidgetitemdelegate.h