summaryrefslogtreecommitdiffstats
path: root/weather/forecasthungitem.cpp
blob: 428b83c4ab9b31ea4bb13f5a5aeb24be451a1383 (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
/****************************************************************************
**
** 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 "forecasthungitem.h"
#include "settings.h"
#include "pixmaploader.h"

#include <QPropertyAnimation>

#define LINE_NAME_SUFFIX    "_line"

typedef struct
{
    const char * const prefix;
    const qreal x;
    const qreal y;

    QString name() const { return prefix; }
    QString lineName() const { return name() + LINE_NAME_SUFFIX; }

    qreal scaledX() const { return Settings::scaleWidth(x); }
    qreal scaledY() const { return Settings::scaleHeight(y); }

    QPixmap pic() const { return PixmapLoader::getPic(name()); }
    QPixmap linePic() const { return PixmapLoader::getPic(lineName()); }

private:

} HungObjectData;

static HungObjectData HungItemsData[ForecastHungItem::TypeCount] = {
    {"cloud_1",         160.0,  3.0},   // Cloud1
    {"cloud_2",         131.0,  6.0},   // Cloud2
    {"cloud_3",         102.0,  11.0},  // Cloud3
    {"cloud_rain_1",    160.0,  3.0},   // CloudRain1
    {"cloud_rain_2",    131.0,  6.0},   // CloudRain2
    {"cloud_rain_3",    102.0,  11.0},  // CloudRain3
    {"cloud_storm_1",   160.0,  3.0},   // CloudStorm1
    {"cloud_storm_2",   131.0,  6.0},   // CloudStorm2
    {"cloud_storm_3",   102.0,  11.0},  // CloudStorm3
    {"cloud_tstorm_1",  160.0,  3.0},   // CloudTStorm1
    {"cloud_tstorm_2",  131.0,  6.0},   // CloudTStorm2
    {"sun",             198.0,  125.0}, // Sun
    {"cold_sun",        121.5,  65.0},  // ColdSun
    {"moon",            97.5,   18.0}   // Moon
};

// ForecastHungItem

ForecastHungItem::ForecastHungItem(ItemType type, QGraphicsItem *parent)
    : QGraphicsItem(parent)
    , m_type(type)
    , m_targetPicTop(0)
{
    setFlag(QGraphicsItem::ItemHasNoContents, true);

    HungObjectData &data = HungItemsData[m_type];

    m_pic = new QGraphicsPixmapItem(data.pic(), this);
    m_line = new QGraphicsPixmapItem(data.linePic(), m_pic);

    m_pic->setPos(0.0, 0.0);

    m_line->setPos(data.scaledX(), data.scaledY() - m_line->boundingRect().bottom());
    reset();
}

int ForecastHungItem::loadImages()
{
    int result = 0;
    for (int i = 0; i < ForecastHungItem::TypeCount; ++i) {
        PixmapLoader::load(HungItemsData[i].name());
        PixmapLoader::load(HungItemsData[i].lineName());
        result += 2;
    }
    return result;
}

qreal ForecastHungItem::lineLenght() const
{
    return m_targetPicTop + HungItemsData[m_type].scaledY();
}

void ForecastHungItem::setLineLenght(qreal lenght)
{
    m_targetPicTop = lenght - HungItemsData[m_type].scaledY();
}

void ForecastHungItem::setLinePos(qreal linePos)
{
    setPos(linePos - HungItemsData[m_type].scaledX(), 0.0);
}

QPointF ForecastHungItem::picPos() const
{
    return QPointF(pos().x(), pos().y() + m_targetPicTop);
}

void ForecastHungItem::setPicPos(QPointF picPos)
{
    setPos(picPos.x(), 0.0);
    m_targetPicTop = picPos.y();
}

void ForecastHungItem::reset()
{
    setPicTop(-m_pic->boundingRect().height());
}

QRectF ForecastHungItem::boundingRect () const
{
    return QRectF(0.0, 0.0, m_pic->boundingRect().width(),
                  m_targetPicTop + m_pic->boundingRect().height());
}

void ForecastHungItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opt, QWidget *widget)
{
    Q_UNUSED(painter);
    Q_UNUSED(opt);
    Q_UNUSED(widget);
}

QAbstractAnimation *ForecastHungItem::getAnimation()
{
    QPropertyAnimation *result = new QPropertyAnimation(this, "picTop");
    result->setStartValue(-m_pic->boundingRect().height());
    result->setEndValue(m_targetPicTop);
    result->setEasingCurve(QEasingCurve::OutBack);
    result->setDuration(500);
    return result;
}