summaryrefslogtreecommitdiffstats
path: root/hyperui/src/draggablepreview.cpp
blob: 20befe533a460510756fc36a6161be25d2881938 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: qt-info@nokia.com
**
** This software, including documentation, is protected by copyright
** controlled by Nokia Corporation.  You may use this software in
** accordance with the terms and conditions contained in the Qt Phone
** Demo License Agreement.
**
****************************************************************************/

#include <QState>
#include <QStateMachine>
#include <QSignalTransition>
#include <QPropertyAnimation>
#include <QParallelAnimationGroup>
#include <QGraphicsSceneMouseEvent>

#include "dataresource.h"
#include "draggablepreview.h"


#define SCALED_POS(sw, sh, scale)                                   \
    QPointF(qRound(sw * 0.5 - (sw * 0.5 + m_leftMargin) * scale),   \
            qRound(sh - (sh + m_topMargin) * 0.5 * scale))


DraggablePreview::DraggablePreview(QGraphicsWidget *item, const QSize &screenSize,
                                   QGraphicsItem *parent)
    : QGraphicsWidget(parent),
      m_item(item),
      m_screenSize(screenSize),
      m_border(Resource::intValue("draggable-preview/border")),
      m_topMargin(Resource::intValue("draggable-preview/margin-top")),
      m_leftMargin(Resource::intValue("draggable-preview/margin-left")),
      m_maximizeRange(Resource::doubleValue("draggable-preview/maximize-range")),
      m_minimumOffset(Resource::intValue("draggable-preview/minimum-offset"))
{
    setFlag(QGraphicsItem::ItemHasNoContents);

    setupInterface();
}

void DraggablePreview::setupInterface()
{
    // add background item
    QPixmap backgroundPixmap = Resource::pixmap("screen_unlock.png");
    m_background = new QGraphicsPixmapItem(backgroundPixmap, this);
    m_background->setPos(0, 0);
    m_background->setFlag(QGraphicsItem::ItemStacksBehindParent);
    m_background->setShapeMode(QGraphicsPixmapItem::BoundingRectShape);

    // add embedded widget
    m_item->setParentItem(this);
    m_item->setFlag(QGraphicsItem::ItemStacksBehindParent);
    m_item->setPos(m_leftMargin + m_border, m_topMargin + m_border);
    m_item->resize(m_screenSize);

    // resize to the background size
    resize(backgroundPixmap.size());

    const int sw = m_screenSize.width();
    const int sh = m_screenSize.height();

    qreal minimumScale = Resource::doubleValue("draggable-preview/minimum-scale");
    qreal draggableScale = Resource::doubleValue("draggable-preview/first-zoom-scale");

    m_draggablePos = SCALED_POS(sw, sh, draggableScale);
    QPointF minimumPos(SCALED_POS(sw, sh, minimumScale));
    QPointF maximumPos(-m_leftMargin - m_border, -m_topMargin - m_border);

    m_maximumOffset = minimumPos.y();

    QStateMachine *machine = new QStateMachine(this);

    m_minimizedState = new QState();
    m_minimizedState->assignProperty(this, "pos", minimumPos);
    m_minimizedState->assignProperty(this, "scale", minimumScale);

    m_draggableState = new QState();
    m_draggableState->assignProperty(this, "pos", m_draggablePos);
    m_draggableState->assignProperty(this, "scale", draggableScale);

    m_maximizedState = new QState();
    m_maximizedState->assignProperty(this, "pos", maximumPos);
    m_maximizedState->assignProperty(this, "scale", 1.0);

    int restoreTime = Resource::intValue("draggable-preview/restore-time");
    int maximizeTime = Resource::intValue("draggable-preview/maximize-time");
    int firstZoomTime = Resource::intValue("draggable-preview/first-zoom-time");

    QSignalTransition *transition;

    // create minimized > draggable state transition
    transition = m_minimizedState->addTransition(this, SIGNAL(draggableStarted()),
                                                 m_draggableState);
    transition->addAnimation(createAnimation(firstZoomTime));

    // create draggable > minimized state transition
    transition = m_draggableState->addTransition(this, SIGNAL(minimizeStarted()),
                                                m_minimizedState);
    transition->addAnimation(createAnimation(restoreTime));

    // create draggable > maximized state transition
    transition = m_draggableState->addTransition(this, SIGNAL(maximizeStarted()),
                                                m_maximizedState);
    transition->addAnimation(createAnimation(maximizeTime,
                                             SLOT(onMaximizeFinished())));

    // this is used just to update the final value when still animating
    transition = m_draggableState->addTransition(this, SIGNAL(draggableUpdate()),
                                                m_draggableState);
    transition->addAnimation(createAnimation(0));

    // add states
    machine->addState(m_minimizedState);
    machine->addState(m_draggableState);
    machine->addState(m_maximizedState);

    setPos(minimumPos);
    setScale(minimumScale);

    machine->setInitialState(m_minimizedState);
    machine->start();
}

QAbstractAnimation *DraggablePreview::createAnimation(int time, const char *slot)
{
    QParallelAnimationGroup *result = new QParallelAnimationGroup();

    QPropertyAnimation *posAnimation = new QPropertyAnimation(this, "pos");
    posAnimation->setEasingCurve(QEasingCurve::InSine);
    posAnimation->setDuration(time);

    QPropertyAnimation *scaleAnimation = new QPropertyAnimation(this, "scale");
    scaleAnimation->setEasingCurve(QEasingCurve::InSine);
    scaleAnimation->setDuration(time);

    result->addAnimation(posAnimation);
    result->addAnimation(scaleAnimation);

    if (slot)
        connect(result, SIGNAL(finished()), slot);

    return result;
}

void DraggablePreview::onMaximizeFinished()
{
    // hide background
    m_background->hide();

    // move menu to front to not block events
    m_item->setFlag(QGraphicsItem::ItemStacksBehindParent, false);

    emit maximizeFinished();

    // detach from parent to avoid inherit transformations
    m_item->setParentItem(0);
    m_item->setPos(0, 0);
}

void DraggablePreview::mousePressEvent(QGraphicsSceneMouseEvent *e)
{
    m_lastPos = e->scenePos();
    m_draggableState->assignProperty(this, "pos", m_draggablePos);
    emit draggableStarted();
}

void DraggablePreview::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
{
    const int offset = qRound(pos().y() +  e->scenePos().y() - m_lastPos.y());
    m_lastPos = e->scenePos();

    const int fy = qBound(m_minimumOffset, offset, m_maximumOffset);

    if (fy < m_draggablePos.y()) {
        m_draggableState->assignProperty(this, "pos",
                                        QPointF(m_draggablePos.x(), fy));
        emit draggableUpdate();
    }
}

void DraggablePreview::mouseReleaseEvent(QGraphicsSceneMouseEvent *)
{
    if (pos().y() < m_minimumOffset + m_maximizeRange)
        emit maximizeStarted();
    else
        emit minimizeStarted();
}