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