~pkunal-parmar/ubuntu-calendar-app/ICalImport

« back to all changes in this revision

Viewing changes to TimeLineBase.qml

New Calendar App management tests. Fixes: https://bugs.launchpad.net/bugs/1300354.

Approved by Ubuntu Phone Apps Jenkins Bot, Nicholas Skaggs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import QtQuick 2.0
2
 
import Ubuntu.Components 0.1
 
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 QtOrganizer 5.0
 
21
 
3
22
import "dateExt.js" as DateExt
4
23
 
5
24
Item {
7
26
 
8
27
    property var delegate;
9
28
    property var day;
10
 
    property int hourHeight: units.gu(10)
11
 
 
12
 
    EventListModel {
13
 
        id: model
14
 
        termStart: bubbleOverLay.day.midnight()
15
 
        termLength: Date.msPerDay
16
 
 
17
 
        onReload: {
18
 
            bubbleOverLay.createEvents();
19
 
        }
20
 
    }
21
 
 
22
 
    TimeSeparator{
 
29
    property int hourHeight: units.gu(8)
 
30
    property var model;
 
31
 
 
32
    Component.onCompleted: {
 
33
        bubbleOverLay.createEvents();
 
34
    }
 
35
 
 
36
    MouseArea {
 
37
        anchors.fill: parent
 
38
        objectName: "mouseArea"
 
39
        onPressAndHold: {
 
40
            var selectedDate = new Date(day);
 
41
            var hour = parseInt(mouseY / hourHeight);
 
42
            selectedDate.setHours(hour)
 
43
            //pageStack.push(Qt.resolvedUrl("NewEvent.qml"), {"date":selectedDate, "model":eventModel});
 
44
            createOrganizerEvent(selectedDate);
 
45
        }
 
46
 
 
47
        onPressed: {
 
48
            intern.now = new Date();
 
49
            if( intern.now.isSameDay( bubbleOverLay.day ) ) {
 
50
                bubbleOverLay.showSeparator();
 
51
            }
 
52
        }
 
53
    }
 
54
 
 
55
    function getTimeFromYPos(y, day) {
 
56
        var date = new Date(day);
 
57
        var time = y / hourHeight;
 
58
        var minutes = time % 1 ;
 
59
        var hour = time - minutes;
 
60
        minutes = parseInt(60 * minutes);
 
61
        minutes = Math.floor(minutes/15) * 15;
 
62
        date.setHours(hour);
 
63
        date.setMinutes(minutes);
 
64
        return date;
 
65
    }
 
66
 
 
67
    function createOrganizerEvent( startDate ) {
 
68
        var event = Qt.createQmlObject("import QtOrganizer 5.0; Event {}", Qt.application,"TimeLineBase.qml");
 
69
        event.collectionId = (model.defaultCollection().collectionId);
 
70
        var endDate = new Date( startDate.getTime() + 3600000 );
 
71
        event.startDateTime = startDate;
 
72
        event.endDateTime = endDate;
 
73
        event.displayLabel = i18n.tr("Untitled");
 
74
        model.saveItem(event);
 
75
    }
 
76
 
 
77
    TimeSeparator {
23
78
        id: separator
24
79
        objectName: "separator"
25
80
        width:  bubbleOverLay.width
 
81
        visible: false
26
82
        z:1
27
83
    }
28
84
 
29
85
    QtObject {
30
86
        id: intern
31
87
        property var now : new Date();
 
88
        property var eventMap;
 
89
        property var unUsedEvents: new Object();
32
90
    }
33
91
 
34
92
    function showEventDetails(event) {
35
 
        pageStack.push(Qt.resolvedUrl("EventDetails.qml"),{"event":event});
 
93
        pageStack.push(Qt.resolvedUrl("EventDetails.qml"), {"event":event,"model":model});
 
94
    }
 
95
 
 
96
    WorkerScript {
 
97
        id: eventLayoutHelper
 
98
        source: "EventLayoutHelper.js"
 
99
 
 
100
        onMessage: {
 
101
            layoutEvents(messageObject.schedules,messageObject.maxDepth);
 
102
        }
 
103
    }
 
104
 
 
105
    function layoutEvents(array, depth) {
 
106
        for(var i=0; i < array.length ; ++i) {
 
107
            var schedule = array[i];
 
108
            var event = intern.eventMap[schedule.id];
 
109
            bubbleOverLay.createEvent(event , schedule.depth, depth +1);
 
110
        }
36
111
    }
37
112
 
38
113
    function createEvents() {
39
 
        bubbleOverLay.destroyAllChildren();
40
 
 
41
 
        for(var i = 0 ; i < model.count ; ++i) {
42
 
            var event = model.get(i);
43
 
            if( event ) {
44
 
                bubbleOverLay.createEvent(event,event.startTime.getHours());
 
114
        if(!bubbleOverLay || bubbleOverLay == undefined || model === undefined || model === null) {
 
115
            return;
 
116
        }
 
117
 
 
118
        destroyAllChildren();
 
119
 
 
120
        var eventMap = {};
 
121
        var allSchs = [];
 
122
 
 
123
        var startDate = new Date(day).midnight();
 
124
        var endDate = new Date(day).endOfDay();
 
125
        var items = model.getItems(startDate,endDate);
 
126
        for(var i = 0; i < items.length; ++i) {
 
127
            var event = items[i];
 
128
 
 
129
            if(event.allDay) {
 
130
                continue;
45
131
            }
 
132
 
 
133
            var schedule = {"startDateTime": event.startDateTime, "endDateTime": event.endDateTime,"id":event.itemId };
 
134
            allSchs.push(schedule);
 
135
            eventMap[event.itemId] = event;
46
136
        }
47
137
 
 
138
        intern.eventMap = eventMap;
 
139
        eventLayoutHelper.sendMessage(allSchs);
 
140
 
48
141
        if( intern.now.isSameDay( bubbleOverLay.day ) ) {
49
 
            bubbleOverLay.showSeparator(intern.now.getHours());
 
142
            bubbleOverLay.showSeparator();
50
143
        }
51
144
    }
52
145
 
53
146
    function destroyAllChildren() {
54
147
        for( var i = children.length - 1; i >= 0; --i ) {
55
 
            if( children[i].objectName === "separator") {
56
 
                children[i].visible = false;
 
148
            if( children[i].objectName === "mouseArea" ||
 
149
                    children[i].objectName === "weekdevider") {
 
150
                continue;
 
151
            }
 
152
            children[i].visible = false;
 
153
            if( children[i].objectName !== "separator") {
 
154
                children[i].clicked.disconnect( bubbleOverLay.showEventDetails );
 
155
                var key = children[i].objectName;
 
156
                if (intern.unUsedEvents[key] === "undefined") {
 
157
                    intern.unUsedEvents[key] = children[i];
 
158
                }
 
159
            }
 
160
        }
 
161
    }
 
162
 
 
163
    function isHashEmpty(hash) {
 
164
        for (var prop in hash) {
 
165
            if (prop)
 
166
                return false;
 
167
        }
 
168
        return true;
 
169
    }
 
170
 
 
171
    function getAKeyFromHash(hash) {
 
172
        for (var prop in hash) {
 
173
            return prop;
 
174
        }
 
175
        return "undefined";
 
176
    }
 
177
 
 
178
    function getUnusedEventBubble() {
 
179
        /* Recycle an item from unUsedEvents, and remove from hash */
 
180
        var key = getAKeyFromHash(intern.unUsedEvents);
 
181
        var unUsedBubble = intern.unUsedEvents[key];
 
182
        delete intern.unUsedEvents[key];
 
183
 
 
184
        return unUsedBubble;
 
185
    }
 
186
 
 
187
    function createEvent( event, depth, sizeOfRow ) {
 
188
        var eventBubble;
 
189
        if( isHashEmpty(intern.unUsedEvents) ) {
 
190
            var incubator = delegate.incubateObject(bubbleOverLay);
 
191
            if (incubator.status !== Component.Ready) {
 
192
                incubator.onStatusChanged = function(status) {
 
193
                    if (status === Component.Ready) {
 
194
                        incubator.object.objectName = children.length;
 
195
                        assignBubbleProperties(incubator.object, event, depth, sizeOfRow);
 
196
                    }
 
197
                }
57
198
            } else {
58
 
                children[i].destroy();
 
199
                incubator.object.objectName = children.length;
 
200
                assignBubbleProperties(incubator.object, event, depth, sizeOfRow);
59
201
            }
 
202
        } else {
 
203
            eventBubble = getUnusedEventBubble();
 
204
            assignBubbleProperties(eventBubble, event, depth, sizeOfRow);
60
205
        }
61
206
    }
62
207
 
63
 
    function createEvent( event ,hour) {
64
 
        var eventBubble = delegate.createObject(bubbleOverLay);
65
 
 
66
 
        eventBubble.clicked.connect( bubbleOverLay.showEventDetails );
67
 
 
68
 
        eventBubble.event = event;
69
 
        eventBubble.title = event.title;
70
 
        eventBubble.location = "Test"//event.location;
71
 
        eventBubble.hour = hour;
72
 
 
73
 
        var yPos = (( event.startTime.getMinutes() * hourHeight) / 60) + hour * hourHeight
 
208
    function assignBubbleProperties(eventBubble, event, depth, sizeOfRow) {
 
209
        var hour = event.startDateTime.getHours();
 
210
        var yPos = (( event.startDateTime.getMinutes() * hourHeight) / 60) + hour * hourHeight
74
211
        eventBubble.y = yPos;
75
212
 
76
 
        var durationMin = (event.endTime.getHours() - event.startTime.getHours()) * 60;
77
 
        durationMin += (event.endTime.getMinutes() - event.startTime.getMinutes());
 
213
        var durationMin = (event.endDateTime.getHours() - event.startDateTime.getHours()) * 60;
 
214
        durationMin += (event.endDateTime.getMinutes() - event.startDateTime.getMinutes());
78
215
        var height = (durationMin * hourHeight )/ 60;
79
 
        eventBubble.height = height;
 
216
        eventBubble.height = (height > eventBubble.minimumHeight) ? height:eventBubble.minimumHeight ;
 
217
 
 
218
        eventBubble.model = bubbleOverLay.model
 
219
        eventBubble.depthInRow = depth;
 
220
        eventBubble.sizeOfRow = sizeOfRow;
 
221
        eventBubble.event = event
 
222
        eventBubble.visible = true;
 
223
        eventBubble.clicked.connect( bubbleOverLay.showEventDetails );
80
224
    }
81
225
 
82
 
    function showSeparator(hour) {
83
 
        var y = ((intern.now.getMinutes() * hourHeight) / 60) + hour * hourHeight;
 
226
    function showSeparator() {
 
227
        var y = ((intern.now.getMinutes() * hourHeight) / 60) + intern.now.getHours() * hourHeight;
84
228
        separator.y = y;
85
229
        separator.visible = true;
86
230
    }