Know bugs fixed.
[lichviet] / qml / LichViet / FullMonth.qml
index 6257664..a3ba5aa 100644 (file)
@@ -26,178 +26,226 @@ import "CacNgayLeTrongNam.js" as CNLTN
 Item{
     id: fullmonth
 
-    Rectangle{
-        id: fullmonthREC
-        anchors.fill: parent; anchors.bottomMargin: 0
-        opacity:0.9
+    property int m_day: screen.curDay
+    property int m_month: screen.curMonth
+    property int m_year: screen.curYear
 
-            Component {
-                id: contactDelegate
-                Item {
-                    width: grid.cellWidth; height: {
-                        if (header)
-                            return grid.cellHeight - 15;
-                        else return grid.cellHeight;
-                    }
-                    Rectangle {
-                        anchors.fill: parent;anchors.rightMargin: 2;anchors.topMargin: 2;
-                        Text { color: {
-                                if (header)
-                                  return  "#000000";
-                                else{
-                                    var stemp = AmLich98.convertSolar2Lunar(txtDATEpv.currentIndex+1,curMonth,curYear,7);
-                                    if (CNLTN.is_holiday(day, month, stemp[0], stemp[1]))
-                                        return theme_manager.theme.markdate.holidays;
-                                    else
-                                        return "#000000"//"#007070"
-                                }
-                            }
-
-                            text: name; anchors.horizontalCenter: parent.horizontalCenter
-                            font.pixelSize: 21;font.bold: true;font.family: "Tahoma"
-                            anchors.topMargin: 0;
-                        }
+    property Component headerRenderer:Component {
+        id: defaultHeaderRenderer
 
-                        color: {
-                            if (header)
-                                return "#FAFAFF";
-                            else{
-                                if (inmonth){
-                                    if (day==initcurDay && month==initcurMonth && year==initcurYear)
-                                        return "#00EBEB";
-                                    else
-                                      return "lightsteelblue";
-                                }
-                                else
-                                    return "#AAAAAA";
-                            }
-                        }
-                         radius: 4
-                         MouseArea{
-                             id:fullmonthMouse
-                             anchors.fill: parent
-
-                             onClicked: {
-                               if (index >= 7){
-                                   LC.reset(day,month,year)
-                                   screen.calculateLunar(day,month,year)
-                                   screen.state="mainscreen"
-                                   screen.refresh()
-                                   maindatemodel.reset()
-                                   txtDATEpv.currentIndex = day-1;
-                               }
-                             }
+        Item {
+            width: cellWidth
+            height: cellHeight
 
-onPressed:{
-    if (index >= 7)
-        parent.color="#F0F0F0"
-}
+            Rectangle {
+                width: parent.width - 4
+                height: parent.height - 4
 
-onReleased:{
-      if (index >= 7){
-          if (inmonth){
-              if (day==initcurDay && month==initcurMonth && year==initcurYear)
-                  parent.color="#00EBEB";
-              else
-                  parent.color="lightsteelblue";
-          }else
-              parent.color="#AAAAAA";
-      }
-}
+                anchors.centerIn: parent
+
+                radius: 4
+                color: "#FAFAFF"
+            }
 
-onCanceled:{
-    if (index >= 7){
-        if (inmonth)
-        {if (day==initcurDay && month==initcurMonth && year==initcurYear) parent.color="#00EBEB"; else parent.color="lightsteelblue";}
-        else
-         parent.color="#AAAAAA";
+            Text {
+                width: cellWidth
+                height: cellHeight
+
+                verticalAlignment: Text.AlignVCenter
+                horizontalAlignment: Text.AlignHCenter
+
+                text: model.modelData
+                font.bold: true
+                font.pointSize: width ? width / 6 : 1
+            }
+        }
     }
 
-}
 
-                         }
-                    }
-                }
+    property Component daysRenderer:
+    Component {
+        Item {
+            width: cellWidth
+            height: cellHeight
+
+            Rectangle {
+                id:recms
+                width: parent.width - 4
+                height: parent.height - 4
+
+                anchors.centerIn: parent
+
+                radius: 4
+                color: model ? model.inmonth ? (model.current ? "blue" : "lightsteelblue") : "#AAAAAA" : "black"
             }
 
-            GridView {
-                id: grid
-                x: -335
-                y: 41
-
-                anchors.horizontalCenterOffset: 15
-                anchors.bottomMargin: -52
-                anchors.topMargin: 42
-                anchors.horizontalCenter: parent.horizontalCenter
-                anchors.bottom: parent.bottom
-                anchors.top: parent.top
-                cellWidth: 110; cellHeight: 50
-
-                boundsBehavior:Flickable.DragAndOvershootBounds
-                flickableDirection:Flickable.VerticalFlick
-
-                model:lmodel
-                delegate: contactDelegate
-                focus: true
-
-                onMovementStarted:{
-                   /* var newMonth = screen.curMonth+1;
-                    LC.reset(screen.curDay,newMonth,screen.curYear)
-                    screen.calculateLunar(screen.curDay,newMonth,screen.curYear)
-                    screen.refresh()
-                    reset()*/
-                }
+            Text {
+                id:txt_solar_day
+                anchors.fill: parent
+                anchors.leftMargin: 10
 
+                verticalAlignment: Text.AlignVCenter
+                horizontalAlignment: Text.AlignLeft
+
+                text: model ? model.day : ""
+                color: model ? model.inmonth ? (model.current ? "red" : model.inholiday ? theme_manager.theme.markdate.holidays : "black") : "grey" : "red"
+                font.pointSize: parent.width ? parent.width / 6 : 1
+                font.bold: true
             }
 
-            ListModel {
-                id:lmodel
+            Text {
+                anchors.fill: parent
+                anchors.leftMargin: txt_solar_day.anchors.leftMargin+40
+                anchors.topMargin: 20
 
-                Component.onCompleted: {
-                    reset()
-                }
+                verticalAlignment: Text.AlignVCenter
+                horizontalAlignment: Text.AlignBottom
 
-                function reset(){
-                    for(var i=0;i<42;i++){
-                        append({header:false,name:"",inmonths:false,day:0,month:0,year:0})
-                    }
-                }
+                text: model ? model.lunar_day : ""
+                color: model ? model.inmonth ? (model.current ? "red" : "#700070") : "grey" : "red"
+                font.pointSize: parent.width ? parent.width / 8 : 1
+            }
 
-                ListElement {
-                                        header:true;inmonth:false;day:0;month:0;year:0
-                    name: "CN"
-                }
+            MouseArea {
+                anchors.fill: parent
 
-                ListElement {
-                    header:true;inmonth:false;day:0;month:0;year:0
-                    name: "Hai"
-                }
+                property real mX
+                property real mY
+                property bool hold: false
 
-                ListElement {
-                    header:true;inmonth:false;day:0;month:0;year:0
-                    name: "Ba"
+                onClicked: {
+                    LC.reset(model.day,m_month,m_year)
+                    screen.calculateLunar(model.day,m_month,m_year)
+                    screen.state="mainscreen"
+                    screen.refresh()
+                    maindatemodel.reset()
+                    txtDATEpv.currentIndex = model.day-1;
                 }
-                ListElement {
-                                        header:true;inmonth:false;day:0;month:0;year:0
-                    name: "Tư"
+
+                onPositionChanged: {
+                    mX = mouseX;
+                    mY = mouseY;
+                    hold=true;
                 }
-                ListElement {
-                                        header:true;inmonth:false;day:0;month:0;year:0
-                    name: "Năm"
+
+                onPressed: {
+                    recms.color="#F0F0F0"
                 }
-                ListElement {
-                                        header:true;inmonth:false;day:0;month:0;year:0
-                    name: "Sáu"
+
+                onReleased: {
+                    if (hold){
+                        if (mouseY+mY<0){
+                           if (Math.abs(mouseY) >= 30){
+                              m_month+=1;
+                               if (m_month >=13){
+                                   m_month=1;
+                                   m_year++;
+                               }
+                           }
+                        }
+                        else
+                        {
+                           if (mouseY >= 50){
+                              m_month-=1;
+                               if(m_month<=0){
+                                   m_month=12;
+                                   m_year--;
+                               }
+                           }
+                        }
+                        reset();
+                        hold=false;
+                    }
                 }
-                ListElement {
-                                        header:true;inmonth:false;day:0;month:0;year:0
-                    name: "Bảy"
+            }
+        }
+    }
+
+    Rectangle{
+        id: fullmonthREC
+        anchors.fill: parent; anchors.bottomMargin: 0
+        opacity:0.9
+
+        ListModel {
+            id: monthModel
+        }
+
+        Grid {
+            id: grid
+
+            anchors.bottomMargin: -52
+            anchors.topMargin: 78
+            anchors.horizontalCenter: parent.horizontalCenter
+            anchors.bottom: parent.bottom
+            anchors.top: parent.top
+
+            columns: 7
+            rows: 7
+
+            Repeater {
+                model: ["CN", "Hai", "Ba", "Tư", "Năm", "Sáu", "Bảy"]
+
+                delegate: Item {
+                    id: headerDelegate
+
+                    width: fullmonthREC.width / grid.columns
+                    height: (fullmonthREC.height-92) / grid.rows
+
+                    property int theIndex: index
+                    property variant theModel: model
+
+                    Loader {
+                        property alias model: headerDelegate.theModel
+                        property alias index: headerDelegate.theIndex
+                        property alias cellWidth: headerDelegate.width
+                        property alias cellHeight: headerDelegate.height
+
+                        anchors.fill: parent
+
+                        sourceComponent: headerRenderer
+                    }
                 }
             }
 
-            Component.onCompleted: {
+            Repeater {
+                model: monthModel
+
+                delegate: Item {
+                    id: daysDelegate
+
+                    property int theIndex: index
+                    property variant theModel: model
+
+                    width: fullmonthREC.width / grid.columns
+                    height: (fullmonthREC.height-92) / grid.rows
+
+                    Loader {
+                        property alias model: daysDelegate.theModel
+                        property alias index: daysDelegate.theIndex
+                        property alias cellWidth: daysDelegate.width
+                        property alias cellHeight: daysDelegate.height
+
+                        anchors.fill: parent
+
+                        sourceComponent: daysRenderer
+                    }
+
+                }
             }
 
+
+        }
+
+        Text {
+            id: txt_caption
+            y: 40
+            width:parent.width
+            text: "Tháng "+m_month+", "+m_year
+            horizontalAlignment: Text.AlignHCenter
+            font.pixelSize: 24
+            font.bold: true
+        }
+
     }
 
     ToolBarSingle { id: toolBar; height: 50;
@@ -210,26 +258,61 @@ onCanceled:{
         }
     }
 
-    states: [
-        State {
-            name: "xemtheothang"
-            PropertyChanges {
-                target: grid
-                width : screen.width
-                height: screen.height
-                cellWidth: {
-                    if (screen.width>=800)
-                         return 110;
-                    else
-                        return 64;
-                }
+    function reset() {
+        monthModel.clear()
+
+        var firstdayofthemonthyear = AmLich.getLunarDate(1,m_month,m_year).jd;
+        var startfrom = (firstdayofthemonthyear+1)%7;
+        var daysofthemonthyear = LC.calDays(m_month,m_year);
+        var j=1;
+        var totalslot = 42;
+
+        var nextmonth = get_nextmonth(totalslot - (startfrom+daysofthemonthyear))
+        var prevmonth = get_prevmonth(startfrom)
+
+        var dayclick, monthclick, yearclick;
+        var datas1,datas,lunarclmonth,inmonth;
+
+        for (var i=0;i<42;i++){
+            if (i >= startfrom && j<= daysofthemonthyear){
+                dayclick = j;
+                monthclick = m_month;
+                yearclick = m_year;
+                inmonth = true;
+                j++;
+            }else{
+                if (i<startfrom)
+                    datas1=prevmonth[i];
+                else
+                    datas1=nextmonth[(totalslot-i)-1];
+
+                dayclick = datas1.duong;
+                monthclick = datas1.month;
+                yearclick = datas1.year;
+                inmonth = false;
             }
+
+            var current = false;
+            if (dayclick == screen.curDay && monthclick==screen.curMonth && yearclick==screen.curYear)
+                current = true
+
+            var lunarcl = AmLich.getLunarDate(dayclick,monthclick,yearclick)
+            var lunar_day = lunarcl.day;
+
+            if (lunarcl.day === 1)
+                lunar_day = lunarcl.day+"/"+lunarcl.month;
+
+            var stemp = AmLich98.convertSolar2Lunar(dayclick,monthclick,yearclick,7);
+            var inholiday = CNLTN.is_holiday(dayclick, monthclick, stemp[0], stemp[1]);
+
+            monthModel.append({"day": dayclick, "current": current, "lunar_day":lunar_day, "inmonth":inmonth,"inholiday":inholiday})
         }
-    ]
+
+    }
 
     function get_prevmonth(startfrom){
-        var cMonth=screen.curMonth;
-        var cYear=screen.curYear;
+        var cMonth=m_month;
+        var cYear=m_year;
         var pmonth = [];
         if (cMonth == 1){
             cMonth = 12
@@ -245,8 +328,8 @@ onCanceled:{
     }
 
     function get_nextmonth(startfrom){
-        var cMonth=screen.curMonth;
-        var cYear=screen.curYear;
+        var cMonth=m_month;
+        var cYear=m_year;
          var pmonth = [];
         if (cMonth==12){
             cMonth = 1;
@@ -261,56 +344,4 @@ onCanceled:{
            return pmonth.reverse();
     }
 
-    function reset(){
-        var firstdayofthemonthyear = AmLich.getLunarDate(1,screen.curMonth,screen.curYear).jd;
-        var startfrom = (firstdayofthemonthyear+1)%7;
-        var daysofthemonthyear = LC.calDays(screen.curMonth,screen.curYear);
-        var j=1;
-
-        var totalslot = 42;
-
-        var nextmonth = get_nextmonth(totalslot - (startfrom+daysofthemonthyear))
-        var prevmonth = get_prevmonth(startfrom)
-
-        var dayclick, monthclick, yearclick;
-
-        while(lmodel.count>7){
-            lmodel.remove(lmodel.count-1);
-        }
-
-        var datas1,datas,lunarclmonth,inmonths;
-
-        for (var i=7;i<49;i++){
-            var realnumber = i - 7;
-            if (realnumber >= startfrom && j<= daysofthemonthyear){
-            var lunarcl = AmLich.getLunarDate(j,screen.curMonth,screen.curYear)
-            if (lunarcl.day==1){
-                lunarclmonth = "/"+lunarcl.month
-            }else{
-                lunarclmonth = ""
-            }
-
-            datas = j+"<br><span style='font-size:18px;float:right;color:#700070;'>&nbsp;&nbsp;"+lunarcl.day+lunarclmonth+"</span>";
-            dayclick = j;
-            monthclick = screen.curMonth;
-            yearclick = screen.curYear;
-            inmonths = true;
-                j++;
-            }else{
-                if (realnumber<startfrom){
-                    datas1= prevmonth[realnumber];
-                }else
-                {
-                    datas1=nextmonth[(totalslot-realnumber)-1];
-                }
-                dayclick = datas1.duong;
-                monthclick = datas1.month;
-                yearclick = datas1.year;
-                datas = "<span style='color:grey;font-weight:normal;'>"+datas1.duong+"</span><br><span style='font-size:18px;float:right;color:#CCFFFF;'>&nbsp;&nbsp;"+datas1.am+"</span>";
-                inmonths=false;
-            }
-
-            lmodel.append({header:false,name:datas,inmonth:inmonths,day:dayclick,month:monthclick,year:yearclick})
-        }
-    }
 }