summaryrefslogtreecommitdiffstats
path: root/weather/citylist.h
blob: 3d9380649ef653b83138d3d3d15a1517e0eff87e (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
#ifndef CITYLIST_H
#define CITYLIST_H

#include <QBasicTimer>
#include <QTime>
#include <QGraphicsItem>

#include "gesturebox.h"
#include "contentlist.h"
#include "forecastdata.h"
#include "pixmapbutton.h"
#include "scrollbar.h"
#include "painttextitem.h"

// CityListScrollBox

class CityListScrollBox : public QObject, public GestureBox
{
    Q_OBJECT
public:
    CityListScrollBox(QGraphicsItem *content, QGraphicsItem *parent = 0);

signals:
    void updated(qreal, qreal, qreal);

protected:
    void gestureMousePress(QPointF pos, bool &startGesture, bool &acceptClick);
    void gestureStart(QPointF pos);
    void gestureMove(QPointF pos, QPointF movement, QPointF speed);
    void gestureEnd(QPointF pos, QPointF speed);

    void timerEvent(QTimerEvent *event);

private:
    QGraphicsItem * const m_content;
    QBasicTimer m_ticker;
    qreal m_speed;
    qreal m_acceleration;
    QTime m_time;
    QPointF m_startPoint;
    ScrollBar *m_scroll;
    bool move(QPointF movement);
    void contentPositionUpdated();
};

// CityListText

class CityListText : public QGraphicsItem
{
public:
    CityListText(const QString &text, QGraphicsItem *parent = 0);
    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);

private:
    TextPainter m_painter;

};

// CityListItem

class CityContentList;

class CityListItem : public ContentListItem
{
    Q_OBJECT
    Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity);
public:
    CityListItem(const ForecastData &data, CityContentList *list, bool deleteVisible);
    void select(bool selected);

    const ForecastData &data() const { return m_data; }

    qreal contentHeight() const;
    QAbstractAnimation *getShowAnimation();
    QAbstractAnimation *getHideAnimation();

    QAbstractAnimation *getButtonAnimation(bool hide);

private slots:
    void removeFromList();

private:
    const qreal m_height;
    const ForecastData m_data;
    CityContentList * const m_list;

    QGraphicsPixmapItem * const m_background;
    QGraphicsPixmapItem * const m_check;
    PixmapButton * const m_delete;
    CityListText * const m_text;

    template<class T> static QAbstractAnimation *getFadeAnimation(T *target, bool hide, int msecs);

private slots:
    void doHide() { hide(); }

};

// CityContentList

class CityContentList : public ContentList
{
    Q_OBJECT
    Q_PROPERTY(qreal top READ getTop WRITE setTop);
public:
    CityContentList(QList<ForecastData> &contentList, QObject *holder);
    void addForecast(const ForecastData &data);
    void select(const QString &selected);

protected:
    QAbstractAnimation *getInsertAnimation(int idx, qreal height);
    QAbstractAnimation *getRemoveAnimation(int idx);

private:
    QObject * const m_holder;
    QList<ForecastData> &m_contentList;

    QAbstractAnimation *createCompoundAnimation(QList<QAbstractAnimation*> list);
    QAbstractAnimation *getItemMoveAnimation(int idx, qreal offset);
    QAbstractAnimation *getMoveAnimation(QObject *object, qreal offset);

    qreal getTop() { return pos().y(); }
    void setTop(qreal top) { setPos(pos().x(), top); }

};

// CityList

class CityList : public QObject, public QGraphicsItem
{
    Q_OBJECT
    Q_PROPERTY(qreal top READ getTop WRITE setTop);
    Q_INTERFACES(QGraphicsItem);
public:
    CityList(QList<ForecastData> &contentList, QGraphicsItem *parent = 0);
    void select(const QString &selected) { m_list->select(selected); }

    QRectF boundingRect () const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);

    static int loadImages();

    qreal initialTop() const;

public slots:
    void addForecast(const ForecastData &data) { m_list->addForecast(data); }

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);

private:
    const qreal m_itemHeight;
    const qreal m_topHeight;
    const QRectF m_boundingRect;
    const QRectF m_paintRect;

    inline QRectF getBoundingRect();
    inline QRectF getPaintRect();

    QGraphicsPixmapItem * const m_top;
    CityContentList * const m_list;
    CityListScrollBox * const m_scrollBox;

    qreal getTop() { return pos().y(); }
    void setTop(qreal top) { setPos(pos().x(), top); }
};

#endif // CITYLIST_H