~sbaldassin/ubuntu-calendar-app/testability

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright (C) 2013-2014 Canonical Ltd
 *
 * This file is part of Ubuntu Calendar App
 *
 * Ubuntu Calendar App is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * Ubuntu Calendar App is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
import QtQuick 2.4
import Ubuntu.Components 1.3

import "dateExt.js" as DateExt
import "colorUtils.js" as Color
import "./3rd-party/lunar.js" as Lunar

Item{
    id: root
    objectName: "MonthComponent"

    property bool isCurrentItem;
    property int currentYear;
    property int currentMonth;

    property var isYearView;
    property bool displayWeekNumber:false
    property bool displayLunarCalendar: false

    property alias dayLabelDelegate : dayLabelRepeater.delegate
    property alias dateLabelDelegate : dateLabelRepeater.delegate
    readonly property alias monthStartDate: intern.monthStart

    property string dayLabelFontSize: "medium"
    property string dateLabelFontSize: "large"
    property string leftLabelFontSize: "large"
    property string rightLabelFontSize: "large"

    signal monthSelected(var date);
    signal dateSelected(var date);

    function updateEvents(events) {
        intern.eventStatus = events
    }

    QtObject{
        id: intern

        property var eventStatus: new Array(42)

        property var today: DateExt.today()
        property int todayDate: today.getDate()
        property int todayMonth: today.getMonth()
        property int todayYear: today.getFullYear()

        //date from month will start, this date might be from previous month
        property var currentDate: new Date(root.currentYear, root.currentMonth, 1, 0, 0, 0, 0)
        property var monthStart: currentDate.weekStart( Qt.locale().firstDayOfWeek )
        property int monthStartDate: monthStart.getDate()
        property int monthStartMonth: monthStart.getMonth()
        property int monthStartYear: monthStart.getFullYear()
        readonly property int daysInStartMonth: Date.daysInMonth(monthStartYear, monthStartMonth)
        readonly property int daysInCurrentMonth: Date.daysInMonth(root.currentYear, root.currentMonth)

        //check if current month is start month
        property bool isCurMonthStartMonth: root.currentMonth === monthStartMonth &&
                                            root.currentYear === monthStartYear

        //check current month is same as today's month
        property bool isCurMonthTodayMonth: todayYear === root.currentYear &&
                                            todayMonth == root.currentMonth
        //offset from current month's first date to start date of current month
        property int offset: isCurMonthStartMonth ? -1 : (daysInStartMonth - monthStartDate)

        property int dateFontSize: FontUtils.sizeToPixels(root.dateLabelFontSize)
        property int dayFontSize: FontUtils.sizeToPixels(root.dayLabelFontSize)

        property int todayIndex: root.isCurrentItem &&
                                 isCurMonthTodayMonth ?
                                     intern.indexByDate(intern.today) : -1

        function indexByDate(date){
            if (!date) {
                return -1;
            }

            if (date.getFullYear() < root.currentYear ||
                (date.getFullYear() === root.currentYear && date.getMonth() < root.currentMonth)) {
                return offset - (Date.daysInMonth(date.getFullYear(), date.getMonth()) - date.getDate());

            } else if (date.getFullYear() === root.currentYear && date.getMonth() === root.currentMonth) {
               return offset + date.getDate();

            } else if (date.getFullYear() > root.currentYear ||
                       (date.getFullYear() === root.currentYear && date.getMonth() > root.currentMonth)) {
                return offset + Date.daysInMonth(root.currentYear, root.currentMonth) + date.getDate();
            }

            return -1;
        }
    }

    UbuntuShape{
        id: todayShape

        visible: (monthGrid.todayItem != null)
        color: UbuntuColors.orange
        width: parent ? Math.min(parent.height, parent.width) / 1.3 : 0
        height: width
        parent: monthGrid.todayItem
        anchors.centerIn: parent
        z: -1
        Rectangle {
            anchors.fill: parent
            anchors.margins: units.gu(0.5)
            color: UbuntuColors.orange
            radius: 5
        }
    }

    Column{
        id: column

        anchors {
            left: weekNumLoader.right;
            right: parent.right;
            top: parent.top;
            bottom: parent.bottom;
            topMargin: units.gu(1.5)
            bottomMargin: units.gu(1)
        }

        spacing: units.gu(1.5)

        Loader {
            width: parent.width
            height: isYearView ? FontUtils.sizeToPixels(root.leftLabelFontSize) : 0;
            sourceComponent: isYearView ? headerComp : undefined
            Component{
                id: headerComp
                ViewHeader{
                    id: monthHeader
                    anchors.fill: parent
                    month: root.currentMonth
                    year: root.currentYear
                    daysInMonth: intern.daysInStartMonth

                    leftLabelFontSize: root.leftLabelFontSize
                    rightLabelFontSize: root.rightLabelFontSize
                }
            }
        }

        Item {
            width: parent.width
            height: dayLabelRow.height + units.gu(1)

            Row{
                id: dayLabelRow
                objectName: "dayLabelRow" + index

                property int dayWidth: width / 7;

                anchors{
                    left: parent.left
                    right: parent.right
                    verticalCenter: parent.verticalCenter
                }

                Repeater{
                    id: dayLabelRepeater
                    model:7
                    delegate: dafaultDayLabelComponent
                }
            }
        }

        Grid {
            id: monthGrid
            objectName: "monthGrid"

            property int dayWidth: width / 7 /*cols*/;
            property int dayHeight: height / 6/*rows*/;
            readonly property var todayItem: (intern.todayIndex != -1) &&
                                             (monthGrid.children.length > intern.todayIndex) ?
                                               dateLabelRepeater.itemAt(intern.todayIndex) : null
            anchors {
                left: parent.left
                right: parent.right
            }
            height: parent.height - monthGrid.y
            columns: 7

            Repeater{
                id: dateLabelRepeater
                model: 42
                delegate: isYearView ? monthWithoutEventsDelegate : monthWithEventsDelegate
            }
        }
    }

    MouseArea {
        id: mouseArea

        function getItemAt(x, y)
        {
            var clickPosition = mouseArea.mapToItem(monthGrid, x, y)
            return monthGrid.childAt(clickPosition.x, clickPosition.y)
        }

        function getIndexOfChild(object, child) {
            for (var i = 0; i <= object.children.length; i++) {
                if (object.children[i] === child) {
                    return i;
                }
            }
            return -1;
        }

        anchors {
            fill: column
            topMargin: monthGrid.y
        }

        onPressAndHold: {
            var dayItem = getItemAt(mouse.x, mouse.y)
            var index = getIndexOfChild(monthGrid, dayItem);
            var selectedDate = intern.monthStart.addDays(index);
            pageStack.push(Qt.resolvedUrl("NewEvent.qml"), {"date":selectedDate, "model":eventModel});
        }
        onClicked: {
            var dayItem = getItemAt(mouse.x, mouse.y)
            var index = getIndexOfChild(monthGrid, dayItem);
            var selectedDate = intern.monthStart.addDays(index);
            if (root.isYearView) {
                //If yearView is clicked then open selected MonthView
                root.monthSelected(selectedDate);
            } else {
                root.dateSelected(selectedDate);
            }
        }
    }

    Loader {
        id: weekNumLoader;
        anchors.left: parent.left;
        width: displayWeekNumber ? parent.width / 7:0;
        height: parent.height;
        visible: displayWeekNumber;
        sourceComponent: displayWeekNumber ? weekNumComp : undefined;
    }

    Component {
        id: weekNumComp

        Column {
            id: weekNumColumn;

            anchors {
                fill: parent
                topMargin: units.gu(1.0)
                bottomMargin: units.gu(1.25)
            }

            Item {
                id: datePlaceHolder;
                objectName:"datePlaceHolder"

                width: parent.width;
                height: isYearView ? units.gu(4.5): units.gu(1.25)
            }

            Item {
                id: weekNumLabelItem;
                objectName: "weekNumLabelItem"

                width: parent.width;
                height: weekNumLabel.height + units.gu(2.0)

                Label{
                    id: weekNumLabel;
                    objectName: "weekNumLabel";
                    width: parent.width;
                    // TRANSLATORS: This is shown in the month view as "Wk" as a title
                    // to indicate the week numbers. It should be a max of up to 3 characters.
                    text: i18n.tr("Wk");
                    horizontalAlignment: Text.AlignHCenter;
                    verticalAlignment: Text.AlignVCenter;
                    font.pixelSize: intern.dayFontSize;
                    font.bold: true
                    color: "black"
                }
            }

            Repeater {
                id: weekNumrepeater;
                model: 6;

                Label{
                    id: weekNum
                    objectName: "weekNum" + index
                    width: parent.width;
                    height: (weekNumColumn.height - weekNumLabelItem.height - datePlaceHolder.height) / 6;
                    text: intern.monthStart.addDays(index * 7).weekNumber(Qt.locale().firstDayOfWeek)
                    horizontalAlignment: Text.AlignHCenter;
                    verticalAlignment: Text.AlignVCenter;
                    font.pixelSize: intern.dayFontSize + 1;
                    font.bold: true
                    color: "black"

                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            var selectedDate = new Date(intern.monthStart.addDays(index * 7))
                            if( isYearView ) {
                                root.monthSelected(selectedDate);
                            } else {
                                root.dateSelected(selectedDate);
                            }
                        }
                    }
                }
            }
        }
    }

    Component{
        id: dafaultDayLabelComponent

        Text {
            id: weekDay
            objectName: "weekDay" + index
            width: parent.dayWidth
            property var day : Qt.locale(Qt.locale().name).standaloneDayName(( Qt.locale().firstDayOfWeek + index), Locale.ShortFormat);
            text: isYearView ? Qt.locale(Qt.locale().name).standaloneDayName(( Qt.locale().firstDayOfWeek + index), Locale.NarrowFormat) : day
            horizontalAlignment: Text.AlignHCenter
            font.pixelSize: intern.dayFontSize
            font.bold: true
            color: "black"
        }
    }
    Component {
        id: monthWithEventsDelegate

        MonthComponentWithEventsDateDelegate {
            property var delegateDate: intern.monthStart.addDays(index)

            date: delegateDate.getDate()
            isCurrentMonth: delegateDate.getMonth() === root.currentMonth
            showEvent: intern.eventStatus[delegateDate.toDateString()] === true
            lunarData: {
                if (!root.displayLunarCalendar)
                    return null

                var lunar = Lunar.calendar.solar2lunar(intern.monthStartYear,
                                                       intern.monthStartMonth + 1,
                                                       intern.monthStartDate + index)
                if (lunar.isTerm) {
                    return {"lunarText": lunar.Term, "isTerm" :lunar.isTerm};
                } else {
                    return {"lunarText": lunar.IDayCn, "isTerm" :lunar.isTerm};
                }
            }
            isToday: intern.todayDate == date && intern.isCurMonthTodayMonth
            width: monthGrid.dayWidth
            height: monthGrid.dayHeight
        }
    }

    Component {
        id: monthWithoutEventsDelegate

        Text {
            width: monthGrid.dayWidth
            height: monthGrid.dayHeight
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            color: {
                var day = intern.monthStartDate + index;
                if ( intern.monthStartDate >= 7 ) {
                    if ( day <= intern.daysInStartMonth ) {
                        text = day;
                        return "#AEA79F";
                    } else {
                        day = day - intern.daysInStartMonth;
                    }
                }

                if ( day <= intern.daysInCurrentMonth ) {
                    text = day;
                    if ( intern.todayDate == day && intern.isCurMonthTodayMonth ) {
                        return "white";
                    } else {
                        return "#5D5D5D";
                    }
                } else {
                    day = day - intern.daysInCurrentMonth;
                    text = day;
                    return "#AEA79F";
                }
            }
        }
    }
}