aboutsummaryrefslogtreecommitdiff
path: root/demos/widgetsgallery/panningbenchmark.cpp
blob: aaf5ea894271e91ecee515f9adfe1aef691abe62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "panningbenchmark.h"
#include "timedemo.h"

#include <MApplicationPage>
#include <MPannableViewport>
#include <MPhysics2DPanning>

#include <QTimer>
#include <qdebug.h>

namespace {
    const int updateInterval = 20; // ms
    const qreal ySpeed = .5; // pixel/ms
}

PanningBenchmark::PanningBenchmark(MApplicationPage *applicationPage, Timedemo *timedemo)
    : TimedemoBenchmark(applicationPage, timedemo)
    , timingStarted(false)
{
    pannableViewport = 0;
    QList<QGraphicsItem *> childItems = applicationPage->childItems();
    foreach(QGraphicsItem * childItem, childItems) {
        if (MPannableViewport * viewport = dynamic_cast<MPannableViewport*>(childItem)) {
            pannableViewport = viewport;
            break;
        }
    }

    if (!pannableViewport) {
        qFatal("Did not find matching viewport of MApplicationWindow");
    }
}

QString PanningBenchmark::name() {
    return "PanningBenchmark";
}

void PanningBenchmark::start()
{
    if (!applicationPage->isActiveWindow()) {
        connect(applicationPage, SIGNAL(appeared()), this, SLOT(waitBeforePanning()));
        applicationPage->appear();
    } else {
        QTimer::singleShot(0, this, SLOT(panDown()));
    }
}

//HACK! if we do not wait pannableViewport->physics()->range() is (0, 0, 0, 0) because
// the widgets are not completely set up yet
void PanningBenchmark::waitBeforePanning()
{
    QTimer::singleShot(500, this, SLOT(panDown()));
}

void PanningBenchmark::panDown()
{
    if (!timingStarted) {
        timedemo->startTiming();
        timingStarted = true;
        formerPosition = pannableViewport->physics()->position();
        timer.start();
    }

    QPointF currentPosition = pannableViewport->physics()->position();
    QRectF range = pannableViewport->physics()->range();

    if (currentPosition.y() < range.height()) {
        pannableViewport->physics()->setPosition(currentPosition + QPointF(0., ySpeed * timer.elapsed()));
        timer.start();
        QTimer::singleShot(updateInterval, this, SLOT(panDown()));
    } else {
        terminateBenchmark();
    }
}

void PanningBenchmark::terminateBenchmark()
{
    timedemo->stopTiming();
    pannableViewport->physics()->setPosition(formerPosition);

    qDebug() << "end" << pannableViewport->widget()->pos() << pannableViewport->physics()->range();

    emit finished();
}