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