92da8a85546d8dfffa32e84e04c58c801b04890e
[profile/ivi/qtdeclarative.git] / examples / declarative / canvas / stockchart / model.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the examples of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 **   * Redistributions of source code must retain the above copyright
16 **     notice, this list of conditions and the following disclaimer.
17 **   * Redistributions in binary form must reproduce the above copyright
18 **     notice, this list of conditions and the following disclaimer in
19 **     the documentation and/or other materials provided with the
20 **     distribution.
21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22 **     the names of its contributors may be used to endorse or promote
23 **     products derived from this software without specific prior written
24 **     permission.
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 #include <QtCore/QAbstractListModel>
41 #include <QtCore/QDate>
42
43 class StockPrice : public QObject
44 {
45     Q_OBJECT
46     Q_PROPERTY(QDate date READ date)
47     Q_PROPERTY(qreal openPrice READ openPrice)
48     Q_PROPERTY(qreal closePrice READ closePrice)
49     Q_PROPERTY(qreal highPrice READ highPrice)
50     Q_PROPERTY(qreal lowPrice READ lowPrice)
51     Q_PROPERTY(qint32 volume READ volume)
52     Q_PROPERTY(qreal adjustedPrice READ adjustedPrice)
53 public:
54
55     StockPrice(QObject *parent = 0)
56         : QObject(parent)
57         , _openPrice(-1)
58         , _closePrice(-1)
59         , _highPrice(-1)
60         , _lowPrice(-1)
61         , _volume(-1)
62         , _adjustedPrice(-1)
63     {
64     }
65     QDate date() const {return _date;}
66     qreal openPrice() const {return _openPrice; }
67     qreal closePrice() const {return _closePrice;}
68     qreal highPrice() const {return _highPrice;}
69     qreal lowPrice() const{return _lowPrice;}
70     qreal adjustedPrice() const{return _adjustedPrice;}
71     qint32 volume() const{return _volume;}
72
73     void setDate(const QDate& date){_date = date;}
74     void setOpenPrice(qreal price){_openPrice = price;}
75     void setClosePrice(qreal price){_closePrice = price;}
76     void setHighPrice(qreal price){_highPrice = price;}
77     void setLowPrice(qreal price){_lowPrice = price;}
78     void setAdjustedPrice(qreal price) {_adjustedPrice = price;}
79     void setVolume(qint32 volume) {_volume = volume;}
80
81 private:
82     QDate _date;
83     qreal _openPrice;
84     qreal _closePrice;
85     qreal _highPrice;
86     qreal _lowPrice;
87     qint32 _volume;
88     qreal _adjustedPrice;
89 };
90
91 class QNetworkReply;
92 class QNetworkAccessManager;
93 class StockModel : public QAbstractListModel
94 {
95     Q_OBJECT
96
97     Q_PROPERTY(QString stockName READ stockName WRITE setStockName NOTIFY stockNameChanged)
98     Q_PROPERTY(QDate startDate READ startDate WRITE setStartDate NOTIFY startDateChanged)
99     Q_PROPERTY(QDate endDate READ endDate WRITE setEndDate NOTIFY endDateChanged)
100     Q_PROPERTY(StockDataCycle dataCycle READ dataCycle WRITE setDataCycle NOTIFY dataCycleChanged)
101
102     Q_ENUMS(StockDataCycle)
103 public:
104     enum StockDataCycle {
105         Daily,
106         Weekly,
107         Monthly,
108         Dividend
109     };
110
111     enum StockModelRoles {
112         DateRole = Qt::UserRole + 1,
113         SectionRole,
114         OpenPriceRole,
115         ClosePriceRole,
116         HighPriceRole,
117         LowPriceRole,
118         VolumeRole,
119         AdjustedPriceRole
120     };
121
122     StockModel(QObject *parent = 0);
123
124     QString stockName() const;
125     void setStockName(const QString& name);
126
127     QDate startDate() const;
128     void setStartDate(const QDate& date);
129
130     QDate endDate() const;
131     void setEndDate(const QDate& date);
132
133     StockDataCycle dataCycle() const;
134     void setDataCycle(StockDataCycle cycle);
135
136     int rowCount(const QModelIndex & parent = QModelIndex()) const;
137
138     QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
139
140 signals:
141     void stockNameChanged();
142     void startDateChanged();
143     void endDateChanged();
144     void dataCycleChanged();
145     void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
146
147 public slots:
148     void requestData();
149     StockPrice* stockPriceAtIndex(int idx) const;
150 private slots:
151     void doRequest();
152     void update(QNetworkReply* reply);
153 private:
154     QString dataCycleString() const;
155     QList<StockPrice*> _prices;
156     QString _stockName;
157     QDate _startDate;
158     QDate _endDate;
159     StockDataCycle _dataCycle;
160     QNetworkAccessManager* _manager;
161     bool _updating;
162 };
163
164
165
166