* fixes for next/prev month calculator
[lichviet] / qml / LichViet / main.js
1 .pragma library
2
3 Qt.include("amlich-hnd.js");
4 Qt.include("amlich-aa98.js");
5 Qt.include("Settings.js");
6 Qt.include("Themes.js");
7 Qt.include("CacNgayLeTrongNam.js");
8 Qt.include("xemngaytotxau.js");
9
10 var localdate = new Date();
11
12 var curDay=localdate.getDate();
13 var curMonth=localdate.getMonth()+1;
14 var curYear=localdate.getFullYear();
15
16 var initcurDay;
17 var initcurMonth;
18 var initcurYear;
19
20 var lunarDay = getLunarDate(curDay,curMonth,curYear).day;
21 var lunarMonth = getLunarDate(curDay,curMonth,curYear).month;
22 var lunarYear = getCanChi(getLunarDate(curDay,curMonth,curYear))[2];
23 var lunarYearInt = getLunarDate(curDay,curMonth,curYear).year;
24 var jd = getLunarDate(curDay,curMonth,curYear).jd;
25 var lunarGioHoangDao = getGioHoangDao(jd);
26
27 var lunarDayCanChi = getCanChi(getLunarDate(curDay,curMonth,curYear))[0];
28 var lunarMonthCanChi = getCanChi(getLunarDate(curDay,curMonth,curYear))[1];
29
30 var day_info = get(curDay,curMonth, curYear, lunarDay, lunarMonth);
31 var isholiday = is_holiday(curDay,curMonth, curYear, lunarDay, lunarMonth);
32
33 var datetimedlg;
34 var amdatetimedlg;
35 var fullmonth;
36
37 function init(){
38     initcurDay = curDay;
39     initcurMonth = curMonth;
40     initcurYear = curYear;
41 }
42
43 function day_reset(){
44     curDay = initcurDay;
45 }
46
47 function calDays(){
48     if (curMonth == 4 || curMonth == 6 || curMonth == 9 || curMonth == 11)
49         return 30;
50     else
51         if (curMonth == 2){
52             var isLeapYear = (curYear % 4 === 0 && curYear % 100 !== 0) || (curYear % 400 ===0);
53             if (isLeapYear)
54                 return 29;
55             else
56                 return 28;
57         }
58     return 31;
59 }
60
61 function calDaysX(month, year){
62     if (month == 4 || month == 6 || month == 9 || month == 11)
63         return 30;
64     else
65         if (month == 2){
66             var isLeapYear = (year % 4 === 0 && year % 100 !== 0) || (year % 400 ===0);
67             if (isLeapYear)
68                 return 29;
69             else
70                 return 28;
71         }
72     else
73     return 31;
74 }
75
76 function thu(){
77     return getTHU(jd);
78 }
79
80 function lunarTietKhi(){
81     return TIETKHI[getSunLongitude(jd+1,7.0)];
82 }
83
84 function reset_to_default_day(txtTHU, txtInfo, txtDAYAmLich){
85     localdate = new Date();
86
87     curDay=localdate.getDate();
88     curMonth=localdate.getMonth()+1;
89     curYear=localdate.getFullYear();
90
91     changeDay(curDay, txtTHU, txtInfo, txtDAYAmLich);
92 }
93
94 function changeDay(newday, txtTHU, txtInfo, txtDAYAmLich){
95     curDay = newday;
96     recalculate();
97
98     txtTHU.text=__txtTHU_format();
99     txtInfo.text=__txtInfo_format();
100     txtDAYAmLich.text=__txtDAYAmLich_format();
101 }
102
103 function createDateTimeDialog(parent){
104     datetimedlg = createObject("DateTimeDialog.qml", parent);
105     amdatetimedlg = createObject("AmDateTimeDialog.qml", parent);
106     fullmonth = createObject("FullMonth.qml", parent);
107 }
108
109 function __txtTHU_format(){
110     return getTHU(jd);
111 }
112
113 function __txtInfo_format(){
114     return day_info;
115 }
116
117 function __txtDAYAmLich_format(){
118     return "Ngày <b>"+lunarDay+" ("+lunarDayCanChi+")</b><br>Tháng <b>"+lunarMonth+" ("+lunarMonthCanChi+")</b><br>Năm <b>"+lunarYearInt+" ("+lunarYear+")</b>";
119 }
120
121 function recalculate(){
122     lunarDay = getLunarDate(curDay,curMonth,curYear).day;
123     lunarMonth = getLunarDate(curDay,curMonth,curYear).month;
124     lunarYear = getCanChi(getLunarDate(curDay,curMonth,curYear))[2];
125     lunarYearInt = getLunarDate(curDay,curMonth,curYear).year;
126     jd = getLunarDate(curDay,curMonth,curYear).jd;
127     lunarGioHoangDao = getGioHoangDao(jd);
128
129     lunarDayCanChi = getCanChi(getLunarDate(curDay,curMonth,curYear))[0];
130     lunarMonthCanChi = getCanChi(getLunarDate(curDay,curMonth,curYear))[1];
131
132     day_info = get(curDay,curMonth, lunarDay, lunarMonth);
133     isholiday = is_holiday(curDay,curMonth, curYear, lunarDay, lunarMonth);
134 }
135
136 function createObject(sourceFile, parentObject) {
137     var component = Qt.createComponent(sourceFile);
138     var guiObject = component.createObject(parentObject);
139
140     if (guiObject === null) {
141         console.log("Error creating object");
142     }
143     else {
144         return guiObject
145     }
146 }