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