press and hold for the MouseListener, needs proper memory management

This commit is contained in:
Sebastian Kügler 2011-11-25 05:06:32 +01:00
parent 2654681dfd
commit 2565770d2d
2 changed files with 42 additions and 1 deletions

View File

@ -21,11 +21,14 @@
#include <QEvent>
#include <QGraphicsSceneMouseEvent>
#include <QtCore/QTimer>
#include <KDebug>
MouseEventListener::MouseEventListener(QDeclarativeItem *parent)
: QDeclarativeItem(parent)
: QDeclarativeItem(parent),
m_pressed(false),
m_pressAndHoldEvent(0)
{
setFiltersChildEvents(true);
setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton|Qt::MidButton|Qt::XButton1|Qt::XButton2);
@ -46,7 +49,14 @@ void MouseEventListener::mousePressEvent(QGraphicsSceneMouseEvent *me)
}
QDeclarativeMouseEvent dme(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
m_pressAndHoldEvent = new QDeclarativeMouseEvent(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
kDebug() << "pressed in mousePressEvent";
emit pressed(&dme);
m_pressed = true;
//delete m_pressAndHoldEvent;
//m_pressAndHoldEvent = &dme;
//new QDeclarativeMouseEvent(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());;
QTimer::singleShot(800, this, SLOT(handlePressAndHold()));
}
void MouseEventListener::mouseMoveEvent(QGraphicsSceneMouseEvent *me)
@ -58,9 +68,22 @@ void MouseEventListener::mouseMoveEvent(QGraphicsSceneMouseEvent *me)
void MouseEventListener::mouseReleaseEvent(QGraphicsSceneMouseEvent *me)
{
QDeclarativeMouseEvent 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);
}
void MouseEventListener::handlePressAndHold()
{
if (m_pressed) {
//QDeclarativeMouseEvent dme = m_pressAndHoldEvent;
//const QPointF myPos = m_pressAndHoldEvent->pos();
//QDeclarativeMouseEvent dme(myPos.x(), myPos.y(), m_pressAndHoldEvent->screenPos().x(), m_pressAndHoldEvent->screenPos().y(), m_pressAndHoldEvent->button(), m_pressAndHoldEvent->buttons(), m_pressAndHoldEvent->modifiers());
emit pressAndHold(m_pressAndHoldEvent);
//delete m_pressAndHoldEvent;
}
}
bool MouseEventListener::sceneEventFilter(QGraphicsItem *item, QEvent *event)
{
if (!isEnabled()) {
@ -73,13 +96,22 @@ bool MouseEventListener::sceneEventFilter(QGraphicsItem *item, QEvent *event)
//the parent will receive events in its own coordinates
const QPointF myPos = item->mapToItem(this, me->pos());
QDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
m_pressAndHoldEvent = new QDeclarativeMouseEvent(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
kDebug() << "pressed in sceneEventFilter";
emit pressed(&dme);
m_pressed = true;
//delete m_pressAndHoldEvent;
//m_pressAndHoldEvent = new QDeclarativeMouseEvent(me->pos().x(), me->pos().y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());;
//m_pressAndHoldEvent = &dme;
QTimer::singleShot(800, this, SLOT(handlePressAndHold()));
break;
}
case QEvent::GraphicsSceneMouseMove: {
QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(event);
const QPointF myPos = item->mapToItem(this, me->pos());
QDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
kDebug() << "positionChanged..." << dme.x() << dme.y();
m_pressAndHoldEvent = new QDeclarativeMouseEvent(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
emit positionChanged(&dme);
break;
}
@ -88,6 +120,7 @@ bool MouseEventListener::sceneEventFilter(QGraphicsItem *item, QEvent *event)
const QPointF myPos = item->mapToItem(this, me->pos());
QDeclarativeMouseEvent dme(myPos.x(), myPos.y(), me->screenPos().x(), me->screenPos().y(), me->button(), me->buttons(), me->modifiers());
emit released(&dme);
m_pressed = false;
break;
}
default:

View File

@ -87,6 +87,14 @@ Q_SIGNALS:
void pressed(QDeclarativeMouseEvent *mouse);
void positionChanged(QDeclarativeMouseEvent *mouse);
void released(QDeclarativeMouseEvent *mouse);
void pressAndHold(QDeclarativeMouseEvent *mouse);
private Q_SLOTS:
void handlePressAndHold();
private:
bool m_pressed;
QDeclarativeMouseEvent* m_pressAndHoldEvent;
QPointF m_pressAndHoldPosition;
};
#endif