Port MouseEventListener to QtQuick2

All those signals work, I'm not quite sure what the eventFilter is used
for, the Item seems to work just fine without it. I've left it disabled
for now, maybe we can kill this code?

CCMAIL:mart@kde.org
This commit is contained in:
Sebastian Kügler 2013-02-15 01:38:40 +01:00
parent 57bac0cdaa
commit cf04f4591b
4 changed files with 101 additions and 94 deletions

View File

@ -1,5 +1,6 @@
/* /*
Copyright 2011 Marco Martin <notmart@gmail.com> Copyright 2011 Marco Martin <notmart@gmail.com>
Copyright 2013 Sebastian Kügler <sebas@kde.org>
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public modify it under the terms of the GNU Library General Public
@ -21,8 +22,8 @@
#include <QApplication> #include <QApplication>
#include <QEvent> #include <QEvent>
#include <QGraphicsSceneMouseEvent> #include <QMouseEvent>
#include <QtCore/QTimer> #include <QTimer>
#include <KDebug> #include <KDebug>
@ -66,85 +67,86 @@ bool MouseEventListener::hoverEnabled() const
return acceptHoverEvents(); return acceptHoverEvents();
} }
// void MouseEventListener::hoverEnterEvent(QGraphicsSceneHoverEvent *event) void MouseEventListener::hoverEnterEvent(QHoverEvent *event)
// { {
// Q_UNUSED(event); Q_UNUSED(event);
//
// m_containsMouse = true; m_containsMouse = true;
// emit containsMouseChanged(true); emit containsMouseChanged(true);
// } }
//
// void MouseEventListener::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) void MouseEventListener::hoverLeaveEvent(QHoverEvent *event)
// { {
// Q_UNUSED(event); Q_UNUSED(event);
//
// m_containsMouse = false; m_containsMouse = false;
// emit containsMouseChanged(false); emit containsMouseChanged(false);
// } }
bool MouseEventListener::containsMouse() const bool MouseEventListener::containsMouse() const
{ {
return m_containsMouse; return m_containsMouse;
} }
// void MouseEventListener::mousePressEvent(QGraphicsSceneMouseEvent *me) void MouseEventListener::mousePressEvent(QMouseEvent *me)
// { {
// if (m_lastEvent == me) { if (m_lastEvent == me) {
// return; return;
// } }
//
// //FIXME: when a popup window is visible: a click anywhere hides it: but the old qgraphicswidget will continue to think it's under the mouse //FIXME: when a popup window is visible: a click anywhere hides it: but the old qgraphicswidget will continue to think it's under the mouse
// //doesn't seem to be any good way to properly reset this. //doesn't seem to be any good way to properly reset this.
// //this msolution will still caused a missed click after the popup is gone, but gets the situation unblocked. //this msolution will still caused a missed click after the popup is gone, but gets the situation unblocked.
// if (!isUnderMouse()) { if (!isUnderMouse()) {
// me->ignore(); me->ignore();
// return; return;
// } }
// m_buttonDownPos[me->button()] = me->pos();
// KDeclarativeMouseEvent dme(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
// m_pressAndHoldEvent = new KDeclarativeMouseEvent(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers()); KDeclarativeMouseEvent dme(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
// emit pressed(&dme); m_pressAndHoldEvent = new KDeclarativeMouseEvent(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
// m_pressed = true; emit pressed(&dme);
// m_pressed = true;
// m_pressAndHoldTimer->start(PressAndHoldDelay);
// } m_pressAndHoldTimer->start(PressAndHoldDelay);
// }
// void MouseEventListener::mouseMoveEvent(QGraphicsSceneMouseEvent *me)
// { void MouseEventListener::mouseMoveEvent(QMouseEvent *me)
// if (m_lastEvent == me) { {
// return; if (m_lastEvent == me) {
// } return;
// }
// KDeclarativeMouseEvent dme(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
// emit positionChanged(&dme); KDeclarativeMouseEvent dme(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
// } emit positionChanged(&dme);
// }
// void MouseEventListener::mouseReleaseEvent(QGraphicsSceneMouseEvent *me)
// { void MouseEventListener::mouseReleaseEvent(QMouseEvent *me)
// if (m_lastEvent == me) { {
// return; if (m_lastEvent == me) {
// } return;
// }
// KDeclarativeMouseEvent dme(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
// m_pressed = false; KDeclarativeMouseEvent dme(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
// emit released(&dme); m_pressed = false;
// emit released(&dme);
// if (QPointF(me->pos() - me->buttonDownPos(me->button())).manhattanLength() <= QApplication::startDragDistance() && m_pressAndHoldTimer->isActive()) {
// emit clicked(&dme); if (QPointF(me->pos() - buttonDownPos(me->button())).manhattanLength() <= QApplication::startDragDistance() && m_pressAndHoldTimer->isActive()) {
// m_pressAndHoldTimer->stop(); emit clicked(&dme);
// } m_pressAndHoldTimer->stop();
// } }
// }
// void MouseEventListener::wheelEvent(QGraphicsSceneWheelEvent *we)
// { void MouseEventListener::wheelEvent(QWheelEvent *we)
// if (m_lastEvent == we) { {
// return; if (m_lastEvent == we) {
// } return;
// }
// KDeclarativeWheelEvent dwe(we->pos(), we->screenPos(), we->delta(), we->buttons(), we->modifiers(), we->orientation());
// emit wheelMoved(&dwe); KDeclarativeWheelEvent dwe(we->pos(), we->globalPos(), we->delta(), we->buttons(), we->modifiers(), we->orientation());
// } emit wheelMoved(&dwe);
// }
void MouseEventListener::handlePressAndHold() void MouseEventListener::handlePressAndHold()
{ {
if (m_pressed) { if (m_pressed) {
@ -153,6 +155,14 @@ void MouseEventListener::handlePressAndHold()
} }
} }
QPointF MouseEventListener::buttonDownPos(int btn) const
{
if (m_buttonDownPos.keys().contains(btn)) {
return m_buttonDownPos.value(btn);
}
return QPointF(0, 0);
}
// bool MouseEventListener::sceneEventFilter(QGraphicsItem *item, QEvent *event) // bool MouseEventListener::sceneEventFilter(QGraphicsItem *item, QEvent *event)
// { // {
@ -164,7 +174,7 @@ void MouseEventListener::handlePressAndHold()
// //
// switch (event->type()) { // switch (event->type()) {
// case QEvent::GraphicsSceneMousePress: { // case QEvent::GraphicsSceneMousePress: {
// QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(event); // QMouseEvent *me = static_cast<QMouseEvent *>(event);
// //the parent will receive events in its own coordinates // //the parent will receive events in its own coordinates
// const QPointF myPos = item->mapToItem(this, me->pos()); // const QPointF myPos = item->mapToItem(this, me->pos());
// KDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers()); // KDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
@ -177,7 +187,7 @@ void MouseEventListener::handlePressAndHold()
// break; // break;
// } // }
// case QEvent::GraphicsSceneMouseMove: { // case QEvent::GraphicsSceneMouseMove: {
// QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(event); // QMouseEvent *me = static_cast<QMouseEvent *>(event);
// const QPointF myPos = item->mapToItem(this, me->pos()); // const QPointF myPos = item->mapToItem(this, me->pos());
// KDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers()); // KDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
// //kDebug() << "positionChanged..." << dme.x() << dme.y(); // //kDebug() << "positionChanged..." << dme.x() << dme.y();
@ -186,21 +196,21 @@ void MouseEventListener::handlePressAndHold()
// break; // break;
// } // }
// case QEvent::GraphicsSceneMouseRelease: { // case QEvent::GraphicsSceneMouseRelease: {
// QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(event); // QMouseEvent *me = static_cast<QMouseEvent *>(event);
// const QPointF myPos = item->mapToItem(this, me->pos()); // const QPointF myPos = item->mapToItem(this, me->pos());
// KDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers()); // KDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
// m_pressed = false; // m_pressed = false;
// //
// emit released(&dme); // emit released(&dme);
// //
// if (QPointF(me->pos() - me->buttonDownPos(me->button())).manhattanLength() <= QApplication::startDragDistance() && m_pressAndHoldTimer->isActive()) { // if (QPointF(me->pos() - buttonDownPos(me->button())).manhattanLength() <= QApplication::startDragDistance() && m_pressAndHoldTimer->isActive()) {
// emit clicked(&dme); // emit clicked(&dme);
// m_pressAndHoldTimer->stop(); // m_pressAndHoldTimer->stop();
// } // }
// break; // break;
// } // }
// case QEvent::GraphicsSceneWheel: { // case QEvent::GraphicsSceneWheel: {
// QGraphicsSceneWheelEvent *we = static_cast<QGraphicsSceneWheelEvent *>(event); // QMouseEvent *we = static_cast<QMouseEvent *>(event);
// KDeclarativeWheelEvent dwe(we->pos(), we->screenPos(), we->delta(), we->buttons(), we->modifiers(), we->orientation()); // KDeclarativeWheelEvent dwe(we->pos(), we->screenPos(), we->delta(), we->buttons(), we->modifiers(), we->orientation());
// emit wheelMoved(&dwe); // emit wheelMoved(&dwe);
// break; // break;

View File

@ -1,5 +1,6 @@
/* /*
Copyright 2011 Marco Martin <notmart@gmail.com> Copyright 2011 Marco Martin <notmart@gmail.com>
Copyright 2013 Sebastian Kügler <sebas@kde.org>
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public modify it under the terms of the GNU Library General Public
@ -143,12 +144,12 @@ public:
bool hoverEnabled() const; bool hoverEnabled() const;
protected: protected:
// void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverEnterEvent(QHoverEvent *event);
// void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QHoverEvent *event);
// void mousePressEvent(QGraphicsSceneMouseEvent *event); void mousePressEvent(QMouseEvent *event);
// void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QMouseEvent *event);
// void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event);
// void wheelEvent(QGraphicsSceneWheelEvent *event); void wheelEvent(QWheelEvent *event);
// bool sceneEventFilter(QGraphicsItem *i, QEvent *e); // bool sceneEventFilter(QGraphicsItem *i, QEvent *e);
Q_SIGNALS: Q_SIGNALS:
@ -163,11 +164,13 @@ Q_SIGNALS:
private Q_SLOTS: private Q_SLOTS:
void handlePressAndHold(); void handlePressAndHold();
private: private:
QPointF buttonDownPos(int btn) const;
bool m_pressed; bool m_pressed;
KDeclarativeMouseEvent* m_pressAndHoldEvent; KDeclarativeMouseEvent* m_pressAndHoldEvent;
QPointF m_pressAndHoldPosition; QHash<int, QPointF> m_buttonDownPos;
//Important: used only for comparison. If you will ever need to access this pointer, make it a QWekapointer //Important: used only for comparison. If you will ever need to access this pointer, make it a QWeakPointer
QEvent *m_lastEvent; QEvent *m_lastEvent;
QTimer *m_pressAndHoldTimer; QTimer *m_pressAndHoldTimer;
bool m_containsMouse; bool m_containsMouse;

View File

@ -30,11 +30,6 @@
#include "mouseeventlistener.h" #include "mouseeventlistener.h"
#include "columnproxymodel.h" #include "columnproxymodel.h"
// void QtExtraComponentsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
// {
// qDebug() << "=========> QtExtras engine : " << uri;
// }
void QtExtraComponentsPlugin::registerTypes(const char *uri) void QtExtraComponentsPlugin::registerTypes(const char *uri)
{ {
Q_ASSERT(uri == QLatin1String("org.kde.qtextracomponents")); Q_ASSERT(uri == QLatin1String("org.kde.qtextracomponents"));

View File

@ -31,7 +31,6 @@ class QtExtraComponentsPlugin : public QQmlExtensionPlugin
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface") Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
public: public:
//void initializeEngine(QQmlEngine *engine, const char *uri);
void registerTypes(const char *uri); void registerTypes(const char *uri);
}; };