~pkunal-parmar/ubuntu-calendar-app/Minor-Performance

« back to all changes in this revision

Viewing changes to calendar.qml

  • Committer: Kunal Parmar
  • Date: 2014-01-18 03:11:01 UTC
  • mto: This revision was merged to the branch mainline in revision 186.
  • Revision ID: pkunal.parmar@gmail.com-20140118031101-cif3k2wdidy9us8s
Print statement removed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2013-2014 Canonical Ltd
3
 
 *
4
 
 * This file is part of Ubuntu Calendar App
5
 
 *
6
 
 * Ubuntu Calendar App is free software: you can redistribute it and/or modify
7
 
 * it under the terms of the GNU General Public License version 3 as
8
 
 * published by the Free Software Foundation.
9
 
 *
10
 
 * Ubuntu Calendar App is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 
 */
18
 
import QtQuick 2.3
19
 
import Ubuntu.Components 1.1
20
 
import Ubuntu.Components.Popups 1.0
21
 
import QtOrganizer 5.0
22
 
import Ubuntu.SyncMonitor 0.1
 
1
import QtQuick 2.0
 
2
import Ubuntu.Components 0.1
 
3
import Ubuntu.Components.Popups 0.1
23
4
 
24
5
import "dateExt.js" as DateExt
 
6
import "GlobalEventModel.js" as GlobalModel
25
7
 
