add acceptedButtons prop as MouseArea

This commit is contained in:
Marco Martin 2013-09-02 15:38:00 +02:00
parent fd9757688a
commit 5005f2c879
2 changed files with 43 additions and 4 deletions

View File

@ -35,7 +35,8 @@ MouseEventListener::MouseEventListener(QQuickItem *parent)
m_pressed(false),
m_pressAndHoldEvent(0),
m_lastEvent(0),
m_containsMouse(false)
m_containsMouse(false),
m_acceptedButtons(Qt::LeftButton)
{
m_pressAndHoldTimer = new QTimer(this);
m_pressAndHoldTimer->setSingleShot(true);
@ -53,6 +54,21 @@ MouseEventListener::~MouseEventListener()
{
}
Qt::MouseButtons MouseEventListener::acceptedButtons() const
{
return m_acceptedButtons;
}
void MouseEventListener::setAcceptedButtons(Qt::MouseButtons buttons)
{
if (buttons == m_acceptedButtons) {
return;
}
m_acceptedButtons = buttons;
emit acceptedButtonsChanged();
}
void MouseEventListener::setHoverEnabled(bool enable)
{
if (enable == acceptHoverEvents()) {
@ -107,7 +123,8 @@ bool MouseEventListener::containsMouse() const
void MouseEventListener::mousePressEvent(QMouseEvent *me)
{
if (m_lastEvent == me) {
if (m_lastEvent == me || !(me->buttons() & m_acceptedButtons)) {
me->setAccepted(false);
return;
}
@ -130,7 +147,8 @@ void MouseEventListener::mousePressEvent(QMouseEvent *me)
void MouseEventListener::mouseMoveEvent(QMouseEvent *me)
{
if (m_lastEvent == me) {
if (m_lastEvent == me || !(me->buttons() & m_acceptedButtons)) {
me->setAccepted(false);
return;
}
@ -140,7 +158,8 @@ void MouseEventListener::mouseMoveEvent(QMouseEvent *me)
void MouseEventListener::mouseReleaseEvent(QMouseEvent *me)
{
if (m_lastEvent == me) {
if (m_lastEvent == me || !(me->buttons() & m_acceptedButtons)) {
me->setAccepted(false);
return;
}
@ -191,6 +210,11 @@ bool MouseEventListener::childMouseEventFilter(QQuickItem *item, QEvent *event)
case QEvent::MouseButtonPress: {
m_lastEvent = event;
QMouseEvent *me = static_cast<QMouseEvent *>(event);
if (!(me->buttons() & m_acceptedButtons)) {
break;
}
//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());
@ -225,6 +249,10 @@ bool MouseEventListener::childMouseEventFilter(QQuickItem *item, QEvent *event)
case QEvent::MouseMove: {
m_lastEvent = event;
QMouseEvent *me = static_cast<QMouseEvent *>(event);
if (!(me->buttons() & m_acceptedButtons)) {
break;
}
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());
//qDebug() << "positionChanged..." << dme.x() << dme.y();
@ -235,6 +263,10 @@ bool MouseEventListener::childMouseEventFilter(QQuickItem *item, QEvent *event)
case QEvent::MouseButtonRelease: {
m_lastEvent = event;
QMouseEvent *me = static_cast<QMouseEvent *>(event);
if (!(me->buttons() & m_acceptedButtons)) {
break;
}
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;

View File

@ -135,6 +135,8 @@ class MouseEventListener : public QQuickItem
*/
Q_PROPERTY(bool containsMouse READ containsMouse NOTIFY containsMouseChanged)
Q_PROPERTY(Qt::MouseButtons acceptedButtons READ acceptedButtons WRITE setAcceptedButtons NOTIFY acceptedButtonsChanged)
public:
MouseEventListener(QQuickItem *parent=0);
~MouseEventListener();
@ -143,6 +145,9 @@ public:
void setHoverEnabled(bool enable);
bool hoverEnabled() const;
Qt::MouseButtons acceptedButtons() const;
void setAcceptedButtons(Qt::MouseButtons buttons);
protected:
void hoverEnterEvent(QHoverEvent *event);
void hoverLeaveEvent(QHoverEvent *event);
@ -162,6 +167,7 @@ Q_SIGNALS:
void wheelMoved(KDeclarativeWheelEvent *wheel);
void containsMouseChanged(bool containsMouseChanged);
void hoverEnabledChanged(bool hoverEnabled);
void acceptedButtonsChanged();
private Q_SLOTS:
void handlePressAndHold();
@ -175,6 +181,7 @@ private:
QEvent *m_lastEvent;
QTimer *m_pressAndHoldTimer;
bool m_containsMouse;
Qt::MouseButtons m_acceptedButtons;
};
#endif