KItemViews

kwidgetitemdelegate.cpp
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#include "kwidgetitemdelegate.h"
10#include "kwidgetitemdelegate_p.h"
11
12#include <QAbstractItemView>
13#include <QApplication>
14#include <QCursor>
15#include <QPainter>
16#include <QStyleOption>
17#include <QTimer>
18#include <QTreeView>
19
20#include "kwidgetitemdelegatepool_p.h"
21
22Q_DECLARE_METATYPE(QList<QEvent::Type>)
23
24/**
25 KWidgetItemDelegatePrivate class that helps to provide binary compatibility between releases.
26 @internal
27*/
28//@cond PRIVATE
29KWidgetItemDelegatePrivate::KWidgetItemDelegatePrivate(KWidgetItemDelegate *q, QObject *parent)
30 : QObject(parent)
31 , widgetPool(new KWidgetItemDelegatePool(q))
32 , q(q)
33{
34}
35
36KWidgetItemDelegatePrivate::~KWidgetItemDelegatePrivate()
37{
38 if (!viewDestroyed) {
39 widgetPool->fullClear();
40 }
41 delete widgetPool;
42}
43
44void KWidgetItemDelegatePrivate::_k_slotRowsInserted(const QModelIndex &parent, int start, int end)
45{
46 Q_UNUSED(end);
47 // We need to update the rows behind the inserted row as well because the widgets need to be
48 // moved to their new position
49 updateRowRange(parent, start, model->rowCount(parent), false);
50}
51
52void KWidgetItemDelegatePrivate::_k_slotRowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
53{
54 updateRowRange(parent, start, end, true);
55}
56
57void KWidgetItemDelegatePrivate::_k_slotRowsRemoved(const QModelIndex &parent, int start, int end)
58{
59 Q_UNUSED(end);
60 // We need to update the rows that come behind the deleted rows because the widgets need to be
61 // moved to the new position
62 updateRowRange(parent, start, model->rowCount(parent), false);
63}
64
65void KWidgetItemDelegatePrivate::_k_slotDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
66{
67 for (int i = topLeft.row(); i <= bottomRight.row(); ++i) {
68 for (int j = topLeft.column(); j <= bottomRight.column(); ++j) {
69 const QModelIndex index = model->index(i, j, topLeft.parent());
70 widgetPool->findWidgets(index, optionView(index));
71 }
72 }
73}
74
75void KWidgetItemDelegatePrivate::_k_slotLayoutChanged()
76{
77 const auto lst = widgetPool->invalidIndexesWidgets();
78 for (QWidget *widget : lst) {
79 widget->setVisible(false);
80 }
81 QTimer::singleShot(0, this, SLOT(initializeModel()));
82}
83
84void KWidgetItemDelegatePrivate::_k_slotModelReset()
85{
86 widgetPool->fullClear();
87 QTimer::singleShot(0, this, SLOT(initializeModel()));
88}
89
90void KWidgetItemDelegatePrivate::_k_slotSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
91{
92 const auto lstSelected = selected.indexes();
93 for (const QModelIndex &index : lstSelected) {
94 widgetPool->findWidgets(index, optionView(index));
95 }
96 const auto lstDeselected = deselected.indexes();
97 for (const QModelIndex &index : lstDeselected) {
98 widgetPool->findWidgets(index, optionView(index));
99 }
100}
101
102void KWidgetItemDelegatePrivate::updateRowRange(const QModelIndex &parent, int start, int end, bool isRemoving)
103{
104 int i = start;
105 while (i <= end) {
106 for (int j = 0; j < model->columnCount(parent); ++j) {
107 const QModelIndex index = model->index(i, j, parent);
108 const QList<QWidget *> widgetList =
109 widgetPool->findWidgets(index,
110 optionView(index),
111 isRemoving ? KWidgetItemDelegatePool::NotUpdateWidgets : KWidgetItemDelegatePool::UpdateWidgets);
112 if (isRemoving) {
113 widgetPool->d->allocatedWidgets.removeAll(widgetList);
114 for (QWidget *widget : widgetList) {
115 const QModelIndex idx = widgetPool->d->widgetInIndex[widget];
116 widgetPool->d->usedWidgets.remove(idx);
117 widgetPool->d->widgetInIndex.remove(widget);
118 delete widget;
119 }
120 }
121 }
122 i++;
123 }
124}
125
126inline QStyleOptionViewItem KWidgetItemDelegatePrivate::optionView(const QModelIndex &index)
127{
128 QStyleOptionViewItem optionView;
129 optionView.initFrom(itemView->viewport());
130 optionView.rect = itemView->visualRect(index);
131 optionView.decorationSize = itemView->iconSize();
132 return optionView;
133}
134
135void KWidgetItemDelegatePrivate::initializeModel(const QModelIndex &parent)
136{
137 if (!model) {
138 return;
139 }
140
141 for (int i = 0; i < model->rowCount(parent); ++i) {
142 for (int j = 0; j < model->columnCount(parent); ++j) {
143 const QModelIndex index = model->index(i, j, parent);
144 if (index.isValid()) {
145 widgetPool->findWidgets(index, optionView(index));
146 }
147 }
148 // Check if we need to go recursively through the children of parent (if any) to initialize
149 // all possible indexes that are shown.
150 const QModelIndex index = model->index(i, 0, parent);
151 if (index.isValid() && model->hasChildren(index)) {
152 initializeModel(index);
153 }
154 }
155}
156//@endcond
157
159 : QAbstractItemDelegate(parent)
160 , d(new KWidgetItemDelegatePrivate(this))
161{
162 Q_ASSERT(itemView);
163
166
167 d->itemView = itemView;
168
169 itemView->viewport()->installEventFilter(d.get()); // mouse events
170 itemView->installEventFilter(d.get()); // keyboard events
171
172 if (qobject_cast<QTreeView *>(itemView)) {
173 connect(itemView, SIGNAL(collapsed(QModelIndex)), d.get(), SLOT(initializeModel()));
174 connect(itemView, SIGNAL(expanded(QModelIndex)), d.get(), SLOT(initializeModel()));
175 }
176}
177
179
181{
182 return d->itemView;
183}
184
186{
187 const QPersistentModelIndex idx = d->widgetPool->d->widgetInIndex.value(QApplication::focusWidget());
188 if (idx.isValid()) {
189 return idx;
190 }
191 // Use the mouse position, if the widget refused to take keyboard focus.
192 const QPoint pos = d->itemView->viewport()->mapFromGlobal(QCursor::pos());
193 return d->itemView->indexAt(pos);
194}
195
196//@cond PRIVATE
197bool KWidgetItemDelegatePrivate::eventFilter(QObject *watched, QEvent *event)
198{
199 if (event->type() == QEvent::Destroy) {
200 // we care for the view since it deletes the widgets (parentage).
201 // if the view hasn't been deleted, it might be that just the
202 // delegate is removed from it, in which case we need to remove the widgets
203 // manually, otherwise they still get drawn.
204 if (watched == itemView) {
205 viewDestroyed = true;
206 }
207 return false;
208 }
209
210 Q_ASSERT(itemView);
211
212 // clang-format off
213 if (model != itemView->model()) {
214 if (model) {
215 disconnect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), q, SLOT(_k_slotRowsInserted(QModelIndex,int,int)));
216 disconnect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), q, SLOT(_k_slotRowsAboutToBeRemoved(QModelIndex,int,int)));
217 disconnect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), q, SLOT(_k_slotRowsRemoved(QModelIndex,int,int)));
218 disconnect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), q, SLOT(_k_slotDataChanged(QModelIndex,QModelIndex)));
219 disconnect(model, SIGNAL(layoutChanged()), q, SLOT(_k_slotLayoutChanged()));
220 disconnect(model, SIGNAL(modelReset()), q, SLOT(_k_slotModelReset()));
221 }
222 model = itemView->model();
223 connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), q, SLOT(_k_slotRowsInserted(QModelIndex,int,int)));
224 connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), q, SLOT(_k_slotRowsAboutToBeRemoved(QModelIndex,int,int)));
225 connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), q, SLOT(_k_slotRowsRemoved(QModelIndex,int,int)));
226 connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), q, SLOT(_k_slotDataChanged(QModelIndex,QModelIndex)));
227 connect(model, SIGNAL(layoutChanged()), q, SLOT(_k_slotLayoutChanged()));
228 connect(model, SIGNAL(modelReset()), q, SLOT(_k_slotModelReset()));
229 QTimer::singleShot(0, this, SLOT(initializeModel()));
230 }
231
232 if (selectionModel != itemView->selectionModel()) {
233 if (selectionModel) {
234 disconnect(selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), q, SLOT(_k_slotSelectionChanged(QItemSelection,QItemSelection)));
235 }
236 selectionModel = itemView->selectionModel();
237 connect(selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), q, SLOT(_k_slotSelectionChanged(QItemSelection,QItemSelection)));
238 QTimer::singleShot(0, this, SLOT(initializeModel()));
239 }
240 // clang-format on
241
242 switch (event->type()) {
243 case QEvent::Polish:
244 case QEvent::Resize:
245 if (!qobject_cast<QAbstractItemView *>(watched)) {
246 QTimer::singleShot(0, this, SLOT(initializeModel()));
247 }
248 break;
249 case QEvent::FocusIn:
250 case QEvent::FocusOut:
251 if (qobject_cast<QAbstractItemView *>(watched)) {
252 const auto lst = selectionModel->selectedIndexes();
253 for (const QModelIndex &index : lst) {
254 if (index.isValid()) {
255 widgetPool->findWidgets(index, optionView(index));
256 }
257 }
258 }
259 break;
260 default:
261 break;
262 }
263
264 return QObject::eventFilter(watched, event);
265}
266//@endcond
267
269{
270 widget->setProperty("goya:blockedEventTypes", QVariant::fromValue(types));
271}
272
274{
275 return widget->property("goya:blockedEventTypes").value<QList<QEvent::Type>>();
276}
277
279{
280 d->_k_slotModelReset();
281}
282
283#include "moc_kwidgetitemdelegate.cpp"
284#include "moc_kwidgetitemdelegate_p.cpp"
This class allows to create item delegates embedding simple widgets to interact with items.
QAbstractItemView * itemView() const
Retrieves the item view this delegate is monitoring.
~KWidgetItemDelegate() override
Destroys an ItemDelegate.
void setBlockedEventTypes(QWidget *widget, const QList< QEvent::Type > &types) const
Sets the list of event types that a widget will block.
QList< QEvent::Type > blockedEventTypes(QWidget *widget) const
Retrieves the list of blocked event types for the given widget.
void resetModel()
trigger a modelReset
KWidgetItemDelegate(QAbstractItemView *itemView, QObject *parent=nullptr)
Creates a new ItemDelegate to be used with a given itemview.
QPersistentModelIndex focusedIndex() const
Retrieves the currently focused index.
Q_SCRIPTABLE Q_NOREPLY void start()
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
QWidget * viewport() const const
QWidget * focusWidget()
QPoint pos()
QModelIndexList indexes() const const
qsizetype removeAll(const AT &t)
int column() const const
bool isValid() const const
QModelIndex parent() const const
int row() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool eventFilter(QObject *watched, QEvent *event)
void installEventFilter(QObject *filterObj)
QVariant property(const char *name) const const
bool setProperty(const char *name, QVariant &&value)
bool isValid() const const
void initFrom(const QWidget *widget)
WA_Hover
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QVariant fromValue(T &&value)
T value() const const
void setMouseTracking(bool enable)
void setAttribute(Qt::WidgetAttribute attribute, bool on)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:32 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.