aboutsummaryrefslogtreecommitdiff
path: root/src/views/mspinnerview.cpp
blob: e2ac6c467cc696b29a86edb414f8b7d4101a669e (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/***************************************************************************
**
** 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 <QPainter>
#include <QPropertyAnimation>

#include "mspinnerview.h"
#include "mspinnerview_p.h"

#include "mprogressindicator.h"

#include "mtheme.h"

MSpinnerViewPrivate::MSpinnerViewPrivate()
    :  q_ptr(NULL),
       controller(NULL),
       pieBrush(Qt::NoBrush),
       piePen(Qt::NoPen),
       positionAnimation(NULL),
       angle(0)
{
}

MSpinnerViewPrivate::~MSpinnerViewPrivate()
{
    delete positionAnimation;
}

void MSpinnerViewPrivate::resumeAnimation()
{
    if (controller->unknownDuration()) {
        if (positionAnimation->state() == QPropertyAnimation::Paused)
            positionAnimation->resume();
    }
}

void MSpinnerViewPrivate::pauseAnimation()
{
    if (positionAnimation->state() == QPropertyAnimation::Running)
        positionAnimation->pause();
}

MSpinnerView::MSpinnerView(MProgressIndicator *controller) :
    MWidgetView(controller),
    d_ptr(new MSpinnerViewPrivate)
{
    Q_D(MSpinnerView);
    d->q_ptr = this;
    d->controller = controller;

    connect(controller, SIGNAL(visibleChanged()), this, SLOT(visibilityChangedSlot()));

    connect(controller, SIGNAL(displayEntered()), this, SLOT(displayEnteredSlot()));
    connect(controller, SIGNAL(displayExited()), this, SLOT(displayExitedSlot()));
}


MSpinnerView::~MSpinnerView()
{
    delete d_ptr;
}

void MSpinnerView::setupAnimation()
{
    Q_D(MSpinnerView);

    if (!d->positionAnimation) {
        d->positionAnimation = new QPropertyAnimation(this, "angle", 0);
        d->positionAnimation->setDuration(style()->period());
        d->positionAnimation->setStartValue(0);
        d->positionAnimation->setEndValue(360);
        d->positionAnimation->setLoopCount(-1);
    }

    if (model()->unknownDuration()) {
        if (d->positionAnimation->state() == QPropertyAnimation::Paused)
            d->positionAnimation->resume();
        else
            d->positionAnimation->start();
    } else {
        if (d->positionAnimation->state() == QPropertyAnimation::Running)
            d->positionAnimation->pause();
    }
}

int MSpinnerView::angle() const
{
    Q_D(const MSpinnerView);

    return d->angle;
}

void MSpinnerView::setAngle(int angle)
{
    Q_D(MSpinnerView);

    d->angle = angle;
    update();
}

void MSpinnerView::updateData(const QList<const char *>& modifications)
{
    MWidgetView::updateData(modifications);

    foreach(const char * member, modifications) {
        if (member == MProgressIndicatorModel::UnknownDuration)
            setupAnimation();
    }

    update();
}

void MSpinnerView::applyStyle()
{
     MWidgetView::applyStyle();

     Q_D(MSpinnerView);

     if (d->positionAnimation)
         d->positionAnimation->setDuration(style()->period());

     update();
}

void MSpinnerView::setupModel()
{
    MWidgetView::setupModel();
                    
    setupAnimation();

    update();
}

void MSpinnerView::drawContents(QPainter *painter, const QStyleOptionGraphicsItem *option) const
{
    Q_UNUSED(option);
    Q_D(const MSpinnerView);

    bool reverse = (d->controller->layoutDirection() == Qt::RightToLeft);

    //drawing background
    if (style()->bgPixmap() && !style()->bgPixmap()->isNull())
        painter->drawPixmap(0, 0, *style()->bgPixmap());

    // calculated values input into rendering
    qreal startAngle = 0.0;
    qreal endAngle = 0.0;

    // calculate values depending on mode
    // (from spinner ring is visible that part where is no pie)
    if (model()->unknownDuration()) {
        if (!reverse) {
            startAngle = angle();
            endAngle = startAngle + 90;
        } else {
            startAngle = -angle();
            endAngle = startAngle - 90;
        }
    } else {
        if (!reverse) {
            startAngle = 0.0;
            endAngle = 360 * (model()->value() - model()->minimum()) / (model()->maximum() - model()->minimum());
        } else {
            startAngle = 0.0;
            endAngle =  -360 * (model()->value() - model()->minimum()) / (model()->maximum() - model()->minimum());
        }
    }

    //drawing foreground (pie)
    if (style()->progressPixmap() && !style()->progressPixmap()->isNull()) {
        d->pieBrush.setTexture(*style()->progressPixmap());

        painter->setBrush(d->pieBrush);
        painter->setPen(d->piePen);
        painter->drawPie(QRect(0, 0, style()->progressPixmap()->width(), style()->progressPixmap()->height()),
                          (90 - endAngle) * 16,
                          (endAngle - startAngle) * 16);
    }
}

void MSpinnerView::visibilityChangedSlot()
{
    Q_D(MSpinnerView);
    if (d->controller->isVisible() && d->controller->isOnDisplay())
        d->resumeAnimation();
    else
        d->pauseAnimation();
}

void MSpinnerView::displayEnteredSlot()
{
    Q_D(MSpinnerView);

    if (d->controller->isVisible())
        d->resumeAnimation();
}

void MSpinnerView::displayExitedSlot()
{
    Q_D(MSpinnerView);
    d->pauseAnimation();
}

#include "moc_mspinnerview.cpp"

// bind controller widget and view widget together by registration macro
M_REGISTER_VIEW_NEW(MSpinnerView, MProgressIndicator)