* Fixes for pathview date component (slowly in date switching)
[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 3 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.0
19 import "amlich-hnd.js" as AmLich
20 import "amlich-aa98.js" as AmLich98
21
22 import "Settings.js" as Settings
23 import "Themes.js" as Themes
24 import "CacNgayLeTrongNam.js" as CNLTN
25
26 Item {
27      id: screen
28      width: 480
29      height: 800
30
31      property int curDay: LC.curDay()
32      property int curMonth: LC.curMonth()
33      property int curYear: LC.curYear()
34
35      property int initcurDay: LC.curDay()
36      property int initcurMonth: LC.curMonth()
37      property int initcurYear:LC.curYear()
38
39      property int lunarDay: AmLich.getLunarDate(curDay,curMonth,curYear).day
40      property int lunarMonth: AmLich.getLunarDate(curDay,curMonth,curYear).month
41      property string lunarYear: AmLich.getCanChi(AmLich.getLunarDate(curDay,curMonth,curYear))[2]
42
43      property int lunarYearInt : AmLich.getLunarDate(curDay,curMonth,curYear).year
44      property int jd : AmLich.getLunarDate(curDay,curMonth,curYear).jd
45      property string lunarGioHoangDao : AmLich.getGioHoangDao(jd)
46
47      property string lunarDayCanChi : AmLich.getCanChi(AmLich.getLunarDate(curDay,curMonth,curYear))[0]
48      property string lunarMonthCanChi : AmLich.getCanChi(AmLich.getLunarDate(curDay,curMonth,curYear))[1]
49
50      property string day_info : CNLTN.get(screen)
51      property bool is_holiday : CNLTN.is_holiday(screen.curDay,screen.curMonth, screen.curYear, screen.lunarDay, screen.lunarMonth)
52
53      state:"mainscreen"
54
55      Loader {
56          id: theme_manager
57          property alias theme: theme_manager.item
58          source: Themes.default_theme()
59      }
60
61      Rectangle {
62          id: background
63          anchors.fill: parent
64          color: "#343434"
65
66          state:"jumptodate"
67          Image {
68              source: theme_manager.theme.background_image; fillMode: Image.Stretch; anchors.fill: parent; opacity: 0.3
69          }
70
71
72          MouseArea {
73              id:mouseareaMM
74              property int  xfrom: 0
75              property int  yfrom: 0
76
77              property int fixed: 40
78              property int rise: 0
79
80              anchors.fill: parent;anchors.topMargin: 60;anchors.bottomMargin:60;
81
82              onPressAndHold:{
83                  if (Settings.pressandhold()==1){
84                     screen.state="xemngaytotxauhoangdao"
85                  }
86              }
87
88              onDoubleClicked:{
89
90                  if (Settings.doubleclicked()==1){
91                      screen.state="datetimedialog"
92                  }
93              }
94          }
95
96          Text {
97              id: txtDMY
98              x: 140
99              y: 77
100              width: 80
101              height: 20
102              color: "#29d999"
103              text: "Tháng "+screen.curMonth+" Năm "+screen.curYear
104              anchors.horizontalCenter: parent.horizontalCenter
105              font.bold: true
106              font.family: "Tahoma"
107              smooth: true
108              horizontalAlignment: Text.AlignHCenter
109              font.pixelSize: 28
110          }
111
112          //------------------------------------------------
113
114                   Rectangle {
115                       id:reelx
116                       width: 260
117                       height: 90
118                       y: 116
119                       anchors.horizontalCenter: parent.horizontalCenter
120                       property int index: 0
121                       onIndexChanged: txtDATEpv.currentIndex = reelx.index
122                       color: "transparent"
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 = AmLich98.convertSolar2Lunar(txtDATEpv.currentIndex+1,curMonth,curYear,7);
135                                           if (CNLTN.is_holiday(txtDATEpv.currentIndex+1,curMonth, stemp[0], stemp[1])){
136                                               return theme_manager.theme.markdate.holidays;
137                                           }else{
138                                           var mark_day = (AmLich.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 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                           highlightMoveDuration:500
162                           Keys.onDownPressed: if (!moving && interactive) incrementCurrentIndex()
163                           Keys.onUpPressed: if (!moving && interactive) decrementCurrentIndex()
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                           onFlickEnded : {
183                           }
184
185                           onMovementEnded: {
186                               calculateLunar(txtDATEpv.currentIndex+1, LC.curMonth(), LC.curYear())
187                           }
188                       }
189                   }
190                   ListModel {
191                       id:maindatemodel
192
193                       function reset(){
194                           var days_of_this_month = LC.calDays(screen.curMonth,screen.curYear);
195                           var days_of_changes = days_of_this_month - maindatemodel.count;
196
197                           var ifs;
198
199                           if (days_of_changes<0){
200                               ifs=Math.abs(days_of_changes);
201                               while(ifs>0){
202                                   maindatemodel.remove(maindatemodel.count-1);
203                                   ifs-=1;
204                               }
205                           }else if(days_of_changes>0){
206                               ifs=maindatemodel.count;
207                               for (var i=1;i<=days_of_changes;i++)
208                                   append({name:i+ifs})
209                           }
210
211                          reelx.index = -1; // I dont now why must be added -1 before screen.curDay-1
212                          reelx.index = screen.curDay-1;
213
214                       }
215
216                       function init(){
217                           while(maindatemodel.count>0){
218                               try{maindatemodel.remove(maindatemodel.count-1)}catch(e){}
219                           }
220
221                           for(var i=1;i<=LC.calDays(screen.curMonth,screen.curYear);i++)
222                               append({name:i})
223
224                           reelx.index = screen.curDay -1
225                       }
226                   }
227
228          //-------------------------------------------------
229                   Component.onCompleted: {
230                      maindatemodel.init()
231                   }
232
233          Text {
234              id: txtTHU
235              x: 151
236              y: 210
237              width: 80
238              height: 20
239              color: "#f0f0f5"
240              text: AmLich.getTHU(screen.jd)
241              horizontalAlignment: Text.AlignHCenter
242              anchors.horizontalCenter: parent.horizontalCenter
243              opacity: 0.6
244              font.bold: true
245              font.pixelSize: 30
246          }
247
248          Text {
249              id: txtInfo
250              x: 151
251              y: (screen.is_holiday ? 270 : 250)
252              width: 80
253              height: 20
254              color: (screen.is_holiday ? theme_manager.theme.markdate.holidays : theme_manager.theme.markdate.not_holidays)
255              text: screen.day_info
256              horizontalAlignment: Text.AlignHCenter
257              anchors.horizontalCenter: parent.horizontalCenter
258              opacity: 0.6
259              font.bold: (screen.is_holiday ? true : false)
260              font.pixelSize: (screen.is_holiday ? 25 : 20)
261          }
262
263          Text {
264
265              id: txtDAYAmLich
266              x: 151
267              y: 330
268              width: 80
269              height: 20
270              color: "#f0f0f5"
271              text: "Ngày <span style='color:#C0C0C0;'>"+screen.lunarDay+" ("+screen.lunarDayCanChi+")</span><br>Tháng <span style='color:#C0C0C0;'>"+screen.lunarMonth+" ("+screen.lunarMonthCanChi+")</span><br>Năm <span style='color:#C0C0C0;'>"+screen.lunarYearInt+" ("+screen.lunarYear+")</span>"
272              horizontalAlignment: Text.AlignHCenter
273              anchors.horizontalCenter: parent.horizontalCenter
274              opacity: 0.6
275              font.bold: true
276              font.pixelSize: 25
277          }
278
279
280          MenuScreen{
281              id: menuScreen
282              state:"menuscreen"
283              anchors.verticalCenter: parent.verticalCenter
284              width: parent.width; height: parent.height-60;
285              x: -(screen.width * 1.5)
286          }
287
288          OptionsScreen{
289              id: optionsScreen
290              state:"optionsscreen"
291              anchors.verticalCenter: parent.verticalCenter
292              width: parent.width; height: parent.height-60;
293              x: -(screen.width * 1.5)
294          }
295
296          ChangeTheme{
297              id: changeTheme
298              state:"changetheme"
299              anchors.verticalCenter: parent.verticalCenter
300              width: parent.width; height: parent.height-60;
301              x: -(screen.width * 1.5)
302          }
303
304          XemNgayTotXauHoangDao{
305              id: xntxhdScreen
306              state:"xemngaytotxauhoangdao"
307              anchors.verticalCenter: parent.verticalCenter
308              width: parent.width; height: parent.height-60;
309              x: -(screen.width * 1.5)
310          }
311
312          DateTimeDialog{
313              id: datetimeDialog
314              state:"datetimedialog"
315              anchors.verticalCenter: parent.verticalCenter
316              width: parent.width; height: parent.height-60;
317              x: -(screen.width * 1.5)
318          }
319
320          AmDateTimeDialog{
321              id: amdatetimeDialog
322              state:"amdatetimedialog"
323              anchors.verticalCenter: parent.verticalCenter
324              width: parent.width; height: parent.height-60;
325              x: -(screen.width * 1.5)
326          }
327
328          FullMonth{
329              id: fullmonthDialog
330              state:"xemtheothang"
331              anchors.verticalCenter: parent.verticalCenter
332              width: parent.width; height: parent.height-60;
333              x: -(screen.width * 1.5)
334          }
335
336          NoFeatureDialog{
337              id: nofeatureDialog
338              state:"nofeaturedialog"
339              anchors.verticalCenter: parent.verticalCenter
340              width: parent.width; height: parent.height-60;
341              x: -(screen.width * 1.5)
342          }
343
344
345          About{
346              id: aboutDialog
347              state:"aboutdialog"
348              anchors.verticalCenter: parent.verticalCenter
349              width: parent.width; height: parent.height-60;
350              x: -(screen.width * 1.5)
351          }
352
353
354          MultiTitleBar { id: titleBar; width: parent.width }
355          ToolBar { id: toolBar; height: 50;
356              y: screen.height - 48
357              width: parent.width; opacity: 0.9
358              button1Label: "Menu"
359              button2Label: "Lựa Chọn"
360              onButton1Clicked:
361              {
362                  screen.state="menuscreen"
363              }
364              onButton2Clicked: {
365                  screen.state="optionsscreen"
366              }
367          }
368      }
369
370      states: [
371          State {
372              name:"mainscreen"
373              StateChangeScript {
374                  script:{
375                  }
376                 }
377          },
378          State {
379              name: "menuscreen";
380              PropertyChanges { target: menuScreen; x: 0; focus:true}
381              PropertyChanges { target: toolBar; y: screen.height }
382              PropertyChanges { target: toolBar }
383                           PropertyChanges { target: mouseareaMM;enabled:false }
384
385          },
386          State {
387              name: "optionsscreen";
388              PropertyChanges { target: optionsScreen; x: 0; focus:true}
389              PropertyChanges { target: toolBar; y: screen.height }
390              PropertyChanges { target: toolBar }
391                           PropertyChanges { target: mouseareaMM;enabled:false }
392
393          },
394          State {
395              name: "changetheme";
396              PropertyChanges { target: changeTheme; x: 0; focus:true}
397              PropertyChanges { target: toolBar; y: screen.height }
398              PropertyChanges { target: toolBar }
399                           PropertyChanges { target: mouseareaMM;enabled:false }
400
401          },
402          State {
403              name: "xemngaytotxauhoangdao";
404              PropertyChanges { target: xntxhdScreen; x: 0; focus:true}
405              PropertyChanges { target: toolBar; y: screen.height }
406              PropertyChanges { target: toolBar }
407              PropertyChanges { target: mouseareaMM;enabled:false }
408
409              StateChangeScript {
410                  script:{
411                      xntxhdScreen.reset()
412                  }
413                 }
414
415          },
416          State {
417              name: "datetimedialog";
418              PropertyChanges { target: datetimeDialog; x: 0; focus:true}
419              PropertyChanges { target: toolBar; y: screen.height }
420              PropertyChanges { target: toolBar }
421              PropertyChanges { target: mouseareaMM;enabled:false }
422                                                                  PropertyChanges { target: reelx;enabled:false }
423              StateChangeScript {
424                  script:{
425                      datetimeDialog.reset()
426                  }
427                 }
428          },
429          State {
430              name: "amdatetimedialog";
431              PropertyChanges { target: amdatetimeDialog; x: 0; focus:true}
432              PropertyChanges { target: toolBar; y: screen.height }
433              PropertyChanges { target: toolBar }
434              PropertyChanges { target: mouseareaMM;enabled:false }
435                                                                  PropertyChanges { target: reelx;enabled:false }
436              StateChangeScript {
437                  script:{
438                      amdatetimeDialog.reset()
439                  }
440                 }
441          },
442          State {
443              name: "nofeaturedialog";
444              PropertyChanges { target: nofeatureDialog; x: 0; focus:true}
445              PropertyChanges { target: toolBar; y: screen.height }
446              PropertyChanges { target: toolBar }
447              PropertyChanges { target: mouseareaMM;enabled:false }
448                                                                  PropertyChanges { target: reelx;enabled:false }
449          },
450          State {
451              name: "aboutdialog";
452              PropertyChanges { target: aboutDialog; x: 0; focus:true}
453              PropertyChanges { target: toolBar; y: screen.height }
454              PropertyChanges { target: toolBar }
455              PropertyChanges { target: mouseareaMM;enabled:false }
456          },
457          State {
458              name: "xemtheothang";
459              PropertyChanges { target: fullmonthDialog; x: 0; focus:true}
460              PropertyChanges { target: toolBar; y: screen.height }
461              PropertyChanges { target: toolBar }
462              StateChangeScript {
463                  script:{
464                      fullmonthDialog.reset()
465                  }
466                 }
467              PropertyChanges { target: mouseareaMM;enabled:false }
468          }
469      ]
470
471      transitions: [
472          Transition { NumberAnimation { properties: "x,y,opacity"; duration: 500; easing.type: Easing.InOutBack } }
473      ]
474
475
476      function lunarTietKhi(){
477          return AmLich.TIETKHI[AmLich.getSunLongitude(screen.jd+1,7.0)];
478      }
479
480
481      function calculateLunar(D,M,Y){
482          screen.curDay =D
483          screen.curMonth = M
484          screen.curYear = Y
485          screen.lunarDay = AmLich.getLunarDate(screen.curDay,screen.curMonth,screen.curYear).day
486          screen.lunarMonth = AmLich.getLunarDate(screen.curDay,screen.curMonth,screen.curYear).month
487          screen.lunarYearInt = AmLich.getLunarDate(screen.curDay,screen.curMonth,screen.curYear).year
488          screen.lunarYear = AmLich.getCanChi(AmLich.getLunarDate(screen.curDay,screen.curMonth,screen.curYear))[2]//AmLich.getYearCanChi(screen.curYear)
489          screen.jd = AmLich.getLunarDate(screen.curDay,screen.curMonth,screen.curYear).jd
490          screen.lunarGioHoangDao = AmLich.getGioHoangDao(screen.jd)
491
492          screen.lunarDayCanChi = AmLich.getCanChi(AmLich.getLunarDate(screen.curDay,screen.curMonth,screen.curYear))[0]
493          screen.lunarMonthCanChi = AmLich.getCanChi(AmLich.getLunarDate(screen.curDay,screen.curMonth,screen.curYear))[1]
494
495      }
496
497      function reset(){
498          calculateLunar(initcurDay, initcurMonth, initcurYear)
499          LC.reset(initcurDay, initcurMonth, initcurYear)
500          screen.state="mainscreen"
501          maindatemodel.reset()
502          txtDATEpv.currentIndex = initcurDay-1;
503      }
504
505      function refresh(){
506
507      }
508
509      Component.onCompleted: {
510
511      }
512 }