c1afc70294039327f52ba8b625c8a7baecf1ed4b
[lichviet] / qml / LichViet / main.qml
1 /*
2 Copyright (C) 2011  by Cuong Le <metacuong@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>
16 */
17
18 import QtQuick 1.1
19
20 import "Themes.js" as Themes
21 import "Settings.js" as Settings
22 import "main.js" as Script
23
24 Item {
25      id: screen
26      width: 480
27      height: 800
28
29      state:"mainscreen"
30
31      signal dialogClose
32
33      property int curMonth: Script.curMonth
34      property int curYear: Script.curYear
35
36      Loader {
37          id: theme_manager
38          property alias theme: theme_manager.item
39          source: Themes.default_theme()
40      }
41
42      Component.onCompleted: {
43         Settings.restore_nomodel();
44         Script.createDateTimeDialog(screen);
45         Script.datetimedlg.toolbar.connect(show_toolbar);
46         Script.datetimedlg.updatedate.connect(update_date);
47         Script.amdatetimedlg.toolbar.connect(show_toolbar);
48         Script.amdatetimedlg.updatedate.connect(update_date);
49         Script.fullmonth.toolbar.connect(show_toolbar);
50         Script.fullmonth.updatedate.connect(update_date);
51         maindatemodel.init();
52         Script.init();
53      }
54
55      function show_toolbar(){
56         screen.state="mainscreen";
57      }
58
59      function update_date(day, month, year){
60          txtDATEpv.currentIndex = day-1;
61          screen.curMonth = month;
62          screen.curYear = year;
63          Script.curMonth = month;
64          Script.curYear = year;
65          Script.changeDay(day, txtTHU, txtInfo, txtDAYAmLich);
66          Script.fullmonth.m_day=Script.curDay;
67          Script.fullmonth.m_month=Script.curMonth;
68          Script.fullmonth.m_year=Script.curYear;
69          Script.fullmonth.reset();
70      }
71
72      Rectangle {
73          id: mecal
74          anchors.fill: parent
75          color: "white"
76
77          state:"jumptodate"
78
79          Image {
80              source: theme_manager.theme.background_image; fillMode: Image.Stretch; anchors.fill: parent
81              opacity: theme_manager.theme.background_opacity
82          }
83
84
85          MouseArea {
86              id:mouseareaMM
87              property int  xfrom: 0
88              property int  yfrom: 0
89
90              property int fixed: 40
91              property int rise: 0
92
93              anchors.fill: parent;anchors.topMargin: 60;anchors.bottomMargin:60;
94
95              onPressAndHold:{
96                  if (Settings.pressandhold()==1){
97                      menu_item_Clicked(4);
98                  }
99              }
100
101              onDoubleClicked:{
102
103                  if (Settings.doubleclicked()==1){
104                      screen.state="switchX";
105                     Script.datetimedlg.state="show";
106                  }
107              }
108          }
109
110          //------------------------------------------------
111
112                   Rectangle {
113                       id:reelx
114                       width: 260
115                       height: 90
116                       y: 116
117                       anchors.horizontalCenter: parent.horizontalCenter
118                       property int index: 0
119                       onIndexChanged: txtDATEpv.currentIndex = reelx.index
120                       color: "transparent"
121
122                       Component {
123                           id: delegate
124                           Column {
125                               id: wrapper
126                               Text {
127                                   id: nameText
128                                   text: name
129                                   font.bold: true
130                                   font.pixelSize: wrapper.PathView.isCurrentItem ? 80 : 40
131                                   color: {
132                                       if (wrapper.PathView.isCurrentItem){
133                                           var stemp = Script.convertSolar2Lunar(txtDATEpv.currentIndex+1,curMonth,curYear,7);
134                                           if (Script.is_holiday(txtDATEpv.currentIndex+1,curMonth, stemp[0], stemp[1])){
135                                               return theme_manager.theme.markdate.holidays;
136                                           }else{
137                                           var mark_day = (Script.getLunarDate(txtDATEpv.currentIndex+1,curMonth,curYear).jd+1)%7;
138                                           if (mark_day === 0) //sunday
139                                               return theme_manager.theme.markdate.sunday;
140                                           if (mark_day == 6) //saturday
141                                               return theme_manager.theme.markdate.saturday;
142                                           else
143                                               return  theme_manager.theme.markdate.otherday_selected;
144                                           }
145                                       }
146                                       else
147                                           return "transparent";//theme_manager.theme.markdate.otherday;
148                                     }
149                               }
150                           }
151                       }
152
153                       PathView {
154                           id:txtDATEpv
155                           anchors.fill: parent
156                           model: maindatemodel
157                           delegate: delegate
158                           pathItemCount: 3
159                           clip: true
160                           Keys.onLeftPressed: decrementCurrentIndex()
161                           Keys.onRightPressed: incrementCurrentIndex()
162                           highlightMoveDuration:500
163                           preferredHighlightBegin: 0.5
164                           preferredHighlightEnd: 0.5
165                           highlightRangeMode: PathView.StrictlyEnforceRange
166                           focus: true
167                           interactive: true
168                           highlight: Rectangle{
169                               anchors.topMargin: 10;
170                               width:110;
171                               height:80;
172                               radius: 8;
173                               color:"#000000"
174                               opacity: 0.3
175                           }
176                           path: Path {
177                               startX: 0
178                               startY: 50
179                               PathLine {x: reelx.width; y: 50}
180                           }
181
182                           onMovementStarted:{
183
184                           }
185
186                           onMovementEnded: {
187                               Script.changeDay(txtDATEpv.currentIndex+1, txtTHU, txtInfo, txtDAYAmLich);
188                               Script.fullmonth.m_day=Script.curDay;
189                               Script.fullmonth.m_month=Script.curMonth;
190                               Script.fullmonth.m_year=Script.curYear;
191                               Script.fullmonth.reset();
192                           }
193                       }
194                   }
195                   ListModel {
196                       id:maindatemodel
197
198                       function reset(){
199                           var days_of_this_month = Script.calDays();
200                           var days_of_changes = days_of_this_month - maindatemodel.count;
201
202                           var ifs;
203
204                           if (days_of_changes<0){
205                               ifs=Math.abs(days_of_changes);
206                               while(ifs>0){
207                                   maindatemodel.remove(maindatemodel.count-1);
208                                   ifs-=1;
209                               }
210                           }else if(days_of_changes>0){
211                               ifs=maindatemodel.count;
212                               for (var i=1;i<=days_of_changes;i++)
213                                   append({name:i+ifs})
214                           }
215
216                          reelx.index = -1;
217                          reelx.index = Script.curDay - 1;
218
219                       }
220
221                       function init(){
222                           for(var i=1;i<=Script.calDays();i++)
223                               append({name:i});
224
225                           reelx.index = Script.curDay - 1;
226                       }
227                   }
228
229          //-------------------------------------------------
230
231          Text {
232              id: txtDMY
233                       x: 140
234                       y: 77
235                       width: 80
236                       height: 20
237                       color: "#29d999"
238                       textFormat: Text.RichText
239                       text: "Tháng <b>"+screen.curMonth+"</b> Năm <b>"+screen.curYear+"</b>"
240                       anchors.horizontalCenter: parent.horizontalCenter
241                       font.bold: false
242                       font.family: "Tahoma"
243                       smooth: true
244                       horizontalAlignment: Text.AlignHCenter
245                       font.pixelSize: 28
246               }
247
248          Text {
249              id: txtTHU
250              x: 151
251              y: 210
252              width: 80
253              height: 20
254              color: "#f0f0f5"
255              text: Script.__txtTHU_format()
256              horizontalAlignment: Text.AlignHCenter
257              anchors.horizontalCenter: parent.horizontalCenter
258              opacity: 0.6
259              font.bold: true
260              font.pixelSize: 30
261          }
262
263          Text {
264              id: txtInfo
265              x: 151
266              y: (screen.is_holiday ? 270 : 250)
267              width: 80
268              height: 20
269              color: (Script.is_holiday ? theme_manager.theme.markdate.holidays : theme_manager.theme.markdate.not_holidays)
270              text: Script.__txtInfo_format()
271              horizontalAlignment: Text.AlignHCenter
272              anchors.horizontalCenter: parent.horizontalCenter
273              opacity: 0.6
274              font.bold: (screen.is_holiday ? true : false)
275              font.pixelSize: (screen.is_holiday ? 25 : 20)
276          }
277
278          Text {
279
280              id: txtDAYAmLich
281              x: 151
282              y: 330
283              width: 80
284              height: 20
285              color: "#f0f0f5"
286              text: Script.__txtDAYAmLich_format()
287              horizontalAlignment: Text.AlignHCenter
288              anchors.horizontalCenter: parent.horizontalCenter
289              opacity: 0.6
290              font.bold: true
291              font.pixelSize: 25
292          }
293
294          ToolBar { id: toolBar; height: 42;
295              y: screen.height - 52
296              width: parent.width; opacity: 0.8
297              button1Label: "Menu"
298              button2Label: "Lựa Chọn"
299              onButton1Clicked:
300              {
301                  screen.state="switchX"
302                  var menuscreen = Script.createObject("MenuScreen.qml", screen);
303                  menuscreen.close.connect(closeDialogs);
304                  menuscreen.item_click.connect(menu_item_Clicked);
305                  menuscreen.state = "show";
306              }
307              onButton2Clicked: {
308                  screen.state="switchX"
309                  var optionsscreen = Script.createObject("OptionsScreen.qml", screen);
310                  optionsscreen.close.connect(closeDialogs);
311                  optionsscreen.state = "show";
312              }
313
314          }
315
316          MultiTitleBar { id: titleBar; width: parent.width }
317
318      }
319
320      function closeDialogs(){
321          dialogClose();
322          screen.state="mainscreen";
323      }
324
325      function menu_item_Clicked(id){
326          var qmlF = "";
327          if (id!=1)
328             screen.state="switchX";
329          switch(id){
330             case 1: Script.reset_to_default_day(txtTHU, txtInfo, txtDAYAmLich);
331                  txtDATEpv.currentIndex = Script.curDay-1;
332                 screen.curMonth = Script.curMonth;
333                 screen.curYear = Script.curYear;
334                 Script.fullmonth.m_day= Script.curDay-1;
335                 Script.fullmonth.m_month= Script.curMonth;
336                 Script.fullmonth.m_year= Script.curYear;
337                 break;
338             case 2: Script.amdatetimedlg.state="show";break;
339             case 3: Script.datetimedlg.state="show";break;
340             case 4: qmlF="XemNgayTotXauHoangDao";break;
341             case 5: Script.fullmonth.state="show";break;
342             case 6: qmlF = "About";break;
343             default:screen.state = "mainscreen";qmlF="";break;
344          }
345          if (qmlF){
346              var qmlobj = Script.createObject(qmlF+".qml", screen);
347              qmlobj.close.connect(closeDialogs);
348              qmlobj.state = "show";
349          }
350      }
351
352      states: [
353          State {
354              name:"mainscreen"
355              StateChangeScript {
356                  script:{
357                  }
358                 }
359          },
360          State {
361              name: "switchX";
362              PropertyChanges { target: toolBar; y: screen.height }
363              PropertyChanges { target: toolBar }
364              PropertyChanges { target: mouseareaMM;enabled:false }
365          }
366      ]
367
368      transitions: [
369          Transition { NumberAnimation { properties: "x,y,opacity"; duration: 300; easing.type: Easing.InOutBack } }
370      ]
371
372 }