summaryrefslogtreecommitdiffstats
path: root/weather/src/citymanager.cpp
blob: ddf7d78c0be23cdbe0551b5d7f28412f0a3a5e88 (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
/****************************************************************************
**
** 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 "citymanager.h"
#include "settings.h"

#include <QGraphicsSceneMouseEvent>
#include <QPropertyAnimation>

static inline qreal getBottomVerticalPos(qreal &bottom, QGraphicsItem *item)
{
    const qreal result = bottom - item->boundingRect().bottom();
    bottom -= item->boundingRect().height();
    return result;
}

// CityManagerContent

CityManagerContent::CityManagerContent(QList<ForecastData> &contentList, QGraphicsItem *parent)
    : QGraphicsItem(parent)
    , m_boundingRect(QPointF(0.0, 0.0), Settings::windowSize())
{
    setFlag(ItemHasNoContents);

    qreal bottom = m_boundingRect.bottom();
    m_addTool = new AddCityTool(contentList, this);
    m_addTool->setPos(0.0, getBottomVerticalPos(bottom, m_addTool));
    m_addTool->setZValue(10.0);

    m_list = new CityList(contentList, this);
    m_list->setPos(0.0, getBottomVerticalPos(bottom, m_list) + m_list->initialTop() + 1);
    m_list->setZValue(0.0);

    connect(m_addTool, SIGNAL(newForecast(ForecastData)),
            this, SLOT(forecastSelected(ForecastData)));

}

void CityManagerContent::forecastSelected(ForecastData data)
{
    m_list->addForecast(data);
}

QRectF CityManagerContent::boundingRect () const
{
    return m_boundingRect;
}

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

qreal CityManagerContent::getHiddenTop()
{
    return m_boundingRect.height() - m_list->pos().y() - 5.0;
}

// CityManager

CityManager::CityManager(QList<ForecastData> contentList, QGraphicsItem *parent)
    : QGraphicsItem(parent)
    , m_boundingRect(QPointF(0.0, 0.0), Settings::windowSize())
    , m_contentList(contentList)
    , m_visible(false)
{
    m_content = new CityManagerContent(m_contentList, this);
    m_content->setPos(0.0, m_content->getHiddenTop());
}

QRectF CityManager::boundingRect () const
{
    return m_boundingRect;
}

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

void CityManager::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    event->accept();
    startAnimation(false);
}

void CityManager::showManager(const QString &selected)
{
    m_content->select(selected);
    startAnimation(true);
}

void CityManager::startAnimation(bool show)
{
    if (show == m_visible)
        return;

    m_visible = show;

    if (m_animation)
        m_animation->stop();

    QPropertyAnimation* animation = new QPropertyAnimation(m_content, "top");

    animation->setEasingCurve(QEasingCurve::OutExpo);
    animation->setEndValue(show ? 0.0 : m_content->getHiddenTop());
    animation->setDuration(500);
    m_animation = animation;
    if (!show)
        connect(animation, SIGNAL(finished()), this, SIGNAL(terminated()));
    m_animation->start(QAbstractAnimation::DeleteWhenStopped);

}