26
8
MainView {
27
9
    id: mainView
28
 
    useDeprecatedToolbar: false
29
 
 
30
 
    // Work-around until this branch lands:
31
 
    // https://code.launchpad.net/~tpeeters/ubuntu-ui-toolkit/optIn-tabsDrawer/+merge/212496
32
 
    //property bool windowActive: typeof window != 'undefined'
33
 
    //onWindowActiveChanged: window.title = i18n.tr("Calendar")
34
10
 
35
11
    // Argument during startup
36
12
    Arguments {
42
18
        // Due to bug #1231558 you have to pass arguments BEFORE app:
43
19
        // qmlscene calendar:///new-event calendar.qml
44
20
 
45
 
        defaultArgument.help: i18n.tr("Calendar app accept four arguments: --starttime, --endtime, --newevent and --eventid. They will be managed by system. See the source for a full comment about them");
 
21
        defaultArgument.help: i18n.tr("Calendar app accept three arguments: --starttime, --endtime and --newevet. They will be managed by system. See the source for a full comment about them");
46
22
        //defaultArgument.required: false;
47
23
        defaultArgument.valueNames: ["URL"]
48
24
 
69
45
         * If newevent isn't set and startime is set, its value is used to choose the right view.
70
46
         * If neither of precendet flags are set, endtime is ignored.
71
47
         * It accepts an integer value of the number of seconds since UNIX epoch in the UTC timezone.
72
 
         *
73
 
         *
74
 
         * Open an existing event
75
 
         * Keyword: eventid (provisional)
76
 
         *
77
 
         * It takes a id of an event and open that event on full page
78
48
         */
79
 
        Argument {
80
 
            name: "eventid"
81
 
            required: false
82
 
            valueNames: ["EVENT_ID"]
83
 
        }
84
49
    }
85
50
 
86
51
    objectName: "calendar"
87
52
    applicationName: "com.ubuntu.calendar"
88
53
 
89
 
    width: units.gu(100)
 
54
    width: units.gu(45)
90
55
    height: units.gu(80)
91
 
    focus: true
92
 
    Keys.forwardTo: [pageStack.currentPage]
93
56
 
94
 
    headerColor: "#E8E8E8"
95
 
    backgroundColor: "#f5f5f5"
96
 
    footerColor: "#ECECEC"
 
57
    headerColor: "#266249"
 
58
    backgroundColor: "#478158"
 
59
    footerColor: "#478158"
97
60
    anchorToKeyboard: true
98
61
 
99
 
    SyncMonitor {
100
 
        id: syncMonitor
101
 
    }
102
 
 
103
62
    PageStack {
104
63
        id: pageStack
105
64
 
106
 
        Component.onCompleted: push(tabs)
107
 
 
108
 
        // This is for wait that the app is load when newEvent is invoked by argument
109
 
        Timer {
110
 
            id: timer
111
 
            interval: 200; running: false; repeat: false
112
 
            onTriggered: {
113
 
                tabs.newEvent();
114
 
            }
115
 
        }
116
 
 
117
 
        Timer {
118
 
            id: applyFilterTimer
119
 
            interval: 200; running: false; repeat: false
120
 
            onTriggered: {
121
 
                eventModel.applyFilterFinal();
122
 
            }
123
 
        }
124
 
 
125
 
        UnionFilter {
126
 
            id: itemTypeFilter
127
 
            DetailFieldFilter{
128
 
                id: eventFilter
129
 
                detail: Detail.ItemType;
130
 
                field: Type.FieldType
131
 
                value: Type.Event
132
 
                matchFlags: Filter.MatchExactly
133
 
            }
134
 
 
135
 
            DetailFieldFilter{
136
 
                id: eventOccurenceFilter
137
 
                detail: Detail.ItemType;
138
 
                field: Type.FieldType
139
 
                value: Type.EventOccurrence
140
 
                matchFlags: Filter.MatchExactly
141
 
            }
142
 
        }
143
 
 
144
 
        CollectionFilter{
145
 
            id: collectionFilter
146
 
        }
147
 
 
148
 
        EventListModel{
149
 
            id: eventModel
150
 
 
151
 
            autoUpdate: true
152
 
            startPeriod: tabs.currentDay
153
 
            endPeriod: tabs.currentDay
154
 
 
155
 
            filter: IntersectionFilter {
156
 
                filters: [ collectionFilter, itemTypeFilter]
157
 
            }
158
 
 
159
 
            function delayedApplyFilter() {
160
 
                applyFilterTimer.restart();
161
 
            }
162
 
 
163
 
            function applyFilterFinal() {
164
 
                var collectionIds = [];
165
 
                var collections = eventModel.getCollections();
166
 
                for(var i=0; i < collections.length ; ++i) {
167
 
                    var collection = collections[i]
168
 
                    if(collection.extendedMetaData("collection-selected") === true) {
169
 
                        collectionIds.push(collection.collectionId);
170
 
                    }
171
 
                }
172
 
                collectionFilter.ids = collectionIds;
173
 
            }
174
 
 
175
 
            Component.onCompleted: {
176
 
                delayedApplyFilter();
177
 
 
178
 
                if (args.values.eventid) {
179
 
                    var requestId = "";
180
 
                    eventModel.onItemsFetched.connect( function(id,fetchedItems) {
181
 
                        if( requestId === id && fetchedItems.length > 0 ) {
182
 
                            var event = fetchedItems[0];
183
 
                            pageStack.push(Qt.resolvedUrl("EventDetails.qml"),{"event":event,"model": eventModel});
184
 
                        }
185
 
                    });
186
 
                    requestId = eventModel.fetchItems([args.values.eventid]);
187
 
                }
188
 
            }
189
 
        }
190
 
 
191
 
        Tabs{
192
 
            id: tabs
193
 
            Keys.forwardTo: [tabs.currentPage.item]
 
65
        Component.onCompleted: push(tabPage)
 
66
 
 
67
        Page{
 
68
            id: tabPage
194
69
 
195
70
            property var currentDay: DateExt.today();
 
71
            property var globalModel;
196
72
 
197
73
            // Arguments on startup
198
74
            property bool newevent: false;
199
75
            property int starttime: -1;
200
76
            property int endtime: -1;
201
77
 
202
 
            selectedTabIndex: monthTab.index
 
78
            onCurrentDayChanged: {
 
79
                if( monthView.currentMonth !== undefined && !monthView.currentMonth.isSameDay(currentDay))
 
80
                    monthView.currentMonth = currentDay.midnight();
 
81
 
 
82
                if( !dayView.currentDay.isSameDay(currentDay))
 
83
                    dayView.currentDay = currentDay
 
84
 
 
85
                if( !weekView.dayStart.isSameDay(currentDay))
 
86
                    weekView.dayStart = currentDay
 
87
 
 
88
                setStartEndDateToModel();
 
89
            }
 
90
 
 
91
            function setStartEndDateToModel() {
 
92
                if(globalModel) {
 
93
                    globalModel.startPeriod =  new Date(currentDay.getFullYear(),0,1,0,0,0,0);
 
94
                    globalModel.endPeriod = new Date(currentDay.getFullYear(),11,31,0,0,0,0);
 
95
                }
 
96
            }
203
97
 
204
98
            function newEvent() {
205
99
                var startDate = new Date();
223
117
                            endDate = new Date(endTime);
224
118
                    }
225
119
                }
226
 
                //pageStack.push(Qt.resolvedUrl("NewEvent.qml"),{"startDate": startDate, "endDate": endDate, //"model":eventModel});
 
120
                pageStack.push(Qt.resolvedUrl("NewEvent.qml"),{"startDate": startDate, "endDate": endDate});
227
121
            }
228
122
 
229
123
            // This function calculate the difference between --endtime and --starttime and choose the better view
236
130
                var difference = endTime - startTime;
237
131
 
238
132
                if (difference > month)
239
 
                    return yearTab.index;   // Year view
 
133
                    return 0;   // Year view
240
134
                else if (difference > 7 * day)
241
 
                    return monthTab.index;   // Month view}
 
135
                    return 1;   // Month view}
242
136
                else if (difference > day)
243
 
                    return weekTab.index;   // Week view
 
137
                    return 2;   // Week view
244
138
                else
245
 
                    return dayTab.index;   // Day view
 
139
                    return 3;   // Day view
246
140
            }
247
141
 
248
142
            // This function parse the argument
258
152
 
259
153
                if (endtimepattern.test(url))
260
154
                    endtime = url.match(/endtime=(\d+)/)[0].replace("endtime=", '');
 
155
 
261
156
            }
