summaryrefslogtreecommitdiffstats
path: root/weather/src/forecastsnow.cpp
blob: d342c1e53cc1487c18b8dc8f8f8b1dc04b352e87 (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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: openBossa - INdT (renato.chencarek@openbossa.org)
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used 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.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** the openBossa stream from INdT (renato.chencarek@openbossa.org).
** $QT_END_LICENSE$
**
****************************************************************************/

#include "forecastsnow.h"
#include "settings.h"
#include "pixmaploader.h"

#include <QPainter>

typedef struct
{
    const char * const prefix;
    QString name() const { return prefix; }
    QPixmap pic() const { return PixmapLoader::getPic(name()); }

private:

} SnowFlaketData;

static const int SnowFlakeCount = 10;
static SnowFlaketData SnowFlakeArray[SnowFlakeCount] = {
    {"snow_flake"},
    {"snow_flake_01"},
    {"snow_flake_02"},
    {"snow_flake_03"},
    {"snow_flake_04"},
    {"snow_flake_05"},
    {"snow_flake_06"},
    {"snow_flake_07"},
    {"snow_flake_08"},
    {"snow_flake_09"},
};

static const qreal maxVerticalSpeed = 70.0;
static const qreal minVerticalSpeed = 30.0;
static const qreal mediumVerticalSpeed = (maxVerticalSpeed + minVerticalSpeed) / 2;

static const qreal maxHorizontalSpeed = 100.0;
static const qreal minHorizontalSpeed = 20.0;

static const qreal windSpeed = -50.0;

static const qreal speedDeviation = 0.15;

static const int deviation1 = 2;
static const int deviation2 = 31;

#define RANDOM_FACTOR   (qreal(qrand()) / qreal(RAND_MAX))

// ForecastSnow

ForecastSnow::SnowFlake::SnowFlake(int type, const QSizeF &bounds)
    : m_pixmap(SnowFlakeArray[type].pic())
    , m_bounds(bounds)
    , m_waitingTime(0)
{
    qreal x = minHorizontalSpeed + windSpeed +
              RANDOM_FACTOR * (maxHorizontalSpeed - minHorizontalSpeed);
    qreal y = minVerticalSpeed + RANDOM_FACTOR * (maxVerticalSpeed - minVerticalSpeed);
    m_speed = QPointF(x, y);
}

int ForecastSnow::loadImages()
{
    for (int i = 0; i < SnowFlakeCount;++i)
        PixmapLoader::load(SnowFlakeArray[i].name());
    return SnowFlakeCount;
}

static inline bool doDeviation(qreal &factor)
{
    if (qrand() % deviation2 > deviation1)
        return false;
    factor = 1.0 + ((qrand() % 2) ? speedDeviation : -speedDeviation);
    return true;
}

void ForecastSnow::SnowFlake::timerEvent(int interval_ms)
{
    if (m_waitingTime < 0) {
        m_waitingTime += interval_ms;
        return;
    }
    const qreal interval_s = qreal(interval_ms) / 1000.0;
    qreal x = m_pos.x() + m_speed.x() * interval_s;
    qreal y = m_pos.y() + m_speed.y() * interval_s;

    y = y >= m_bounds.height() ? -m_pixmap.height() : y;
    x = x <= -m_pixmap.width() ? m_bounds.width()
                               : x >= m_bounds.width() ? -m_pixmap.width() : x;

    m_pos = QPointF(x, y);

    qreal factor;
    if (doDeviation(factor))
        m_speed.setY(qMax(qMin(m_speed.y() * factor, maxVerticalSpeed), minVerticalSpeed));
    if (doDeviation(factor)) {
        qreal x = m_speed.x() - windSpeed;
        x = qMax(qMin(x * factor, maxHorizontalSpeed), minHorizontalSpeed) + windSpeed;
        m_speed.setX(x);
    }
}

void ForecastSnow::SnowFlake::paint(QPainter *painter)
{
    if (isVisible() && m_waitingTime >= 0)
        painter->drawPixmap(m_pos, m_pixmap);
}

ForecastSnow::ForecastSnow(int count, const QRectF &bounds, QGraphicsItem *parent)
    : m_bounds(bounds)
    , m_parent(parent)
    , m_visible(false)
{
    qsrand(QTime(0, 0).secsTo(QTime::currentTime()) * qrand());
    for (int i = 0; i < count; ++i)
        m_flakes.append(new SnowFlake(qrand() % 10, m_bounds.size()));
    updateFLakesPositions();
}

void ForecastSnow::updateFLakesPositions()
{
    qsrand(QTime(0, 0).secsTo(QTime::currentTime()) * qrand());

    int maxTime = -1000 * m_bounds.height() / mediumVerticalSpeed;

    foreach(SnowFlake *flake, m_flakes) {

        const qreal height = -flake->height();
        const qreal width = m_bounds.width() - flake->width();

        flake->setPos(width *RANDOM_FACTOR, height *RANDOM_FACTOR);
        flake->setWaitingTime(maxTime * RANDOM_FACTOR);
    }
}

void ForecastSnow::paint(QPainter *painter)
{
    if (!m_visible)
        return;

    painter->translate(m_bounds.topLeft());

    foreach(SnowFlake *flake, m_flakes)
        flake->paint(painter);

    painter->translate(QPointF(-m_bounds.left(), -m_bounds.top()));
}

void ForecastSnow::start()
{
    m_lastTick = QTime::currentTime();
    m_ticker.start(30, this);
    show();
}

void ForecastSnow::stop()
{
    m_ticker.stop();
}

void ForecastSnow::timerEvent(QTimerEvent *event)
{
    Q_UNUSED(event);
    QTime now = QTime::currentTime();
    int interval = m_lastTick.msecsTo(now);
    m_lastTick = now;
    foreach(SnowFlake *flake, m_flakes)
        flake->timerEvent(interval);
    if (m_parent)
        m_parent->update(m_bounds);
}