diff options
author | Jarno Malmari <ext-jarno.malmari@nokia.com> | 2010-12-20 19:46:28 +0200 |
---|---|---|
committer | Daniel d'Andrada <daniel.dandrada@nokia.com> | 2011-01-17 16:38:48 +0200 |
commit | ab79494fe0b9901fbf047ca2cd7a97f10ff13571 (patch) | |
tree | 08ce9ad4351cc826a45c6f6c3b6773c655bf6aee | |
parent | 0b881650d813656eed497e695ced5b60b3234aa4 (diff) |
Changes: Add bottom docking test to Ut_MInputWidgetRelocator
RevBy: Daniel d'Andrada
Details: Separated ScrollableWidget and Scroller classes from Ut_MScrollChain
so they can be used in Ut_MInputWidgetRelocator as well.
-rw-r--r-- | tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.cpp | 237 | ||||
-rw-r--r-- | tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.h | 38 | ||||
-rw-r--r-- | tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.pro | 18 | ||||
-rw-r--r-- | tests/ut_mscrollchain/scrollablewidget.cpp | 38 | ||||
-rw-r--r-- | tests/ut_mscrollchain/scrollablewidget.h | 23 | ||||
-rw-r--r-- | tests/ut_mscrollchain/scroller.cpp | 51 | ||||
-rw-r--r-- | tests/ut_mscrollchain/scroller.h | 24 | ||||
-rw-r--r-- | tests/ut_mscrollchain/ut_mscrollchain.cpp | 89 | ||||
-rw-r--r-- | tests/ut_mscrollchain/ut_mscrollchain.h | 20 | ||||
-rw-r--r-- | tests/ut_mscrollchain/ut_mscrollchain.pro | 8 |
10 files changed, 315 insertions, 231 deletions
diff --git a/tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.cpp b/tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.cpp index 3c8d0684..ea17a527 100644 --- a/tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.cpp +++ b/tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.cpp @@ -25,14 +25,12 @@ #include "minputwidgetrelocator.h" #include "mkeyboardstatetracker.h" #include "ut_minputwidgetrelocator.h" +#include "scrollablewidget.h" +#include "scroller.h" #include <QGraphicsView> namespace { - QRect gLastTargetRect; - int gScrollCallCount = 0; - QPointer<QGraphicsWidget> gDummyWidget; - // Stubbed style values. These are what subject sees as values read from MRelocatorStyle. qreal gStyleVerticalAnchorPosition = 0.0; qreal gStyleTopNoGoMargin = 0.0; @@ -42,37 +40,6 @@ namespace { Q_DECLARE_METATYPE(Ut_MInputWidgetRelocator::Operations); Q_DECLARE_METATYPE(Ut_MInputWidgetRelocator::Zone); - -class InputWidget : public QGraphicsWidget -{ -public: - InputWidget(QGraphicsItem *parent = 0) - :QGraphicsWidget(parent) - { - setFlag(QGraphicsItem::ItemIsFocusable, true); - setFlag(QGraphicsItem::ItemAcceptsInputMethod, true); - resize(100, 20); - } - - virtual ~InputWidget() - { - } - - QRect microFocusRect() const - { - return rect().toRect(); // only height is important at the moment - } - -protected: - QVariant inputMethodQuery(Qt::InputMethodQuery query) const - { - if (query == Qt::ImMicroFocus) { - return microFocusRect(); - } - return QGraphicsWidget::inputMethodQuery(query); - } -}; - void Ut_MInputWidgetRelocator::initTestCase() { qRegisterMetaType<Operations>("Operations"); @@ -98,8 +65,8 @@ void Ut_MInputWidgetRelocator::initTestCase() rootElement->setFlag(QGraphicsItem::ItemHasNoContents, true); scene->addItem(rootElement); - gDummyWidget = QPointer<QGraphicsWidget>(new QGraphicsWidget); - scene->addItem(gDummyWidget); + Scroller::maximumOffset = QPoint(1000, 1000); + MScrollChain::registerScroller<ScrollableWidget>(QSharedPointer<Scroller>(new Scroller)); } void Ut_MInputWidgetRelocator::cleanupTestCase() @@ -115,10 +82,23 @@ void Ut_MInputWidgetRelocator::cleanupTestCase() void Ut_MInputWidgetRelocator::init() { subject = new MInputWidgetRelocator(scene, rootElement, M::Landscape); + + parentScrollableWidget = QPointer<ScrollableWidget>(new ScrollableWidget(rootElement)); + parentScrollableWidget->resize(1000, 1000); + childScrollableWidget = QPointer<ScrollableWidget>(new ScrollableWidget); + childScrollableWidget->resize(1000, 1000); + inputWidget = QPointer<InputWidget>(new InputWidget); + + parentScrollableWidget->setContentItem(childScrollableWidget); + childScrollableWidget->setContentItem(inputWidget); } void Ut_MInputWidgetRelocator::cleanup() { + delete inputWidget; + delete childScrollableWidget; + delete parentScrollableWidget; + delete subject; subject = 0; } @@ -126,54 +106,50 @@ void Ut_MInputWidgetRelocator::cleanup() void Ut_MInputWidgetRelocator::testPostponeAndUpdate_data() { QTest::addColumn<Operations>("operations"); - QTest::addColumn<int>("numberOfUpdates"); + QTest::addColumn<bool>("updated"); // Operations RotationEnd, HwKbChange, and SipChange always sets an update pending. - QTest::newRow("begin rotation") << (Operations() << RotationBegin) << 0; - QTest::newRow("end rotation") << (Operations() << RotationEnd) << 1; - QTest::newRow("begin scene window animation") << (Operations() << SceneWindowAppearing) << 0; + QTest::newRow("begin rotation") << (Operations() << RotationBegin) << false; + QTest::newRow("end rotation") << (Operations() << RotationEnd) << true; + QTest::newRow("begin scene window animation") << (Operations() << SceneWindowAppearing) << false; QTest::newRow("begin and end scene window animation") - << (Operations() << SceneWindowAppearing << SceneWindowAppeared) << 0; + << (Operations() << SceneWindowAppearing << SceneWindowAppeared) << false; QTest::newRow("rotation during animation, animation not finished") - << (Operations() << SceneWindowAppearing << RotationBegin << RotationEnd) << 0; + << (Operations() << SceneWindowAppearing << RotationBegin << RotationEnd) << false; QTest::newRow("rotation during animation, animation finished") << (Operations() << SceneWindowAppearing << RotationBegin << RotationEnd - << SceneWindowAppeared) << 1; + << SceneWindowAppeared) << true; - QTest::newRow("hwkb changed state") << (Operations() << HwKbChange) << 1; - QTest::newRow("hwkb changed state but animating") << (Operations() << SceneWindowAppearing << HwKbChange) << 0; - QTest::newRow("hwkb changed state but rotating") << (Operations() << RotationBegin << HwKbChange) << 0; + QTest::newRow("hwkb changed state") << (Operations() << HwKbChange) << true; + QTest::newRow("hwkb changed state but animating") << (Operations() << SceneWindowAppearing << HwKbChange) << false; + QTest::newRow("hwkb changed state but rotating") << (Operations() << RotationBegin << HwKbChange) << false; - QTest::newRow("sip changed") << (Operations() << SipChange) << 1; - QTest::newRow("sip changed but animating") << (Operations() << SceneWindowAppearing << SipChange) << 0; - QTest::newRow("sip changed but rotating") << (Operations() << RotationBegin << SipChange) << 0; + QTest::newRow("sip changed") << (Operations() << SipChange) << true; + QTest::newRow("sip changed but animating") << (Operations() << SceneWindowAppearing << SipChange) << false; + QTest::newRow("sip changed but rotating") << (Operations() << RotationBegin << SipChange) << false; QTest::newRow("sip changed, rotation finished but animation didn't") << (Operations() << RotationBegin << SceneWindowAppearing - << SipChange << RotationEnd) << 0; + << SipChange << RotationEnd) << false; QTest::newRow("sip changed, rotation and animation finished") << (Operations() << RotationBegin << SceneWindowAppearing - << SipChange << RotationEnd << SceneWindowAppeared) << 1; + << SipChange << RotationEnd << SceneWindowAppeared) << true; } void Ut_MInputWidgetRelocator::testPostponeAndUpdate() { QFETCH(Operations, operations); - QFETCH(int, numberOfUpdates); + QFETCH(bool, updated); const QRect sipRect(QPoint(0, 300), scene->sceneRect().size().toSize()); MInputMethodState::instance()->setInputMethodArea(sipRect); - // Only create input widget to be able to follow MScrollChain::run(). - InputWidget widget(rootElement); - moveWidgetToZone(&widget, UpperNoGoZone, scene->sceneRect().toRect(), M::Landscape); - widget.setPos(-100, -100); // Position somewhere beyond nogo zones so relocation takes place. - widget.setFocus(); - - gScrollCallCount = 0; + const QPointF invalidPos(-1000, -1000); + inputWidget->setPos(invalidPos); // Position somewhere beyond nogo zones so relocation takes place. + inputWidget->setFocus(); foreach (Operation op, operations) { switch (op) { @@ -205,7 +181,10 @@ void Ut_MInputWidgetRelocator::testPostponeAndUpdate() } } - QCOMPARE(gScrollCallCount, numberOfUpdates); + const QPointF actualPos = rootElement->mapFromItem(inputWidget, 0, 0); + qDebug() << "actual: " << actualPos << ", invalid Pos: " << invalidPos; + bool actualUpdated = actualPos != invalidPos; + QCOMPARE(actualUpdated, updated); } void Ut_MInputWidgetRelocator::testTargetPosition_data() @@ -255,15 +234,12 @@ void Ut_MInputWidgetRelocator::testTargetPosition() exposedContentRect.setBottom(sipRect.top()); } - InputWidget widget(rootElement); - moveWidgetToZone(&widget, initialZone, exposedContentRect, orientation); - widget.setFocus(); - - // Set target rect of stub first as invalid. - const QRect invalidRect(-10, -10, -10, -10); - gLastTargetRect = invalidRect; + moveWidgetToZone(inputWidget, initialZone, exposedContentRect, orientation); + inputWidget->setFocus(); + QRectF actualRectBefore = rootElement->mapRectFromItem(inputWidget, inputWidget->rect()); subject->update(); + QRectF actualRectAfter = rootElement->mapRectFromItem(inputWidget, inputWidget->rect()); // Test two cases here: // 1) If widget was initially beyond nogo zones the new position should be at anchor point. @@ -272,16 +248,87 @@ void Ut_MInputWidgetRelocator::testTargetPosition() switch (initialZone) { case UpperNoGoZone: case LowerNoGoZone: - QCOMPARE(static_cast<qreal>(gLastTargetRect.y()), verticalAnchorPosition(orientation)); - QVERIFY(allowedZone(exposedContentRect, orientation).contains(gLastTargetRect)); + QCOMPARE(actualRectAfter.y(), verticalAnchorPosition(orientation)); + QVERIFY(allowedZone(exposedContentRect, orientation).contains(actualRectAfter.toRect())); break; case AllowedZone: // Already in allowed zone. Shouldn't have moved. - QCOMPARE(gLastTargetRect, invalidRect); + QCOMPARE(actualRectAfter, actualRectBefore); break; } } +void Ut_MInputWidgetRelocator::testDockBottom_data() +{ + QTest::addColumn<M::Orientation>("orientation"); + QTest::addColumn<QRect>("sipRect"); + QTest::addColumn<Zone>("initialZone"); + + // Sip rectangle is given in scene coordinates. + const QSize screenSize(MDeviceProfile::instance()->resolution()); + const QRect sipRect(0, screenSize.height() / 2, + screenSize.height() - (screenSize.height() / 2), + screenSize.width()); + + QTest::newRow("no sip, widget at upper nogo zone") << M::Landscape << QRect() << UpperNoGoZone; + QTest::newRow("no sip, widget already visible") << M::Landscape << QRect() << AllowedZone; + QTest::newRow("no sip, widget at lower nogo zone") << M::Landscape << QRect() << LowerNoGoZone; + QTest::newRow("with sip, widget at upper nogo zone") << M::Landscape << sipRect << UpperNoGoZone; + QTest::newRow("with sip, widget already visible") << M::Landscape << sipRect << AllowedZone; + QTest::newRow("with sip, widget at lower nogo zone") << M::Landscape << sipRect << LowerNoGoZone; + + QTest::newRow("PT, no sip, widget at upper nogo zone") << M::Portrait << QRect() << UpperNoGoZone; + QTest::newRow("PT, no sip, widget already visible") << M::Portrait << QRect() << AllowedZone; + QTest::newRow("PT, no sip, widget at lower nogo zone") << M::Portrait << QRect() << LowerNoGoZone; + QTest::newRow("PT, with sip, widget at upper nogo zone") << M::Portrait << sipRect << UpperNoGoZone; + QTest::newRow("PT, with sip, widget already visible") << M::Portrait << sipRect << AllowedZone; + QTest::newRow("PT, with sip, widget at lower nogo zone") << M::Portrait << sipRect << LowerNoGoZone; +} + +void Ut_MInputWidgetRelocator::testDockBottom() +{ + QFETCH(M::Orientation, orientation); + QFETCH(QRect, sipRect); + QFETCH(Zone, initialZone); + + // Apply orientation + subject->handleRotationBegin(); + subject->handleRotationFinished(orientation); + + updateStubbedStyleValues(orientation); + + MInputMethodState::instance()->setInputMethodArea(sipRect); + QRect exposedContentRect(rootElement->mapRectFromScene(scene->sceneRect()).toRect()); + if (!sipRect.isNull()) { + sipRect = rootElement->mapRectFromScene(sipRect).toRect(); + // Assume sip comes from bottom of screen. + exposedContentRect.setBottom(sipRect.top()); + } + + // We will dock childScrollableWidget. + childScrollableWidget->setProperty("dockBottom", QVariant(true)); + childScrollableWidget->resize(10, 10); + + moveWidgetToZone(inputWidget, initialZone, exposedContentRect, orientation); + inputWidget->setFocus(); + + subject->update(); + const QRectF dockedRectAfter = rootElement->mapRectFromItem(childScrollableWidget, + childScrollableWidget->rect()); + + qDebug() << "docked rect bottom: " << dockedRectAfter.toRect().bottom() + << " vs. exposed content rectangle bottom: " << exposedContentRect.bottom(); + + // No matter where the input widget was it should always dock to bottom. + // FIXME: Since the scrolling system does not play with qreals we might get rounding errors here. + // Allow 1 pixel error. + qreal error = dockedRectAfter.bottom() + - static_cast<qreal>(exposedContentRect.bottom()); + QVERIFY(qAbs<qreal>(error) <= 1.0); +} + +// Helper methods + QRect Ut_MInputWidgetRelocator::allowedZone(const QRect &exposedContentRect, M::Orientation orientation) const { return exposedContentRect.adjusted(0, topNoGoMargin(orientation), @@ -346,54 +393,6 @@ void Ut_MInputWidgetRelocator::updateStubbedStyleValues(M::Orientation orientati gStyleBottomNoGoMargin = bottomNoGoMargin(orientation); } -// Stubbing MScrollChain - -MScrollChain::MScrollChain(const QGraphicsItem *, const QGraphicsItem *) - : rootItem(0), scrollTarget(0) -{ -} - -MScrollChain::~MScrollChain() -{ -} - -int MScrollChain::count() const -{ - return 1; -} - -const QGraphicsWidget *MScrollChain::widgetAt(int index) const -{ - Q_UNUSED(index); - return gDummyWidget; -} - -void MScrollChain::restore(const MScrollChain *excludeChain) -{ - Q_UNUSED(excludeChain); -} - -void MScrollChain::addBottomUpScroll(const QRect &targetRect, const QPoint &originPoint, int index) -{ - gLastTargetRect = targetRect; - Q_UNUSED(originPoint); - Q_UNUSED(index); -} - -void MScrollChain::addMinimalScroll(const QRect &localRect, - int startingIndex, - int untilIndex) -{ - Q_UNUSED(localRect); - Q_UNUSED(startingIndex); - Q_UNUSED(untilIndex); -} - -void MScrollChain::applyScrolling() -{ - ++gScrollCallCount; -} - QTEST_APPLESS_MAIN(Ut_MInputWidgetRelocator) // Stubbing MRelocatorStyle which is defined in stub mrelocatorstyle.h in current test directory. diff --git a/tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.h b/tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.h index 6d0b9b84..3f8be945 100644 --- a/tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.h +++ b/tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.h @@ -20,16 +20,17 @@ #ifndef UT_MINPUTWIDGETRELOCATOR_H #define UT_MINPUTWIDGETRELOCATOR_H +#include <QGraphicsWidget> #include <QtTest/QtTest> #include <QObject> #include "mapplication.h" class MInputWidgetRelocator; -class QGraphicsWidget; class QGraphicsScene; class QGraphicsView; class InputWidget; +class ScrollableWidget; class Ut_MInputWidgetRelocator : public QObject { @@ -66,6 +67,8 @@ private slots: void testPostponeAndUpdate(); void testTargetPosition_data(); void testTargetPosition(); + void testDockBottom_data(); + void testDockBottom(); private: QRect allowedZone(const QRect &exposedContentRect, M::Orientation orientation) const; @@ -82,6 +85,39 @@ private: QGraphicsScene *scene; QGraphicsView *view; QGraphicsWidget *rootElement; + QPointer<ScrollableWidget> parentScrollableWidget; + QPointer<ScrollableWidget> childScrollableWidget; + QPointer<InputWidget> inputWidget; +}; + +class InputWidget : public QGraphicsWidget +{ +public: + InputWidget(QGraphicsItem *parent = 0) + :QGraphicsWidget(parent) + { + setFlag(QGraphicsItem::ItemIsFocusable, true); + setFlag(QGraphicsItem::ItemAcceptsInputMethod, true); + resize(100, 20); + } + + virtual ~InputWidget() + { + } + + QRect microFocusRect() const + { + return rect().toRect(); // only height is important at the moment + } + +protected: + QVariant inputMethodQuery(Qt::InputMethodQuery query) const + { + if (query == Qt::ImMicroFocus) { + return microFocusRect(); + } + return QGraphicsWidget::inputMethodQuery(query); + } }; #endif // UT_MINPUTWIDGETRELOCATOR_H diff --git a/tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.pro b/tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.pro index a38198bc..8fe3d206 100644 --- a/tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.pro +++ b/tests/ut_minputwidgetrelocator/ut_minputwidgetrelocator.pro @@ -1,17 +1,27 @@ include(../common_top.pri) TARGET = ut_minputwidgetrelocator INCLUDEPATH += \ - $$MSRCDIR/corelib/core + $$MSRCDIR/corelib/core \ + ../ut_mscrollchain # unit test and unit classes -SOURCES += ut_minputwidgetrelocator.cpp \ - $$MSRCDIR/corelib/core/minputwidgetrelocator.cpp +SOURCES += \ + ut_minputwidgetrelocator.cpp \ + $$MSRCDIR/corelib/core/minputwidgetrelocator.cpp \ + $$MSRCDIR/corelib/core/mscrollchain.cpp \ + $$MSRCDIR/corelib/core/mabstractscroller.cpp \ + ../ut_mscrollchain/scroller.cpp \ + ../ut_mscrollchain/scrollablewidget.cpp # unit test and unit classes HEADERS += \ ut_minputwidgetrelocator.h \ mapplicationwindow_p.h \ mrelocatorstyle.h \ - $$MSRCDIR/corelib/core/minputwidgetrelocator.h + $$MSRCDIR/corelib/core/minputwidgetrelocator.h \ + $$MSRCDIR/corelib/core/mscrollchain.h \ + $$MSRCDIR/corelib/core/mabstractscroller.h \ + ../ut_mscrollchain/scroller.h \ + ../ut_mscrollchain/scrollablewidget.h include(../common_bot.pri) diff --git a/tests/ut_mscrollchain/scrollablewidget.cpp b/tests/ut_mscrollchain/scrollablewidget.cpp new file mode 100644 index 00000000..7dd8b3b0 --- /dev/null +++ b/tests/ut_mscrollchain/scrollablewidget.cpp @@ -0,0 +1,38 @@ +#include "scrollablewidget.h" + +ScrollableWidget::ScrollableWidget(QGraphicsItem *parent) + : QGraphicsWidget(parent), + mContentItem(0) +{ + // Not really needed but illustrates the use of this widget. + setFlag(QGraphicsItem::ItemClipsChildrenToShape, true); +} +ScrollableWidget::~ScrollableWidget() {} + +void ScrollableWidget::setContentItem(QGraphicsItem *item) +{ + delete mContentItem; + mContentItem = item; + item->setParentItem(this); + item->setPos(QPointF()); +} + +const QGraphicsItem *ScrollableWidget::contentItem() const +{ + return mContentItem; +} + +void ScrollableWidget::scrollContents(const QPoint &offset) +{ + if (mContentItem) { + mContentItem->setPos(mContentItem->pos() + offset); + } +} + +void ScrollableWidget::clearScroll() +{ + if (mContentItem) { + mContentItem->setPos(QPointF()); + } +} + diff --git a/tests/ut_mscrollchain/scrollablewidget.h b/tests/ut_mscrollchain/scrollablewidget.h new file mode 100644 index 00000000..21dd4535 --- /dev/null +++ b/tests/ut_mscrollchain/scrollablewidget.h @@ -0,0 +1,23 @@ +#ifndef SCROLLABLEWIDGET_H +#define SCROLLABLEWIDGET_H + +#include <QGraphicsWidget> + +class ScrollableWidget : public QGraphicsWidget +{ + Q_OBJECT +public: + ScrollableWidget(QGraphicsItem *parent = 0); + virtual ~ScrollableWidget(); + + void setContentItem(QGraphicsItem *item); + const QGraphicsItem *contentItem() const; + + void scrollContents(const QPoint &offset); + void clearScroll(); + +private: + QGraphicsItem *mContentItem; +}; + +#endif // SCROLLABLEWIDGET_H diff --git a/tests/ut_mscrollchain/scroller.cpp b/tests/ut_mscrollchain/scroller.cpp new file mode 100644 index 00000000..6c1a398b --- /dev/null +++ b/tests/ut_mscrollchain/scroller.cpp @@ -0,0 +1,51 @@ +#include "scroller.h" +#include "scrollablewidget.h" + +QPoint Scroller::maximumOffset; + +Scroller::Scroller() +{ +} + +Scroller::~Scroller() +{ +} + +QPoint Scroller::queryScrollingAmount(const QGraphicsWidget *widget, + const QRect &targetRect, + const QPoint &originPoint, + const QPoint ¤tOffset) +{ + const ScrollableWidget *scrollable = dynamic_cast<const ScrollableWidget *>(widget); + if (!scrollable) { + return QPoint(); + } + + const QRect boundaries(scrollable->boundingRect().toRect()); + + QRect movedTargetRect(targetRect); + moveRectInsideArea(boundaries, movedTargetRect); + + const QPoint offset(movedTargetRect.topLeft() - originPoint); + const QPoint lowerBound(-maximumOffset - currentOffset); + const QPoint upperBound(maximumOffset - currentOffset); + const QPoint limitedOffset(qBound(lowerBound.x(), offset.x(), upperBound.x()), + qBound(lowerBound.y(), offset.y(), upperBound.y())); + return limitedOffset; +} + +void Scroller::applyScrolling(QGraphicsWidget *widget, const QPoint &contentsOffset) +{ + ScrollableWidget *scrollableWidget = dynamic_cast<ScrollableWidget *>(widget); + if (scrollableWidget) { + scrollableWidget->scrollContents(contentsOffset); + } +} + +void Scroller::restoreScrolling(QGraphicsWidget *widget) +{ + ScrollableWidget *scrollableWidget = dynamic_cast<ScrollableWidget *>(widget); + if (scrollableWidget) { + scrollableWidget->clearScroll(); + } +} diff --git a/tests/ut_mscrollchain/scroller.h b/tests/ut_mscrollchain/scroller.h new file mode 100644 index 00000000..b446b804 --- /dev/null +++ b/tests/ut_mscrollchain/scroller.h @@ -0,0 +1,24 @@ +#ifndef SCROLLER_H +#define SCROLLER_H + +#include <mabstractscroller.h> + +class ScrollableWidget; + +class Scroller : public MAbstractScroller +{ +public: + Scroller(); + virtual ~Scroller(); + + virtual QPoint queryScrollingAmount(const QGraphicsWidget *widget, + const QRect &targetRect, + const QPoint &originPoint, + const QPoint ¤tOffset); + virtual void applyScrolling(QGraphicsWidget *widget, const QPoint &contentsOffset); + virtual void restoreScrolling(QGraphicsWidget *widget); + + static QPoint maximumOffset; // absolute offset values +}; + +#endif // SCROLLER_H diff --git a/tests/ut_mscrollchain/ut_mscrollchain.cpp b/tests/ut_mscrollchain/ut_mscrollchain.cpp index 329e43e5..34d1ad44 100644 --- a/tests/ut_mscrollchain/ut_mscrollchain.cpp +++ b/tests/ut_mscrollchain/ut_mscrollchain.cpp @@ -21,6 +21,9 @@ #include "mabstractscroller.h" #include "mscrollchain.h" +#include "scroller.h" +#include "scrollablewidget.h" + #include <MDeviceProfile> #include <QGraphicsWidget> #include <QGraphicsScene> @@ -28,92 +31,6 @@ Q_DECLARE_METATYPE(QList<int>); -ScrollableWidget::ScrollableWidget(QGraphicsItem *parent) - : QGraphicsWidget(parent), - mContentItem(0) -{ - // Not really needed but illustrates the use of this widget. - setFlag(QGraphicsItem::ItemClipsChildrenToShape, true); -} -ScrollableWidget::~ScrollableWidget() {} - -void ScrollableWidget::setContentItem(QGraphicsItem *item) -{ - delete mContentItem; - mContentItem = item; - item->setParentItem(this); - item->setPos(QPointF()); -} - -const QGraphicsItem *ScrollableWidget::contentItem() const -{ - return mContentItem; -} - -void ScrollableWidget::scrollContents(const QPoint &offset) -{ - if (mContentItem) { - mContentItem->setPos(mContentItem->pos() + offset); - } -} - -void ScrollableWidget::clearScroll() -{ - if (mContentItem) { - mContentItem->setPos(QPointF()); - } -} - -class Scroller : public MAbstractScroller -{ -public: - Scroller() {} - virtual ~Scroller(){} - - virtual QPoint queryScrollingAmount(const QGraphicsWidget *widget, - const QRect &targetRect, - const QPoint &originPoint, - const QPoint ¤tOffset) - { - const ScrollableWidget *scrollable = dynamic_cast<const ScrollableWidget *>(widget); - if (!scrollable) { - return QPoint(); - } - - const QRect boundaries(scrollable->boundingRect().toRect()); - - QRect movedTargetRect(targetRect); - moveRectInsideArea(boundaries, movedTargetRect); - - const QPoint offset(movedTargetRect.topLeft() - originPoint); - const QPoint lowerBound(-maximumOffset - currentOffset); - const QPoint upperBound(maximumOffset - currentOffset); - const QPoint limitedOffset(qBound(lowerBound.x(), offset.x(), upperBound.x()), - qBound(lowerBound.y(), offset.y(), upperBound.y())); - return limitedOffset; - } - - virtual void applyScrolling(QGraphicsWidget *widget, const QPoint &contentsOffset) - { - ScrollableWidget *scrollableWidget = dynamic_cast<ScrollableWidget *>(widget); - if (scrollableWidget) { - scrollableWidget->scrollContents(contentsOffset); - } - } - - virtual void restoreScrolling(QGraphicsWidget *widget) - { - ScrollableWidget *scrollableWidget = dynamic_cast<ScrollableWidget *>(widget); - if (scrollableWidget) { - scrollableWidget->clearScroll(); - } - } - - static QPoint maximumOffset; // absolute offset values -}; - -QPoint Scroller::maximumOffset; - void Ut_MScrollChain::initTestCase() { qRegisterMetaType< QList<int> >("QList<int>"); diff --git a/tests/ut_mscrollchain/ut_mscrollchain.h b/tests/ut_mscrollchain/ut_mscrollchain.h index b7304a39..836f3240 100644 --- a/tests/ut_mscrollchain/ut_mscrollchain.h +++ b/tests/ut_mscrollchain/ut_mscrollchain.h @@ -29,25 +29,7 @@ class MScrollChain; class MSceneWindow; class QGraphicsScene; - -#include <QGraphicsWidget> - -class ScrollableWidget : public QGraphicsWidget -{ - Q_OBJECT -public: - ScrollableWidget(QGraphicsItem *parent = 0); - virtual ~ScrollableWidget(); - - void setContentItem(QGraphicsItem *item); - const QGraphicsItem *contentItem() const; - - void scrollContents(const QPoint &offset); - void clearScroll(); - -private: - QGraphicsItem *mContentItem; -}; +class QGraphicsWidget; class Ut_MScrollChain : public QObject { diff --git a/tests/ut_mscrollchain/ut_mscrollchain.pro b/tests/ut_mscrollchain/ut_mscrollchain.pro index c702ba4e..d90d592d 100644 --- a/tests/ut_mscrollchain/ut_mscrollchain.pro +++ b/tests/ut_mscrollchain/ut_mscrollchain.pro @@ -5,11 +5,15 @@ INCLUDEPATH += $$MSRCDIR/corelib/core # unit test and unit classes SOURCES += ut_mscrollchain.cpp \ $$MSRCDIR/corelib/core/mscrollchain.cpp \ - $$MSRCDIR/corelib/core/mabstractscroller.cpp + $$MSRCDIR/corelib/core/mabstractscroller.cpp \ + scrollablewidget.cpp \ + scroller.cpp # unit test and unit classes HEADERS += ut_mscrollchain.h \ $$MSRCDIR/corelib/core/mscrollchain.h \ - $$MSRCDIR/corelib/core/mabstractscroller.h + $$MSRCDIR/corelib/core/mabstractscroller.h \ + scroller.h \ + scrollablewidget.h include(../common_bot.pri) |