summaryrefslogtreecommitdiffstats
path: root/weather/src/addcitytool.h
blob: f0f6b1fd76c787159c10d966f82641fe7856712f (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
#ifndef ADDCITYTOOL_H
#define ADDCITYTOOL_H

#include <QGraphicsItem>
#include <QGraphicsPixmapItem>
#include <QBasicTimer>
#include <QGraphicsSimpleTextItem>
#include <QPen>
#include <QBrush>
#include <QFont>
 #include <QGraphicsProxyWidget>
#include <QLineEdit>
#include <QList>

#include "pixmapbutton.h"
#include "forecastprovider.h"
#include "loading.h"
#include "forecastdata.h"

class AddCityScreen : public QGraphicsItem
{
public:
    AddCityScreen(const QRectF &boundingRect, QGraphicsItem *parent = 0);

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

private:
    const QRectF m_boundingRect;
};

class AddCityLineEdit : public QLineEdit
{
    Q_OBJECT
public:
    AddCityLineEdit(QWidget *parent = 0);
    void editReset();
protected:
    void focusInEvent(QFocusEvent *event);
    void focusOutEvent(QFocusEvent *event);
private:
    bool m_clean;
private slots:
    void textEditedSlot(const QString & text);
};


class AddCityFirstScreen : public QObject, public AddCityScreen
{
    Q_OBJECT
public:
    AddCityFirstScreen(const QRectF &boundingRect, QGraphicsItem *parent = 0);
    void clean();

signals:
    void citySelected(const QString &city);

private:
    QGraphicsPixmapItem * const m_textBackground;
    PixmapButton * const m_button;
    QString m_text;
    QGraphicsProxyWidget *m_proxy;
    AddCityLineEdit *m_lineEdit;

private slots:
    void buttonClick();
};

class AddCitySearchScreen : public QObject , public AddCityScreen
{
    Q_OBJECT
public:
    AddCitySearchScreen(const QRectF &boundingRect, QGraphicsItem *parent = 0);
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
    void setCityName(const QString &name);
    void cancel() { reset(); }

signals:
    void forecastReceived(const ForecastData &forecast);
    void forecastRequestError(const QString &name);

private:
    QString m_city;
    int m_reqId;
    Loading *m_loading;

    void reset();

private slots:
    void forecastResponse(int reqId, const ForecastData &forecast);
};

class AddCityErrorScreen : public QObject, public QGraphicsPixmapItem
{
    Q_OBJECT
public:
    enum ErrorType
    {
        NotFound,
        AlreadyInList
    };
    AddCityErrorScreen(QGraphicsItem *parent = 0);
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
    void setCityName(const QString &name, ErrorType type);

signals:
    void closed();

private:
    PixmapButton * const m_button;
    QString m_city;
    ErrorType m_type;

    void paintNotFound(QPainter *painter);
    void paintAlreadyInList(QPainter *painter);
};
class AddCityTool : public QObject, public QGraphicsPixmapItem
{
    Q_OBJECT
public:
    AddCityTool(const QList<ForecastData> &content, QGraphicsItem *parent = 0);
    ~AddCityTool();
    void cancel();

    static int loadImages();

private:
    const QList<ForecastData> &m_content;
    AddCityFirstScreen * const m_firstScreen;
    AddCitySearchScreen * const m_SearchScreen;
    AddCityErrorScreen * const m_ErrorScreen;

    AddCityFirstScreen *createFirstScreen();
    AddCitySearchScreen *createSearchScreen();
    AddCityErrorScreen *createErrorScreen();

    void setCurrentScreen(QGraphicsItem *screen);

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);

signals:
    void newForecast(const ForecastData &forecast);

private slots:
    void errorScreenClosed();
    void forecastReceived(const ForecastData &forecast);
    void forecastRequestError(const QString &name);
    void citySelected(const QString &city);

};

#endif // ADDCITYTOOL_H