Initial Release
[marketstoday] / src / qml / ConfigTickersComponent.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 import "Library/js/DBUtility.js" as DBUtility
9
10 Item {
11     id: tickerTab
12     property int componentWidth
13     property int itemHeight
14     signal logRequest(string strMessage)
15
16     Component.onCompleted: {
17             DBUtility.initialize();
18             loadSymbols();
19     }
20
21     function loadSymbols(){
22         var symbolsArray = DBUtility.getAllSymbols();
23         if (symbolsArray && symbolsArray.length > 0){
24             var i = 0;
25             for (i = 0; i< symbolsArray.length; i++) {
26                 logRequest("Appending "+symbolsArray[i]+ " to ListModel");
27                 symbolsListModel.append({"symbol": symbolsArray[i]});
28             }
29             logRequest("ListModel count is  "+symbolsListModel.count);
30         }
31     }
32
33     function removeSymbol(symbol,index){
34         logRequest("Removing symbol "+symbol+" at index "+index);
35
36         var result = DBUtility.removeSymbol(symbol);
37         if (result != "Error"){
38             symbolsListModel.remove(index);
39         }
40         else{
41             logRequest("Error: DB error while removing "+symbol+" at index "+index);
42         }
43
44     }
45
46     function addSymbol(symbol){
47         if (symbol && symbol.length > 0){
48             symbol = symbol.toUpperCase();
49             logRequest("Adding symbol "+symbol);
50             var result = DBUtility.addSymbol(symbol);
51             logRequest("Result is "+result);
52
53             if (result != "Error"){
54                 symbolsListModel.append({"symbol": symbol});
55             }
56             else{
57                 logRequest("Error: DB error while adding "+symbol);
58             }
59         }
60         else{
61             logRequest("Error: Invalid symbol "+symbol);
62         }
63     }
64
65     ListModel {
66         id: symbolsListModel
67     }
68
69     Component {
70         id: tickersListDelegate
71
72         Item {
73             id: wrapper; width: componentWidth; height: itemHeight
74             Rectangle { id: listRecord;
75                 color: "black";
76                 opacity: index % 2 ? 0.2 : 0.4;
77                 height: parent.height - 2;
78                 width: parent.width; y: 1 }
79
80             Text {
81                 text: symbol;
82                 anchors.left: parent.left
83                 anchors.leftMargin: 30
84                 anchors.verticalCenter: parent.verticalCenter
85                 verticalAlignment: Text.AlignVCenter
86                 width: parent.width - 120;
87                 height: parent.height
88                 font.pixelSize: 18;
89                 font.bold: true;
90                 elide: Text.ElideRight;
91                 color: "white";
92                 style: Text.Raised;
93                 styleColor: "black"
94             }
95
96             Rectangle {
97                 id: removeButtonArea
98                 width: 120
99                 height: parent.height
100                 anchors.right: parent.right
101                 color: "#00000000";
102
103                 Image {
104                     source: "Library/images/remove.png"
105                     anchors.centerIn: parent
106                     width: 32; height: 32
107                 }
108
109                 MouseArea{
110                     id:removeButtonMouseArea
111                     anchors.fill: parent
112                     onClicked: {
113                         removeSymbol(symbol,index)
114                     }
115                 }
116
117                 states: State {
118                          name: "pressed"; when: removeButtonMouseArea.pressed
119                          PropertyChanges { target: removeButtonArea; color: "#9a9a9a"}
120                 }
121             }
122         }
123     }
124
125     Rectangle {
126         id: newSymbolRow
127         //width: parent.width
128         width: componentWidth
129         height: itemHeight;
130         anchors.top: parent.top
131         color: "#343434"
132
133         Item {
134             id: lineEditItem
135             width: parent.width - 120
136             height: parent.height
137             anchors.verticalCenter: parent.verticalCenter
138             anchors.left: parent.left
139             BorderImage { source: "Library/images/lineedit.sci"; anchors.fill: parent }
140             TextInput{
141                 id: newSymbol
142                 width: parent.width
143                 height: parent.height
144                 anchors.left: parent.left
145                 anchors.leftMargin: 5
146                 anchors.verticalCenter: parent.verticalCenter
147                 maximumLength:25
148                 font.pixelSize: 18
149                 font.bold: true
150                 font.capitalization: Font.AllUppercase
151                 color: "#151515"; selectionColor: "green"
152                 KeyNavigation.tab: addButton
153                 Keys.onReturnPressed: {
154                     logRequest("Return pressed");
155                     addSymbol(newSymbol.text.trim());
156                     newSymbol.text = "";
157                 }
158                 Keys.onEnterPressed: {
159                     logRequest("Enter pressed");
160                     addSymbol(newSymbol.text.trim());
161                     newSymbol.text = "";
162                 }
163                 focus: true
164             }
165         }
166
167         Rectangle {
168             id: addButtonArea
169             anchors.verticalCenter: parent.verticalCenter
170             anchors.right: parent.right
171             width: 120
172             height: parent.height
173             color:"#343434"
174             Image {
175                 id: addButton
176                 source: "Library/images/add.png"
177                 width: 32; height: 32
178                 anchors.centerIn: parent
179             }
180             MouseArea{
181                 id:addButtonMouseArea
182                 anchors.fill: parent
183                 onClicked: {
184                      addSymbol(newSymbol.text.trim());
185                      newSymbol.text = "";
186                 }
187             }
188             states: State {
189                      name: "pressed"; when: addButtonMouseArea.pressed
190                      PropertyChanges { target: addButtonArea; color: "#9a9a9a"}
191             }
192         }
193     }
194     Rectangle{
195         anchors.top: newSymbolRow.bottom
196         anchors.bottom: parent.bottom
197         width: parent.width;
198
199         color:"#343434"
200         ListView{
201             id: symbolsListView
202             anchors.fill: parent
203             model: symbolsListModel
204             delegate: tickersListDelegate
205         }
206
207     }
208 }