9c78ac8c6630a7ee7f7b31e466eb8952509c2af1
[marketstoday] / src / qml / MarketsTodayLegacyApp.qml
1 /*
2 @version: 0.2
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: mainPage
17
18     signal showConfigInNewWindow
19     signal showStockDetails(string strSymbol)
20     signal quoteRefreshStarted
21     signal quoteRefreshCompleted
22     signal quoteRefreshFailed(string strMessage)
23     signal checkNetworkStatus
24
25     property int itemHeight: 50
26     property int titleBarHeight: 60
27     property int toolBarHeight: 40
28     property int componentWidth: mainPage.width
29     property int autoUpdateInterval: 300000
30     property bool updateWeekDaysOnly: false
31     property bool updateOnSavedNetworksOnly: false
32     property string rssURL: "http://finance.yahoo.com/rss/topfinstories"
33     property string lastUpdatedTimeStamp
34     property bool isDesktopWidget
35     //property string selectedSymbol:"YHOO"
36     property string selectedSymbol:sharedContext.getStockSymbol()
37
38     function reloadData(){
39         CoreLib.reloadQuotes();
40         CoreLib.reloadNews();
41     }
42
43     function initialize(){        
44         var componentToDisplay = sharedContext.getComponentToDisplay();
45         if (componentToDisplay === "StockQuoteDetails"){
46             uiLoader.sourceComponent = stockDetailsComponent;
47             titleBar.buttonType = "Close";
48             titleBar.displayMenu = false;
49             toolBar.displayIcons = false;
50         }
51         else{
52             DBUtility.initialize();
53             CoreLib.initialize();
54             uiLoader.sourceComponent = stockQuotesUIComponent;
55         }
56     }
57
58     Component.onCompleted: {
59         initialize();
60     }
61
62     Timer {
63         id: autoUpdateTimer
64         interval: autoUpdateInterval
65         //running: (autoUpdateInterval == 0? false:true)
66         repeat: true
67         onTriggered: {
68             if (!updateWeekDaysOnly){
69                 logUtility.logMessage("Allowed to update all days of the week");
70                 mainPage.reloadData();
71                 //checkNetworkStatus();
72             }
73             else if (Common.isTodayAWeekDay()){
74                 logUtility.logMessage("Today is a weekday");
75                 mainPage.reloadData();
76                 //checkNetworkStatus();
77             }
78             else{
79                 logUtility.logMessage("Update not triggered: Today is not a weekday");
80             }
81         }
82     }
83
84     ListModel{
85         id: stockQuoteDataModel
86     }
87
88     ListModel {
89         id: newsDataModel
90     }
91
92     Rectangle {
93         id: background
94         anchors.fill: parent;
95         color: "#343434"
96         clip: true
97
98         Component {
99             id: stockQuotesDelegate
100
101             Item {
102                 id: wrapper; width: mainPage.componentWidth; height: mainPage.itemHeight
103                 Item {
104                     Rectangle { color: "black"; opacity: index % 2 ? 0.2 : 0.4; height: wrapper.height - 2; width: wrapper.width; y: 1
105                         Image{
106                             id: informationIcon
107                             width: 32
108                             height: 32
109                             z: 10
110                             anchors {right: parent.right; rightMargin: 10; verticalCenter: parent.verticalCenter}
111                             visible: false
112                             source: "Library/images/information.png"
113                             MouseArea{
114                                 anchors.fill: parent;
115                                 onPressed: {
116                                     //console.log("Image clicked");
117                                     mainPage.selectedSymbol = symbol;
118                                     uiLoader.sourceComponent = stockDetailsComponent;
119                                     titleBar.buttonType = "Back";
120                                     titleBar.displayMenu = false;
121                                     toolBar.displayIcons = false;
122                                 }
123                             }
124                         }
125
126                         MouseArea {
127                             anchors.fill: parent
128                             onPressed:{
129                                 informationIcon.visible = true;
130                                 //console.log("Rectangle clicked");
131                             }
132                         }
133                     }
134
135                     Row {
136                         x: 30;y: 15;
137                         width: mainPage.componentWidth - 60;
138                         spacing: 5
139
140                         Text { text: stockName; width: parent.width * 30/100; font.pixelSize: 18; font.bold: true; elide: Text.ElideRight; color: "white"; style: Text.Raised; styleColor: "black" }
141                         Text { text: lastTradedPrice; width: parent.width * 15/100; font.pixelSize: 18; horizontalAlignment: Text.AlignLeft; elide: Text.ElideLeft; color: "#cccccc"; style: Text.Raised; styleColor: "black" }
142                         Text { text: change !== ""? (change + " ("+changePercentage+")"):""; width: parent.width * 25/100;  font.pixelSize: 18; horizontalAlignment: Text.AlignLeft; elide: Text.ElideRight
143                                 color: if(change >= 0){"green";} else {"red";}
144                                     style: Text.Raised; styleColor: "black" }
145                         Text { text: volume; width: parent.width * 15/100; font.pixelSize: 18; horizontalAlignment: Text.AlignLeft; elide: Text.ElideLeft; color: "#cccccc"; style: Text.Raised; styleColor: "black" }
146                         Text { text: marketCap; width: parent.width * 15/100; font.pixelSize: 18; horizontalAlignment: Text.AlignLeft; elide: Text.ElideLeft; color: "#cccccc"; style: Text.Raised; styleColor: "black" }
147                     }
148                 }
149             }
150         }
151
152         Component {
153             id: newsDelegate
154
155             Item {
156                 id: newsWrapper; width: mainPage.componentWidth; height: mainPage.itemHeight
157                 Item {
158                     anchors.fill: parent
159                     Rectangle { color: "black"; opacity: index % 2 ? 0.2 : 0.4; height: newsWrapper.height - 2; width: newsWrapper.width; y: 1 }
160                     Text {
161                         anchors.verticalCenter: parent.verticalCenter
162                         anchors.left: parent.left
163                         anchors.leftMargin: 10
164                         anchors.right: parent.right
165                         text: title; font.pixelSize: 18
166                         font.bold: false;
167                         verticalAlignment: Text.AlignVCenter
168                         horizontalAlignment: Text.AlignLeft
169                         elide: Text.ElideRight;
170                         color: "white";
171                         style: Text.Raised;
172                         styleColor: "black"
173                     }
174                     MouseArea{
175                         anchors.fill: parent
176                         onClicked: Qt.openUrlExternally(link);
177                     }
178                 }
179             }
180         }
181
182         Library.TitleBar {
183             id: titleBar;
184             width: parent.width; height: mainPage.titleBarHeight;
185             anchors.top: parent.top
186             title: "Markets Today";
187             buttonType: "Close";
188             z: 5  //required to keep Titlebar and Menu buttons on top of everything else
189
190             onCloseClicked: Qt.quit()
191
192             onTickersClicked: {
193                 uiLoader.sourceComponent = configTickersComponent;
194                 titleBar.buttonType = "Back";
195                 titleBar.displayMenu = false;
196                 toolBar.displayIcons = false;
197             }
198
199             onOptionsClicked: {
200                 uiLoader.sourceComponent = configParamsComponent;
201                 titleBar.buttonType = "Back";
202                 titleBar.displayMenu = false;
203                 toolBar.displayIcons = false;
204             }
205
206             onBackClicked: {
207                 uiLoader.sourceComponent = stockQuotesUIComponent;
208                 titleBar.buttonType = "Close";
209                 titleBar.displayMenu = false;
210                 toolBar.displayIcons = true;
211                 mainPage.initialize();
212             }
213         }
214
215         Loader {
216             id: uiLoader
217             width: parent.width
218             anchors.top: titleBar.bottom
219             anchors.bottom: toolBar.top            
220         }
221
222         Library.ToolBar {
223             id:toolBar
224             width: parent.width; height: mainPage.toolBarHeight
225             anchors.bottom: parent.bottom
226             opacity: 0.9
227             displayNavigation: false
228             onReloadButtonClicked: mainPage.reloadData();
229
230             onNewsButtonClicked: {
231                 uiLoader.sourceComponent = newsComponent;
232                 toolBar.displayIcons = true;
233                 toolBar.targetContentType = "Stocks";
234             }
235
236             onStocksButtonClicked: {
237                 uiLoader.sourceComponent = stockQuotesUIComponent;
238                 toolBar.displayIcons = true;
239                 toolBar.targetContentType = "News";
240             }
241
242
243             Connections {
244                 target: mainPage
245                 onQuoteRefreshStarted:{
246                     if (!toolBar.updatePending) toolBar.updatePending = true;
247                 }
248                 onQuoteRefreshCompleted:{
249                     toolBar.updatePending = false;
250                 }
251             }
252         }
253
254
255
256         Component {
257             id: stockQuotesUIComponent
258             Item {
259                 Rectangle{
260                     id: listViewWrapper
261                     width: parent.width
262                     anchors.top: parent.top
263                     anchors.bottom: footerText.top
264                     color: "#343434"
265
266                     ListView {
267                         id: stockQuotesView
268                         anchors.fill: parent
269                         flickDeceleration: 500
270                         model: stockQuoteDataModel
271                         delegate:  stockQuotesDelegate
272                         focus:true
273                         snapMode: ListView.SnapToItem
274                     }
275                 }
276
277                 Rectangle{
278                     id: footerText
279                     width: parent.width
280                     height: 25
281                     color: "#343434"
282                     anchors.bottom: parent.bottom
283                     Text {
284                         id: timeStamp
285                         anchors.fill: parent
286                         text: mainPage.lastUpdatedTimeStamp
287                         horizontalAlignment: Text.AlignRight; verticalAlignment: Text.AlignVCenter
288                         width: parent.width; font.pixelSize: 12; elide: Text.ElideRight;
289                         color: "#cccccc"
290                         style: Text.Raised; styleColor: "black"
291
292                         Connections {
293                             target: mainPage
294                             onQuoteRefreshCompleted:{
295                                 timeStamp.text = mainPage.lastUpdatedTimeStamp;
296                             }
297                         }
298                     }
299                 }
300             }
301         }
302
303         Component{
304             id: stockDetailsComponent
305             StockDetailsComponent {
306                 symbol: selectedSymbol
307                 onLogRequest: logUtility.logMessage(strMessage)
308             }
309         }
310
311         Component {
312             id: newsComponent
313             Item {
314                 Rectangle{
315                     width: parent.width
316                     anchors.top: parent.top
317                     anchors.bottom: parent.bottom
318                     color: "#343434"
319
320                     ListView {
321                         id: newsView
322                         anchors.fill: parent
323                         flickDeceleration: 500
324                         model: newsDataModel
325                         delegate:  newsDelegate
326                         focus:true
327                         snapMode: ListView.SnapToItem
328                     }
329                 }
330             }
331         }                
332
333         Component {
334             id: configTickersComponent
335
336             ConfigTickersComponent{
337                 itemHeight: mainPage.itemHeight
338                 componentWidth: mainPage.componentWidth
339                 onLogRequest: logUtility.logMessage(strMessage)
340             }
341         }
342
343         Component {
344             id: configParamsComponent
345             ConfigParametersComponent{
346                 onLogRequest: logUtility.logMessage(strMessage)
347             }
348         }       
349     }
350 }