aboutsummaryrefslogtreecommitdiff
path: root/transitioncontrol.cpp
blob: 51f07f0e3125c22b1a8a5dc3a9d5658070d2e54f (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
#include "transitioncontrol.h"

#include <QObject>
#include <QThread>

#include "transitionworkerthread.h"

// The mouse button used for interaction
static const Qt::MouseButton MOUSE_BUTTON = Qt::RightButton;

TransitionControl::TransitionControl(QQuickItem *parent)
    : QQuickItem(parent)
    , m_t(1.0)
    , m_startPos(-1)
    , m_shouldRewind(false)
{
}

void TransitionControl::setT(double t)
{
    if (t < 0.0 || t > 1.0) {
        return;
    }

    m_t = t;
    emit tChanged(m_t);
}

void TransitionControl::mousePressEvent(QMouseEvent *event)
{
    if (event->button() != MOUSE_BUTTON
        || !(event->modifiers() & Qt::ControlModifier)) {
        event->ignore();
        return;
    }

    m_t = 1.0;
    m_startPos = event->pos().x();
}

void TransitionControl::mouseMoveEvent(QMouseEvent *event)
{
    int x = event->pos().x();
    if (!(event->buttons() & MOUSE_BUTTON) || x > m_startPos || x < 0) {
        event->ignore();
        return;
    }

    m_shouldRewind = true;
    m_t = double(x) / m_startPos;
    emit tChanged(m_t);
}

void TransitionControl::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() != MOUSE_BUTTON) {
        event->ignore();
        return;
    }

    // Back to initial state
    m_startPos = -1;

    if (m_shouldRewind) {
        m_shouldRewind = false;

        // We now have to smoothly go back to m_t == 1.0
        m_rewindThread = new TransitionWorkerThread(this);
        connect(m_rewindThread, &QThread::finished, m_rewindThread, &QObject::deleteLater);
        m_rewindThread->start();
    }
}