Version 0.2
[marketstoday] / src / qml / MarketsTodayApp.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
101         Library.TitleBar {
102             id: titleBar;
103             width: parent.width; height: 60;
104             anchors.top: parent.top
105             title: "Markets Today";
106             buttonType: "Close";
107
108             onCloseClicked: {
109                 console.log("Closing..");
110                 console.log(stockQuoteDataModel.count);
111                 Qt.quit();
112             }
113
114             onTickersClicked: {
115                 uiLoader.sourceComponent = configTickersComponent;
116                 titleBar.buttonType = "Back";
117                 titleBar.displayMenu = false;
118                 toolBar.displayIcons = false;
119             }
120
121             onOptionsClicked: {
122                 uiLoader.sourceComponent = configParamsComponent;
123                 titleBar.buttonType = "Back";
124                 titleBar.displayMenu = false;
125                 toolBar.displayIcons = false;
126             }
127
128             onBackClicked: {
129                 uiLoader.sourceComponent = stockQuotesUIComponent;
130                 titleBar.buttonType = "Close";
131                 titleBar.displayMenu = false;
132                 toolBar.displayIcons = true;
133             }
134         }
135
136         Loader {
137             id: uiLoader
138             width: parent.width
139             anchors.top: titleBar.bottom
140             anchors.bottom: toolBar.top
141             sourceComponent: stockQuotesUIComponent
142         }
143
144         Library.ToolBar {
145             id:toolBar
146             width: parent.width; height: 40
147             anchors.bottom: parent.bottom
148             opacity: 0.9
149             displayNavigation: false
150             onReloadButtonClicked: screen.reloadQuotes();
151             onNewsButtonClicked: Qt.openUrlExternally("http://finance.yahoo.com");
152
153             Connections {
154                 target: screen
155                 onQuoteRefreshStarted:{
156                     if (!toolBar.updatePending) toolBar.updatePending = true;
157                 }
158                 onQuoteRefreshCompleted:{
159                     toolBar.updatePending = false;
160                     console.log(screen.lastUpdatedTimeStamp);
161                 }
162             }
163         }
164
165
166
167         Component {
168             id: stockQuotesUIComponent
169             Item {
170                 Rectangle{
171                     id: listViewWrapper
172                     width: parent.width
173                     anchors.top: parent.top
174                     anchors.bottom: footerText.top
175                     color: "#343434"
176
177                     ListView {
178                         id: stockQuotesView
179                         anchors.fill: parent
180                         flickDeceleration: 500
181                         model: stockQuoteDataModel
182                         delegate:  stockQuotesDelegate
183                         focus:true
184                     }
185                 }
186
187                 Rectangle{
188                     id: footerText
189                     width: parent.width
190                     height: 25
191                     color: "#343434"
192                     anchors.bottom: parent.bottom
193                     Text {
194                         id: timeStamp
195                         anchors.fill: parent
196                         text: screen.lastUpdatedTimeStamp
197                         horizontalAlignment: Text.AlignRight; verticalAlignment: Text.AlignVCenter
198                         width: parent.width; font.pixelSize: 12; elide: Text.ElideRight;
199                         color: "#cccccc"
200                         style: Text.Raised; styleColor: "black"
201
202                         Connections {
203                             target: screen
204                             onQuoteRefreshCompleted:{
205                                 timeStamp.text = screen.lastUpdatedTimeStamp;
206                             }
207                         }
208                     }
209                 }
210             }
211         }
212
213         Component {
214             id: configTickersComponent
215
216             ConfigTickersComponent{
217                 itemHeight: screen.itemHeight
218                 componentWidth: screen.componentWidth
219                 onLogRequest: logUtility.logMessage(strMessage)
220             }
221         }
222
223         Component {
224             id: configParamsComponent
225             ConfigParametersComponent{
226                 onLogRequest: logUtility.logMessage(strMessage)
227             }
228         }
229     }
230 }