Version 0.2
[marketstoday] / src / qml / MarketsTodayWidget.qml
1 /*
2 @version: 0.1
3 @author: Sudheer K. <scifi1947 at gmail.com>
4 @license: GNU General Public License
5 */
6
7 import Qt 4.7
8
9 import "Library" as Library
10 import "Library/js/ISODate.js" as DateLib
11 import "Library/js/DBUtility.js" as DBUtility
12 import "Library/js/Common.js" as Common
13 import "Library/js/CoreLogic.js" as CoreLib
14
15 Item {
16     id: screen
17
18     signal showConfigInNewWindow
19     signal quoteRefreshStarted
20     signal quoteRefreshCompleted
21     signal checkNetworkStatus
22
23     property int itemHeight: 50
24     property int componentWidth: screen.width
25     property int autoUpdateInterval: 300000
26     property bool updateWeekDaysOnly: false
27     property bool updateOnSavedNetworksOnly: false
28     property string lastUpdatedTimeStamp
29     property bool isDesktopWidget
30
31     function reloadQuotes(){
32         CoreLib.reloadQuotes();
33     }
34
35     function initialize(){
36         CoreLib.initialize();
37     }
38
39     Component.onCompleted: {
40         initialize();
41     }
42
43     Timer {
44         id: autoUpdateTimer
45         interval: autoUpdateInterval
46         //running: (autoUpdateInterval == 0? false:true)
47         repeat: true
48         onTriggered: {
49             if (!updateWeekDaysOnly){
50                 logUtility.logMessage("Allowed to update all days of the week");
51                 //reloadQuotes();
52                 checkNetworkStatus();
53             }
54             else if (Common.isTodayAWeekDay()){
55                 logUtility.logMessage("Today is a weekday");
56                 //reloadQuotes();
57                 checkNetworkStatus();
58             }
59             else{
60                 logUtility.logMessage("Update not triggered: Today is not a weekday");
61             }
62         }
63     }
64
65     ListModel{
66         id: stockQuoteDataModel
67     }
68
69     Rectangle {
70         id: background
71         anchors.fill: parent;
72         color: "#343434"
73         clip: true
74
75         Component {
76             id: stockQuotesDelegate
77
78             Item {
79                 id: wrapper; width: componentWidth; height: itemHeight
80                 Item {
81                     Rectangle { color: "black"; opacity: index % 2 ? 0.2 : 0.4; height: wrapper.height - 2; width: wrapper.width; y: 1 }
82                     Row {
83                         x: 30;y: 15;
84                         width: componentWidth - 40;
85                         spacing: 5
86
87                         Text { text: if (width >= 250) {stockName;} else {symbol;} width: parent.width * 35/100; font.pixelSize: 18; font.bold: true; elide: Text.ElideRight; color: "white"; style: Text.Raised; styleColor: "black" }
88                         Text { text: lastTradedPrice; width: parent.width * 25/100; font.pixelSize: 18; elide: Text.ElideLeft; color: "#cccccc"; style: Text.Raised; styleColor: "black" }
89                         Text { text: change; width: parent.width * 20/100; font.pixelSize: 18; elide: Text.ElideRight
90                             color: if(change >= 0){"green";} else {"red";}
91                                 style: Text.Raised; styleColor: "black" }
92                         Text { text: changePercentage; width: parent.width * 20/100; font.pixelSize: 18; elide: Text.ElideRight;
93                             color: if(change >= 0){"green";} else {"red";}
94                                 style: Text.Raised; styleColor: "black" }
95                     }
96                 }
97             }
98         }
99
100         Library.TitleBar {
101             id: titleBar;
102             width: parent.width; height: 60;
103             anchors.top: parent.top
104             title: "Markets Today";
105             buttonType: "";
106         }
107
108         Rectangle{
109             id: pathViewWrapper
110             width: parent.width
111             anchors.top: titleBar.bottom
112             anchors.bottom: footerText.top
113             color: "#343434"
114
115             PathView {
116                 id: stockQuotesView
117                 anchors.fill: parent
118                 flickDeceleration: 500
119                 //preferredHighlightBegin: 1/stockQuotesView.count
120                 //preferredHighlightEnd: 1/stockQuotesView.count
121                 //pathItemCount: count
122                 focus: true
123                 interactive: true
124                 model: stockQuoteDataModel
125                 delegate:  stockQuotesDelegate
126                 path: Path {
127                     startX: width / 2
128                     startY: itemHeight/2
129                     PathLine {
130                         x: width / 2
131                         y: stockQuotesView.count * itemHeight  + itemHeight/2
132                     }
133                 }
134                 Keys.onDownPressed: if (!moving && interactive) incrementCurrentIndex()
135                 Keys.onUpPressed: if (!moving && interactive) decrementCurrentIndex()
136
137                 Connections {
138                     target:  screen
139                     onQuoteRefreshCompleted:{
140                         stockQuotesView.currentIndex = 0;
141                     }
142                 }
143
144                 Connections {
145                     target: toolBar
146                     onDownButtonClicked: {
147                         if (!stockQuotesView.moving && stockQuotesView.interactive)
148                             stockQuotesView.currentIndex = stockQuotesView.currentIndex + 1
149                     }
150                     onUpButtonClicked: {
151                         if (!stockQuotesView.moving && stockQuotesView.interactive)
152                             stockQuotesView.currentIndex = stockQuotesView.currentIndex - 1
153                     }
154                }
155             }
156         }
157
158         Rectangle{
159             id: footerText
160             width: parent.width
161             height: 25
162             color: "#343434"
163             anchors.bottom: toolBar.top
164             Text {
165                 id: timeStamp
166                 anchors.fill: parent
167                 text: screen.lastUpdatedTimeStamp
168                 horizontalAlignment: Text.AlignRight; verticalAlignment: Text.AlignVCenter
169                 width: parent.width; font.pixelSize: 12; elide: Text.ElideRight;
170                 color: "#cccccc"
171                 style: Text.Raised; styleColor: "black"
172
173                 Connections {
174                     target: screen
175                     onQuoteRefreshCompleted:{
176                         timeStamp.text = screen.lastUpdatedTimeStamp;
177                     }
178                 }
179             }
180         }
181
182         Library.ToolBar {
183             id:toolBar
184             width: parent.width; height: 40
185             anchors.bottom: parent.bottom
186             opacity: 0.9
187             displayNavigation: true
188             onReloadButtonClicked: screen.reloadQuotes();
189             onNewsButtonClicked: Qt.openUrlExternally("http://finance.yahoo.com");
190             Connections {
191                 target: screen
192                 onQuoteRefreshStarted:{
193                     if (!toolBar.updatePending) toolBar.updatePending = true;
194                 }
195                 onQuoteRefreshCompleted:{
196                     toolBar.updatePending = false;
197                     console.log(screen.lastUpdatedTimeStamp);
198                 }
199             }
200         }
201     }
202 }