diff options
author | Michal Guminiak <michal.guminiak@teleca.com> | 2010-10-19 15:52:24 +0200 |
---|---|---|
committer | Dominik Kapusta <dominik.kapusta@teleca.com> | 2010-11-10 09:03:47 +0100 |
commit | 92df7bab65a2d2fbc52b4d3a0a8887c5ea573b1e (patch) | |
tree | e1e3305c3da70f3e4e5bfb6d1e1f375ecb74b237 | |
parent | 9d2446c75a4cc1c3a320a1ee478d872e705ac4fd (diff) |
Fixes: NB#182815 - Pannable widget is not transparent wrt. passing focus to child widgets
RevBy: Jarno Malmari, Michael Hasselmann, Dominik Kapusta
ModifiedBy: Dominik
Details: This patch completely removes mouse event replaying functionality
in the pannable widget and moves necessary pieces to mscene. This should allow
better focus handling in the scene and also should remove all crashes related
to mouse event being bounced between pannable widgets when two of them are
placed on the scene.
Modifications: Provide MScenePrivate::init() and rename
MScenePrivate::sentCancel to cancelSent.
20 files changed, 226 insertions, 891 deletions
diff --git a/src/corelib/events/mpangesture.cpp b/src/corelib/events/mpangesture.cpp index 8f6560c0..98bfaf56 100644 --- a/src/corelib/events/mpangesture.cpp +++ b/src/corelib/events/mpangesture.cpp @@ -22,7 +22,8 @@ MPanGesture::MPanGesture(QObject *parent) : QPanGesture(parent), startPos(QPointF()), - panDirection(0) + panDirection(0), + pressed(false) { } diff --git a/src/corelib/events/mpangesture_p.h b/src/corelib/events/mpangesture_p.h index 2bb34c55..1504ea49 100644 --- a/src/corelib/events/mpangesture_p.h +++ b/src/corelib/events/mpangesture_p.h @@ -51,6 +51,7 @@ private: */ QPointF startPos; Qt::Orientations panDirection; + bool pressed; friend class MPanRecognizer; }; diff --git a/src/corelib/events/mpanrecognizer.cpp b/src/corelib/events/mpanrecognizer.cpp index 3376f077..8070c4b4 100644 --- a/src/corelib/events/mpanrecognizer.cpp +++ b/src/corelib/events/mpanrecognizer.cpp @@ -58,7 +58,11 @@ MPanRecognizer::~MPanRecognizer() QGesture* MPanRecognizer::create(QObject* target) { - Q_UNUSED(target); + if (target && target->isWidgetType() == false) { + return 0; // this assumes the target is a QGraphicsObject, so we return + // NULL to indicate that the recognizer doesn't support graphicsobject and + // graphicsscene events. + } return new MPanGesture; } @@ -70,7 +74,7 @@ QGestureRecognizer::Result MPanRecognizer::recognize( QGesture* gesture, Q_D(MPanRecognizer); MPanGesture *panGesture = static_cast<MPanGesture*>(gesture); - const QGraphicsSceneMouseEvent *ev = static_cast<const QGraphicsSceneMouseEvent *>(event); + const QMouseEvent *ev = static_cast<const QMouseEvent *>(event); const QTouchEvent *touchEvent = static_cast<const QTouchEvent *>(event); QGestureRecognizer::Result result = QGestureRecognizer::CancelGesture; qreal distX, distY; @@ -87,39 +91,40 @@ QGestureRecognizer::Result MPanRecognizer::recognize( QGesture* gesture, } break; - case QEvent::GraphicsSceneMousePress: - - panGesture->startPos = ev->scenePos(); - panGesture->setHotSpot(ev->screenPos()); + case QEvent::MouseButtonPress: + panGesture->startPos = ev->pos(); + panGesture->setHotSpot(ev->globalPos()); + panGesture->pressed = true; if (panGesture->state() != Qt::NoGesture) { result = QGestureRecognizer::TriggerGesture; } else { result = QGestureRecognizer::MayBeGesture; } - break; - case QEvent::GraphicsSceneMouseRelease: + case QEvent::MouseButtonRelease: + panGesture->pressed = false; if (panGesture->state() != Qt::NoGesture) { result = QGestureRecognizer::FinishGesture; } else { result = QGestureRecognizer::CancelGesture; } - break; - case QEvent::GraphicsSceneMouseMove: - + case QEvent::MouseMove: panGesture->setLastOffset(panGesture->offset()); - panGesture->setOffset(ev->scenePos() - panGesture->startPos); + panGesture->setOffset(ev->pos() - panGesture->startPos); distX = abs(panGesture->offset().x()); distY = abs(panGesture->offset().y()); - if (panGesture->state() == Qt::NoGesture) { + if (panGesture->pressed) { - if (distX > d->style->distanceThreshold() || distY > d->style->distanceThreshold()) { + if (panGesture->state() != Qt::NoGesture || + distX > d->style->distanceThreshold() || + distY > d->style->distanceThreshold()) + { panGesture->panDirection = (distX > distY ? Qt::Horizontal : Qt::Vertical); result = QGestureRecognizer::TriggerGesture; } else { @@ -127,7 +132,7 @@ QGestureRecognizer::Result MPanRecognizer::recognize( QGesture* gesture, } } else { - result = QGestureRecognizer::TriggerGesture; + result = QGestureRecognizer::CancelGesture; } if (panGesture->panDirection.testFlag(Qt::Vertical)) { @@ -143,6 +148,7 @@ QGestureRecognizer::Result MPanRecognizer::recognize( QGesture* gesture, } break; + default: result = QGestureRecognizer::Ignore; break; @@ -157,6 +163,7 @@ void MPanRecognizer::reset(QGesture* gesture) panGesture->setLastOffset(QPointF()); panGesture->startPos = QPointF(); panGesture->panDirection = 0; + panGesture->pressed = false; QGestureRecognizer::reset(gesture); } diff --git a/src/corelib/events/mtapandholdrecognizer.cpp b/src/corelib/events/mtapandholdrecognizer.cpp index d077c006..d357f3cd 100644 --- a/src/corelib/events/mtapandholdrecognizer.cpp +++ b/src/corelib/events/mtapandholdrecognizer.cpp @@ -60,7 +60,6 @@ MTapAndHoldRecognizer::~MTapAndHoldRecognizer() QGesture* MTapAndHoldRecognizer::create(QObject* target) { - Q_UNUSED(target); if (target && target->isWidgetType() == false) { return 0; // this assumes the target is a QGraphicsObject, so we return // NULL to indicate that the recognizer doesn't support graphicsobject and diff --git a/src/corelib/scene/mscene.cpp b/src/corelib/scene/mscene.cpp index 0c6ec454..2fbd52b9 100644 --- a/src/corelib/scene/mscene.cpp +++ b/src/corelib/scene/mscene.cpp @@ -19,6 +19,7 @@ #include <QList> #include <QGraphicsItem> +#include <QGraphicsWidget> #include <QPainter> #include <QTime> #include <QTimer> @@ -52,10 +53,43 @@ const qreal MarginBackgroundOpacity = 0.4; const QColor MarginColor = QColor(Qt::red); const int MarginBorderWidth = 2; +void copyGraphicsSceneMouseEvent(QGraphicsSceneMouseEvent &target, const QGraphicsSceneMouseEvent &source) +{ + + target.setPos(source.pos()); + target.setScenePos(source.scenePos()); + target.setScreenPos(source.screenPos()); + + target.setButtons(source.buttons()); + Qt::MouseButtons buttons = source.buttons(); + Qt::MouseButton buttonbit = (Qt::MouseButton)0x1; + while (buttons != 0x0) { + if (buttons & buttonbit) { + // Button pressed + target.setButtonDownPos(buttonbit, source.buttonDownPos(buttonbit)); + target.setButtonDownScenePos(buttonbit, source.buttonDownScenePos(buttonbit)); + target.setButtonDownScreenPos(buttonbit, source.buttonDownScreenPos(buttonbit)); + + // Unset button + buttons = buttons & ~buttonbit; + } + buttonbit = (Qt::MouseButton)(buttonbit << 1); + } + + target.setLastPos(source.lastPos()); + target.setLastScenePos(source.lastScenePos()); + target.setLastScreenPos(source.lastScreenPos()); + target.setButton(source.button()); + target.setModifiers(source.modifiers()); +} MScenePrivate::MScenePrivate() : q_ptr(0), manager(0), + eventEater(new QGraphicsWidget), + cancelSent(false), + initialPressTimer(0), + mousePressEvent(QEvent::GraphicsSceneMousePress), emuPoint1(1), emuPoint2(2), pinchEmulationEnabled(false) @@ -66,6 +100,28 @@ MScenePrivate::~MScenePrivate() { } +void MScenePrivate::init() +{ + Q_Q(MScene); + + QColor fpsBackgroundColor(FpsBackgroundColor); + fpsBackgroundColor.setAlphaF(FpsBackgroundOpacity); + fpsBackgroundBrush = QBrush(fpsBackgroundColor); + + QColor boundingRectLineColor(BoundingRectLineColor); + boundingRectLinePen = QPen(boundingRectLineColor); + + QColor boundingRectFillColor(BoundingRectFillColor); + boundingRectFillBrush = QBrush(boundingRectFillColor); + + initialPressTimer = new QTimer(q); + initialPressTimer->setSingleShot(true); + initialPressTimer->setInterval(100); + + q->addItem(eventEater); + q->connect(initialPressTimer, SIGNAL(timeout()), SLOT(_q_initialPressDeliveryTimeout())); +} + void MScenePrivate::setSceneManager(MSceneManager *sceneManager) { manager = sceneManager; @@ -399,8 +455,6 @@ void MScenePrivate::handleGestureEvent(QEvent* event) case Qt::GestureStarted: if (panelAcceptedGestures.contains(gesture->gestureType())) panelAcceptedGestures.removeAt(panelAcceptedGestures.indexOf(gesture->gestureType())); - else if (gestureEvent->isAccepted(gesture)) - childrenAcceptedGestures.append(gesture->gestureType()); break; case Qt::GestureCanceled: if (childrenAcceptedGestures.contains(gesture->gestureType())) @@ -408,10 +462,8 @@ void MScenePrivate::handleGestureEvent(QEvent* event) break; case Qt::GestureFinished: if (childrenAcceptedGestures.contains(gesture->gestureType())) { - if (q->mouseGrabberItem()) { - MCancelEvent cancelEvent; - q->sendEvent(q->mouseGrabberItem(),&cancelEvent); - } + if (q->mouseGrabberItem() && cancelSent != true) + q->sendCancel(); childrenAcceptedGestures.removeAt(childrenAcceptedGestures.indexOf(gesture->gestureType())); } default: @@ -426,6 +478,18 @@ void MScenePrivate::notifyGestureCaughtByPanel(Qt::GestureType gestureType) panelAcceptedGestures.append(gestureType); } +void MScenePrivate::notifyGestureAcceptedByChild(Qt::GestureType gestureType) +{ + if (!childrenAcceptedGestures.contains(gestureType)) + childrenAcceptedGestures.append(gestureType); +} + +void MScenePrivate::_q_initialPressDeliveryTimeout() +{ + Q_Q(MScene); + q->QGraphicsScene::event(&mousePressEvent); +} + MScene::MScene(QObject *parent) : QGraphicsScene(parent), d_ptr(new MScenePrivate) @@ -433,17 +497,7 @@ MScene::MScene(QObject *parent) Q_D(MScene); d->q_ptr = this; - d->manager = 0; - QColor fpsBackgroundColor(FpsBackgroundColor); - fpsBackgroundColor.setAlphaF(FpsBackgroundOpacity); - d->fpsBackgroundBrush = QBrush(fpsBackgroundColor); - - QColor boundingRectLineColor(BoundingRectLineColor); - d->boundingRectLinePen = QPen(boundingRectLineColor); - - QColor boundingRectFillColor(BoundingRectFillColor); - d->boundingRectFillBrush = QBrush(boundingRectFillColor); - + d->init(); setItemIndexMethod(QGraphicsScene::NoIndex); } @@ -459,6 +513,18 @@ MSceneManager *MScene::sceneManager() return d->manager; } +void MScene::sendCancel() +{ + Q_D(MScene); + + if (mouseGrabberItem()) { + MCancelEvent cancelEvent; + sendEvent(mouseGrabberItem(),&cancelEvent); + } + d->eventEater->grabMouse(); + d->cancelSent = true; +} + bool MScene::event(QEvent *event) { Q_D(MScene); @@ -467,10 +533,36 @@ bool MScene::event(QEvent *event) return true; } - bool retValue = QGraphicsScene::event(event); + bool retValue = false; - if (event->type() == QEvent::Gesture) + if (event->type() == QEvent::GraphicsSceneMousePress) { + + QGraphicsSceneMouseEvent *mouseEvent = static_cast<QGraphicsSceneMouseEvent*>(event); + copyGraphicsSceneMouseEvent(d->mousePressEvent, *mouseEvent); + + d->initialPressTimer->start(); + retValue = true; + } else if (event->type() == QEvent::GraphicsSceneMouseMove) { + retValue = QGraphicsScene::event(event); + } else if (event->type() == QEvent::GraphicsSceneMouseRelease) { + if (d->initialPressTimer->isActive()) { + //Didn't send the press yet... + d->initialPressTimer->stop(); + d->_q_initialPressDeliveryTimeout(); + } + retValue = QGraphicsScene::event(event); + if (d->cancelSent) { + d->eventEater->ungrabMouse(); + d->cancelSent = false; + retValue = true; + } + } else { + retValue = QGraphicsScene::event(event); + } + + if (event->type() == QEvent::Gesture) { d->handleGestureEvent(event); + } return retValue; } @@ -621,3 +713,5 @@ void MScene::drawForeground(QPainter *painter, const QRectF &rect) painter->drawPixmap(d->emuPoint2.screenPos() - pixmapCenterDelta, *tapPixmap); } } + +#include "moc_mscene.cpp" diff --git a/src/corelib/scene/mscene.h b/src/corelib/scene/mscene.h index fd911e50..1e9de489 100644 --- a/src/corelib/scene/mscene.h +++ b/src/corelib/scene/mscene.h @@ -80,6 +80,8 @@ public: */ MSceneManager *sceneManager(); + void sendCancel(); + protected: //! \reimp bool event(QEvent *event); @@ -93,6 +95,7 @@ protected: private: Q_DISABLE_COPY(MScene) Q_DECLARE_PRIVATE(MScene) + Q_PRIVATE_SLOT(d_func(), void _q_initialPressDeliveryTimeout()) #ifdef UNIT_TEST friend class Ut_MScene; @@ -101,6 +104,7 @@ private: friend class MSceneManager; friend class MWindowPrivate; friend class MSceneWindow; + friend class MWidget; }; #endif diff --git a/src/corelib/scene/mscene_p.h b/src/corelib/scene/mscene_p.h index 326d6792..d77463c9 100644 --- a/src/corelib/scene/mscene_p.h +++ b/src/corelib/scene/mscene_p.h @@ -27,6 +27,7 @@ class MSceneManager; class MOnDisplayChangeEvent; +class QGraphicsSceneMouseEvent; class MScenePrivate { @@ -48,6 +49,8 @@ class MScenePrivate public: MScenePrivate(); virtual ~MScenePrivate(); + + void init(); void setSceneManager(MSceneManager *sceneManager); void onDisplayChangeEvent(MOnDisplayChangeEvent *event); @@ -83,11 +86,20 @@ public: method. */ void notifyGestureCaughtByPanel(Qt::GestureType gestureType); + void notifyGestureAcceptedByChild(Qt::GestureType gestureType); + +public Q_SLOTS: + void _q_initialPressDeliveryTimeout(); protected: MScene *q_ptr; MSceneManager *manager; + QGraphicsWidget* eventEater; + bool cancelSent; + QTimer *initialPressTimer; + QGraphicsSceneMouseEvent mousePressEvent; + //Two finger gestures emulation variables QTouchEvent::TouchPoint emuPoint1, emuPoint2; bool pinchEmulationEnabled; diff --git a/src/corelib/widgets/core/mwidget.cpp b/src/corelib/widgets/core/mwidget.cpp index 0d8258a2..af9f05ad 100644 --- a/src/corelib/widgets/core/mwidget.cpp +++ b/src/corelib/widgets/core/mwidget.cpp @@ -42,7 +42,8 @@ #include <mcancelevent.h> #include <mondisplaychangeevent.h> #include <morientationchangeevent.h> -#include <mscene.h> +#include "mscene.h" +#include "mscene_p.h" #include <mscenemanager.h> #include "mobjectmenu.h" @@ -359,6 +360,11 @@ void MWidget::gestureEvent(QGestureEvent *event) QSwipeGesture* swipeState = static_cast<QSwipeGesture *>(gesture); swipeGestureEvent(event,swipeState); } + if (event->isAccepted(gesture)) { + MScene *mScene = qobject_cast<MScene *>(scene()); + if (mScene) + mScene->d_func()->notifyGestureAcceptedByChild(gesture->gestureType()); + } } } diff --git a/src/corelib/widgets/mpannablewidget.cpp b/src/corelib/widgets/mpannablewidget.cpp index bfbeab8f..449ffa24 100644 --- a/src/corelib/widgets/mpannablewidget.cpp +++ b/src/corelib/widgets/mpannablewidget.cpp @@ -29,70 +29,13 @@ #include "mondisplaychangeevent.h" #include "mwidgetcreator.h" +#include "mscene.h" M_REGISTER_WIDGET(MPannableWidget) namespace { - //! Limiter for the resent list - const int ResentListMaxSize = 10; //! Z-value of the glass const int ZValueGlass = 2; - //! Hardcoded timeout value for delivering initial press event; - const int InitialPressDeliveryTimeoutValue = 140; - -} - -/* - * MPannableWidget handles the event interception by setting up a - * 'glass' widget on top of the pannable widget and its possible - * children. Glass forwards all mouse events to pannable widget which - * interprets them. The press event is just stored first and in case - * the gesture turns out to be not a panning one, the press is resend - * to scene. It is also put to the exclude list and when seen for the - * second time in mouse press handler, it is ignored. For this to - * work, just before the resending the mouse is ungrabbed in the glass - * which enables Qt to forward the event deeper after ignoring. - * After the forwarded press, all events go directly to the possible - * widget under the glass. - * - * If debugging of glass events is needed, it is recommend to override - * sceneEvent since also the mouse grabs and ungrabs are seen on that - * level. - */ - -/** - * Copies QGraphicsSceneMouseEvent data from one instance to another. - * Note that this function copies only data defined in QGraphicsSceneMouseEvent. - * Target event will keep its event type and other properties defined in QGraphicsSceneMouseEvent - * subclasses. - */ -void copyGraphicsSceneMouseEvent(QGraphicsSceneMouseEvent &target, const QGraphicsSceneMouseEvent &source) -{ - target.setPos(source.pos()); - target.setScenePos(source.scenePos()); - target.setScreenPos(source.screenPos()); - - target.setButtons(source.buttons()); - Qt::MouseButtons buttons = source.buttons(); - Qt::MouseButton buttonbit = (Qt::MouseButton)0x1; - while (buttons != 0x0) { - if (buttons & buttonbit) { - // Button pressed - target.setButtonDownPos(buttonbit, source.buttonDownPos(buttonbit)); - target.setButtonDownScenePos(buttonbit, source.buttonDownScenePos(buttonbit)); - target.setButtonDownScreenPos(buttonbit, source.buttonDownScreenPos(buttonbit)); - - // Unset button - buttons = buttons & ~buttonbit; - } - buttonbit = (Qt::MouseButton)(buttonbit << 1); - } - - target.setLastPos(source.lastPos()); - target.setLastScenePos(source.lastScenePos()); - target.setLastScreenPos(source.lastScreenPos()); - target.setButton(source.button()); - target.setModifiers(source.modifiers()); } MPannableWidgetGlass::MPannableWidgetGlass(QGraphicsItem *parent) : @@ -112,29 +55,11 @@ QRectF MPannableWidgetGlass::boundingRect() const return QRectF(QPointF(), pannableWidget->size()); } - void MPannableWidgetGlass::mousePressEvent(QGraphicsSceneMouseEvent *event) { pannableWidget->glassMousePressEvent(event); } - -void MPannableWidgetGlass::mouseMoveEvent(QGraphicsSceneMouseEvent *event) -{ - pannableWidget->glassMouseMoveEvent(event); -} - - -void MPannableWidgetGlass::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) -{ - pannableWidget->glassMouseReleaseEvent(event); -} - -void MPannableWidgetGlass::timerEvent(QTimerEvent *event) -{ - pannableWidget->glassTimerEvent(event); -} - void MPannableWidgetGlass::cancelEvent(MCancelEvent *event) { pannableWidget->cancelEvent(event); @@ -146,11 +71,7 @@ void MPannableWidgetGlass::tapAndHoldGestureEvent(QGestureEvent *event, QTapAndH } MPannableWidgetPrivate::MPannableWidgetPrivate() : - pressEvent(QEvent::GraphicsSceneMousePress), physics(0), - mouseGrabber(0), - resentList(), - pressDeliveryTimerId(0), panGestureCausedCancelEvent(false) { } @@ -160,93 +81,12 @@ MPannableWidgetPrivate::~MPannableWidgetPrivate() delete physics; } -void MPannableWidgetPrivate::translateEventToItemCoordinates(const QGraphicsItem *srcItem, const QGraphicsItem *destItem, QGraphicsSceneMouseEvent *event) -{ - event->setLastPos(destItem->mapFromItem(srcItem, event->lastPos())); - event->setPos(destItem->mapFromItem(srcItem, event->pos())); -} - -void MPannableWidgetPrivate::deliverPressEvent() -{ - Q_Q(MPannableWidget); - - if (physics->inMotion()) - { - physics->stop(); - return; - } - - if (q->scene() == NULL) { - // We have been removed from the scene - // while waiting for timer to expire. - // Reset the states and wait for next events. - resetPhysics(); - resetMouseGrabber(); - return; - } - - glass->ungrabMouse(); - q->resendEvent(&pressEvent); - mouseGrabber = q->scene()->mouseGrabberItem(); - glass->grabMouse(); -} - -void MPannableWidgetPrivate::initialPressStartTimer() -{ - pressDeliveryTimerId = glass->startTimer(InitialPressDeliveryTimeoutValue); -} - -void MPannableWidgetPrivate::initialPressStopTimer() -{ - if (pressDeliveryTimerId) { - glass->killTimer(pressDeliveryTimerId); - pressDeliveryTimerId = 0; - } -} - void MPannableWidgetPrivate::resetPhysics() { physics->pointerRelease(); physics->stop(); } -void MPannableWidgetPrivate::resetMouseGrabber() -{ - Q_Q(MPannableWidget); - - mouseGrabber = 0; - if (q->scene() && glass == q->scene()->mouseGrabberItem()) { - glass->ungrabMouse(); - } -} - -void MPannableWidgetPrivate::deliverMouseEvent(QGraphicsSceneMouseEvent *event) -{ - Q_Q(MPannableWidget); - if (mouseGrabber && q->scene()->items().contains(mouseGrabber)) { - translateEventToItemCoordinates(glass, mouseGrabber, event); - q->scene()->sendEvent(mouseGrabber, event); - } -} - -MPannableWidget* MPannableWidgetPrivate::parentPannableWidget() -{ - Q_Q(MPannableWidget); - - QGraphicsWidget* parentWidget = q->parentWidget(); - MPannableWidget* parentPannableWidget = 0; - - // Looking for topmost enabled pannablewidget. - while(parentWidget) { - MPannableWidget* candidate = qobject_cast<MPannableWidget*>(parentWidget); - if (candidate && candidate->panDirection() != 0) - parentPannableWidget = candidate; - parentWidget = parentWidget->parentWidget(); - } - - return parentPannableWidget; -} - MPannableWidget::MPannableWidget(MPannableWidgetPrivate *dd, MPannableWidgetModel *model, QGraphicsItem *parent) : MWidgetController(dd, model, parent) @@ -393,90 +233,23 @@ QPointF MPannableWidget::position() const } -bool MPannableWidget::checkForResent(QEvent *event) -{ - Q_D(MPannableWidget); - - const int size = d->resentList.size(); - for (int i = 0; i < size; ++i) { - const MPannableWidgetPrivate::resentItem &item = d->resentList.at(i); - if (item.type == (static_cast<QGraphicsSceneMouseEvent *>(event))->type() && - item.screenPos == (static_cast<QGraphicsSceneMouseEvent *>(event))->screenPos() && - item.button == (static_cast<QGraphicsSceneMouseEvent *>(event))->button()) { - - d->resentList.removeAt(i); - - return true; - } - } - - return false; -} - void MPannableWidget::updatePosition(const QPointF &position) { Q_UNUSED(position); } - -void MPannableWidget::glassMousePressEvent(QGraphicsSceneMouseEvent *event) -{ - Q_D(MPannableWidget); - - if (event->button() != Qt::LeftButton) { - // Glass: Ignoring, not a left button - - event->ignore(); - return; - } - - if (checkForResent(event)) { - // Glass: Ignoring, already seen - - event->ignore(); - return; - } - - if (panDirection() == 0) { - // Glass: Ignoring, I'm disabled - event->ignore(); - return; - } - - MPannableWidget *parentPannableWidget = d->parentPannableWidget(); - if (parentPannableWidget) { - - //We are stopping here, because if there is a parent pannable widget - //then the initial press timeout has already happened. - d->physics->stop(); - - //My parent will handle this mouse press. - event->ignore(); - return; - } - - copyGraphicsSceneMouseEvent(d->pressEvent, *event); - d->initialPressStartTimer(); -} - -void MPannableWidget::glassMouseReleaseEvent(QGraphicsSceneMouseEvent *event) +void MPannableWidget::glassMousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) { Q_D(MPannableWidget); - - if (d->pressDeliveryTimerId) { - d->deliverPressEvent(); - d->initialPressStopTimer(); + // This handler is called by the glass tapAndHoldGestureEvent method. + if (isEnabled() && d->physics->inMotion()) { + // The viewport is still moving, + // let's swallow this event + d->resetPhysics(); + mouseEvent->accept(); + } else { + mouseEvent->ignore(); } - - d->deliverMouseEvent(event); - d->resetMouseGrabber(); -} - - -void MPannableWidget::glassMouseMoveEvent(QGraphicsSceneMouseEvent *event) -{ - Q_D(MPannableWidget); - d->deliverMouseEvent(event); } void MPannableWidget::panGestureEvent(QGestureEvent *event, QPanGesture* panGesture) @@ -511,16 +284,10 @@ void MPannableWidget::panGestureEvent(QGestureEvent *event, QPanGesture* panGest { case Qt::GestureStarted: - //If we have parent pannable widget, then it is the parent - //who handles the cancelling of mouse press. We need to inform - //it that this needs to be done. { - MPannableWidget *parentPannableWidget = d->parentPannableWidget(); - if (parentPannableWidget) { - parentPannableWidget->sendCancel(); - } else { - sendCancel(); - } + MScene *mScene = qobject_cast<MScene *>(scene()); + if (mScene) + mScene->sendCancel(); } d->physics->pointerPress(QPointF()); @@ -555,56 +322,6 @@ void MPannableWidget::tapAndHoldGestureEvent(QGestureEvent *event, QTapAndHoldGe } } -void MPannableWidget::orientationChangeEvent(MOrientationChangeEvent *event) -{ - Q_D(MPannableWidget); - d->resetPhysics(); - - MWidgetController::orientationChangeEvent(event); -} - -void MPannableWidget::glassTimerEvent(QTimerEvent *event) -{ - Q_D(MPannableWidget); - if (event->timerId() == d->pressDeliveryTimerId) { - d->deliverPressEvent(); - d->initialPressStopTimer(); - } -} - -void MPannableWidget::resendEvent(QGraphicsSceneMouseEvent *event) -{ - Q_D(MPannableWidget); - - struct MPannableWidgetPrivate::resentItem resentItem; - - if ((scene() == NULL) || (scene()->views().size() == 0)) { - - // If this widget has been removed from the scene and/or there - // is no view, return - return; - } - - if (event->type() == QEvent::GraphicsSceneMousePress) { - // Puts the event to exclude list - - resentItem.type = event->type(); - resentItem.screenPos = event->screenPos(); - resentItem.button = event->button(); - - // Size limiter for the list. Prevents the list from growing in - // unlimited fashion in case of an unexpected loss of events - - while (d->resentList.size() > ResentListMaxSize) { - d->resentList.removeLast(); - } - - d->resentList.append(resentItem); - } - - QApplication::sendEvent(scene(), event); -} - void MPannableWidget::cancelEvent(MCancelEvent *event) { Q_UNUSED(event); @@ -615,12 +332,7 @@ void MPannableWidget::cancelEvent(MCancelEvent *event) return; } - sendCancel(); d->resetPhysics(); - - //We will still receive mouse release, but - //we aren't interested in it. - d->resetMouseGrabber(); } // onDisplayChangeEvent in MWidget handles MPannableWidgets in a @@ -660,27 +372,12 @@ void MPannableWidget::onDisplayChangeEvent(MOnDisplayChangeEvent *event) } } -void MPannableWidget::sendCancel() +void MPannableWidget::orientationChangeEvent(MOrientationChangeEvent *event) { Q_D(MPannableWidget); - MCancelEvent cancelEvent; - - if ((scene() == NULL) || (scene()->views().size() == 0)) { - - // If this widget has been removed from the scene and/or there - // is no view, return - return; - } - - if (d->pressDeliveryTimerId) { - // The initial MousePress event hasn't been delivered yet. - d->initialPressStopTimer(); - } else if (d->mouseGrabber && scene()->items().contains(d->mouseGrabber)) { - scene()->sendEvent(d->mouseGrabber, &cancelEvent); - d->mouseGrabber = 0; - } - + d->resetPhysics(); + MWidgetController::orientationChangeEvent(event); } void MPannableWidget::setPanDirection(const Qt::Orientations &panDirection) diff --git a/src/corelib/widgets/mpannablewidget.h b/src/corelib/widgets/mpannablewidget.h index 437747c2..e910310d 100644 --- a/src/corelib/widgets/mpannablewidget.h +++ b/src/corelib/widgets/mpannablewidget.h @@ -299,41 +299,11 @@ private: void glassMousePressEvent(QGraphicsSceneMouseEvent *event); /*! - * \brief Method for handling the release events from the glass. - */ - void glassMouseReleaseEvent(QGraphicsSceneMouseEvent *event); - - /*! - * \brief Method for handling the move events from the glass. - */ - void glassMouseMoveEvent(QGraphicsSceneMouseEvent *event); - - /*! - * \brief Method for delivering timer events. - */ - void glassTimerEvent(QTimerEvent* event); - - /*! - * \brief Method for checking if the event is on the resent list - * and should pass the glass when it hits it the second time. - */ - bool checkForResent(QEvent *event); - - /*! - * \brief Method for posting a captured event back to viewport. - */ - void resendEvent(QGraphicsSceneMouseEvent *event); - - /*! - * \brief Method for sending cancel event to viewport. - */ - void sendCancel(); - - /*! * \brief Init method. */ void init(); + #ifdef UNIT_TEST // Test unit is defined as a friend of production code to access private members friend class Ut_MPannableWidget; diff --git a/src/corelib/widgets/mpannablewidget_p.h b/src/corelib/widgets/mpannablewidget_p.h index 6129623b..c703d977 100644 --- a/src/corelib/widgets/mpannablewidget_p.h +++ b/src/corelib/widgets/mpannablewidget_p.h @@ -43,9 +43,6 @@ public: virtual QRectF boundingRect() const; virtual void mousePressEvent(QGraphicsSceneMouseEvent *event); - virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event); - virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); - virtual void timerEvent(QTimerEvent* event); virtual void cancelEvent(MCancelEvent *event); virtual void tapAndHoldGestureEvent(QGestureEvent *event, QTapAndHoldGesture *gesture); @@ -65,63 +62,14 @@ public: MPhysics2DPanning *physics; MPannableWidgetGlass *glass; - QGraphicsItem *mouseGrabber; - struct resentItem { - QEvent::Type type; - QPoint screenPos; - Qt::MouseButton button; - }; - - QList<struct resentItem> resentList; - - int pressDeliveryTimerId; bool panGestureCausedCancelEvent; public: /*! - * \brief Internal method necessary to correctly handle event positions. - */ - void translateEventToItemCoordinates(const QGraphicsItem *srcItem, - const QGraphicsItem *destItem, - QGraphicsSceneMouseEvent *event); - - /*! * \brief Method used for resetting state of the physics engine. */ void resetPhysics(); - - /*! - * \brief Method used for resetting glass and mouse grabber. - */ - void resetMouseGrabber(); - - /*! - * \brief Method used for delivering event to interested widget. - */ - void deliverMouseEvent(QGraphicsSceneMouseEvent *event); - - /*! - * \brief Method used for delivering initial mouse press. It will set - * the mouseGrabber if it is necessary at this point. - */ - void deliverPressEvent(); - - /*! - * \brief Method used to start a timer which will trigger the delivery - * of initial mouse press event. - */ - void initialPressStartTimer(); - - /*! - * \brief Method used for cancelling the mouse press delivery timer. - */ - void initialPressStopTimer(); - - /*! - * \brief Method used for getting an enabled parent pannable widget. - */ - MPannableWidget* parentPannableWidget(); }; #endif diff --git a/tests/ut_mappletinventory/ut_mappletinventory.pro b/tests/ut_mappletinventory/ut_mappletinventory.pro index e09b4c76..9817f348 100644 --- a/tests/ut_mappletinventory/ut_mappletinventory.pro +++ b/tests/ut_mappletinventory/ut_mappletinventory.pro @@ -4,6 +4,7 @@ include(../common_mextensions.pri) INCLUDEPATH += \ $$MSRCDIR/include \ $$MSRCDIR/corelib/widgets \ + $$MSRCDIR/corelib/scene \ $$MSRCDIR/events MODEL_HEADERS += \ @@ -16,7 +17,8 @@ SOURCES += \ $$MSRCDIR/extensions/mashup/appletinstallation/mappletinstantiator.cpp \ $$MSRCDIR/corelib/widgets/core/mwidgetcontroller.cpp \ $$MSRCDIR/corelib/widgets/core/mwidget.cpp \ - $$MSRCDIR/corelib/widgets/mwidgetmodel.cpp + $$MSRCDIR/corelib/widgets/mwidgetmodel.cpp \ + $$MSRCDIR/corelib/scene/mscene.cpp \ # service classes SOURCES += \ @@ -36,6 +38,7 @@ HEADERS += \ $$MSRCDIR/corelib/widgets/core/mwidget_p.h \ $$MSRCDIR/corelib/widgets/mwidgetmodel_p.h \ $$MSRCDIR/corelib/widgets/mobjectmenu.h \ + $$MSRCDIR/corelib/scene/mscene.h \ $$MODEL_HEADERS include(../common_bot.pri) diff --git a/tests/ut_mappletinventoryview/ut_mappletinventoryview.pro b/tests/ut_mappletinventoryview/ut_mappletinventoryview.pro index 8e1195b1..cc0ab64a 100644 --- a/tests/ut_mappletinventoryview/ut_mappletinventoryview.pro +++ b/tests/ut_mappletinventoryview/ut_mappletinventoryview.pro @@ -7,6 +7,7 @@ INCLUDEPATH += $$MSRCDIR/corelib/style INCLUDEPATH += $$MSRCDIR/corelib/widgets INCLUDEPATH += $$MSRCDIR/corelib/widgets/core INCLUDEPATH += $$MSRCDIR/corelib/core +INCLUDEPATH += $$MSRCDIR/corelib/scene STYLE_HEADERS += $$MSRCDIR/extensions/style/mappletinventorystyle.h MODEL_HEADERS += $$MSRCDIR/corelib/widgets/mwidgetmodel.h \ @@ -22,7 +23,8 @@ SOURCES += \ SOURCES += \ $$MSRCDIR/corelib/widgets/core/mwidgetcontroller.cpp \ $$MSRCDIR/corelib/widgets/core/mwidget.cpp \ - $$MSRCDIR/corelib/widgets/mwidgetmodel.cpp + $$MSRCDIR/corelib/widgets/mwidgetmodel.cpp \ + $$MSRCDIR/corelib/scene/mscene.cpp \ # service classes SOURCES += \ @@ -42,6 +44,7 @@ HEADERS += \ $$MSRCDIR/extensions/style/mappletinventorystyle.h \ $$MSRCDIR/corelib/widgets/mwidgetmodel_p.h \ $$MSRCDIR/corelib/widgets/mobjectmenu.h \ + $$MSRCDIR/corelib/scene/mscene.h \ $$MODEL_HEADERS \ $$STYLE_HEADERS diff --git a/tests/ut_mextensionarea/ut_mextensionarea.pro b/tests/ut_mextensionarea/ut_mextensionarea.pro index 40989dfe..8e700160 100644 --- a/tests/ut_mextensionarea/ut_mextensionarea.pro +++ b/tests/ut_mextensionarea/ut_mextensionarea.pro @@ -3,14 +3,16 @@ include(../common_mextensions.pri) INCLUDEPATH += \ $$MSRCDIR/corelib/widgets \ - $$MSRCDIR/corelib/style + $$MSRCDIR/corelib/style \ + $$MSRCDIR/corelib/scene \ # unit test and unit classes SOURCES += \ ut_mextensionarea.cpp \ $$MSRCDIR/extensions/applicationextension/mextensionarea.cpp \ $$MSRCDIR/corelib/widgets/core/mwidgetcontroller.cpp \ - $$MSRCDIR/corelib/widgets/core/mwidget.cpp + $$MSRCDIR/corelib/widgets/core/mwidget.cpp \ + $$MSRCDIR/corelib/scene/mscene.cpp \ # service classes SOURCES += \ @@ -24,6 +26,7 @@ HEADERS += \ $$MSRCDIR/extensions/mashup/mashup/mappletid.h \ $$MSRCDIR/corelib/widgets/core/mwidgetcontroller_p.h \ $$MSRCDIR/corelib/widgets/core/mwidget_p.h \ - $$MSRCDIR/corelib/widgets/mobjectmenu.h + $$MSRCDIR/corelib/widgets/mobjectmenu.h \ + $$MSRCDIR/corelib/scene/mscene.h \ include(../common_bot.pri) diff --git a/tests/ut_mmashupcanvas/ut_mmashupcanvas.pro b/tests/ut_mmashupcanvas/ut_mmashupcanvas.pro index 13bfe8a4..a7680525 100644 --- a/tests/ut_mmashupcanvas/ut_mmashupcanvas.pro +++ b/tests/ut_mmashupcanvas/ut_mmashupcanvas.pro @@ -3,7 +3,8 @@ include(../common_mextensions.pri) INCLUDEPATH += \ $$MSRCDIR/corelib/widgets \ - $$MSRCDIR/corelib/style + $$MSRCDIR/corelib/style \ + $$MSRCDIR/corelib/scene \ # unit test and unit classes SOURCES += \ @@ -11,7 +12,8 @@ SOURCES += \ $$MSRCDIR/extensions/mashup/mashup/mmashupcanvas.cpp \ $$MSRCDIR/corelib/widgets/core/mwidgetcontroller.cpp \ $$MSRCDIR/corelib/widgets/core/mwidget.cpp \ - $$MSRCDIR/extensions/applicationextension/mextensionarea.cpp + $$MSRCDIR/extensions/applicationextension/mextensionarea.cpp \ + $$MSRCDIR/corelib/scene/mscene.cpp \ # service classes SOURCES += \ @@ -25,6 +27,7 @@ HEADERS += \ $$MSRCDIR/extensions/mashup/mashup/mappletid.h \ $$MSRCDIR/corelib/widgets/core/mwidgetcontroller_p.h \ $$MSRCDIR/corelib/widgets/core/mwidget_p.h \ - $$MSRCDIR/corelib/widgets/mobjectmenu.h + $$MSRCDIR/corelib/widgets/mobjectmenu.h \ + $$MSRCDIR/corelib/scene/mscene.h \ include(../common_bot.pri) diff --git a/tests/ut_mpannableviewport/ut_mpannableviewport.pro b/tests/ut_mpannableviewport/ut_mpannableviewport.pro index dc94aa5b..688d15bc 100644 --- a/tests/ut_mpannableviewport/ut_mpannableviewport.pro +++ b/tests/ut_mpannableviewport/ut_mpannableviewport.pro @@ -1,5 +1,7 @@ include(../common_top.pri) -INCLUDEPATH += $$MSRCDIR/corelib/widgets +INCLUDEPATH += \ + $$MSRCDIR/corelib/widgets \ + $$MSRCDIR/corelib/scene \ TARGET = ut_mpannableviewport @@ -17,6 +19,7 @@ SOURCES += \ $$MSRCDIR/corelib/widgets/mpannablewidget.cpp \ $$MSRCDIR/corelib/widgets/mpannableviewportlayout.cpp \ $$MSRCDIR/corelib/widgets/mpannableviewport.cpp \ + $$MSRCDIR/corelib/scene/mscene.cpp \ # unit test and unit classes HEADERS += \ @@ -26,5 +29,6 @@ HEADERS += \ $$MSRCDIR/corelib/widgets/mpannableviewport.h \ $$MSRCDIR/corelib/widgets/core/mwidgetcontroller_p.h \ $$MSRCDIR/corelib/widgets/core/mwidget_p.h \ + $$MSRCDIR/corelib/scene/mscene.h \ include(../common_bot.pri) diff --git a/tests/ut_mpannablewidget/ut_mpannablewidget.cpp b/tests/ut_mpannablewidget/ut_mpannablewidget.cpp index abbc11ec..3821daf6 100644 --- a/tests/ut_mpannablewidget/ut_mpannablewidget.cpp +++ b/tests/ut_mpannablewidget/ut_mpannablewidget.cpp @@ -62,20 +62,6 @@ struct PhysicsState { }; PhysicsState *physicsState = 0; -struct MouseGrabberState { - - MouseGrabberState() { - grabbedMouse = false; - ungrabbedMouse = false; - } - - bool grabbedMouse; - bool ungrabbedMouse; -}; -MouseGrabberState *mouseGrabberState = 0; - -//Stubs - //Physics stubs: void MPhysics2DPanning::start() { @@ -101,102 +87,6 @@ void MPhysics2DPanning::pointerRelease() physicsState->pointerReleased = true; } -bool MPhysics2DPanning::inMotion() const -{ - return physicsState->physicsIsMoving; -} - -//QGraphicsObject stubs: -void QGraphicsObject::grabGesture(Qt::GestureType, Qt::GestureFlags) -{ -} - -//QGraphicsItem stubs: -void QGraphicsItem::grabMouse() -{ - mouseGrabberState->grabbedMouse = true; -} - -void QGraphicsItem::ungrabMouse() -{ - mouseGrabberState->ungrabbedMouse = true; -} - -QGraphicsScene *QGraphicsItem::scene() const -{ - return (QGraphicsScene*)1; -} - -//QGraphiscScene stubs: - QGraphicsItem* QGraphicsScene::mouseGrabberItem() const -{ - return dummyItem; -} - - QList<QGraphicsItem*> QGraphicsScene::items() const -{ - QList<QGraphicsItem*> itemList; - if (dummyItem) { - itemList.append(dummyItem); - } - return itemList; -} - - QList<QGraphicsView *> QGraphicsScene::views() const - { - QList<QGraphicsView *> viewList; - viewList.append(0); - return viewList; - } - - bool QGraphicsScene::sendEvent(QGraphicsItem *item, QEvent *event) - { - if (item == dummyItem) { - - if (event->type() == MCancelEvent::eventNumber()) { - dummyItem->cancelReceived = true; - } - - switch (event->type()) { - case QEvent::GraphicsSceneMousePress: - dummyItem->mousePressReceived = true; - break; - - case QEvent::GraphicsSceneMouseRelease: - dummyItem->mouseReleaseReceived = true; - break; - - case QEvent::GraphicsSceneMouseMove: - dummyItem->mouseMoveReceived = true; - break; - - default: - break; - } - } - return true; - } - - QPointF QGraphicsItem::mapFromItem(const QGraphicsItem */*item*/, const QPointF &/*point*/) const - { - return QPointF(); - } - - QPoint QGraphicsView::mapFromScene(const QPointF& /*point*/) const - { - return QPoint(); - } - - QWidget *QAbstractScrollArea::viewport() const - { - return 0; - } - - void QCoreApplication::postEvent(QObject* /*receiver*/, QEvent */*event*/) - { - qDebug("QCoreApplication::postEvent() - called"); - } - Qt::GestureState currentPanState = Qt::NoGesture; Qt::GestureState QGesture::state() const { @@ -217,7 +107,6 @@ void Ut_MPannableWidget::init() { dummyItem = new DummyGraphicsItem; physicsState = new PhysicsState; - mouseGrabberState = new MouseGrabberState; widget = new MPannableWidget; } @@ -225,174 +114,9 @@ void Ut_MPannableWidget::cleanup() { delete widget; delete physicsState; - delete mouseGrabberState; delete dummyItem; } -void Ut_MPannableWidget::mousePressAndReleaseAreDeliveredToGrabber() -{ - QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); - pressEvent.setButton(Qt::LeftButton); - QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); - releaseEvent.setButton(Qt::LeftButton); - - QTimerEvent timerEvent(1); - - widget->d_func()->glass->mousePressEvent(&pressEvent); - - QCOMPARE(widget->d_func()->mouseGrabber, (QGraphicsItem*)0); - - widget->d_func()->pressDeliveryTimerId = 1; - widget->d_func()->glass->timerEvent(&timerEvent); - - QCOMPARE(widget->d_func()->mouseGrabber, dummyItem); - QVERIFY2(widget->d_func()->resentList.at(0).type == QEvent::GraphicsSceneMousePress, "Mouse press was not sent"); - - widget->d_func()->glass->mouseReleaseEvent(&releaseEvent); - - QCOMPARE(widget->d_func()->mouseGrabber, (QGraphicsItem*)0); - QCOMPARE(dummyItem->mouseReleaseReceived, true); -} - -void Ut_MPannableWidget::mouseMoveIsDelieveredToGrabberIfNoPanningIsRecognized() -{ - QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); - pressEvent.setButton(Qt::LeftButton); - QGraphicsSceneMouseEvent moveEvent(QEvent::GraphicsSceneMouseMove); - moveEvent.setButton(Qt::LeftButton); - QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); - releaseEvent.setButton(Qt::LeftButton); - - QTimerEvent timerEvent(1); - - widget->d_func()->glass->mousePressEvent(&pressEvent); - - QCOMPARE(widget->d_func()->mouseGrabber, (QGraphicsItem*)0); - - widget->d_func()->pressDeliveryTimerId = 1; - widget->d_func()->glass->timerEvent(&timerEvent); - - QCOMPARE(widget->d_func()->mouseGrabber, dummyItem); - QVERIFY2(widget->d_func()->resentList.at(0).type == QEvent::GraphicsSceneMousePress, "Mouse press was not sent"); - - widget->d_func()->glass->mouseMoveEvent(&moveEvent); - QCOMPARE(widget->d_func()->mouseGrabber, dummyItem); - QCOMPARE(dummyItem->mouseMoveReceived, true); - QCOMPARE(dummyItem->mouseReleaseReceived, false); - - widget->d_func()->glass->mouseReleaseEvent(&releaseEvent); - - QCOMPARE(widget->d_func()->mouseGrabber, (QGraphicsItem*)0); - QCOMPARE(dummyItem->mouseReleaseReceived, true); -} - -void Ut_MPannableWidget::mousePressWithNoLeftButtonIsIgnored() -{ - QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); - pressEvent.setButton(Qt::RightButton); - - widget->d_func()->glass->mousePressEvent(&pressEvent); - - QCOMPARE(widget->d_func()->mouseGrabber, (QGraphicsItem*)0); - QCOMPARE(widget->d_func()->pressDeliveryTimerId, 0); - QVERIFY(pressEvent.isAccepted() == false); -} - -void Ut_MPannableWidget::pannableViewportInsidePannableViewportIgnoresMousePress() -{ - MPannableWidget *innerWidget = new MPannableWidget(widget); - - QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); - pressEvent.setButton(Qt::LeftButton); - - innerWidget->d_func()->glass->mousePressEvent(&pressEvent); - - QCOMPARE(innerWidget->d_func()->mouseGrabber, (QGraphicsItem*)0); - QCOMPARE(innerWidget->d_func()->pressDeliveryTimerId, 0); - QVERIFY(pressEvent.isAccepted() == false); -} - -void Ut_MPannableWidget::cancelEventStopPhysicsAndResetsStateOfWidget() -{ - QPanGesture panGesture; - - QList<QGesture*> gestureList; - gestureList.append(&panGesture); - QGestureEvent event(gestureList); - - QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); - pressEvent.setButton(Qt::LeftButton); - - QTimerEvent timerEvent(1); - - - widget->d_func()->glass->mousePressEvent(&pressEvent); //mousePress delivered to glass, - //delivery timer is started. - widget->d_func()->pressDeliveryTimerId = 1; - widget->d_func()->glass->timerEvent(&timerEvent); //timer expired, mouse press should be delivered to - //the widget inside pannable widget. - - QCOMPARE(widget->d_func()->mouseGrabber, dummyItem); - - currentPanState = Qt::GestureStarted; - widget->panGestureEvent(&event, &panGesture); - QCOMPARE(physicsState->pointerPressed, true); - QCOMPARE(physicsState->pointerMoved, true); - QCOMPARE(physicsState->pointerReleased, false); - - QCOMPARE(mouseGrabberState->ungrabbedMouse, true); //pannable widget ungrabbed mouse; - QCOMPARE(mouseGrabberState->grabbedMouse, true); //and grabbed it again later. - - MCancelEvent cancelEvent; //Sending cancel event - widget->cancelEvent(&cancelEvent); - - mouseGrabberState->grabbedMouse = false; - mouseGrabberState->ungrabbedMouse = false; - - QCOMPARE(physicsState->pointerReleased, true); //Pannable widget should reset it's state - QCOMPARE(physicsState->physicsStopped, true); //after cancel event. - QCOMPARE(mouseGrabberState->grabbedMouse, false); -} - -void Ut_MPannableWidget::disabledWidgetShouldIgnoreMousePressAndPanEvents() -{ - widget->setVerticalPanningPolicy(MPannableWidget::PanningAlwaysOff); - widget->setHorizontalPanningPolicy(MPannableWidget::PanningAlwaysOff); - - QCOMPARE(physicsState->pointerPressed, false); - QCOMPARE(physicsState->pointerMoved, false); - QCOMPARE(physicsState->pointerReleased, true); //Disabling widget resets the state of physics. - physicsState->pointerReleased = false; //Cleaning up the state tracker. - - QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); - pressEvent.setButton(Qt::LeftButton); - - widget->d_func()->glass->mousePressEvent(&pressEvent); - - QCOMPARE(physicsState->pointerPressed, false); - QCOMPARE(physicsState->pointerMoved, false); - QCOMPARE(physicsState->pointerReleased, false); - - QCOMPARE(widget->d_func()->mouseGrabber, (QGraphicsItem*)0); - QCOMPARE(widget->d_func()->pressDeliveryTimerId, 0); - QVERIFY(pressEvent.isAccepted() == false); - - QPanGesture panGesture; - - QList<QGesture*> gestureList; - gestureList.append(&panGesture); - QGestureEvent event(gestureList); - - currentPanState = Qt::GestureStarted; - panGesture.setOffset(QPointF(100,0)); - - widget->panGestureEvent(&event, &panGesture); - QCOMPARE(physicsState->pointerPressed, false); - QCOMPARE(physicsState->pointerMoved, false); - QCOMPARE(physicsState->pointerReleased, false); - QCOMPARE(event.isAccepted(&panGesture), false); -} - void Ut_MPannableWidget::panGestureMovesPhysicsPointer() { QPanGesture panGesture; @@ -443,37 +167,6 @@ void Ut_MPannableWidget::panGestureAgainstPanningDirectionIsIgnored() QCOMPARE(event.isAccepted(&panGesture), false); } -void Ut_MPannableWidget::panGestureCancelsMouseEvents() -{ - QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); - QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); - QTimerEvent timerEvent(1); - - widget->glassMousePressEvent(&pressEvent); - - QCOMPARE(widget->d_func()->mouseGrabber, (QGraphicsItem*)0); - - widget->d_func()->pressDeliveryTimerId = 1; - widget->glassTimerEvent(&timerEvent); - - QCOMPARE(widget->d_func()->mouseGrabber, dummyItem); - QVERIFY2(widget->d_func()->resentList.at(0).type == QEvent::GraphicsSceneMousePress, "Mouse press was not sent"); - - QPanGesture panGesture; - - QList<QGesture*> gestureList; - gestureList.append(&panGesture); - QGestureEvent event(gestureList); - - currentPanState = Qt::GestureStarted; - widget->panGestureEvent(&event, &panGesture); - QCOMPARE(physicsState->pointerPressed, true); - QCOMPARE(physicsState->pointerMoved, true); - QCOMPARE(physicsState->pointerReleased, false); - - QCOMPARE(dummyItem->cancelReceived, true); -} - class CustomPhysics : public MPhysics2DPanning { public: @@ -513,79 +206,4 @@ void Ut_MPannableWidget::settingPhysicsToNULLShouldNotBreakTheWidget() } -void Ut_MPannableWidget::ignoredPanGestureShouldNotCancelMouseEvents() -{ - QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); - pressEvent.setButton(Qt::LeftButton); - QGraphicsSceneMouseEvent moveEvent(QEvent::GraphicsSceneMouseMove); - moveEvent.setButton(Qt::LeftButton); - QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); - releaseEvent.setButton(Qt::LeftButton); - - QTimerEvent timerEvent(1); - - widget->d_func()->glass->mousePressEvent(&pressEvent); - - QCOMPARE(widget->d_func()->mouseGrabber, (QGraphicsItem*)0); - - widget->d_func()->pressDeliveryTimerId = 1; - widget->d_func()->glass->timerEvent(&timerEvent); - - QCOMPARE(widget->d_func()->mouseGrabber, dummyItem); - QVERIFY2(widget->d_func()->resentList.at(0).type == QEvent::GraphicsSceneMousePress, "Mouse press was not sent"); - - QPanGesture panGesture; - panGesture.setOffset(QPointF(50,0)); - - QList<QGesture*> gestureList; - gestureList.append(&panGesture); - QGestureEvent event(gestureList); - - currentPanState = Qt::GestureStarted; - widget->panGestureEvent(&event, &panGesture); - QCOMPARE(physicsState->pointerPressed, false); - QCOMPARE(physicsState->pointerMoved, false); - QCOMPARE(physicsState->pointerReleased, false); - - widget->d_func()->glass->mouseMoveEvent(&moveEvent); - QCOMPARE(widget->d_func()->mouseGrabber, dummyItem); - QCOMPARE(dummyItem->mouseMoveReceived, true); - QCOMPARE(dummyItem->mouseReleaseReceived, false); - - widget->d_func()->glass->cancelEvent(new MCancelEvent()); - widget->d_func()->glass->mouseReleaseEvent(&releaseEvent); - - QCOMPARE(widget->d_func()->mouseGrabber, (QGraphicsItem*)0); - QCOMPARE(dummyItem->mouseReleaseReceived, true); - - QCOMPARE(dummyItem->cancelReceived, false); -} - -void Ut_MPannableWidget::tapAndHoldGestureShouldBeGrabbedIfViewportIsMoving() -{ - QTapAndHoldGesture tapAndHoldGesture; - - QList<QGesture*> gestureList; - gestureList.append(&tapAndHoldGesture); - QGestureEvent gestureEvent(gestureList); - - physicsState->physicsIsMoving = true; - - widget->d_func()->glass->tapAndHoldGestureEvent(&gestureEvent, &tapAndHoldGesture); - - QCOMPARE(gestureEvent.isAccepted(&tapAndHoldGesture), true); - - physicsState->physicsIsMoving = false; - - widget->d_func()->glass->tapAndHoldGestureEvent(&gestureEvent, &tapAndHoldGesture); - - QCOMPARE(gestureEvent.isAccepted(&tapAndHoldGesture), false); -} - -void Ut_MPannableWidget::testPanThreshold() -{ - widget->setPanThreshold(100); - QCOMPARE(widget->panThreshold(), qreal(0)); -} - QTEST_APPLESS_MAIN(Ut_MPannableWidget); diff --git a/tests/ut_mpannablewidget/ut_mpannablewidget.h b/tests/ut_mpannablewidget/ut_mpannablewidget.h index f2ad56c1..88ac8a17 100644 --- a/tests/ut_mpannablewidget/ut_mpannablewidget.h +++ b/tests/ut_mpannablewidget/ut_mpannablewidget.h @@ -42,24 +42,12 @@ private slots: void init(); void cleanup(); - void mousePressAndReleaseAreDeliveredToGrabber(); - void mouseMoveIsDelieveredToGrabberIfNoPanningIsRecognized(); - void mousePressWithNoLeftButtonIsIgnored(); - void cancelEventStopPhysicsAndResetsStateOfWidget(); - void pannableViewportInsidePannableViewportIgnoresMousePress(); - void disabledWidgetShouldIgnoreMousePressAndPanEvents(); - void usingCustomPhysics(); void settingNewPhysicsShouldEmitPhysicsChangeSignal(); void settingPhysicsToNULLShouldNotBreakTheWidget(); void panGestureMovesPhysicsPointer(); void panGestureAgainstPanningDirectionIsIgnored(); - void panGestureCancelsMouseEvents(); - void ignoredPanGestureShouldNotCancelMouseEvents(); - - void tapAndHoldGestureShouldBeGrabbedIfViewportIsMoving(); - void testPanThreshold(); private: MPannableWidget *widget; diff --git a/tests/ut_mpanrecognizer/ut_mpanrecognizer.cpp b/tests/ut_mpanrecognizer/ut_mpanrecognizer.cpp index d2c056d7..f3d7b05f 100644 --- a/tests/ut_mpanrecognizer/ut_mpanrecognizer.cpp +++ b/tests/ut_mpanrecognizer/ut_mpanrecognizer.cpp @@ -23,7 +23,8 @@ #include "mpangesture_p.h" -#include <QGraphicsSceneMouseEvent> +#include <QGraphicsView> +#include <QMouseEvent> #include <QtTest/QtTest> #include <MApplication> @@ -67,11 +68,15 @@ Qt::GestureState QGesture::state() const } MApplication *app; +QGraphicsView *view; void Ut_MPanRecognizer::initTestCase() { static int argc = 1; static char *app_name[1] = { (char *) "./ut_mpanrecognizer" }; app = new MApplication(argc, app_name); + + view = new QGraphicsView(); + view->show(); } void Ut_MPanRecognizer::cleanupTestCase() @@ -92,25 +97,14 @@ void Ut_MPanRecognizer::cleanup() void Ut_MPanRecognizer::testCreateGesture() { - panGesture = static_cast<MPanGesture*>(recognizer->create(this)); + panGesture = static_cast<MPanGesture*>(recognizer->create(view)); } void Ut_MPanRecognizer::testRecognize() { - QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); - pressEvent.setPos(QPointF(0,0)); - pressEvent.setScenePos(QPointF(0,0)); - pressEvent.setScreenPos(QPoint(0,0)); - - QGraphicsSceneMouseEvent moveEvent(QEvent::GraphicsSceneMouseMove); - moveEvent.setPos(QPointF(0,100)); - moveEvent.setScenePos(QPointF(0,100)); - moveEvent.setScreenPos(QPoint(0,100)); - - QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); - releaseEvent.setPos(QPointF(0,100)); - releaseEvent.setScenePos(QPointF(0,100)); - releaseEvent.setScreenPos(QPoint(0,100)); + QMouseEvent pressEvent(QEvent::MouseButtonPress,QPoint(0,0), Qt::LeftButton, Qt::LeftButton, 0); + QMouseEvent moveEvent(QEvent::MouseMove,QPoint(0,100), Qt::LeftButton, Qt::LeftButton, 0); + QMouseEvent releaseEvent(QEvent::MouseButtonRelease,QPoint(0,100), Qt::LeftButton, Qt::LeftButton, 0); QGestureRecognizer::Result currentState; currentState = recognizer->recognize(panGesture, 0, &pressEvent); @@ -128,15 +122,8 @@ void Ut_MPanRecognizer::testRecognize() void Ut_MPanRecognizer::testTapIsNotRecognizedAsPan() { - QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); - pressEvent.setPos(QPointF(0,0)); - pressEvent.setScenePos(QPointF(0,0)); - pressEvent.setScreenPos(QPoint(0,0)); - - QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); - pressEvent.setPos(QPointF(0,0)); - pressEvent.setScenePos(QPointF(0,0)); - pressEvent.setScreenPos(QPoint(0,0)); + QMouseEvent pressEvent(QEvent::MouseButtonPress,QPoint(0,0), Qt::LeftButton, Qt::LeftButton, 0); + QMouseEvent releaseEvent(QEvent::MouseButtonRelease,QPoint(0,0), Qt::LeftButton, Qt::LeftButton, 0); QGestureRecognizer::Result currentState; currentState = recognizer->recognize(panGesture, 0, &pressEvent); @@ -148,36 +135,20 @@ void Ut_MPanRecognizer::testTapIsNotRecognizedAsPan() void Ut_MPanRecognizer::testTheMovementInDirectionOtherThanRecognizedIsZeroed() { - QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress); - pressEvent.setPos(QPointF(0,0)); - pressEvent.setScenePos(QPointF(0,0)); - pressEvent.setScreenPos(QPoint(0,0)); - - QGraphicsSceneMouseEvent moveEvent(QEvent::GraphicsSceneMouseMove); - moveEvent.setPos(QPointF(0,100)); - moveEvent.setScenePos(QPointF(0,100)); - moveEvent.setScreenPos(QPoint(0,100)); - - QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease); - releaseEvent.setPos(QPointF(0,100)); - releaseEvent.setScenePos(QPointF(0,100)); - releaseEvent.setScreenPos(QPoint(0,100)); + QMouseEvent pressEvent(QEvent::MouseButtonPress,QPoint(0,0), Qt::LeftButton, Qt::LeftButton, 0); + QMouseEvent moveEvent1(QEvent::MouseMove,QPoint(0,100), Qt::LeftButton, Qt::LeftButton, 0); + QMouseEvent moveEvent2(QEvent::MouseMove,QPoint(30,100), Qt::LeftButton, Qt::LeftButton, 0); QGestureRecognizer::Result currentState; currentState = recognizer->recognize(panGesture, 0, &pressEvent); QCOMPARE( currentState, QGestureRecognizer::MayBeGesture); - currentState = recognizer->recognize(panGesture, 0, &moveEvent); + currentState = recognizer->recognize(panGesture, 0, &moveEvent1); QCOMPARE( currentState, QGestureRecognizer::TriggerGesture); //Artificially setting state of QGesture object. currentGestureState = Qt::GestureUpdated; - - moveEvent.setPos(QPointF(30,100)); - moveEvent.setScenePos(QPointF(30,100)); - moveEvent.setScreenPos(QPoint(30,100)); - - recognizer->recognize(panGesture, 0, &moveEvent); + recognizer->recognize(panGesture, 0, &moveEvent2); QCOMPARE( panGesture->offset().x(), 0.0); } diff --git a/tests/ut_mtoolbar/ut_mtoolbar.pro b/tests/ut_mtoolbar/ut_mtoolbar.pro index d7eb10a1..214a3edf 100644 --- a/tests/ut_mtoolbar/ut_mtoolbar.pro +++ b/tests/ut_mtoolbar/ut_mtoolbar.pro @@ -1,18 +1,21 @@ include(../common_top.pri) TARGET = ut_mtoolbar - -INCLUDEPATH += $$MSRCDIR/corelib/widgets $$MSRCDIR/corelib/style +INCLUDEPATH += $$MSRCDIR/corelib/widgets \ + $$MSRCDIR/corelib/style \ + $$MSRCDIR/corelib/scene SOURCES += \ ut_mtoolbar.cpp \ $$MSRCDIR/corelib/widgets/mtoolbar.cpp \ $$MSRCDIR/corelib/widgets/core/mwidgetcontroller.cpp \ $$MSRCDIR/corelib/widgets/core/mwidget.cpp \ - + $$MSRCDIR/corelib/scene/mscene.cpp \ + HEADERS += \ ut_mtoolbar.h \ $$MSRCDIR/corelib/widgets/mtoolbar_p.h \ $$MSRCDIR/corelib/widgets/core/mwidgetcontroller_p.h \ + $$MSRCDIR/corelib/scene/mscene.h \ include(../common_bot.pri) |