* ready to release new version 1.0.3
[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.1
19
20 import "Themes.js" as Themes
21 import "main.js" as Script
22
23 Item{
24     id: fullmonth
25
26     anchors.verticalCenter: parent.verticalCenter
27     width: parent.width; height: parent.height;
28     x:-(parent.width * 1.5)
29
30     signal toolbar
31     signal updatedate(int day, int month, int year)
32
33     Loader {
34         id: theme_manager
35         property alias theme: theme_manager.item
36         source: Themes.default_theme()
37     }
38
39
40     Component.onCompleted: {
41         reset();
42     }
43
44     property int m_day: Script.curDay
45     property int m_month: Script.curMonth
46     property int m_year: Script.curYear
47
48     property Component headerRenderer:Component {
49         id: defaultHeaderRenderer
50
51         Item {
52             width: cellWidth
53             height: cellHeight
54
55             Rectangle {
56                 width: parent.width - 4
57                 height: parent.height - 4
58
59                 anchors.centerIn: parent
60
61                 radius: 4
62                 color: "#FAFAFF"
63             }
64
65             Text {
66                 width: cellWidth
67                 height: cellHeight
68
69                 verticalAlignment: Text.AlignVCenter
70                 horizontalAlignment: Text.AlignHCenter
71
72                 text: model.modelData
73                 font.bold: true
74                 font.pointSize: parent.width > parent.height ? width ? width / 6 : 1 : width / 4
75             }
76         }
77     }
78
79
80     property Component daysRenderer:
81     Component {
82         Item {
83             width: cellWidth
84             height: cellHeight
85
86             Rectangle {
87                 id:recms
88                 width: parent.width - 4
89                 height: parent.height - 4
90
91                 anchors.centerIn: parent
92
93                 radius: 4
94                 color: model ? model.inmonth ? (model.current ? "blue" : "lightsteelblue") : "#AAAAAA" : "black"
95             }
96
97             Text {
98                 id:txt_solar_day
99                 anchors.fill: parent
100                 anchors.leftMargin: parent.width > parent.height ? 10 : 5
101
102                 verticalAlignment: Text.AlignVCenter
103                 horizontalAlignment: Text.AlignLeft
104
105                 text: model ? model.day : ""
106                 color: model ? model.inmonth ? (model.current ? "grey" : model.inholiday ? theme_manager.theme.markdate.holidays : "black") : "grey" : "red"
107                 font.pointSize: parent.width > parent.height ? parent.width ? parent.width / 6 : 1 : parent.width / 4
108                 font.bold: true
109             }
110
111             Text {
112                 anchors.fill: parent
113                 anchors.leftMargin: txt_solar_day.anchors.leftMargin+ parent.width > parent.height ? 60 : 35
114                 anchors.topMargin: 20
115
116                 verticalAlignment: Text.AlignVCenter
117                 horizontalAlignment: Text.AlignBottom
118
119                 text: model ? model.lunar_day : ""
120                 color: model ? model.inmonth ? (model.current ? "white" : "#700070") : "grey" : "red"
121                 font.pointSize: parent.width > parent.height ? parent.width ? parent.width / 8 : 1 : parent.width / 5
122             }
123
124             MouseArea {
125                 id:memouse
126                 anchors.fill: parent
127
128                 property real mX
129                 property real mY
130                 property bool hold: false
131                 property bool clicked: false
132
133                 onClicked: {
134                     if (model.inmonth){
135                         fullmonth.updatedate(model.day, m_month, m_year)
136                         hold=false;
137                         m_day = model.day;
138                         fullmonth.state="close";
139                         fullmonth.toolbar();
140
141                     }else{
142                         if (model.isprevmonth){
143                             m_month--;
144                             if(m_month<=0){
145                                 m_month=12;
146                                 m_year--;
147                             }
148                         }else{
149                             m_month++;
150                             if (m_month >=13){
151                                 m_month=1;
152                                 m_year++;
153                             }
154                         }
155                         fullmonth.reset();
156                     }
157                 }
158
159                 onPositionChanged: {
160                     mX = mouseX;
161                     mY = mouseY;
162                     hold=true;
163                     clicked=false;
164                 }
165
166                 onPressed: {
167                     recms.color="#F0F0F0"
168                     clicked=true;
169                 }
170
171                 onReleased: {
172                     recms.color=model ? model.inmonth ? (model.current ? "blue" : "lightsteelblue") : "#AAAAAA" : "black";
173                     if (hold){
174                         if (mouseY+mY<0){
175                            if (Math.abs(mouseY) >= 100){
176                               m_month++;
177                                if (m_month >=13){
178                                    m_month=1;
179                                    m_year++;
180                                }
181                                reset();
182                            }
183                         }
184                         else
185                         {
186                            if (mouseY >= 100){
187                               m_month--;
188                                if(m_month<=0){
189                                    m_month=12;
190                                    m_year--;
191                                }
192                                reset();
193                            }
194                         }
195                         hold=false;
196                     }
197                 }
198             }
199         }
200     }
201
202     Rectangle{
203         id: fullmonthREC
204         anchors.fill: parent; anchors.bottomMargin: 0
205         opacity:0.9
206
207         ListModel {
208             id: monthModel
209         }
210
211         Grid {
212             id: grid
213
214             anchors.topMargin: 40
215             anchors.horizontalCenter: parent.horizontalCenter
216             anchors.bottom: parent.bottom
217             anchors.top: parent.top
218
219             columns: 7
220             rows: 7
221
222             Repeater {
223                 model: ["CN", "Hai", "Ba", "Tư", "Năm", "Sáu", "Bảy"]
224
225                 delegate: Item {
226                     id: headerDelegate
227
228                     width: fullmonthREC.width / grid.columns
229                     height: (fullmonthREC.height-92) / grid.rows
230
231                     property int theIndex: index
232                     property variant theModel: model
233
234                     Loader {
235                         property alias model: headerDelegate.theModel
236                         property alias index: headerDelegate.theIndex
237                         property alias cellWidth: headerDelegate.width
238                         property alias cellHeight: headerDelegate.height
239
240                         anchors.fill: parent
241
242                         sourceComponent: headerRenderer
243                     }
244                 }
245             }
246
247             Repeater {
248                 model: monthModel
249
250                 delegate: Item {
251                     id: daysDelegate
252
253                     property int theIndex: index
254                     property variant theModel: model
255
256                     width: fullmonthREC.width / grid.columns
257                     height: (fullmonthREC.height-92) / grid.rows
258
259                     Loader {
260                         property alias model: daysDelegate.theModel
261                         property alias index: daysDelegate.theIndex
262                         property alias cellWidth: daysDelegate.width
263                         property alias cellHeight: daysDelegate.height
264
265                         anchors.fill: parent
266
267                         sourceComponent: daysRenderer
268                     }
269
270                 }
271             }
272
273
274         }
275
276     }
277
278     Rectangle {
279         width : fullmonth.width
280         height: 45
281         color:"white"
282         opacity: 0.5
283     }
284
285     Text {
286         id: title
287
288         anchors { horizontalCenter: fullmonth.horizontalCenter; top: fullmonth.top; topMargin: 10 }
289         font.pixelSize: 22
290         color: "black"
291         text: "Tháng "+fullmonth.m_month+", "+fullmonth.m_year
292         smooth: true
293         font.bold: true
294     }
295
296     ToolBarSingle {
297         id: toolBar; height: 42;
298         y: parent.height-52
299         width: parent.width;
300         button1Label: "Quay Về"
301         onButton1Clicked:{
302             fullmonth.state="close";
303             fullmonth.toolbar();
304         }
305     }
306
307     function reset() {
308         monthModel.clear()
309
310         var firstdayofthemonthyear = Script.getLunarDate(1,m_month,m_year).jd;
311         var startfrom = (firstdayofthemonthyear+1)%7;
312         var daysofthemonthyear = Script.calDays(m_month,m_year);
313         var j=1;
314         var totalslot = 42;
315
316         var nextmonth = get_nextmonth(totalslot - (startfrom+daysofthemonthyear))
317         var prevmonth = get_prevmonth(startfrom)
318
319         var dayclick, monthclick, yearclick;
320         var datas1,datas,lunarclmonth,inmonth,isprevmonth;
321
322         for (var i=0;i<42;i++){
323             if (i >= startfrom && j<= daysofthemonthyear){
324                 dayclick = j;
325                 monthclick = m_month;
326                 yearclick = m_year;
327                 inmonth = true;
328                 isprevmonth=false;
329                 j++;
330             }else{
331                 if (i<startfrom){
332                     datas1=prevmonth[i];
333                     isprevmonth = true;
334                 }
335                 else{
336                     datas1=nextmonth[(totalslot-i)-1];
337                     isprevmonth = false;
338                 }
339
340                 dayclick = datas1.duong;
341                 monthclick = datas1.month;
342                 yearclick = datas1.year;
343                 inmonth = false;
344             }
345
346             var current = false;
347             if (dayclick == Script.curDay && monthclick==Script.curMonth && yearclick==Script.curYear)
348                 current = true
349
350             var lunarcl = Script.getLunarDate(dayclick,monthclick,yearclick)
351             var lunar_day = lunarcl.day;
352
353             if (lunarcl.day === 1)
354                 lunar_day = lunarcl.day+"/"+lunarcl.month;
355
356             var stemp = Script.convertSolar2Lunar(dayclick,monthclick,yearclick,7);
357             var inholiday = Script.is_holiday(dayclick, monthclick, stemp[0], stemp[1]);
358
359             monthModel.append({"day": dayclick, "current": current, "lunar_day":lunar_day, "inmonth":inmonth,"inholiday":inholiday,"isprevmonth":isprevmonth})
360         }
361
362     }
363
364     function get_prevmonth(startfrom){
365         var cMonth=m_month;
366         var cYear=m_year;
367         var pmonth = [];
368         if (cMonth == 1){
369             cMonth = 12
370             cYear--;
371         }else
372             cMonth--;
373          var daysofthemonthyear = Script.calDays(cMonth,cYear);
374         for (var i=daysofthemonthyear;i>daysofthemonthyear-startfrom;i--){
375             var lunarcl = Script.getLunarDate(i,cMonth,cYear)
376             pmonth.push({duong:i, am:lunarcl.day, month:cMonth, year:cYear})
377         }
378         return pmonth.reverse()
379     }
380
381     function get_nextmonth(startfrom){
382         var cMonth=m_month;
383         var cYear=m_year;
384          var pmonth = [];
385         if (cMonth==12){
386             cMonth = 1;
387             cYear++;
388         }else
389             cMonth++;
390
391         for (var i=1;i<=startfrom;i++){
392           var lunarcl = Script.getLunarDate(i,cMonth,cYear)
393             pmonth.push({duong:i, am:lunarcl.day, month:cMonth, year:cYear});
394         }
395            return pmonth.reverse();
396     }
397
398     states: [
399         State {
400             name: "show"
401             AnchorChanges { target: fullmonth; anchors.right: parent.right }
402         },
403         State {
404             name: "close"
405             AnchorChanges { target: fullmonth; anchors.right: parent.left }
406         }
407     ]
408
409     transitions: Transition {
410         AnchorAnimation { easing.type: Easing.OutQuart; duration: 300 }
411        }
412
413 }