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