diff options
author | Jarno Malmari <ext-jarno.malmari@nokia.com> | 2010-12-31 09:08:08 +0200 |
---|---|---|
committer | Pekka Vuorela <pekka.ta.vuorela@nokia.com> | 2011-01-05 17:29:18 +0200 |
commit | 0940ea05991df96ada11845d9d5fea7aa0769de6 (patch) | |
tree | 028967a3f2febab8c1537894a2aff7e4e1d0a736 | |
parent | 2440ae8e7e969db0fb52082d136795ec97638f0d (diff) |
Changes: Disable panning while text magnifier is in use.
RevBy: Pekka Vuorela
Details: MTextMagnifier, or rather its overlay parent, grabs the
pan gesture and by consuming it the gesture never reaches pannable
widgets.
-rw-r--r-- | src/views/mtextmagnifier.cpp | 54 | ||||
-rw-r--r-- | src/views/mtextmagnifier.h | 22 | ||||
-rw-r--r-- | tests/tests.pro | 1 | ||||
-rw-r--r-- | tests/ut_mtextmagnifier/.gitignore | 2 | ||||
-rw-r--r-- | tests/ut_mtextmagnifier/mtextmagnifierstyle.h | 31 | ||||
-rw-r--r-- | tests/ut_mtextmagnifier/ut_mtextmagnifier.cpp | 117 | ||||
-rw-r--r-- | tests/ut_mtextmagnifier/ut_mtextmagnifier.h | 52 | ||||
-rw-r--r-- | tests/ut_mtextmagnifier/ut_mtextmagnifier.pro | 15 |
8 files changed, 286 insertions, 8 deletions
diff --git a/src/views/mtextmagnifier.cpp b/src/views/mtextmagnifier.cpp index a49a82d4..5997ab86 100644 --- a/src/views/mtextmagnifier.cpp +++ b/src/views/mtextmagnifier.cpp @@ -26,11 +26,11 @@ #include <QPainter> #include <QStyleOptionGraphicsItem> + MTextMagnifier::MTextMagnifier(const QGraphicsItem &sourceItem) : MStylableWidget(0), sourceItem(sourceItem) { - overlay.setFlag(QGraphicsItem::ItemHasNoContents, true); setParentItem(&overlay); } @@ -44,10 +44,12 @@ void MTextMagnifier::appear() { // Appear in the scene of the source item. overlay.appear(sourceItem.scene()); + overlay.grabGesture(Qt::PanGesture); } void MTextMagnifier::disappear() { + overlay.ungrabGesture(Qt::PanGesture); overlay.disappear(); } @@ -58,7 +60,7 @@ void MTextMagnifier::setMagnifiedPosition(const QPointF &sourceItemPos) bool MTextMagnifier::isAppeared() const { - return (overlay.sceneWindowState() != MSceneWindow::Disappeared); + return overlay.isAppeared(); } void MTextMagnifier::drawContents(QPainter *painter, @@ -97,11 +99,14 @@ void MTextMagnifier::drawContents(QPainter *painter, const_cast<QGraphicsItem *>(&sourceItem)->paint(&offscreenPainter, &sourceItemOption); offscreenPainter.resetTransform(); - offscreenPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn); - offscreenPainter.drawPixmap(offscreenSurface->rect(), *style()->magnifierMask()); - - offscreenPainter.setCompositionMode(QPainter::CompositionMode_DestinationOver); - offscreenPainter.drawPixmap(offscreenSurface->rect(), *style()->magnifierFrame()); + if (style()->magnifierMask()) { + offscreenPainter.setCompositionMode(QPainter::CompositionMode_DestinationIn); + offscreenPainter.drawPixmap(offscreenSurface->rect(), *style()->magnifierMask()); + } + if (style()->magnifierFrame()) { + offscreenPainter.setCompositionMode(QPainter::CompositionMode_DestinationOver); + offscreenPainter.drawPixmap(offscreenSurface->rect(), *style()->magnifierFrame()); + } offscreenPainter.end(); // Paint the result to screen @@ -115,6 +120,9 @@ void MTextMagnifier::drawContents(QPainter *painter, void MTextMagnifier::applyStyle() { + if (!style()->magnifierFrame()) { + return; + } // Update bounding rectangle. prepareGeometryChange(); QSize magnifierSize(style()->magnifierFrame()->size()); @@ -144,3 +152,35 @@ void MTextMagnifier::prepareOffscreenSurface(const QSize &size) } offscreenSurface.reset(new QPixmap(size)); } + + +// Magnifier overlay widget + +MagnifierOverlay::MagnifierOverlay() +{ + setFlag(QGraphicsItem::ItemHasNoContents, true); + + // Occupy whole screen to be able to catch panning gestures. The size does not really matter + // as long as it covers whole screen in portrait and landscape. + setManagedManually(true); + const QSizeF visibleSceneSize = sceneManager()->visibleSceneSize(M::Landscape); + const qreal size = qMax<qreal>(visibleSceneSize.width(), visibleSceneSize.height()); + setPreferredSize(size, size); +} + +bool MagnifierOverlay::isAppeared() const +{ + return sceneWindowState() != MSceneWindow::Disappeared; +} + +void MagnifierOverlay::panGestureEvent(QGestureEvent *event, QPanGesture *panGesture) +{ + // Accept gesture if magnifier is appeared. This is done to prevent panning of + // application page but will of course prevent other uses of pan gestures as well. + if (panGesture->state() == Qt::GestureStarted + && isAppeared()) { + event->accept(panGesture); + } else { + event->ignore(panGesture); + } +} diff --git a/src/views/mtextmagnifier.h b/src/views/mtextmagnifier.h index 1cfb9159..4a95c5cd 100644 --- a/src/views/mtextmagnifier.h +++ b/src/views/mtextmagnifier.h @@ -24,7 +24,24 @@ #include <MOverlay> +#include <MSceneManager> +#include <QDebug> +#include <QPanGesture> + //! \internal + +//! Overlay widget to allow magnifier to stay on top of other widgets. +class MagnifierOverlay : public MOverlay +{ +public: + MagnifierOverlay(); + bool isAppeared() const; + +protected: + virtual void panGestureEvent(QGestureEvent *event, QPanGesture *panGesture); + friend class Ut_MTextMagnifier; +}; + /*! \brief Magnifier widget to magnify text. * * The class magnifies contents of a certain source item. @@ -74,10 +91,13 @@ private: QScopedPointer<QPixmap> offscreenSurface; QScopedPointer<QBitmap> mask; - MOverlay overlay; + MagnifierOverlay overlay; M_STYLABLE_WIDGET(MTextMagnifierStyle) + + friend class Ut_MTextMagnifier; }; + //! \internal_end #endif diff --git a/tests/tests.pro b/tests/tests.pro index 4befe738..f8893d40 100644 --- a/tests/tests.pro +++ b/tests/tests.pro @@ -75,6 +75,7 @@ SUBDIRS = \ ut_msliderview \ ut_mtextedit \ ut_mtexteditview \ + ut_mtextmagnifier \ ut_mthemedaemonprotocol \ ut_mtheme \ ut_mtoolbar \ diff --git a/tests/ut_mtextmagnifier/.gitignore b/tests/ut_mtextmagnifier/.gitignore new file mode 100644 index 00000000..2f9f8340 --- /dev/null +++ b/tests/ut_mtextmagnifier/.gitignore @@ -0,0 +1,2 @@ +ut_mtextmagnifier + diff --git a/tests/ut_mtextmagnifier/mtextmagnifierstyle.h b/tests/ut_mtextmagnifier/mtextmagnifierstyle.h new file mode 100644 index 00000000..3730b0d7 --- /dev/null +++ b/tests/ut_mtextmagnifier/mtextmagnifierstyle.h @@ -0,0 +1,31 @@ +#ifndef MTEXTMAGNIFIERSTYLE_H +#define MTEXTMAGNIFIERSTYLE_H + +#include <MWidgetStyle> + +class QString; +class MWidgetController; + +class MTextMagnifierStyle +{ +public: + qreal magnification() const { return 0.0; } + QPixmap *magnifierMask() const { return 0; } + QPixmap *magnifierFrame() const { return 0; } + QPointF visualOffset() const { return QPointF(); } + +}static gMagnifierStyle; + +class MTextMagnifierStyleContainer : public MWidgetStyleContainer +{ +public: + void initialize(const QString &, const QString &, const MWidgetController *){} + void setModeDefault() const {} + const MTextMagnifierStyle* operator->() const + { + return &gMagnifierStyle; + } +}; + + +#endif // MTEXTMAGNIFIERSTYLE_H diff --git a/tests/ut_mtextmagnifier/ut_mtextmagnifier.cpp b/tests/ut_mtextmagnifier/ut_mtextmagnifier.cpp new file mode 100644 index 00000000..4e8a13c2 --- /dev/null +++ b/tests/ut_mtextmagnifier/ut_mtextmagnifier.cpp @@ -0,0 +1,117 @@ +/*************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (directui@nokia.com) +** +** This file is part of libmeegotouch. +** +** If you have questions regarding the use of this file, please contact +** Nokia at directui@nokia.com. +** +** This library is free software; you can redistribute it and/or +** modify it under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation +** and appearing in the file LICENSE.LGPL included in the packaging +** of this file. +** +****************************************************************************/ + +#include "ut_mtextmagnifier.h" +#include "views/mtextmagnifier.h" + +#include <QEvent> +#include <QGraphicsWidget> +#include <QMetaType> +#include <QPainter> +#include <QPanGesture> +#include <QSignalSpy> + +#include <MApplication> +#include <MApplicationWindow> +#include <MCancelEvent> +#include <MScene> + +namespace { + Qt::GestureState gGestureState = Qt::GestureStarted; +} + +Qt::GestureState QGesture::state() const +{ + return gGestureState; +} + +void Ut_MTextMagnifier::initTestCase() +{ + static int dummyArgc = 1; + static char *dummyArgv[1] = { (char *) "./ut_mtextmagnifier" }; + MApplication::setLoadMInputContext(false); + m_app = new MApplication(dummyArgc, dummyArgv); + m_appWindow = new MApplicationWindow; + + magnifiedWidget = new QGraphicsWidget; + m_appWindow->scene()->addItem(magnifiedWidget); +} + +void Ut_MTextMagnifier::cleanupTestCase() +{ + delete magnifiedWidget; + magnifiedWidget = 0; + delete m_appWindow; + m_appWindow = 0; + delete m_app; + m_app = 0; +} + + +void Ut_MTextMagnifier::init() +{ + subject = new MTextMagnifier(*magnifiedWidget); +} + + +void Ut_MTextMagnifier::cleanup() +{ + delete subject; + subject = 0; +} + +void Ut_MTextMagnifier::testMagnifierConsumesPanGesture_data() +{ + QTest::addColumn<Qt::GestureState>("gestureState"); + QTest::addColumn<bool>("wasConsumed"); + + // This is needs to work to prevent panning while magnifier is shown. + QTest::newRow("gesture started") << Qt::GestureStarted << true; + + // Other gesture states should be ignored. + QTest::newRow("gesture canceled") << Qt::GestureCanceled << false; + QTest::newRow("gesture updated") << Qt::GestureUpdated << false; + QTest::newRow("gesture finished") << Qt::GestureFinished << false; +} + +void Ut_MTextMagnifier::testMagnifierConsumesPanGesture() +{ + QFETCH(Qt::GestureState, gestureState); + QFETCH(bool, wasConsumed); + + subject->appear(); + QVERIFY(subject->isAppeared()); + + // Create gesture object and put it inside an event. + QPanGesture panGesture; + gGestureState = gestureState; + + QList<QGesture *> gestures; + gestures << &panGesture; + QGestureEvent event(gestures); + event.setAccepted(Qt::PanGesture, false); + + // It is actually overlay who gets the event. + subject->overlay.gestureEvent(&event); + + QCOMPARE(event.isAccepted(Qt::PanGesture), wasConsumed); +} + +QTEST_APPLESS_MAIN(Ut_MTextMagnifier) + diff --git a/tests/ut_mtextmagnifier/ut_mtextmagnifier.h b/tests/ut_mtextmagnifier/ut_mtextmagnifier.h new file mode 100644 index 00000000..d1c0acb8 --- /dev/null +++ b/tests/ut_mtextmagnifier/ut_mtextmagnifier.h @@ -0,0 +1,52 @@ +/*************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (directui@nokia.com) +** +** This file is part of libmeegotouch. +** +** If you have questions regarding the use of this file, please contact +** Nokia at directui@nokia.com. +** +** This library is free software; you can redistribute it and/or +** modify it under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation +** and appearing in the file LICENSE.LGPL included in the packaging +** of this file. +** +****************************************************************************/ + +#ifndef UT_MTEXTMAGNIFIER_H +#define UT_MTEXTMAGNIFIER_H + +#include <QObject> +#include <QtTest/QtTest> + +class MApplication; +class MApplicationWindow; +class MTextMagnifier; +class QGraphicsWidget; + +class Ut_MTextMagnifier : public QObject +{ + Q_OBJECT + +private slots: + void init(); + void cleanup(); + void initTestCase(); + void cleanupTestCase(); + + void testMagnifierConsumesPanGesture_data(); + void testMagnifierConsumesPanGesture(); + +private: + MApplication *m_app; + MApplicationWindow *m_appWindow; + MTextMagnifier *subject; + QGraphicsWidget *magnifiedWidget; +}; + +#endif + diff --git a/tests/ut_mtextmagnifier/ut_mtextmagnifier.pro b/tests/ut_mtextmagnifier/ut_mtextmagnifier.pro new file mode 100644 index 00000000..c8cb03b4 --- /dev/null +++ b/tests/ut_mtextmagnifier/ut_mtextmagnifier.pro @@ -0,0 +1,15 @@ +include(../common_top.pri) +TARGET = ut_mtextmagnifier + +INCLUDEPATH += $$MSRCDIR + +SOURCES += \ + ut_mtextmagnifier.cpp \ + $$MSRCDIR/views/mtextmagnifier.cpp + +HEADERS += \ + ut_mtextmagnifier.h \ + mtextmagnifierstyle.h \ + $$MSRCDIR/views/mtextmagnifier.h + +include(../common_bot.pri) |