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 2013 Sebastian Kügler <sebas@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
@ -21,8 +22,8 @@
#include <QApplication>
#include <QEvent>
#include <QGraphicsSceneMouseEvent>
#include <QtCore/QTimer>
#include <QMouseEvent>
#include <QTimer>
#include <KDebug>
@ -66,85 +67,86 @@ bool MouseEventListener::hoverEnabled() const
return acceptHoverEvents();
}
// void MouseEventListener::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
// {
// Q_UNUSED(event);
//
// m_containsMouse = true;
// emit containsMouseChanged(true);
// }
//
// void MouseEventListener::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
// {
// Q_UNUSED(event);
//
// m_containsMouse = false;
// emit containsMouseChanged(false);
// }
void MouseEventListener::hoverEnterEvent(QHoverEvent *event)
{
Q_UNUSED(event);
m_containsMouse = true;
emit containsMouseChanged(true);
}
void MouseEventListener::hoverLeaveEvent(QHoverEvent *event)
{
Q_UNUSED(event);
m_containsMouse = false;
emit containsMouseChanged(false);
}
bool MouseEventListener::containsMouse() const
{
return m_containsMouse;
}
// void MouseEventListener::mousePressEvent(QGraphicsSceneMouseEvent *me)
// {
// if (m_lastEvent == me) {
// 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
// //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.
// if (!isUnderMouse()) {
// me->ignore();
// return;
// }
//
// 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());
// emit pressed(&dme);
// m_pressed = true;
//
// m_pressAndHoldTimer->start(PressAndHoldDelay);
// }
//
// void MouseEventListener::mouseMoveEvent(QGraphicsSceneMouseEvent *me)
// {
// 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);
// }
//
// void MouseEventListener::mouseReleaseEvent(QGraphicsSceneMouseEvent *me)
// {
// 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;
// emit released(&dme);
//
// if (QPointF(me->pos() - me->buttonDownPos(me->button())).manhattanLength() <= QApplication::startDragDistance() && m_pressAndHoldTimer->isActive()) {
// emit clicked(&dme);
// m_pressAndHoldTimer->stop();
// }
// }
//
// void MouseEventListener::wheelEvent(QGraphicsSceneWheelEvent *we)
// {
// if (m_lastEvent == we) {
// return;
// }
//
// KDeclarativeWheelEvent dwe(we->pos(), we->screenPos(), we->delta(), we->buttons(), we->modifiers(), we->orientation());
// emit wheelMoved(&dwe);
// }
//
void MouseEventListener::mousePressEvent(QMouseEvent *me)
{
if (m_lastEvent == me) {
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
//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.
if (!isUnderMouse()) {
me->ignore();
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());
emit pressed(&dme);
m_pressed = true;
m_pressAndHoldTimer->start(PressAndHoldDelay);
}
void MouseEventListener::mouseMoveEvent(QMouseEvent *me)
{
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);
}
void MouseEventListener::mouseReleaseEvent(QMouseEvent *me)
{
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;
emit released(&dme);
if (QPointF(me->pos() - buttonDownPos(me->button())).manhattanLength() <= QApplication::startDragDistance() && m_pressAndHoldTimer->isActive()) {
emit clicked(&dme);
m_pressAndHoldTimer->stop();
}
}
void MouseEventListener::wheelEvent(QWheelEvent *we)
{
if (m_lastEvent == we) {
return;
}
KDeclarativeWheelEvent dwe(we->pos(), we->globalPos(), we->delta(), we->buttons(), we->modifiers(), we->orientation());
emit wheelMoved(&dwe);
}
void MouseEventListener::handlePressAndHold()
{
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)
// {
@ -164,7 +174,7 @@ void MouseEventListener::handlePressAndHold()
//
// switch (event->type()) {
// case QEvent::GraphicsSceneMousePress: {
// QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(event);
// QMouseEvent *me = static_cast<QMouseEvent *>(event);
// //the parent will receive events in its own coordinates
// 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());
@ -177,7 +187,7 @@ void MouseEventListener::handlePressAndHold()
// break;
// }
// case QEvent::GraphicsSceneMouseMove: {
// QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(event);
// QMouseEvent *me = static_cast<QMouseEvent *>(event);
// 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());
// //kDebug() << "positionChanged..." << dme.x() << dme.y();
@ -186,21 +196,21 @@ void MouseEventListener::handlePressAndHold()
// break;
// }
// case QEvent::GraphicsSceneMouseRelease: {
// QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(event);
// QMouseEvent *me = static_cast<QMouseEvent *>(event);
// 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());
// m_pressed = false;
//
// 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);
// m_pressAndHoldTimer->stop();
// }
// break;
// }
// 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());
// emit wheelMoved(&dwe);
// break;

View File

@ -1,5 +1,6 @@
/*
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
modify it under the terms of the GNU Library General Public
@ -143,12 +144,12 @@ public:
bool hoverEnabled() const;
protected:
// void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
// void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
// void mousePressEvent(QGraphicsSceneMouseEvent *event);
// void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
// void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
// void wheelEvent(QGraphicsSceneWheelEvent *event);
void hoverEnterEvent(QHoverEvent *event);
void hoverLeaveEvent(QHoverEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event);
// bool sceneEventFilter(QGraphicsItem *i, QEvent *e);
Q_SIGNALS:
@ -163,11 +164,13 @@ Q_SIGNALS:
private Q_SLOTS:
void handlePressAndHold();
private:
QPointF buttonDownPos(int btn) const;
bool m_pressed;
KDeclarativeMouseEvent* m_pressAndHoldEvent;
QPointF m_pressAndHoldPosition;
//Important: used only for comparison. If you will ever need to access this pointer, make it a QWekapointer
QHash<int, QPointF> m_buttonDownPos;
//Important: used only for comparison. If you will ever need to access this pointer, make it a QWeakPointer
QEvent *m_lastEvent;
QTimer *m_pressAndHoldTimer;
bool m_containsMouse;

View File

@ -30,11 +30,6 @@
#include "mouseeventlistener.h"
#include "columnproxymodel.h"
// void QtExtraComponentsPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
// {
// qDebug() << "=========> QtExtras engine : " << uri;
// }
void QtExtraComponentsPlugin::registerTypes(const char *uri)
{
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")
public:
//void initializeEngine(QQmlEngine *engine, const char *uri);
void registerTypes(const char *uri);
};