~artmello/ubuntu-calendar-app/ubuntu-calendar-app-adaptive_layout

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
/*
 * Copyright (C) 2013-2016 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 QtOrganizer 5.0
import Ubuntu.Components 1.3
import "./js/dateExt.js" as DateExt
import "./3rd-party/lunar.js" as Lunar

PageWithBottomEdge {
    id: root
    objectName: "AgendaView"

    property var anchorDate: new Date()

    signal dateSelected(var date)

    function goToBeginning() {
        eventList.positionViewAtBeginning();
    }

    function hasEnabledCalendars() {
        var enabled_calendars = eventListModel.getCollections().filter( function( item ) {
            return item.extendedMetaData( "collection-selected" );
        } );

        return !!enabled_calendars.length;
    }

    Keys.forwardTo: [eventList]
    createEventAt: anchorDate

    // Page Header
    header: PageHeader {
        title: i18n.tr("Agenda")
        leadingActionBar.actions: mainView.wideModeView ? [] : adaptivePageLayout.tabsAction
        trailingActionBar.actions: [
            commonHeaderActions.settingsAction,
        ]
        flickable: eventList
    }

    // make sure that the model is updated after create a new event if it is marked as auto-update false
    onEventSaved: eventListModel.updateIfNecessary()
    onEventDeleted: eventListModel.updateIfNecessary()


    // ListModel to hold all events for upcoming 7days.
    EventListModel {
        id: eventListModel
        objectName: "agendaEventListModel"

        startPeriod: anchorDate.midnight();
        endPeriod: anchorDate.addDays(7).endOfDay()
        filter: model ? model.filter : null
        active: root.pageSelected && root.active
        sortOrders: [
            SortOrder{
                blankPolicy: SortOrder.BlanksFirst
                detail: Detail.EventTime
                field: EventTime.FieldStartDateTime
                direction: Qt.AscendingOrder
            }
        ]
    }

    // spinner. running while agenda is loading.
    ActivityIndicator {
        z:2
        visible: running
        running: eventListModel.isLoading
        anchors.centerIn: parent
    }

    // Label to be shown when there is no upcoming events or if no calendar is selected.
    Label {
        id: noEventsOrCalendarsLabel
        anchors.centerIn: parent
        visible: (eventList.itemCount === 0) && !eventListModel.isLoading
        text: !root.hasEnabledCalendars() ? i18n.tr("You have no calendars enabled") : i18n.tr( "No upcoming events" )
    }

    // button to be shown when no calendar is selected (onClick will take user to list of all calendars)
    Button {
        anchors {
            top: noEventsOrCalendarsLabel.bottom;
            horizontalCenter: noEventsOrCalendarsLabel.horizontalCenter;
            topMargin: units.gu(1.5)
        }
        color: UbuntuColors.orange
        visible: !root.hasEnabledCalendars()
        text: i18n.tr( "Enable calendars" )

        onClicked: {
            pageStack.push(Qt.resolvedUrl("CalendarChoicePopup.qml"),{"model": model});
            pageStack.currentPage.collectionUpdated.connect(model.delayedApplyFilter);
        }
    }

    // Main ListView with all upcoming events.
    ListView {
        id: eventList
        objectName: "eventList"

        anchors{
            fill: parent
            bottomMargin: root.bottomEdgeHeight
        }
        visible: eventListModel.itemCount > 0
        model: eventListModel
        delegate: listDelegate
    }

    // Scrollbar
    Scrollbar{
        flickableItem: eventList
        align: Qt.AlignTrailing
    }

    // ListView delegate
    Component{
        id: listDelegate

        // Main item to hold listitem delegate.
        Item {
            property var event: eventListModel.items[index];
            property var prevEvent: eventListModel.items[index-1];
            property var date: event.startDateTime.toLocaleDateString()
            property var startTime: event.startDateTime.toLocaleTimeString(Qt.locale(), Locale.ShortFormat)
            property var endTime: event.endDateTime.toLocaleTimeString(Qt.locale(), Locale.ShortFormat)
            property var lunarDate: Lunar.calendar.solar2lunar(event.startDateTime.getFullYear(),
                                                               event.startDateTime.getMonth() + 1,
                                                               event.startDateTime.getDate())

            width: parent.width
            height: eventContainer.height

            // main Column to hold   header-(date) and event details-(start/end time, Description and location)
            Column {
                id: eventContainer
                objectName: "eventContainer" + index

                width: parent.width
                anchors.top: parent.top

                // header ListItem eg. ( Friday, October 29th 2015 )
                ListItem {
                    width: parent.width
                    height: visible ? units.gu(4) : 0
                    color: "#F7F7F7"
                    highlightColor: "#EDEDED"
                    visible: index === 0 ? true : prevEvent === undefined ? false : prevEvent.startDateTime.midnight() < event.startDateTime.midnight() ? true : false

                    ListItemLayout {
                        id: listitemlayout
                        padding.top: units.gu(1)
                        title.text: mainView.displayLunarCalendar ? ("%1 %2 %3 %4 %5").arg(lunarDate.gzYear).arg(lunarDate .IMonthCn).arg(lunarDate.IDayCn)
                                                                                      .arg(lunarDate.gzDay).arg(lunarDate.isTerm ? lunarDate.Term : "")
                                                                  : date
                        title.color: event.startDateTime.toLocaleDateString() === new Date().toLocaleDateString() ? UbuntuColors.orange : UbuntuColors.darkGrey
                    }

                    // onClicked new page with daily view will open.
                    onClicked: {
                        Haptics.play()
                        dateSelected(event.startDateTime);
                    }
                }

                // Main ListItem to hold details about event eg. ( 19:30 -   Beer with the team )
                //                                               ( 20:00     Hicter             )
                ListItem {
                    id: detailsListItem

                    width: parent.width
                    height: detailsListitemlayout.height
                    color: "white"
                    highlightColor: "#F7F7F7"

                    ListItemLayout {
                        id: detailsListitemlayout

                        title.font.bold: true
                        title.text: event.displayLabel ? event.displayLabel : i18n.tr("no event name set")
                        subtitle.font.pixelSize: title.font.pixelSize
                        subtitle.text: event.location ? event.location : i18n.tr("no location")
                        subtitle.color: event.location ? UbuntuColors.coolGrey : "#B3B3B3"
                        subtitle.font.italic: event.location ? false : true


                        // item to hold SlotsLayout.Leading items: timeStart timeEnad and little calendar indication icon.
                        Item {
                            width: timeLabelStart.width + units.gu(2)
                            height: parent.height
                            SlotsLayout.overrideVerticalPositioning: true
                            SlotsLayout.position: SlotsLayout.Leading

                            // Little icon in left top corner of every event to indicate color of the calendar.
                            UbuntuShape {
                                id: calendarIndicator

                                anchors.verticalCenter: parent.verticalCenter
                                width: units.gu(1)
                                height: width
                                aspect: UbuntuShape.DropShadow
                                backgroundColor: eventListModel.collection(event.collectionId).color
                            }

                            // start time event Label
                            Label {
                                id: timeLabelStart

                                anchors {left: calendarIndicator.right; leftMargin: units.gu(1)}
                                fontSize: "small"
                                y: detailsListitemlayout.mainSlot.y + detailsListitemlayout.title.y
                                   + detailsListitemlayout.title.baselineOffset - baselineOffset
                                text: startTime.concat('-')
                            }

                            // finish time event Label
                            Label {
                                id: timeLabelEnd

                                anchors {left: calendarIndicator.right; leftMargin: units.gu(1)}
                                fontSize: "small"
                                y: detailsListitemlayout.mainSlot.y + detailsListitemlayout.subtitle.y
                                   + detailsListitemlayout.subtitle.baselineOffset - baselineOffset;
                                text: endTime
                            }
                        }

                    }

                    // new page will open to edit selected event
                    onClicked: {
                        Haptics.play()
                        pageStack.push(Qt.resolvedUrl("EventDetails.qml"), {"event":event,"model":eventListModel});
                    }
                }
            }
        }
    }
}