Expose screen to MouseEventListener events

This commit is contained in:
David Edmundson 2013-12-13 17:15:07 +01:00
parent a26913ccac
commit 906caec817
2 changed files with 37 additions and 19 deletions

View File

@ -25,7 +25,7 @@
#include <QMouseEvent>
#include <QTimer>
#include <QQuickWindow>
#include <QScreen>
#include <QDebug>
static const int PressAndHoldDelay = 800;
@ -107,12 +107,12 @@ void MouseEventListener::hoverMoveEvent(QHoverEvent * event)
}
QQuickWindow *w = window();
QPoint screenPos = QPoint();
QPoint screenPos;
if (w) {
w->mapToGlobal(event->pos());
screenPos = w->mapToGlobal(event->pos());
}
KDeclarativeMouseEvent dme(event->pos().x(), event->pos().y(), screenPos.x(), screenPos.y(), Qt::NoButton, Qt::NoButton, event->modifiers());
KDeclarativeMouseEvent dme(event->pos().x(), event->pos().y(), screenPos.x(), screenPos.y(), Qt::NoButton, Qt::NoButton, event->modifiers(), 0);
emit positionChanged(&dme);
}
@ -137,8 +137,8 @@ void MouseEventListener::mousePressEvent(QMouseEvent *me)
}
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(), screenForGlobalPos(me->globalPos()));
m_pressAndHoldEvent = new KDeclarativeMouseEvent(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers(), screenForGlobalPos(me->globalPos()));
emit pressed(&dme);
m_pressed = true;
@ -152,7 +152,7 @@ void MouseEventListener::mouseMoveEvent(QMouseEvent *me)
return;
}
KDeclarativeMouseEvent dme(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(), screenForGlobalPos(me->globalPos()));
emit positionChanged(&dme);
}
@ -163,7 +163,7 @@ void MouseEventListener::mouseReleaseEvent(QMouseEvent *me)
return;
}
KDeclarativeMouseEvent dme(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(), screenForGlobalPos(me->globalPos()));
m_pressed = false;
emit released(&dme);
@ -217,8 +217,8 @@ bool MouseEventListener::childMouseEventFilter(QQuickItem *item, QEvent *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());
m_pressAndHoldEvent = new KDeclarativeMouseEvent(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(), screenForGlobalPos(me->globalPos()));
m_pressAndHoldEvent = new KDeclarativeMouseEvent(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers(), screenForGlobalPos(me->globalPos()));
//qDebug() << "pressed in sceneEventFilter";
m_buttonDownPos[me->button()] = me->pos();
emit pressed(&dme);
@ -236,14 +236,14 @@ bool MouseEventListener::childMouseEventFilter(QQuickItem *item, QEvent *event)
const QPointF myPos = item->mapToItem(this, he->pos());
QQuickWindow *w = window();
QPoint screenPos = QPoint();
QPoint screenPos;
if (w) {
w->mapToGlobal(myPos.toPoint());
screenPos = w->mapToGlobal(myPos.toPoint());
}
KDeclarativeMouseEvent dme(myPos.x(), myPos.y(), screenPos.x(), screenPos.y(), Qt::NoButton, Qt::NoButton, he->modifiers());
KDeclarativeMouseEvent dme(myPos.x(), myPos.y(), screenPos.x(), screenPos.y(), Qt::NoButton, Qt::NoButton, he->modifiers(), 0);
//qDebug() << "positionChanged..." << dme.x() << dme.y();
m_pressAndHoldEvent = new KDeclarativeMouseEvent(myPos.x(), myPos.y(), screenPos.x(), screenPos.y(), Qt::NoButton, Qt::NoButton, he->modifiers());
m_pressAndHoldEvent = new KDeclarativeMouseEvent(myPos.x(), myPos.y(), screenPos.x(), screenPos.y(), Qt::NoButton, Qt::NoButton, he->modifiers(),0);
emit positionChanged(&dme);
break;
}
@ -255,9 +255,9 @@ bool MouseEventListener::childMouseEventFilter(QQuickItem *item, QEvent *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());
KDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers(), screenForGlobalPos(me->globalPos()));
//qDebug() << "positionChanged..." << dme.x() << dme.y();
m_pressAndHoldEvent = new KDeclarativeMouseEvent(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
m_pressAndHoldEvent = new KDeclarativeMouseEvent(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers(), screenForGlobalPos(me->globalPos()));
emit positionChanged(&dme);
break;
}
@ -266,7 +266,7 @@ bool MouseEventListener::childMouseEventFilter(QQuickItem *item, QEvent *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());
KDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers(), screenForGlobalPos(me->globalPos()));
m_pressed = false;
emit released(&dme);
@ -292,5 +292,16 @@ bool MouseEventListener::childMouseEventFilter(QQuickItem *item, QEvent *event)
// return false;
}
QScreen* MouseEventListener::screenForGlobalPos(const QPoint& globalPos)
{
foreach(QScreen *screen, QGuiApplication::screens()) {
if (screen->geometry().contains(globalPos)) {
return screen;
}
}
return 0;
}
#include "mouseeventlistener.moc"

View File

@ -33,19 +33,22 @@ class KDeclarativeMouseEvent : public QObject
Q_PROPERTY(int button READ button)
Q_PROPERTY(Qt::MouseButtons buttons READ buttons)
Q_PROPERTY(Qt::KeyboardModifiers modifiers READ modifiers)
Q_PROPERTY(QScreen* screen READ screen CONSTANT)
public:
KDeclarativeMouseEvent(int x, int y, int screenX, int screenY,
Qt::MouseButton button,
Qt::MouseButtons buttons,
Qt::KeyboardModifiers modifiers)
Qt::KeyboardModifiers modifiers,
QScreen* screen)
: m_x(x),
m_y(y),
m_screenX(screenX),
m_screenY(screenY),
m_button(button),
m_buttons(buttons),
m_modifiers(modifiers)
m_modifiers(modifiers),
m_screen(screen)
{}
int x() const { return m_x; }
@ -55,6 +58,7 @@ public:
int button() const { return m_button; }
Qt::MouseButtons buttons() const { return m_buttons; }
Qt::KeyboardModifiers modifiers() const { return m_modifiers; }
QScreen* screen() const { return m_screen; }
// only for internal usage
void setX(int x) { m_x = x; }
@ -68,6 +72,7 @@ private:
Qt::MouseButton m_button;
Qt::MouseButtons m_buttons;
Qt::KeyboardModifiers m_modifiers;
QScreen *m_screen;
};
class KDeclarativeWheelEvent : public QObject
@ -173,6 +178,8 @@ private Q_SLOTS:
void handlePressAndHold();
private:
static QScreen* screenForGlobalPos(const QPoint &globalPos);
QPointF buttonDownPos(int btn) const;
bool m_pressed;
KDeclarativeMouseEvent* m_pressAndHoldEvent;