Added more information to the lunar calendar
[lichviet] / qml / LichViet / AmDateTimeDialog.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 Item{
23     id: amdatetime
24     state: "switch_me"
25
26     Rectangle {
27         id: rectangle1
28         x: 0
29         y: -16
30         anchors.centerIn: parent
31         width:460
32         height: 234
33         color: "#19262b"
34         radius: 8
35         border.color: "#b57e8f"
36         visible: true
37         opacity:0.9
38
39         Button {
40             id: button2
41             x: 313
42             anchors.right: parent.right; anchors.rightMargin: 8; y: 185; width: 140; height: 40
43             text: "OK"
44             onClicked: {
45                  var lday = lunarday_pathview.currentIndex+1;
46                  var lmonth=lunarmonth_pathview.currentIndex+1;
47                  var lyear=lunaryear_pathview.currentIndex+1900;
48
49                 var lnhuan=0;
50
51                 var thangnhuan = -1;
52
53                 for (var l=0;l<12;l++)
54                     if (lunarmonth_model.get(l).nhuan){
55                         thangnhuan = l;
56                     }
57
58                  if(lunarmonth_model.get(lunarmonth_pathview.currentIndex).nhuan){
59                      lnhuan=1;
60                      lmonth=lmonth-1;
61                  }
62
63                  if (thangnhuan!=-1 && lunarmonth_pathview.currentIndex> thangnhuan)
64                      lmonth-=1;
65
66                 var finalSolarDay = AmLich98.convertLunar2Solar(lday,lmonth,lyear,lnhuan,7)
67
68                  LC.reset(finalSolarDay[0],finalSolarDay[1],finalSolarDay[2])
69                  screen.calculateLunar( finalSolarDay[0], finalSolarDay[1], finalSolarDay[2])
70                  screen.state="mainscreen"
71                  screen.refresh()
72
73                  maindatemodel.reset()
74                  txtDATEpv.currentIndex =  finalSolarDay[0]-1;
75
76             }
77         }
78
79         Button {
80             id: button3
81             x: 413
82             y: 6
83             width: 39
84             height: 40
85             text: "X"
86             anchors.rightMargin: 56
87             onClicked: {screen.state="mainscreen"}
88         }
89
90         Rectangle {
91             id: rectangle2
92             x: 23
93             y: 89
94             width: 415
95             height: 56
96             radius: 9
97             gradient: Gradient {
98                 GradientStop {
99                     position: 0
100                     color: "#ffffff"
101                 }
102
103                 GradientStop {
104                     position: 0.1
105                     color: "#7c6969"
106                 }
107             }
108             border.color: "#b57e8f"
109
110             Rectangle {
111                 id:lmk
112                 x: 207
113                 y: -39
114                 width: 198; height: 129
115                 gradient: Gradient {
116                     GradientStop {
117                         position: 0.89
118                         color: "#148ebe"
119                     }
120
121                     GradientStop {
122                         position: 0.01
123                         color: "#19262b"
124                     }
125
126                     GradientStop {
127                         position: 0.13
128                         color: "#6eb3d5"
129                     }
130
131                     GradientStop {
132                         position: 1
133                         color: "#19262b"
134                     }
135                 }
136             }
137
138             /**LUNAR YEAR**/
139             Rectangle {
140                 id:lunaryear_rec
141                 state: "switch_to_year"
142                 x: 217
143                 y: -39
144                 width: 178; height: 129
145                 color:"transparent"
146
147                 property int index: 0
148                 onIndexChanged: lunaryear_pathview.currentIndex = lunaryear_rec.index
149
150                 Component {
151                     id: lunaryear_delegate
152                     Column {
153                         id: wrapper
154                         Text {
155                             id: nameText
156                             text: name
157                             font.pointSize: 16
158                             width: lunaryear_rec.width
159                             color: PathView.isCurrentItem ? "#FFFFFF" : "grey"
160                             verticalAlignment:Text.AlignLeft
161                             font.bold: true
162                         }
163                     }
164                 }
165
166                 PathView {
167                     id:lunaryear_pathview
168                     anchors.fill: parent
169                     model: lunaryear_model
170                     delegate: lunaryear_delegate
171                      pathItemCount: 3
172                      clip: true
173                      preferredHighlightBegin: 0.5
174                      preferredHighlightEnd: 0.5
175                      highlightRangeMode: PathView.StrictlyEnforceRange
176                      focus: true
177                      path: Path {
178                          startX: 90
179                          startY: 0
180                          PathLine {x: 90; y: lunaryear_rec.height}
181                      }
182
183                      onMovementEnded: {
184                          //console.log(lunaryear_pathview.currentIndex)
185                          AmLich.getYearInfo(lunaryear_pathview.currentIndex+1900);
186                          lunarmonth_model.change(lunaryear_pathview.currentIndex+1900, lunarmonth_pathview.currentIndex+1);
187                      }
188                 }
189                 MouseArea {
190                     id:lunaryear_mousearea
191                     anchors.fill: parent
192                     onClicked: {
193                         amdatetime.state="switch_to_year"
194                     }
195                 }
196             }
197
198             ListModel {
199                 id:lunaryear_model
200                 Component.onCompleted: {
201                     init()
202                  }
203
204                 function init(){
205                     for(var i=1900;i<2099;i++){
206                         var lunarYear = AmLich.getYearCanChi(i)
207                         append({name:i+" ("+lunarYear+")"})
208                     }
209                 }
210
211                 function change(YYYY){
212                     lunaryear_pathview.currentIndex = YYYY - 1900
213                 }
214             }
215             /***/
216
217             /**LUNAR DAY*/
218                 Rectangle{
219                     state:"switch_to_day"
220                     id:lunarday_rec
221                     x: -20
222                     y: -39
223                     width: 80; height: 129
224                     color:"transparent"
225
226                     property int index: 0
227                     onIndexChanged: lunarday_pathview.currentIndex = lunarday_rec.index
228
229                     Component {
230                         id: lunarday_delegate
231                         Column {
232                             id: wrapper
233
234                             Text {
235                                 id: nameText
236                                 text: name
237                                 font.pointSize: 18
238                                 width: lunarday_rec.width
239                                 color: PathView.isCurrentItem ? "#FFFFFF" : "grey"
240                                 verticalAlignment:Text.AlignVCenter
241                                 font.bold: true
242                             }
243
244                         }
245                     }
246
247                     PathView {
248                         id:lunarday_pathview
249                         anchors.fill: parent
250                         model: lunarday_model
251                         delegate: lunarday_delegate
252                          pathItemCount: 1
253                          clip: true
254                          preferredHighlightBegin: 0.5
255                          preferredHighlightEnd: 0.5
256                          highlightRangeMode: PathView.StrictlyEnforceRange
257                          focus: true
258                          path: Path {
259                              startX: 90
260                              startY: 0
261                              PathLine {x: 90; y: lunarday_rec.height}
262                          }
263                     }
264
265                     MouseArea {
266                         id:lunarday_mousearea
267                         anchors.fill: parent
268                         onClicked: {
269                             amdatetime.state="switch_to_day"
270                         }
271                     }
272
273                 }
274
275                 ListModel {
276                     id:lunarday_model
277                     Component.onCompleted: {
278                         for(var i=1;i<=30;i++)
279                             append({name:i})
280                     }
281
282                     function change(MM,DD){
283
284                         while(lunarday_model.count>0){
285                             lunarday_model.remove(lunarday_model.count-1)
286                         }
287
288
289                         var daystotal = AmLich.RMM[MM-1];
290
291                         if(lunarmonth_model.get(MM-1).nhuan){
292
293                                 for (var i=1;i<=AmLich.LML;i++)
294                                      lunarday_model.append({name:i})
295
296                         }else{
297                                     for (var i=1;i<=daystotal;i++)
298                                          lunarday_model.append({name:i})
299
300                         }
301
302                         /*if(lunarmonth_model.get(MM-1).nhuan){
303                             if(AmLich.LML > lunarday_model.count){
304                                 for (var i=lunarday_model.count+1;i<=AmLich.LML;i++)
305                                      lunarday_model.append({name:i})
306                             }else
307                                 if(AmLich.LML< lunarday_model.count){
308                                 while(lunarday_model.count>AmLich.LML){
309                                      lunarday_model.remove(lunarday_model.count-1);
310                                 }
311                            }
312
313                         }else{
314                                 if(daystotal > lunarday_model.count){
315                                     for (var i=lunarday_model.count+1;i<=daystotal;i++)
316                                          lunarday_model.append({name:i})
317                                 }else
318                                     if(daystotal < lunarday_model.count){
319                                     while(lunarday_model.count>daystotal){
320                                          lunarday_model.remove(lunarday_model.count-1);
321                                     }
322                                }
323                         }*/
324
325                         /*
326                         var nhuansetru = 30 - AmLich.LML;
327
328                         if(lunarmonth_model.get(MM-1).nhuan && lunarday_model.count==30){
329                             var j=0;
330                             while(j<nhuansetru){
331                                 lunarday_model.remove(lunarday_model.count-1);
332                                 j++;
333                             }
334                         }
335                         else if(lunarday_model.count<30){
336                             for (var i=lunarday_model.count+1;i<=30;i++)
337                             lunarday_model.append({name:i})
338                         }*/
339
340                         lunarday_rec.index = DD-1;
341                         lunarday_pathview.currentIndex= DD-1;
342
343                     }
344                 }
345
346             /***/
347
348
349                 /**LUNAR MONTH*/
350                     Rectangle{
351                         state:"switch_to_month"
352                         id:lunarmonth_rec
353                         x: 60
354                         y: -39
355                         width: 150; height: 129
356                         color:"transparent"
357
358                         property int index: 0
359                         onIndexChanged: lunarmonth_pathview.currentIndex = lunarmonth_rec.index
360
361                         Component {
362                             id: lunarmonth_delegate
363                             Column {
364                                 id: wrapper
365
366                                 Text {
367                                     id: nameText
368                                     text: name
369                                     font.pointSize: 16
370                                     width: lunarmonth_rec.width
371                                     color: PathView.isCurrentItem ? "#FFFFFF" : "grey"
372                                     verticalAlignment:Text.AlignVCenter
373                                     font.bold: true
374                                 }
375
376                             }
377                         }
378
379                         PathView {
380                             id:lunarmonth_pathview
381                             anchors.fill: parent
382                             model: lunarmonth_model
383                             delegate: lunarmonth_delegate
384                              pathItemCount: 1
385                              clip: true
386                              preferredHighlightBegin: 0.5
387                              preferredHighlightEnd: 0.5
388                              highlightRangeMode: PathView.StrictlyEnforceRange
389                              focus: true
390                              path: Path {
391                                  startX: 90
392                                  startY: 0
393                                  PathLine {x: 90; y: lunarmonth_rec.height}
394                              }
395                              onMovementEnded: {
396                                  //console.log(lunaryear_pathview.currentIndex)
397                                 // console.log(AmLich.LML)
398
399                                 lunarday_model.change(lunarmonth_pathview.currentIndex+1,lunarday_pathview.currentIndex+1);
400                              }
401                         }
402
403                         MouseArea {
404                             id:lunarmonth_mousearea
405                             anchors.fill: parent
406                             onClicked: {
407                                 amdatetime.state="switch_to_month"
408                             }
409                         }
410
411                     }
412
413                     ListModel {
414                         id:lunarmonth_model
415
416                         Component.onCompleted: {
417
418                         }
419
420                         function change(YYYY, MM){
421                             while(lunarmonth_model.count>0){
422                                 lunarmonth_model.remove(lunarmonth_model.count-1)
423                             }
424                             readd(YYYY, MM)
425
426                            lunarday_model.change(lunarmonth_pathview.currentIndex+1,lunarday_pathview.currentIndex+1);
427                         }
428
429                         function readd(YYYY, MM){
430                             var yTest = YYYY;
431                             var preFix = "Tháng ";
432                             var a=AmLich.leapMonth(yTest)
433                             var t=0;
434                             if(a){ // nam nhuan
435                                  for(var i=0;i<=12;i++){
436                                     var l=AmLich.getYearInfo(yTest)[i].month
437                                      if (t==l)
438                                          lunarmonth_model.append({name:preFix+l+" (N)", nhuan:true})
439                                      else
440                                          lunarmonth_model.append({name:preFix+l, nhuan:false})
441                                      t=l
442                                  }
443
444                             }else
445                                 {
446                                 for(var i=1;i<=12;i++)
447                                     lunarmonth_model.append({name:preFix+i, nhuan:false})
448                             }
449
450                             lunarmonth_rec.index = MM -1;
451                             lunarmonth_pathview.currentIndex = MM-1;
452
453                         }
454                     }
455
456                 /***/
457         }
458
459     }
460
461     Component.onCompleted: {
462
463     }
464
465     states: [
466         State {
467             name: "switch_to_day";
468             PropertyChanges { target:lmk;x:10;width:60;}
469             PropertyChanges { target:lunaryear_pathview;pathItemCount:1;}
470                                     PropertyChanges { target:lunarmonth_pathview;pathItemCount:1;}
471             PropertyChanges { target:lunarday_pathview;pathItemCount:3;}
472             PropertyChanges { target:lunarday_mousearea;visible:false;}
473             PropertyChanges { target:lunaryear_mousearea;visible:true;}
474             PropertyChanges { target:lunarmonth_mousearea;visible:true;}
475         },
476         State {
477             name: "switch_to_year";
478             PropertyChanges { target:lmk;x:207;width:198;}
479             PropertyChanges { target:lunarday_pathview;pathItemCount:1;}
480                                     PropertyChanges { target:lunarmonth_pathview;pathItemCount:1;}
481             PropertyChanges { target:lunaryear_pathview;pathItemCount:3;}
482             PropertyChanges { target:lunarday_mousearea;visible:true;}
483             PropertyChanges { target:lunaryear_mousearea;visible:false;}
484                         PropertyChanges { target:lunarmonth_mousearea;visible:true;}
485         },
486         State {
487             name: "switch_to_month";
488             PropertyChanges { target:lmk;x:60;width:150;}
489             PropertyChanges { target:lunarday_pathview;pathItemCount:1;}
490             PropertyChanges { target:lunaryear_pathview;pathItemCount:1;}
491                         PropertyChanges { target:lunarmonth_pathview;pathItemCount:3;}
492             PropertyChanges { target:lunarday_mousearea;visible:true;}
493             PropertyChanges { target:lunaryear_mousearea;visible:true;}
494                         PropertyChanges { target:lunarmonth_mousearea;visible:false;}
495         }
496     ]
497
498     transitions: [
499         Transition { NumberAnimation { properties: "x,y,opacity"; duration: 100; easing.type: Easing.InOutBack } }
500     ]
501
502     function reset(){
503         var acs = AmLich.getYearInfo(screen.curYear)
504         var a=AmLich.getLunarDate(screen.curDay,screen.curMonth,screen.curYear)
505                 var finalmonth=a.month;
506         if (a.leap)
507             finalmonth+=1;
508         else{
509         var aaa=AmLich.leapMonth(screen.curYear)
510         var t=-1;
511
512         if (aaa){
513            for (var i=0;i<=12;i++){
514                var l=acs[i].month
515                 if (t==l && a.leap==0){
516                     if (a.month>i){
517                     finalmonth = finalmonth+1;
518                     break;
519                     }
520                 }
521                 t=l
522             }
523
524         }
525         }
526
527         lunaryear_model.change(a.year)
528         lunarmonth_model.change(a.year, finalmonth)
529         lunarday_model.change(finalmonth,a.day)
530
531     }
532 }