caead896bffe4e971dac1982c2bcacab129fd49f
[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: 800
27      height: 480
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          maindatemodel.reset();
71      }
72
73      Rectangle {
74          id: mecal
75          anchors.fill: parent
76          color: "white"
77
78          state:"jumptodate"
79
80          Image {
81              source: theme_manager.theme.background_image; fillMode: Image.Stretch; anchors.fill: parent
82              opacity: theme_manager.theme.background_opacity
83          }
84
85
86          MouseArea {
87              id:mouseareaMM
88              property int  xfrom: 0
89              property int  yfrom: 0
90
91              property int fixed: 40
92              property int rise: 0
93
94              anchors.fill: parent;anchors.topMargin: 60;anchors.bottomMargin:60;
95
96              onPressAndHold:{
97                  if (Settings.pressandhold()==1){
98                      menu_item_Clicked(4);
99                  }
100              }
101
102              onDoubleClicked:{
103
104                  if (Settings.doubleclicked()==1){
105                      screen.state="switchX";
106                     Script.datetimedlg.state="show";
107                  }
108              }
109          }
110
111          //------------------------------------------------
112
113                   Rectangle {
114                       id:reelx
115                       width: 260
116                       height: 90
117                       y: 116
118                       anchors.horizontalCenter: parent.horizontalCenter
119                       property int index: 0
120                       onIndexChanged: txtDATEpv.currentIndex = reelx.index
121                       color: "transparent"
122
123                       Component {
124                           id: delegate
125                           Column {
126                               id: wrapper
127                               Text {
128                                   id: nameText
129                                   text: name
130                                   font.bold: true
131                                   font.pixelSize: wrapper.PathView.isCurrentItem ? 80 : 40
132                                   color: {
133                                       if (wrapper.PathView.isCurrentItem){
134                                           var stemp = Script.convertSolar2Lunar(txtDATEpv.currentIndex+1,curMonth,curYear,7);
135                                           if (Script.is_holiday(txtDATEpv.currentIndex+1,curMonth, stemp[0], stemp[1])){
136                                               return theme_manager.theme.markdate.holidays;
137                                           }else{
138                                           var mark_day = (Script.getLunarDate(txtDATEpv.currentIndex+1,curMonth,curYear).jd+1)%7;
139                                           if (mark_day === 0) //sunday
140                                               return theme_manager.theme.markdate.sunday;
141                                           if (mark_day == 6) //saturday
142                                               return theme_manager.theme.markdate.saturday;
143                                           else
144                                               return  theme_manager.theme.markdate.otherday_selected;
145                                           }
146                                       }
147                                       else
148                                           return "transparent";//theme_manager.theme.markdate.otherday;
149                                     }
150                               }
151                           }
152                       }
153
154                       PathView {
155                           id:txtDATEpv
156                           anchors.fill: parent
157                           model: maindatemodel
158                           delegate: delegate
159                           pathItemCount: 3
160                           clip: true
161                           Keys.onLeftPressed: decrementCurrentIndex()
162                           Keys.onRightPressed: incrementCurrentIndex()
163                           highlightMoveDuration:500
164                           preferredHighlightBegin: 0.5
165                           preferredHighlightEnd: 0.5
166                           highlightRangeMode: PathView.StrictlyEnforceRange
167                           focus: true
168                           interactive: true
169                           highlight: Rectangle{
170                               anchors.topMargin: 10;
171                               width:110;
172                               height:80;
173                               radius: 8;
174                               color:"#000000"
175                               opacity: 0.3
176                           }
177                           path: Path {
178                               startX: 0
179                               startY: 50
180                               PathLine {x: reelx.width; y: 50}
181                           }
182
183                           onMovementStarted:{
184
185                           }
186
187                           onMovementEnded: {
188                               Script.changeDay(txtDATEpv.currentIndex+1, txtTHU, txtInfo, txtDAYAmLich);
189                               Script.fullmonth.m_day=Script.curDay;
190                               Script.fullmonth.m_month=Script.curMonth;
191                               Script.fullmonth.m_year=Script.curYear;
192                               Script.fullmonth.reset();
193                           }
194                       }
195                   }
196                   ListModel {
197                       id:maindatemodel
198
199                       function reset(){
200                           var days_of_this_month = Script.calDays();
201                           var days_of_changes = days_of_this_month - maindatemodel.count;
202
203                           var ifs;
204
205                           if (days_of_changes<0){
206                               ifs=Math.abs(days_of_changes);
207                               while(ifs>0){
208                                   maindatemodel.remove(maindatemodel.count-1);
209                                   ifs-=1;
210                               }
211                           }else if(days_of_changes>0){
212                               ifs=maindatemodel.count;
213                               for (var i=1;i<=days_of_changes;i++)
214                                   append({name:i+ifs})
215                           }
216
217                          reelx.index = -1;
218                          reelx.index = Script.curDay - 1;
219
220                       }
221
222                       function init(){
223                           for(var i=1;i<=Script.calDays();i++)
224                               append({name:i});
225
226                           reelx.index = Script.curDay - 1;
227                       }
228                   }
229
230          //-------------------------------------------------
231
232          Text {
233              id: txtDMY
234                       x: 151
235                       y: 77
236                       height: 20
237                       color: "#29d999"
238                       text: "Tháng <b>"+screen.curMonth+"</b> Năm <b>"+screen.curYear+"</b>"
239                       horizontalAlignment: Text.AlignHCenter
240                       anchors.horizontalCenter: parent.horizontalCenter
241                       font.bold: false
242                       font.family: "Tahoma"
243                       smooth: true
244                       font.pixelSize: 28
245               }
246
247          Text {
248              id: txtTHU
249              x: 151
250              y: 210
251              height: 20
252              color: "#f0f0f5"
253              text: Script.__txtTHU_format()
254              horizontalAlignment: Text.AlignHCenter
255              anchors.horizontalCenter: parent.horizontalCenter
256              opacity: 0.6
257              font.bold: true
258              font.pixelSize: 30
259          }
260
261          Text {
262              id: txtInfo
263              x: 151
264              y: (screen.is_holiday ? 270 : 250)
265              height: 20
266              color: (Script.is_holiday ? theme_manager.theme.markdate.holidays : theme_manager.theme.markdate.not_holidays)
267              text: Script.__txtInfo_format()
268              horizontalAlignment: Text.AlignHCenter
269              anchors.horizontalCenter: parent.horizontalCenter
270              opacity: 0.6
271              font.bold: (screen.is_holiday ? true : false)
272              font.pixelSize: (screen.is_holiday ? 25 : 20)
273          }
274
275          Text {
276
277              id: txtDAYAmLich
278              x: 151
279              y: 330
280              height: 20
281              color: theme_manager.theme.markdate.lunardate
282              text: Script.__txtDAYAmLich_format()
283              horizontalAlignment: Text.AlignHCenter
284              anchors.horizontalCenter: parent.horizontalCenter
285              opacity: 0.6
286              font.bold: true
287              font.pixelSize: 25
288          }
289
290          ToolBar { id: toolBar; height: 42;
291              y: screen.height - 52
292              width: parent.width; opacity: 0.8
293              button1Label: "Menu"
294              button2Label: "Lựa Chọn"
295              onButton1Clicked:
296              {
297                  screen.state="switchX"
298                  var menuscreen = Script.createObject("MenuScreen.qml", screen);
299                  menuscreen.close.connect(closeDialogs);
300                  menuscreen.item_click.connect(menu_item_Clicked);
301                  menuscreen.state = "show";
302              }
303              onButton2Clicked: {
304                  screen.state="switchX"
305                  var optionsscreen = Script.createObject("OptionsScreen.qml", screen);
306                  optionsscreen.close.connect(closeDialogs);
307                  optionsscreen.state = "show";
308              }
309
310          }
311
312          MultiTitleBar { id: titleBar; width: parent.width }
313
314      }
315
316      function closeDialogs(){
317          dialogClose();
318          screen.state="mainscreen";
319      }
320
321      function menu_item_Clicked(id){
322          var qmlF = "";
323          if (id!=1)
324             screen.state="switchX";
325          switch(id){
326             case 1: Script.reset_to_default_day(txtTHU, txtInfo, txtDAYAmLich);
327                  txtDATEpv.currentIndex = Script.curDay-1;
328                 screen.curMonth = Script.curMonth;
329                 screen.curYear = Script.curYear;
330                 Script.fullmonth.m_day= Script.curDay-1;
331                 Script.fullmonth.m_month= Script.curMonth;
332                 Script.fullmonth.m_year= Script.curYear;
333                 break;
334             case 2: Script.amdatetimedlg.state="show";break;
335             case 3: Script.datetimedlg.state="show";break;
336             case 4: qmlF="XemNgayTotXauHoangDao";break;
337             case 5: Script.fullmonth.state="show";break;
338             case 6: qmlF = "About";break;
339             default:screen.state = "mainscreen";qmlF="";break;
340          }
341          if (qmlF){
342              var qmlobj = Script.createObject(qmlF+".qml", screen);
343              qmlobj.close.connect(closeDialogs);
344              qmlobj.state = "show";
345          }
346      }
347
348      states: [
349          State {
350              name:"mainscreen"
351              StateChangeScript {
352                  script:{
353                  }
354                 }
355          },
356          State {
357              name: "switchX";
358              PropertyChanges { target: titleBar; y: -50 }
359              PropertyChanges { target: toolBar; y: screen.height }
360              PropertyChanges { target: toolBar }
361              PropertyChanges { target: mouseareaMM;enabled:false }
362          }
363      ]
364
365      transitions: [
366          Transition { NumberAnimation { properties: "x,y,opacity"; duration: 300; easing.type: Easing.InOutBack } }
367      ]
368
369 }