summaryrefslogtreecommitdiffstats
path: root/qml/components/TrackSwitcher.qml
blob: d570a1cbadc731ca9ca2eee295839126b1d7c4b7 (plain)
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
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
**     of its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.1
import TalkSchedule 1.0

import "functions.js" as Functions

Rectangle {
    id: root
    objectName: "trackSwitcher"
    property bool isViewScrolling: false
    property var firstEvent
    color: Theme.colors.white

    DaySwitcher {
        id: daysWitcher
        anchors.top: parent.top
        width: parent.width
        height: Theme.sizes.titleHeight
    }

    SortFilterModel{
        id: currentDayTracksModel
        sortRole: "trackNumber"
        filterRole: "day"
        filterRegExp: new RegExp(daysWitcher.dayId)
        model: ModelsSingleton.trackModel
        function data(index, role)
        {
            return get(index, role)
        }
    }
    SortFilterModel {
        id: currentDayBreaksModel
        model: ModelsSingleton.breakModel
        sortRole: "start"
        filterRole: "day"
        filterRegExp: new RegExp(daysWitcher.dayId)
        function data(index, role)
        {
            return get(index, role)
        }
    }

    // Keep the connection in case the model would not be ready at startup
    Connections {
        target: ModelsSingleton.trackModel
        onDataReady: currentDayTracksModel.model = ModelsSingleton.trackModel
    }

    Connections {
        target: ModelsSingleton.breakModel
        onDataReady: currentDayBreaksModel.model = ModelsSingleton.breakModel
    }

    Connections {
        target: daysWitcher
        onDayIdChanged: {
            ModelsSingleton.timeListModel.fileNameTag = "TimeObject." + daysWitcher.dayId
            ModelsSingleton.timeListModel.load()
            ModelsSingleton.timeListModel.tracksTodayModel = currentDayTracksModel
        }
    }

    function setFirstEvent(time)
    {
        if (!Functions.isStartTimeAfterNow(firstEvent)) {
            if (time < firstEvent || Functions.isStartTimeAfterNow(time))
                firstEvent = time
        } else {
            if (time < firstEvent && Functions.isStartTimeAfterNow(time))
                firstEvent = time
        }
    }

    function getTimeRange(model)
    {
        var isEndTime = true
        var earliestTime = model.data(0, "start")
        if (firstEvent === undefined)
            firstEvent = earliestTime
        setFirstEvent(earliestTime)
        var latestTime = model.data(0, "end") // earliestTime
        var time

        var timeHours
        var earliestHours = Functions.getHour(earliestTime)
        var latestHours = Functions.getHour(latestTime, isEndTime)

        // Count here what is the first hour and last hour that needs to be shown in time listView
        // for example 10.00 11.00 12.00 ... or 08.00 09.00 10.00
        var modelCountAfterStart = model.rowCount() - 1;
        var halfModelCount = Math.floor(modelCountAfterStart / 2)
        var needOneMoreItem = halfModelCount * 2 !== modelCountAfterStart
        for (var i = 1; i < halfModelCount; i++) {

            var time1 = model.data(i, "start")
            setFirstEvent(time1)
            var time2 = model.data(i+1, "start")
            setFirstEvent(time2)

            time = time1 < time2 ? time1 : time2
            timeHours = Functions.getHour(time)
            earliestHours = Functions.getHour(earliestTime)
            if (timeHours < earliestHours )
                earliestTime = time

            time1 = model.data(i, "end")
            time2 = model.data(i+1, "end")
            time = time1 < time2 ? time2 : time1
            timeHours = Functions.getHour(time, isEndTime)
            latestHours = Functions.getHour(latestTime, isEndTime)
            if (timeHours > latestHours)
                latestTime = time
        }

        if (needOneMoreItem) {
            time = model.data(model.rowCount() - 1, "start")
            setFirstEvent(time)
            timeHours = Functions.getHour(time)
            earliestHours = Functions.getHour(earliestTime)
            if (timeHours < earliestHours )
                earliestTime = time

            time = model.data(model.rowCount() - 1, "end")
            timeHours = Functions.getHour(time, isEndTime)
            latestHours = Functions.getHour(latestTime, isEndTime)
            if (timeHours > latestHours)
                latestTime = time
        }

        var temp = []
        var timeCount = Functions.getHour(latestTime, isEndTime) - Functions.getHour(earliestTime)
        var hours = Functions.getHour(earliestTime)

        for (var j = 0; j <= timeCount; j++) {
            var date = new Date
            date.setHours(hours + j)
            // HACK, ISOString ignores timezone offset, so add it to date
            date.setHours(date.getHours()+ date.getHours() - date.getUTCHours())
            date.setMinutes(0)
            temp.push(date.toISOString())
        }

        return temp
    }

    Connections {
        target: ModelsSingleton.timeListModel
        onDataReady: {
            firstEvent = undefined
            flickable1.contentX = 0
            var temp = []
            var tempTimeEvents = []
            var tempTimeBreaks = []
            if (ModelsSingleton.timeListModel.rowCount() > 0)
                tempTimeEvents = getTimeRange(ModelsSingleton.timeListModel)
            if (currentDayBreaksModel.rowCount() > 0)
                tempTimeBreaks = getTimeRange(currentDayBreaksModel)

            if (tempTimeBreaks.length > 0) {
                var firstEventTime = Functions.getHour(tempTimeEvents[0])
                var lastEventTime = Functions.getHour(tempTimeEvents[tempTimeEvents.length - 1], true)
                var firstBreakTime = Functions.getHour(tempTimeBreaks[0])
                var lastBreakTime = Functions.getHour(tempTimeBreaks[tempTimeBreaks.length - 1], true)

                var earliestTime = firstEventTime < firstBreakTime ? tempTimeEvents[0] : tempTimeBreaks[0]
                var latestTime = lastEventTime < lastBreakTime ? tempTimeBreaks[tempTimeBreaks.length - 1] : tempTimeEvents[tempTimeEvents.length - 1]

                var timeCount = Functions.getHour(latestTime, true) - Functions.getHour(earliestTime)
                var hours = Functions.getHour(earliestTime)

                for (var j = 0; j <= timeCount; j++) {
                    var date = new Date
                    date.setHours(hours + j)
                    // HACK, ISOString ignores timezone offset, so add it to date
                    date.setHours(date.getHours()+ date.getHours() - date.getUTCHours())
                    date.setMinutes(0)
                    temp.push(date.toISOString())
                }
            } else {
                temp = tempTimeEvents
            }

            timeColumn.timeList = temp
            if (!!firstEvent)
                flickable1.contentX = Functions.countTrackPosition(firstEvent)
        }
    }

    TrackHeader {
        id: trackHeader
        z: 3
        anchors.top: daysWitcher.bottom
        anchors.topMargin: Theme.sizes.dayLabelHeight
        anchors.left: parent.left
        width: Theme.sizes.trackHeaderWidth
        model: currentDayTracksModel
        height: listView.height
    }

    Flickable {
        id: flickable1
        anchors.left: trackHeader.right
        anchors.top: daysWitcher.bottom
        height: parent.height
        width: root.width
        clip: true
        contentWidth: timeColumn.width
        flickableDirection: Flickable.HorizontalFlick
        Column {
            spacing: 0
            Row {
                id: timeColumn
                property var timeList: []
                height: Theme.sizes.dayLabelHeight
                z: 2
                Repeater {
                    id: timeColumnList
                    model: timeColumn.timeList
                    delegate: Rectangle {
                        color: Theme.colors.white
                        width: Theme.sizes.timeColumnWidth
                        height: timeColumn.height
                        Text {
                            id: repeaterText;
                            anchors.verticalCenter: parent.verticalCenter
                            anchors.left: parent.left
                            font.pointSize: Theme.fonts.seven_pt
                            text: Qt.formatTime(timeColumn.timeList[index], "h:mm")
                        }
                    }
                }
            }
            ListView {
                id: listView
                height: root.height - daysWitcher.height - timeColumn.height
                width: parent.width
                interactive: true
                clip: true
                boundsBehavior: Flickable.StopAtBounds
                delegate: Track {}
                model: currentDayTracksModel
                Component.onCompleted: breakColumn.height = listView.contentHeight
                Item {
                    id: breakColumn
                    z: 2
                    anchors.top: parent.top
                    anchors.bottom: parent.bottom
                    width: parent.width
                    Repeater {
                        id: breaks
                        model: currentDayBreaksModel
                        Rectangle {
                            color: mouseArea.pressed ? Theme.colors.lightgray : Theme.colors.smokewhite
                            anchors.top: breakColumn.top
                            x: Functions.countTrackPosition(start)
                            width: Functions.countTrackWidth(start, end) - Theme.margins.ten
                            height: Math.min(breakColumn.height, listView.contentHeight - Theme.margins.ten)
                            Text {
                                function info()
                                {
                                    var info = name +  "\n" + Qt.formatTime(start, "h:mm") + " - " + Qt.formatTime(end, "h:mm")
                                    if (!!performer)
                                        info = info + "\n" + Theme.text.by.arg(performer)
                                    if (!!room)
                                        info = info + "\n" + Theme.text.room.arg(room)
                                    return info
                                }

                                width: parent.width
                                verticalAlignment: Text.AlignVCenter
                                height: parent.height
                                horizontalAlignment: Text.AlignHCenter
                                font.pointSize: Theme.fonts.seven_pt
                                text: info()
                                color: Theme.colors.darkgray
                            }
                            MouseArea {
                                id: mouseArea
                                anchors.fill: parent
                                enabled: !!associatedEventId
                                onClicked: stack.push({"item" : Qt.resolvedUrl("Event.qml"), "properties" : {"eventId" : associatedEventId}})
                            }
                        }
                    }
                }
                onContentYChanged: {
                    if (isViewScrolling === false) {
                        isViewScrolling = true;
                        trackHeader.contentY = listView.contentY
                        isViewScrolling = false;
                    }
                }
            }
        }

    }
}