262
157
 
263
158
            Component.onCompleted: {
264
159
                // If an url has been set
265
160
                if (args.defaultArgument.at(0)) {
266
161
                    parseArguments(args.defaultArgument.at(0))
267
 
                    tabs.currentDay = new Date()
 
162
                    tabPage.currentDay = new Date()
268
163
                    // If newevent has been called on startup
269
164
                    if (newevent) {
270
165
                        timer.running = true;
271
166
                    }
272
167
                    else if (starttime !== -1) { // If no newevent has been setted, but starttime
273
168
                        var startTime = parseInt(starttime);
274
 
                        tabs.currentDay = new Date(startTime);
 
169
                        tabPage.currentDay = new Date(startTime);
275
170
 
276
171
                        // If also endtime has been settend
277
172
                        if (endtime !== -1) {
292
187
                else {
293
188
                    tabs.selectedTabIndex= 1;
294
189
                }
 
190
 
 
191
                globalModel = GlobalModel.gloablModel();
 
192
                setStartEndDateToModel();
295
193
            } // End of Component.onCompleted:
296
194
 
297
 
            EventActions {
298
 
                id: commonHeaderActions
299
 
            }
300
 
 
301
 
            Keys.onTabPressed: {
302
 
                if( event.modifiers & Qt.ControlModifier) {
303
 
                    var currentTab = tabs.selectedTabIndex;
304
 
                    currentTab ++;
305
 
                    if( currentTab >= tabs.tabChildren.length){
306
 
                        currentTab = 0;
307
 
                    }
308
 
                    tabs.selectedTabIndex = currentTab;
309
 
                }
310
 
            }
311
 
 
312
 
            Keys.onBacktabPressed: {
313
 
                if( event.modifiers & Qt.ControlModifier) {
314
 
                    var currentTab = tabs.selectedTabIndex;
315
 
                    currentTab --;
316
 
                    if( currentTab < 0){
317
 
                        currentTab = tabs.tabChildren.length -1;
318
 
                    }
319
 
                    tabs.selectedTabIndex = currentTab;
320
 
                }
321
 
            }
322
 
 
323
 
            Tab{
324
 
                id: yearTab
325
 
                objectName: "yearTab"
326
 
                title: i18n.tr("Year")
327
 
                page: Loader{
328
 
                    id: yearViewLoader
329
 
                    objectName: "yearViewLoader"
330
 
                    source: tabs.selectedTab == yearTab ? Qt.resolvedUrl("YearView.qml"):""
331
 
                    onLoaded: {
332
 
                        item.currentYear = tabs.currentDay.getFullYear();
333
 
                    }
334
 
 
335
 
                    anchors{
336
 
                        left: parent.left
337
 
                        right: parent.right
338
 
                        bottom: parent.bottom
339
 
                    }
340
 
 
341
 
                    Connections{
342
 
                        target: yearViewLoader.item
343
 
                        onMonthSelected: {
344
 
                            var now = DateExt.today();
345
 
                            if( date.getMonth() === now.getMonth()
346
 
                                    && date.getFullYear() === now.getFullYear()) {
347
 
                                tabs.currentDay = now;
348
 
                            } else {
349
 
                                tabs.currentDay = date.midnight();
350
 
                            }
351
 
                            tabs.selectedTabIndex = monthTab.index;
352
 
                        }
353
 
                    }
354
 
                }
355
 
            }
356
 
 
357
 
            Tab{
358
 
                id: monthTab
359
 
                objectName: "monthTab"
360
 
                title: i18n.tr("Month")
361
 
                page: Loader{
362
 
                    id: monthViewLoader
363
 
                    objectName: "monthViewLoader"
364
 
                    source: tabs.selectedTab == monthTab ? Qt.resolvedUrl("MonthView.qml"):""
365
 
                    onLoaded: {
366
 
                        item.currentMonth = tabs.currentDay.midnight();
367
 
                    }
368
 
 
369
 
                    anchors{
370
 
                        left: parent.left
371
 
                        right: parent.right
372
 
                        bottom: parent.bottom
373
 
                    }
374
 
 
375
 
                    Connections{
376
 
                        target: monthViewLoader.item
377
 
                        onDateSelected: {
378
 
                            tabs.currentDay = date;
379
 
                            tabs.selectedTabIndex = dayTab.index;
380
 
                        }
381
 
                    }
382
 
                }
383
 
            }
384
 
 
385
 
            Tab{
386
 
                id: weekTab
387
 
                objectName: "weekTab"
388
 
                title: i18n.tr("Week")
389
 
                page: Loader{
390
 
                    id: weekViewLoader
391
 
                    objectName: "weekViewLoader"
392
 
                    source: tabs.selectedTab == weekTab ? Qt.resolvedUrl("WeekView.qml"):""
393
 
                    onLoaded: {
394
 
                        item.isCurrentPage= Qt.binding(function() { return tabs.selectedTab == weekTab })
395
 
                        item.dayStart = tabs.currentDay;
396
 
                    }
397
 
 
398
 
                    anchors{
399
 
                        left: parent.left
400
 
                        right: parent.right
401
 
                        bottom: parent.bottom
402
 
                    }
403
 
 
404
 
                    Connections{
405
 
                        target: weekViewLoader.item
406
 
                        onDayStartChanged: {
407
 
                            tabs.currentDay = weekViewLoader.item.dayStart;
408
 
                        }
409
 
 
410
 
                        onDateSelected: {
411
 
                            tabs.currentDay = date;
412
 
                            tabs.selectedTabIndex = dayTab.index;
413
 
                        }
414
 
                    }
415
 
                }
416
 
            }
417
 
 
418
 
            Tab{
419
 
                id: dayTab
420
 
                objectName: "dayTab"
421
 
                title: i18n.tr("Day")
422
 
                page: Loader{
423
 
                    id: dayViewLoader
424
 
                    objectName: "dayViewLoader"
425
 
                    source: tabs.selectedTab == dayTab ? Qt.resolvedUrl("DayView.qml"):""
426
 
                    onLoaded: {
427
 
                        item.isCurrentPage= Qt.binding(function() { return tabs.selectedTab == dayTab })
428
 
                        item.currentDay = tabs.currentDay;
429
 
                    }
430
 
 
431
 
                    anchors{
432
 
                        left: parent.left
433
 
                        right: parent.right
434
 
                        bottom: parent.bottom
435
 
                    }
436
 
 
437
 
                    Connections{
438
 
                        target: dayViewLoader.item
439
 
                        onCurrentDayChanged: {
440
 
                            tabs.currentDay = dayViewLoader.item.currentDay;
441
 
                        }
442
 
 
443
 
                        onDateSelected: {
444
 
                            tabs.currentDay = date;
445
 
                        }
446
 
                    }
447
 
                }
448
 
            }
449
 
 
450
 
            Tab {
451
 
                id: agendaTab
452
 
                objectName: "agendaTab"
453
 
                title: i18n.tr("Agenda")
454
 
                page: Loader {
455
 
                    id: agendaViewLoader
456
 
                    objectName: "agendaViewLoader"
457
 
                    source: tabs.selectedTab == agendaTab ? Qt.resolvedUrl("AgendaView.qml"):""
458
 
 
459
 
                    onLoaded: {
460
 
                        item.currentDay = tabs.currentDay;
461
 
                    }
462
 
 
463
 
                    anchors{
464
 
                        left: parent.left
465
 
                        right: parent.right
466
 
                        bottom: parent.bottom
467
 
                    }
468
 
 
469
 
                    Connections{
470
 
                        target: agendaViewLoader.item
471
 
                        onDateSelected: {
472
 
                            tabs.currentDay = date;
473
 
                            tabs.selectedTabIndex = dayTab.index;
 
195
            // This is for wait that the app is load when newEvent is invoked by argument
 
196
            Timer {
 
197
                id: timer
 
198
                interval: 200;
 
199
                running: false;
 
200
                repeat: false
 
201
                onTriggered: {
 
202
                    tabPage.newEvent();
 
203
                }
 
204
            }
 
205
 
 
206
            ToolbarItems {
 
207
                id: commonToolBar
 
208
 
 
209
                ToolbarButton {
 
210
                    objectName: "todaybutton"
 
211
                    action: Action {
 
212
                        iconSource: Qt.resolvedUrl("calendar-today.svg");
 
213
                        text: i18n.tr("Today");
 
214
                        onTriggered: {
 
215
                            tabPage.currentDay = (new Date()).midnight();
 
216
                        }
 
217
                    }
 
218
                }
 
219
                ToolbarButton {
 
220
                    objectName: "neweventbutton"
 
221
                    action: Action {
 
222
                        iconSource: Qt.resolvedUrl("new-event.svg");
 
223
                        text: i18n.tr("New Event");
 
224
                        onTriggered: {
 
225
                            pageStack.push(Qt.resolvedUrl("NewEvent.qml"),{"date":tabPage.currentDay});
 
226
                        }
 
227
                    }
 
228
                }
 
229
            }
 
230
 
 
231
            Tabs{
 
232
                id: tabs
 
233
                Tab{
 
234
                    objectName: "yearTab"
 
235
                    title: i18n.tr("Year")
 
236
                    page: Page{
 
237
                        objectName: "yearPage"
 
238
                        anchors.fill: parent
 
239
                        tools: commonToolBar
 
240
                        YearView{
 
241
                            onMonthSelected: {
 
242
                                tabs.selectedTabIndex = 1
 
243
                                var now = DateExt.today();
 
244
                                if( date.getMonth() === now.getMonth()
 
245
                                        && date.getFullYear() === now.getFullYear()) {
 
246
                                    monthView.currentMonth = now
 
247
                                } else {
 
248
                                    monthView.currentMonth = date.midnight();
 
249
                                }
 
250
                            }
 
251
                        }
 
252
                    }
 
253
                }
 
254
                Tab {
 
255
                    id: monthTab
 
256
                    objectName: "monthTab"
 
257
                    title: i18n.tr("Month")
 
258
                    page: MonthView{
 
259
                        anchors.fill: parent
 
260
                        tools: commonToolBar
 
261
                        id: monthView
 
262
 
 
263
                        onDateSelected: {
 
264
                            tabs.selectedTabIndex  = 3
 
265
                            tabPage.currentDay = date;
 
266
                        }
 
267
                    }
 
268
                }
 
269
                Tab{
 
270
                    objectName: "weekTab"
 
271
                    title: i18n.tr("Week")
 
272
                    page: Page{
 
273
                        anchors.fill: parent
 
274
                        tools: commonToolBar
 
275
                        WeekView{
 
276
                            id: weekView
 
277
                            anchors.fill: parent
 
278
 
 
279
                            onDayStartChanged: {
 
280
                                tabPage.currentDay = dayStart;
 
281
                            }
 
282
                        }
 
283
                    }
 
284
                }
 
285
 
 
286
                Tab{
 
287
                    objectName: "dayTab"
 
288
                    title: i18n.tr("Day")
 
289
                    page: Page{
 
290
                        anchors.fill: parent
 
291
                        tools: commonToolBar
 
292
                        DayView{
 
293
                            id: dayView
 
294
                            anchors.fill: parent
 
295
 
 
296
                            onCurrentDayChanged: {
 
297
                                tabPage.currentDay = currentDay;
 
298
                            }
474
299
                        }
475
300
                    }
476
301
                }