* Theme now is simple and very fast
[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     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                          //console.log(lunaryear_pathview.currentIndex)
231                          Script.getYearInfo(lunaryear_pathview.currentIndex+1900);
232                          lunarmonth_model.change(lunaryear_pathview.currentIndex+1900, lunarmonth_pathview.currentIndex+1);
233                      }
234                 }
235                 MouseArea {
236                     id:lunaryear_mousearea
237                     anchors.fill: parent
238                     onClicked: {
239                         rec22.state="switch_to_year"
240                     }
241                 }
242             }
243
244             ListModel {
245                 id:lunaryear_model
246                 Component.onCompleted: {
247                     //init()
248                  }
249
250                 function init(){
251                     for(var i=1900;i<2099;i++){
252                         var lunarYear = Script.getYearCanChi(i)
253                         append({name:i+" ("+lunarYear+")"})
254                     }
255                 }
256
257                 function change(YYYY){
258                     lunaryear_pathview.currentIndex=-1;
259                     lunaryear_pathview.currentIndex = YYYY - 1900
260                 }
261             }
262             /***/
263
264             /**LUNAR DAY*/
265                 Rectangle{
266                     state:"switch_to_day"
267                     id:lunarday_rec
268                     x: -20
269                     y: -39
270                     width: 80; height: 129
271                     color:"transparent"
272
273                     property int index: 0
274                     onIndexChanged: lunarday_pathview.currentIndex = lunarday_rec.index
275
276                     Component {
277                         id: lunarday_delegate
278                         Column {
279                             id: wrapper
280
281                             Text {
282                                 id: nameText
283                                 text: name
284                                 font.pointSize: 18
285                                 width: lunarday_rec.width
286                                 color: PathView.isCurrentItem ? "#FFFFFF" : "grey"
287                                 verticalAlignment:Text.AlignVCenter
288                                 font.bold: true
289                             }
290
291                         }
292                     }
293
294                     PathView {
295                         id:lunarday_pathview
296                         anchors.fill: parent
297                         model: lunarday_model
298                         delegate: lunarday_delegate
299                          pathItemCount: 1
300                          clip: true
301                          preferredHighlightBegin: 0.5
302                          preferredHighlightEnd: 0.5
303                          highlightRangeMode: PathView.StrictlyEnforceRange
304                          focus: true
305                          path: Path {
306                              startX: 90
307                              startY: 0
308                              PathLine {x: 90; y: lunarday_rec.height}
309                          }
310                     }
311
312                     MouseArea {
313                         id:lunarday_mousearea
314                         anchors.fill: parent
315                         onClicked: {
316                             rec22.state="switch_to_day"
317                         }
318                     }
319
320                 }
321
322                 ListModel {
323                     id:lunarday_model
324                     Component.onCompleted: {
325
326                     }
327
328                     function init(){
329                         for(var i=1;i<=30;i++)
330                             lunarday_model.append({name:i})
331                     }
332
333                     function change(MM,DD){
334
335                         while(lunarday_model.count>0){
336                             lunarday_model.remove(lunarday_model.count-1)
337                         }
338
339
340                         var daystotal = Script.RMM[MM-1];
341
342                         if(lunarmonth_model.get(MM-1).nhuan){
343
344                                 for (var i=1;i<=Script.LML;i++)
345                                      lunarday_model.append({name:i})
346
347                         }else{
348                                     for (var i=1;i<=daystotal;i++)
349                                          lunarday_model.append({name:i})
350
351                         }
352
353                         /*if(lunarmonth_model.get(MM-1).nhuan){
354                             if(AmLich.LML > lunarday_model.count){
355                                 for (var i=lunarday_model.count+1;i<=AmLich.LML;i++)
356                                      lunarday_model.append({name:i})
357                             }else
358                                 if(AmLich.LML< lunarday_model.count){
359                                 while(lunarday_model.count>AmLich.LML){
360                                      lunarday_model.remove(lunarday_model.count-1);
361                                 }
362                            }
363
364                         }else{
365                                 if(daystotal > lunarday_model.count){
366                                     for (var i=lunarday_model.count+1;i<=daystotal;i++)
367                                          lunarday_model.append({name:i})
368                                 }else
369                                     if(daystotal < lunarday_model.count){
370                                     while(lunarday_model.count>daystotal){
371                                          lunarday_model.remove(lunarday_model.count-1);
372                                     }
373                                }
374                         }*/
375
376                         /*
377                         var nhuansetru = 30 - AmLich.LML;
378
379                         if(lunarmonth_model.get(MM-1).nhuan && lunarday_model.count==30){
380                             var j=0;
381                             while(j<nhuansetru){
382                                 lunarday_model.remove(lunarday_model.count-1);
383                                 j++;
384                             }
385                         }
386                         else if(lunarday_model.count<30){
387                             for (var i=lunarday_model.count+1;i<=30;i++)
388                             lunarday_model.append({name:i})
389                         }*/
390
391                         lunarday_pathview.currentIndex=-1;
392                         lunarday_pathview.currentIndex= DD-1;
393
394                     }
395                 }
396
397             /***/
398
399
400                 /**LUNAR MONTH*/
401                     Rectangle{
402                         state:"switch_to_month"
403                         id:lunarmonth_rec
404                         x: 60
405                         y: -39
406                         width: 150; height: 129
407                         color:"transparent"
408
409                         property int index: 0
410                         onIndexChanged: lunarmonth_pathview.currentIndex = lunarmonth_rec.index
411
412                         Component {
413                             id: lunarmonth_delegate
414                             Column {
415                                 id: wrapper
416
417                                 Text {
418                                     id: nameText
419                                     text: name
420                                     font.pointSize: 16
421                                     width: lunarmonth_rec.width
422                                     color: PathView.isCurrentItem ? "#FFFFFF" : "grey"
423                                     verticalAlignment:Text.AlignVCenter
424                                     font.bold: true
425                                 }
426
427                             }
428                         }
429
430                         PathView {
431                             id:lunarmonth_pathview
432                             anchors.fill: parent
433                             model: lunarmonth_model
434                             delegate: lunarmonth_delegate
435                              pathItemCount: 1
436                              clip: true
437                              preferredHighlightBegin: 0.5
438                              preferredHighlightEnd: 0.5
439                              highlightRangeMode: PathView.StrictlyEnforceRange
440                              focus: true
441                              path: Path {
442                                  startX: 90
443                                  startY: 0
444                                  PathLine {x: 90; y: lunarmonth_rec.height}
445                              }
446                              onMovementEnded: {
447                                  //console.log(lunaryear_pathview.currentIndex)
448                                 // console.log(AmLich.LML)
449
450                                 lunarday_model.change(lunarmonth_pathview.currentIndex+1,lunarday_pathview.currentIndex+1);
451                              }
452                         }
453
454                         MouseArea {
455                             id:lunarmonth_mousearea
456                             anchors.fill: parent
457                             onClicked: {
458                                 rec22.state="switch_to_month"
459                             }
460                         }
461
462                     }
463
464                     ListModel {
465                         id:lunarmonth_model
466
467                         Component.onCompleted: {
468
469                         }
470
471                         function change(YYYY, MM){
472                             while(lunarmonth_model.count>0){
473                                 lunarmonth_model.remove(lunarmonth_model.count-1)
474                             }
475                             readd(YYYY, MM)
476
477                            lunarday_model.change(lunarmonth_pathview.currentIndex+1,lunarday_pathview.currentIndex+1);
478                         }
479
480                         function readd(YYYY, MM){
481                             var yTest = YYYY;
482                             var preFix = "Tháng ";
483                             var a=Script.leapMonth(yTest)
484                             var t=0;
485                             if(a){ // nam nhuan
486                                  for(var i=0;i<=12;i++){
487                                     var l=Script.getYearInfo(yTest)[i].month
488                                      if (t==l)
489                                          lunarmonth_model.append({name:preFix+l+" (N)", nhuan:true})
490                                      else
491                                          lunarmonth_model.append({name:preFix+l, nhuan:false})
492                                      t=l
493                                  }
494
495                             }else
496                                 {
497                                 for(var i=1;i<=12;i++)
498                                     lunarmonth_model.append({name:preFix+i, nhuan:false})
499                             }
500
501                             //lunarmonth_rec.index = MM -1;
502                             lunarmonth_pathview.currentIndex=-1;
503                             lunarmonth_pathview.currentIndex = MM-1;
504
505                         }
506                     }
507
508                 /***/
509         }
510
511     }
512
513     Component.onCompleted: {
514         reset();
515     }
516
517     states: [
518         State {
519             name: "show"
520             AnchorChanges { target: amdatetime; anchors.right: parent.right }
521         },
522         State {
523             name: "close"
524             AnchorChanges { target: amdatetime; anchors.right: parent.left }
525         }
526     ]
527
528     function reset(){
529         var acs = Script.getYearInfo(Script.curYear)
530         var a=Script.getLunarDate(Script.curDay,Script.curMonth,Script.curYear)
531                 var finalmonth=a.month;
532         if (a.leap)
533             finalmonth+=1;
534         else{
535         var aaa=Script.leapMonth(Script.curYear)
536         var t=-1;
537
538         if (aaa){
539            for (var i=0;i<=12;i++){
540                var l=acs[i].month
541                 if (t==l && a.leap===0){
542                     if (a.month>i){
543                     finalmonth = finalmonth+1;
544                     break;
545                     }
546                 }
547                 t=l
548             }
549
550         }
551         }
552
553         lunaryear_model.init()
554         lunarday_model.init()
555         lunaryear_model.change(a.year)
556         lunarmonth_model.change(a.year, finalmonth)
557         lunarday_model.change(finalmonth,a.day)
558
559     }
560
561     transitions: Transition {
562         AnchorAnimation { easing.type: Easing.OutQuart; duration: 300 }
563     }
564 }