aboutsummaryrefslogtreecommitdiff
path: root/src/views/mspinnerview.cpp
blob: 52c636f2b75df232882467460c1baafad3f74048 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/***************************************************************************
**
** 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 <QTimeLine>
#include <QTimer>
#include <QGraphicsSceneResizeEvent>
#include <math.h>

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

#include "mprogressindicator.h"
#include "mprogressindicator_p.h"
#include "mviewcreator.h"

#include "mtheme.h"
#include "mscalableimage.h"
#include "mdebug.h"

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

const int SpinnerRefreshRate = 30;

MSpinnerViewPrivate::MSpinnerViewPrivate()
    :  q_ptr(0),
       controller(0),
       inactiveElement(0),
       activeElement(0),
       position(0),
       elapsed(0),
       timer(0)
{
}


MSpinnerViewPrivate::~MSpinnerViewPrivate()
{
    MTheme::releasePixmap(activeElement);
    MTheme::releasePixmap(inactiveElement);

    delete timer;
}


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(visibilityChanged()));
}


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

void MSpinnerViewPrivate::animationTimeout()
{
    Q_Q(MSpinnerView);

    if (q->model()->unknownDuration()) {

        // calculate interval in secs and add it to elapsed time
        qreal interval = (qreal) timer->interval() / 1000.0;
        elapsed += interval;

        // calculate how many steps we should take
        int steps = (int)(elapsed * (qreal) q->style()->speed());
        if (steps > 0) {
            // subtract the amount we will step from the elapsed time
            elapsed -= steps * (1.0 / (qreal) q->style()->speed());
            // and perform the stepping
            position = (position + steps) % elements.count();
            // redraw
            q->update();
        }
    }
}

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

    Q_D(MSpinnerView);

    foreach(const char * member, modifications) {
        if (member == MProgressIndicatorModel::UnknownDuration) {
            if (model()->unknownDuration()) {
                if (!d->timer) {
                    d->timer = new QTimer(this);
                    connect(d->timer, SIGNAL(timeout()), this, SLOT(animationTimeout()));
                }
                if (d->controller->isVisible())
                    d->timer->start(SpinnerRefreshRate);
            } else {
                delete d->timer;
                d->timer = NULL;
            }
        }
    }

    update();
}

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

    Q_D(MSpinnerView);

    if (model()->unknownDuration()) {
        if (!d->timer) {
            d->timer = new QTimer(this);
            connect(d->timer, SIGNAL(timeout()), this, SLOT(animationTimeout()));
        }
        if (d->controller->isVisible())
            d->timer->start(SpinnerRefreshRate);
    } else {
        delete d->timer;
        d->timer = NULL;
    }

    update();
}


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

    Q_D(const MSpinnerView);

    const int elementsCount = d->elements.count();
    if (style()->inactiveImage() && style()->activeImage()) {

        // size of an element
        QSize size(style()->elementSize(), style()->elementSize());

        if (model()->unknownDuration()) {
            // every n'th should be active
            int span = (d->elements.count() / style()->activeElementCount());
            // this tells the active index within the span
            int activeIndexInSpan = d->position % span;

            // draw all elements with proper image
            for (int i = 0; i < elementsCount; ++i) {
                if ((i % span) == activeIndexInSpan) {
                    painter->drawPixmap(QRect(d->elements[i], size), *style()->activeImage());
                } else {
                    painter->drawPixmap(QRect(d->elements[i], size), *style()->inactiveImage());
                }
            }
        } else {
            // active element count
            int active = (model()->value() - model()->minimum()) * d->elements.count() /
                         qMax(model()->maximum() - model()->minimum(), 1);

            // draw active elements
            for (int i = 0; i < active; ++i) {
                painter->drawPixmap(QRect(d->elements[i], size), *style()->activeImage());
            }

            // draw inactive elements
            for (int i = active; i < elementsCount; ++i) {
                painter->drawPixmap(QRect(d->elements[i], size), *style()->inactiveImage());
            }
        }
    }
}

void MSpinnerViewPrivate::calculateShape(QSizeF size)
{
    Q_Q(MSpinnerView);

    QSizeF s = size - QSizeF(q->style()->elementSize(), q->style()->elementSize());

    qreal diameter = qMin(s.width(), s.height());

    // clear existing elements
    elements.clear();

    if (q->style()->elementCount() > 0) {
        // center point
        QPoint center(size.width() * 0.5, size.height() * 0.5);

        // radius for the spinner
        qreal radius = diameter * 0.5f;

        // half element size
        qreal halfElementSize = q->style()->elementSize() * 0.5;

        qreal angle = 0;
        qreal span = (M_PI * 2.0) / q->style()->elementCount();

        // calculate spherical shape and store points where the item should be drawn
        const int elementsCount = q->style()->elementCount();
        for (int i = 0; i < elementsCount; ++i) {

            QPoint position;
            position.setX(center.x() + (sinf(angle) * radius) - halfElementSize);
            position.setY(center.y() - (cosf(angle) * radius) - halfElementSize);

            elements.append(position);

            angle += span;
        }
    }
}

void MSpinnerView::resizeEvent(QGraphicsSceneResizeEvent *event)
{
    Q_D(MSpinnerView);
    d->calculateShape(event->newSize());
}

void MSpinnerView::applyStyle()
{
    Q_D(MSpinnerView);
    MWidgetView::applyStyle();
    d->calculateShape(size());
}


void MSpinnerViewPrivate::visibilityChanged()
{
    if (timer) {
        if (controller->isVisible()) {
            timer->start(SpinnerRefreshRate);
        } else {
            timer->stop();
        }
    }
}

#include "moc_mspinnerview.cpp"

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