add hover events

This commit is contained in:
Marco Martin 2012-10-04 19:21:06 +02:00
parent 7a1f75af9b
commit 8836f476ef
2 changed files with 53 additions and 1 deletions

View File

@ -29,7 +29,8 @@ MouseEventListener::MouseEventListener(QDeclarativeItem *parent)
: QDeclarativeItem(parent), : QDeclarativeItem(parent),
m_pressed(false), m_pressed(false),
m_pressAndHoldEvent(0), m_pressAndHoldEvent(0),
m_lastEvent(0) m_lastEvent(0),
m_containsMouse(false)
{ {
qmlRegisterType<KDeclarativeMouseEvent>(); qmlRegisterType<KDeclarativeMouseEvent>();
qmlRegisterType<KDeclarativeWheelEvent>(); qmlRegisterType<KDeclarativeWheelEvent>();
@ -42,6 +43,38 @@ MouseEventListener::~MouseEventListener()
{ {
} }
void MouseEventListener::setHoverEnabled(bool enable)
{
if (enable == acceptHoverEvents()) {
return;
}
setAcceptHoverEvents(enable);
emit hoverEnabledChanged(enable);
}
bool MouseEventListener::hoverEnabled() const
{
return acceptHoverEvents();
}
void MouseEventListener::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
m_containsMouse = true;
emit containsMouseChanged(true);
}
void MouseEventListener::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
m_containsMouse = false;
emit containsMouseChanged(false);
}
bool MouseEventListener::containsMouse() const
{
return m_containsMouse;
}
void MouseEventListener::mousePressEvent(QGraphicsSceneMouseEvent *me) void MouseEventListener::mousePressEvent(QGraphicsSceneMouseEvent *me)
{ {
if (m_lastEvent == me) { if (m_lastEvent == me) {

View File

@ -123,12 +123,28 @@ private:
class MouseEventListener : public QDeclarativeItem class MouseEventListener : public QDeclarativeItem
{ {
Q_OBJECT Q_OBJECT
/**
* This property holds whether hover events are handled.
* By default hover events are disabled
*/
Q_PROPERTY(bool hoverEnabled READ hoverEnabled WRITE setHoverEnabled NOTIFY hoverEnabledChanged)
/**
* True if this MouseEventListener or any of its children contains the mouse cursor: this property will change only when the mouse button is pressed if hoverEnabled is false
*/
Q_PROPERTY(bool containsMouse READ containsMouse NOTIFY containsMouseChanged)
public: public:
MouseEventListener(QDeclarativeItem *parent=0); MouseEventListener(QDeclarativeItem *parent=0);
~MouseEventListener(); ~MouseEventListener();
bool containsMouse() const;
void setHoverEnabled(bool enable);
bool hoverEnabled() const;
protected: protected:
void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
void mousePressEvent(QGraphicsSceneMouseEvent *event); void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseMoveEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
@ -141,6 +157,8 @@ Q_SIGNALS:
void released(KDeclarativeMouseEvent *mouse); void released(KDeclarativeMouseEvent *mouse);
void pressAndHold(KDeclarativeMouseEvent *mouse); void pressAndHold(KDeclarativeMouseEvent *mouse);
void wheelMoved(KDeclarativeWheelEvent *wheel); void wheelMoved(KDeclarativeWheelEvent *wheel);
void containsMouseChanged(bool containsMouseChanged);
void hoverEnabledChanged(bool hoverEnabled);
private Q_SLOTS: private Q_SLOTS:
void handlePressAndHold(); void handlePressAndHold();
@ -150,6 +168,7 @@ private:
QPointF m_pressAndHoldPosition; QPointF m_pressAndHoldPosition;
//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 QWekapointer
QEvent *m_lastEvent; QEvent *m_lastEvent;
bool m_containsMouse;
}; };
#endif #endif