22 #include "kwidgetitemdelegatepool_p.h"
26 #include <QtCore/qobjectdefs.h>
27 #include <QtCore/QMetaObject>
28 #include <QtCore/QMetaMethod>
29 #include <QtCore/QMetaProperty>
30 #include <QtCore/QPair>
31 #include <QtCore/QHash>
32 #include <QtCore/QList>
33 #include <QtGui/QWidget>
34 #include <QtGui/QAbstractItemView>
35 #include <QtGui/QApplication>
36 #include <QtGui/QInputEvent>
37 #include <QtGui/QAbstractProxyModel>
42 #include "kwidgetitemdelegate_p.h"
46 Q_DECLARE_METATYPE(QModelIndex)
53 class KWidgetItemDelegateEventListener
57 KWidgetItemDelegateEventListener(KWidgetItemDelegatePoolPrivate *poolPrivate, QObject *parent = 0)
59 , poolPrivate(poolPrivate)
63 virtual bool eventFilter(QObject *watched, QEvent *event);
66 KWidgetItemDelegatePoolPrivate *poolPrivate;
71 , eventListener(new KWidgetItemDelegateEventListener(this))
77 : d(new KWidgetItemDelegatePoolPrivate(delegate))
81 KWidgetItemDelegatePool::~KWidgetItemDelegatePool()
83 delete d->eventListener;
87 QList<QWidget*> KWidgetItemDelegatePool::findWidgets(
const QPersistentModelIndex &idx,
88 const QStyleOptionViewItem &option,
89 UpdateWidgetsEnum updateWidgets)
const
98 if (
const QAbstractProxyModel *proxyModel = qobject_cast<const QAbstractProxyModel*>(idx.model())) {
99 index = proxyModel->mapToSource(idx);
104 if (!index.isValid()) {
108 if (d->usedWidgets.contains(index)) {
109 result = d->usedWidgets[index];
112 d->delegate->setProperty(
"goya:creatingWidgetForIndex", QVariant::fromValue(index));
113 result = d->delegate->createItemWidgets();
114 d->delegate->setProperty(
"goya:creatingWidgetForIndex",
QVariant());
115 d->allocatedWidgets << result;
116 d->usedWidgets[index] = result;
117 foreach (
QWidget *widget, result) {
118 d->widgetInIndex[widget] = index;
119 widget->setParent(d->delegate->d->itemView->viewport());
120 widget->installEventFilter(d->eventListener);
121 widget->setVisible(
true);
125 if (updateWidgets == UpdateWidgets) {
126 foreach (
QWidget *widget, result) {
127 widget->setVisible(
true);
130 d->delegate->updateItemWidgets(result, option, idx);
132 foreach (
QWidget *widget, result) {
133 widget->move(widget->x() + option.rect.left(), widget->y() + option.rect.top());
143 foreach (
QWidget *widget, d->widgetInIndex.keys()) {
147 index = proxyModel->mapFromSource(d->widgetInIndex[widget]);
149 index = d->widgetInIndex[widget];
151 if (!index.isValid()) {
158 void KWidgetItemDelegatePool::fullClear()
161 qDeleteAll(d->widgetInIndex.keys());
163 d->allocatedWidgets.clear();
164 d->usedWidgets.clear();
165 d->widgetInIndex.clear();
168 bool KWidgetItemDelegateEventListener::eventFilter(
QObject *watched, QEvent *event)
172 if (event->type() == QEvent::Destroy && !poolPrivate->clearing) {
173 kWarning() <<
"User of KWidgetItemDelegate should not delete widgets created by createItemWidgets!";
176 poolPrivate->widgetInIndex.remove(widget);
177 QWidget *viewport = poolPrivate->delegate->d->itemView->viewport();
178 QApplication::sendEvent(viewport, event);
180 if (dynamic_cast<QInputEvent*>(event) && !poolPrivate->delegate->blockedEventTypes(widget).contains(event->type())) {
181 QWidget *viewport = poolPrivate->delegate->d->itemView->viewport();
182 switch(event->type()) {
183 case QEvent::MouseMove:
184 case QEvent::MouseButtonPress:
185 case QEvent::MouseButtonRelease:
186 case QEvent::MouseButtonDblClick: {
187 QMouseEvent *mouseEvent =
static_cast<QMouseEvent*
>(event);
188 QMouseEvent evt(event->type(), viewport->mapFromGlobal(mouseEvent->globalPos()),
189 mouseEvent->button(), mouseEvent->buttons(), mouseEvent->modifiers());
190 QApplication::sendEvent(viewport, &evt);
193 case QEvent::Wheel: {
194 QWheelEvent *wheelEvent =
static_cast<QWheelEvent*
>(event);
195 QWheelEvent evt(viewport->mapFromGlobal(wheelEvent->globalPos()),
196 wheelEvent->delta(), wheelEvent->buttons(), wheelEvent->modifiers(),
197 wheelEvent->orientation());
198 QApplication::sendEvent(viewport, &evt);
201 case QEvent::TabletMove:
202 case QEvent::TabletPress:
203 case QEvent::TabletRelease:
204 case QEvent::TabletEnterProximity:
205 case QEvent::TabletLeaveProximity: {
206 QTabletEvent *tabletEvent =
static_cast<QTabletEvent*
>(event);
207 QTabletEvent evt(event->type(), viewport->mapFromGlobal(tabletEvent->globalPos()),
208 tabletEvent->globalPos(), tabletEvent->hiResGlobalPos(), tabletEvent->device(),
209 tabletEvent->pointerType(), tabletEvent->pressure(), tabletEvent->xTilt(),
210 tabletEvent->yTilt(), tabletEvent->tangentialPressure(), tabletEvent->rotation(),
211 tabletEvent->z(), tabletEvent->modifiers(), tabletEvent->uniqueId());
212 QApplication::sendEvent(viewport, &evt);
216 QApplication::sendEvent(viewport, event);
221 return QObject::eventFilter(watched, event);
static QDebug kWarning(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)