a2be992d2beafe531732360a9f5e4632b98100ee
[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
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: width ? width / 6 : 1
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: 10
101
102                 verticalAlignment: Text.AlignVCenter
103                 horizontalAlignment: Text.AlignLeft
104
105                 text: model ? model.day : ""
106                 color: model ? model.inmonth ? (model.current ? "red" : model.inholiday ? theme_manager.theme.markdate.holidays : "black") : "grey" : "red"
107                 font.pointSize: parent.width ? parent.width / 6 : 1
108                 font.bold: true
109             }
110
111             Text {
112                 anchors.fill: parent
113                 anchors.leftMargin: txt_solar_day.anchors.leftMargin+40
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 ? "red" : "#700070") : "grey" : "red"
121                 font.pointSize: parent.width ? parent.width / 8 : 1
122             }
123
124             MouseArea {
125                 anchors.fill: parent
126
127                 property real mX
128                 property real mY
129                 property bool hold: false
130
131                 onClicked: {
132                     /*LC.reset(model.day,m_month,m_year)
133                     screen.calculateLunar(model.day,m_month,m_year)
134                     screen.state="mainscreen"
135                     screen.refresh()
136                     maindatemodel.reset()*/
137                     fullmonth.state="close";
138                     fullmonth.toolbar();
139                     fullmonth.updatedate(model.day, m_month, m_year)
140                 }
141
142                 onPositionChanged: {
143                     mX = mouseX;
144                     mY = mouseY;
145                     hold=true;
146                 }
147
148                 onPressed: {
149                     recms.color="#F0F0F0"
150                 }
151
152                 onReleased: {
153                     if (hold){
154                         if (mouseY+mY<0){
155                            if (Math.abs(mouseY) >= 30){
156                               m_month+=1;
157                                if (m_month >=13){
158                                    m_month=1;
159                                    m_year++;
160                                }
161                            }
162                         }
163                         else
164                         {
165                            if (mouseY >= 50){
166                               m_month-=1;
167                                if(m_month<=0){
168                                    m_month=12;
169                                    m_year--;
170                                }
171                            }
172                         }
173                         reset();
174                         hold=false;
175                     }
176                 }
177             }
178         }
179     }
180
181     Rectangle{
182         id: fullmonthREC
183         anchors.fill: parent; anchors.bottomMargin: 0
184         opacity:0.9
185
186         ListModel {
187             id: monthModel
188         }
189
190         Grid {
191             id: grid
192
193             anchors.topMargin: 40
194             anchors.horizontalCenter: parent.horizontalCenter
195             anchors.bottom: parent.bottom
196             anchors.top: parent.top
197
198             columns: 7
199             rows: 7
200
201             Repeater {
202                 model: ["CN", "Hai", "Ba", "Tư", "Năm", "Sáu", "Bảy"]
203
204                 delegate: Item {
205                     id: headerDelegate
206
207                     width: fullmonthREC.width / grid.columns
208                     height: (fullmonthREC.height-92) / grid.rows
209
210                     property int theIndex: index
211                     property variant theModel: model
212
213                     Loader {
214                         property alias model: headerDelegate.theModel
215                         property alias index: headerDelegate.theIndex
216                         property alias cellWidth: headerDelegate.width
217                         property alias cellHeight: headerDelegate.height
218
219                         anchors.fill: parent
220
221                         sourceComponent: headerRenderer
222                     }
223                 }
224             }
225
226             Repeater {
227                 model: monthModel
228
229                 delegate: Item {
230                     id: daysDelegate
231
232                     property int theIndex: index
233                     property variant theModel: model
234
235                     width: fullmonthREC.width / grid.columns
236                     height: (fullmonthREC.height-92) / grid.rows
237
238                     Loader {
239                         property alias model: daysDelegate.theModel
240                         property alias index: daysDelegate.theIndex
241                         property alias cellWidth: daysDelegate.width
242                         property alias cellHeight: daysDelegate.height
243
244                         anchors.fill: parent
245
246                         sourceComponent: daysRenderer
247                     }
248
249                 }
250             }
251
252
253         }
254
255     }
256
257     Rectangle {
258         width : fullmonth.width
259         height: 45
260         color:"white"
261         opacity: 0.5
262     }
263
264     Text {
265         id: title
266
267         anchors { horizontalCenter: fullmonth.horizontalCenter; top: fullmonth.top; topMargin: 10 }
268         font.pixelSize: 22
269         color: "black"
270         text: "Tháng "+fullmonth.m_month+", "+fullmonth.m_year
271         smooth: true
272         font.bold: true
273     }
274
275     ToolBarSingle {
276         id: toolBar; height: 42;
277         y: parent.height-52
278         width: parent.width;
279         button1Label: "Quay Về"
280         onButton1Clicked:{
281             fullmonth.state="close";
282             fullmonth.toolbar();
283         }
284     }
285
286     function reset() {
287         monthModel.clear()
288
289         var firstdayofthemonthyear = Script.getLunarDate(1,m_month,m_year).jd;
290         var startfrom = (firstdayofthemonthyear+1)%7;
291         var daysofthemonthyear = Script.calDays(m_month,m_year);
292         var j=1;
293         var totalslot = 42;
294
295         var nextmonth = get_nextmonth(totalslot - (startfrom+daysofthemonthyear))
296         var prevmonth = get_prevmonth(startfrom)
297
298         var dayclick, monthclick, yearclick;
299         var datas1,datas,lunarclmonth,inmonth;
300
301         for (var i=0;i<42;i++){
302             if (i >= startfrom && j<= daysofthemonthyear){
303                 dayclick = j;
304                 monthclick = m_month;
305                 yearclick = m_year;
306                 inmonth = true;
307                 j++;
308             }else{
309                 if (i<startfrom)
310                     datas1=prevmonth[i];
311                 else
312                     datas1=nextmonth[(totalslot-i)-1];
313
314                 dayclick = datas1.duong;
315                 monthclick = datas1.month;
316                 yearclick = datas1.year;
317                 inmonth = false;
318             }
319
320             var current = false;
321             if (dayclick == Script.curDay && monthclick==Script.curMonth && yearclick==Script.curYear)
322                 current = true
323
324             var lunarcl = Script.getLunarDate(dayclick,monthclick,yearclick)
325             var lunar_day = lunarcl.day;
326
327             if (lunarcl.day === 1)
328                 lunar_day = lunarcl.day+"/"+lunarcl.month;
329
330             var stemp = Script.convertSolar2Lunar(dayclick,monthclick,yearclick,7);
331             var inholiday = Script.is_holiday(dayclick, monthclick, stemp[0], stemp[1]);
332
333             monthModel.append({"day": dayclick, "current": current, "lunar_day":lunar_day, "inmonth":inmonth,"inholiday":inholiday})
334         }
335
336     }
337
338     function get_prevmonth(startfrom){
339         var cMonth=m_month;
340         var cYear=m_year;
341         var pmonth = [];
342         if (cMonth == 1){
343             cMonth = 12
344             cYear--;
345         }else
346             cMonth--;
347          var daysofthemonthyear = Script.calDays(cMonth,cYear);
348         for (var i=daysofthemonthyear;i>daysofthemonthyear-startfrom;i--){
349             var lunarcl = Script.getLunarDate(i,cMonth,cYear)
350             pmonth.push({duong:i, am:lunarcl.day, month:cMonth, year:cYear})
351         }
352         return pmonth.reverse()
353     }
354
355     function get_nextmonth(startfrom){
356         var cMonth=m_month;
357         var cYear=m_year;
358          var pmonth = [];
359         if (cMonth==12){
360             cMonth = 1;
361             cYear++;
362         }else
363             cMonth++;
364
365         for (var i=1;i<=startfrom;i++){
366           var lunarcl = Script.getLunarDate(i,cMonth,cYear)
367             pmonth.push({duong:i, am:lunarcl.day, month:cMonth, year:cYear});
368         }
369            return pmonth.reverse();
370     }
371
372     states: [
373         State {
374             name: "show"
375             AnchorChanges { target: fullmonth; anchors.right: parent.right }
376         },
377         State {
378             name: "close"
379             AnchorChanges { target: fullmonth; anchors.right: parent.left }
380         }
381     ]
382
383     transitions: Transition {
384         AnchorAnimation { easing.type: Easing.OutQuart; duration: 300 }
385     }
386
387 }