IndoorMapInfoSheetOpeningHoursDelegate.qml 6.18 KB
Newer Older
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
/*
    SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

import QtQuick 2.12
import QtQuick.Controls 2.1 as QQC2
import org.kde.kirigami 2.7 as Kirigami
import org.kde.kopeninghours 1.0
import org.kde.itinerary 1.0

Column {
    id: root
    property var model
    property var mapData

    property var oh: {
        var v = OpeningHoursParser.parse(model.value);
        v.region = root.mapData.regionCode;
        v.timeZone = root.mapData.timeZone;
        v.setLocation(root.mapData.center.y, root.mapData.center.x);
        if (v.error != OpeningHours.NoError) {
            console.log("Opening hours parsing error:", v.error, root.mapData.region, root.mapData.timeZone)
        }
        return v;
    }

    QQC2.Label {
        property var currentInterval: root.oh.interval(new Date())

        id: currentState
33
        text: intervalModel.currentState // TODO we could update this every minute
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
        color: {
            switch (currentInterval.state) {
                case Interval.Open: return Kirigami.Theme.positiveTextColor;
                case Interval.Closed: return Kirigami.Theme.negativeTextColor;
                default: return Kirigami.Theme.textColor;
            }
        }
        visible: text !== ""
    }

    Component {
        id: intervalDelegate
        Item {
            id: delegateRoot
            property var dayData: model
            implicitHeight: row.implicitHeight
            Row {
                id: row
                QQC2.Label {
Volker Krause's avatar
Volker Krause committed
53
                    text: dayData.shortDayName
54
55
                    width: delegateRoot.ListView.view.labelWidth + Kirigami.Units.smallSpacing
                    Component.onCompleted: delegateRoot.ListView.view.labelWidth = Math.max(delegateRoot.ListView.view.labelWidth, implicitWidth)
Volker Krause's avatar
Volker Krause committed
56
                    font.bold: dayData.isToday
57
58
59
60
61
62
                }
                Repeater {
                    model: dayData.intervals
                    Rectangle {
                        id: intervalBox
                        property var interval: modelData
63
                        property var closeColor: Kirigami.Theme.negativeBackgroundColor;
64
65
66
                        color: {
                            switch (interval.state) {
                                case Interval.Open: return Kirigami.Theme.positiveBackgroundColor;
67
                                case Interval.Closed: return intervalBox.closeColor;
68
69
70
71
72
                                case Interval.Unknown: return Kirigami.Theme.neutralBackgroundColor;
                            }
                            return "transparent";
                        }
                        width: {
73
                            var ratio = (interval.estimatedEnd - interval.begin) / (24 * 60 * 60 * 1000);
74
75
76
                            return ratio * (delegateRoot.ListView.view.width - delegateRoot.ListView.view.labelWidth - Kirigami.Units.smallSpacing);
                        }
                        height: Kirigami.Units.gridUnit
77
78
79
80
81
82
                        gradient: Gradient {
                            orientation: Gradient.Horizontal
                            GradientStop { position: 0.0; color: intervalBox.color }
                            GradientStop { position: (interval.end - interval.begin) / (interval.estimatedEnd - interval.begin); color: intervalBox.color }
                            GradientStop { position: 1.0; color: interval.hasOpenEndTime ? intervalBox.closeColor : intervalBox.color }
                        }
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

                        QQC2.Label {
                            id: commentLabel
                            text: interval.comment
                            anchors.centerIn: parent
                            visible: commentLabel.implicitWidth < intervalBox.width
                            font.italic: true
                        }
                    }
                }
            }
            Rectangle {
                id: nowMarker
                property double position: (Date.now() - dayData.dayBegin) / (24 * 60 * 60 * 1000)
                visible: position >= 0.0 && position < 1.0
                color: Kirigami.Theme.textColor
                width: 2
                height: Kirigami.Units.gridUnit
                x: position * (delegateRoot.ListView.view.width - delegateRoot.ListView.view.labelWidth - Kirigami.Units.smallSpacing)
                    + delegateRoot.ListView.view.labelWidth + Kirigami.Units.smallSpacing
            }
        }
    }

    IntervalModel {
        id: intervalModel
        openingHours: root.oh
Volker Krause's avatar
Volker Krause committed
110
111
112
        // TODO we could use the layover time here, if available and in the future
        beginDate: intervalModel.beginOfWeek(new Date())
        endDate: new Date(intervalModel.beginDate.getTime() + 7 * 24 * 3600 * 1000)
113
114
    }

115
116
117
118
    FontMetrics {
        id: fm
    }

119
120
121
122
123
124
125
126
127
128
129
130
131
    ListView {
        id: intervalView
        width: parent.width
        height: contentHeight
        boundsBehavior: Flickable.StopAtBounds
        visible: root.oh.error == OpeningHours.NoError
        model: intervalModel
        delegate: intervalDelegate
        property int labelWidth: 0
        spacing: Kirigami.Units.smallSpacing
        clip: true
        header: Row {
            id: intervalHeader
132
133
            property int colCount: (intervalView.width - Kirigami.Units.smallSpacing - intervalView.labelWidth) / fm.advanceWidth(intervalModel.formatTimeColumnHeader(12, 59)) < 8 ? 4 : 8
            property int itemWidth: (intervalHeader.ListView.view.width -  intervalHeader.ListView.view.labelWidth - Kirigami.Units.smallSpacing) / colCount
134
135
            x: intervalHeader.ListView.view.labelWidth + Kirigami.Units.smallSpacing + intervalHeader.itemWidth/2
            Repeater {
Volker Krause's avatar
Volker Krause committed
136
                // TODO we might need to use less when space constrained horizontally
137
                model: colCount - 1
138
                QQC2.Label {
139
                    text: intervalModel.formatTimeColumnHeader((modelData + 1) * 24/colCount, 0)
140
141
142
143
144
145
146
147
148
149
150
151
152
                    width: intervalHeader.itemWidth
                    horizontalAlignment: Qt.AlignHCenter
                }
            }
        }
    }

    QQC2.Label {
        id: fallbackLabel
        visible: !intervalView.visible
        text: model.value.replace(/;\s*/g, "\n")
    }
}