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

View File

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