a3ba5aad89e6aaac54c56ed425d65b515329cc44
[lichviet] / qml / LichViet / FullMonth.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 import "../DatePicker/component" as Comp
20
21 import "amlich-hnd.js" as AmLich
22 import "amlich-aa98.js" as AmLich98
23
24 import "CacNgayLeTrongNam.js" as CNLTN
25
26 Item{
27     id: fullmonth
28
29     property int m_day: screen.curDay
30     property int m_month: screen.curMonth
31     property int m_year: screen.curYear
32
33     property Component headerRenderer:Component {
34         id: defaultHeaderRenderer
35
36         Item {
37             width: cellWidth
38             height: cellHeight
39
40             Rectangle {
41                 width: parent.width - 4
42                 height: parent.height - 4
43
44                 anchors.centerIn: parent
45
46                 radius: 4
47                 color: "#FAFAFF"
48             }
49
50             Text {
51                 width: cellWidth
52                 height: cellHeight
53
54                 verticalAlignment: Text.AlignVCenter
55                 horizontalAlignment: Text.AlignHCenter
56
57                 text: model.modelData
58                 font.bold: true
59                 font.pointSize: width ? width / 6 : 1
60             }
61         }
62     }
63
64
65     property Component daysRenderer:
66     Component {
67         Item {
68             width: cellWidth
69             height: cellHeight
70
71             Rectangle {
72                 id:recms
73                 width: parent.width - 4
74                 height: parent.height - 4
75
76                 anchors.centerIn: parent
77
78                 radius: 4
79                 color: model ? model.inmonth ? (model.current ? "blue" : "lightsteelblue") : "#AAAAAA" : "black"
80             }
81
82             Text {
83                 id:txt_solar_day
84                 anchors.fill: parent
85                 anchors.leftMargin: 10
86
87                 verticalAlignment: Text.AlignVCenter
88                 horizontalAlignment: Text.AlignLeft
89
90                 text: model ? model.day : ""
91                 color: model ? model.inmonth ? (model.current ? "red" : model.inholiday ? theme_manager.theme.markdate.holidays : "black") : "grey" : "red"
92                 font.pointSize: parent.width ? parent.width / 6 : 1
93                 font.bold: true
94             }
95
96             Text {
97                 anchors.fill: parent
98                 anchors.leftMargin: txt_solar_day.anchors.leftMargin+40
99                 anchors.topMargin: 20
100
101                 verticalAlignment: Text.AlignVCenter
102                 horizontalAlignment: Text.AlignBottom
103
104                 text: model ? model.lunar_day : ""
105                 color: model ? model.inmonth ? (model.current ? "red" : "#700070") : "grey" : "red"
106                 font.pointSize: parent.width ? parent.width / 8 : 1
107             }
108
109             MouseArea {
110                 anchors.fill: parent
111
112                 property real mX
113                 property real mY
114                 property bool hold: false
115
116                 onClicked: {
117                     LC.reset(model.day,m_month,m_year)
118                     screen.calculateLunar(model.day,m_month,m_year)
119                     screen.state="mainscreen"
120                     screen.refresh()
121                     maindatemodel.reset()
122                     txtDATEpv.currentIndex = model.day-1;
123                 }
124
125                 onPositionChanged: {
126                     mX = mouseX;
127                     mY = mouseY;
128                     hold=true;
129                 }
130
131                 onPressed: {
132                     recms.color="#F0F0F0"
133                 }
134
135                 onReleased: {
136                     if (hold){
137                         if (mouseY+mY<0){
138                            if (Math.abs(mouseY) >= 30){
139                               m_month+=1;
140                                if (m_month >=13){
141                                    m_month=1;
142                                    m_year++;
143                                }
144                            }
145                         }
146                         else
147                         {
148                            if (mouseY >= 50){
149                               m_month-=1;
150                                if(m_month<=0){
151                                    m_month=12;
152                                    m_year--;
153                                }
154                            }
155                         }
156                         reset();
157                         hold=false;
158                     }
159                 }
160             }
161         }
162     }
163
164     Rectangle{
165         id: fullmonthREC
166         anchors.fill: parent; anchors.bottomMargin: 0
167         opacity:0.9
168
169         ListModel {
170             id: monthModel
171         }
172
173         Grid {
174             id: grid
175
176             anchors.bottomMargin: -52
177             anchors.topMargin: 78
178             anchors.horizontalCenter: parent.horizontalCenter
179             anchors.bottom: parent.bottom
180             anchors.top: parent.top
181
182             columns: 7
183             rows: 7
184
185             Repeater {
186                 model: ["CN", "Hai", "Ba", "Tư", "Năm", "Sáu", "Bảy"]
187
188                 delegate: Item {
189                     id: headerDelegate
190
191                     width: fullmonthREC.width / grid.columns
192                     height: (fullmonthREC.height-92) / grid.rows
193
194                     property int theIndex: index
195                     property variant theModel: model
196
197                     Loader {
198                         property alias model: headerDelegate.theModel
199                         property alias index: headerDelegate.theIndex
200                         property alias cellWidth: headerDelegate.width
201                         property alias cellHeight: headerDelegate.height
202
203                         anchors.fill: parent
204
205                         sourceComponent: headerRenderer
206                     }
207                 }
208             }
209
210             Repeater {
211                 model: monthModel
212
213                 delegate: Item {
214                     id: daysDelegate
215
216                     property int theIndex: index
217                     property variant theModel: model
218
219                     width: fullmonthREC.width / grid.columns
220                     height: (fullmonthREC.height-92) / grid.rows
221
222                     Loader {
223                         property alias model: daysDelegate.theModel
224                         property alias index: daysDelegate.theIndex
225                         property alias cellWidth: daysDelegate.width
226                         property alias cellHeight: daysDelegate.height
227
228                         anchors.fill: parent
229
230                         sourceComponent: daysRenderer
231                     }
232
233                 }
234             }
235
236
237         }
238
239         Text {
240             id: txt_caption
241             y: 40
242             width:parent.width
243             text: "Tháng "+m_month+", "+m_year
244             horizontalAlignment: Text.AlignHCenter
245             font.pixelSize: 24
246             font.bold: true
247         }
248
249     }
250
251     ToolBarSingle { id: toolBar; height: 50;
252         y: screen.height-78
253         width: parent.width;
254         button1Label: "Quay Về"
255         onButton1Clicked:
256         {
257             screen.state="mainscreen"
258         }
259     }
260
261     function reset() {
262         monthModel.clear()
263
264         var firstdayofthemonthyear = AmLich.getLunarDate(1,m_month,m_year).jd;
265         var startfrom = (firstdayofthemonthyear+1)%7;
266         var daysofthemonthyear = LC.calDays(m_month,m_year);
267         var j=1;
268         var totalslot = 42;
269
270         var nextmonth = get_nextmonth(totalslot - (startfrom+daysofthemonthyear))
271         var prevmonth = get_prevmonth(startfrom)
272
273         var dayclick, monthclick, yearclick;
274         var datas1,datas,lunarclmonth,inmonth;
275
276         for (var i=0;i<42;i++){
277             if (i >= startfrom && j<= daysofthemonthyear){
278                 dayclick = j;
279                 monthclick = m_month;
280                 yearclick = m_year;
281                 inmonth = true;
282                 j++;
283             }else{
284                 if (i<startfrom)
285                     datas1=prevmonth[i];
286                 else
287                     datas1=nextmonth[(totalslot-i)-1];
288
289                 dayclick = datas1.duong;
290                 monthclick = datas1.month;
291                 yearclick = datas1.year;
292                 inmonth = false;
293             }
294
295             var current = false;
296             if (dayclick == screen.curDay && monthclick==screen.curMonth && yearclick==screen.curYear)
297                 current = true
298
299             var lunarcl = AmLich.getLunarDate(dayclick,monthclick,yearclick)
300             var lunar_day = lunarcl.day;
301
302             if (lunarcl.day === 1)
303                 lunar_day = lunarcl.day+"/"+lunarcl.month;
304
305             var stemp = AmLich98.convertSolar2Lunar(dayclick,monthclick,yearclick,7);
306             var inholiday = CNLTN.is_holiday(dayclick, monthclick, stemp[0], stemp[1]);
307
308             monthModel.append({"day": dayclick, "current": current, "lunar_day":lunar_day, "inmonth":inmonth,"inholiday":inholiday})
309         }
310
311     }
312
313     function get_prevmonth(startfrom){
314         var cMonth=m_month;
315         var cYear=m_year;
316         var pmonth = [];
317         if (cMonth == 1){
318             cMonth = 12
319             cYear--;
320         }else
321             cMonth--;
322          var daysofthemonthyear = LC.calDays(cMonth,cYear);
323         for (var i=daysofthemonthyear;i>daysofthemonthyear-startfrom;i--){
324             var lunarcl = AmLich.getLunarDate(i,cMonth,cYear)
325             pmonth.push({duong:i, am:lunarcl.day, month:cMonth, year:cYear})
326         }
327         return pmonth.reverse()
328     }
329
330     function get_nextmonth(startfrom){
331         var cMonth=m_month;
332         var cYear=m_year;
333          var pmonth = [];
334         if (cMonth==12){
335             cMonth = 1;
336             cYear++;
337         }else
338             cMonth++;
339
340         for (var i=1;i<=startfrom;i++){
341           var lunarcl = AmLich.getLunarDate(i,cMonth,cYear)
342             pmonth.push({duong:i, am:lunarcl.day, month:cMonth, year:cYear});
343         }
344            return pmonth.reverse();
345     }
346
347 }