Warning, /libraries/kopeninghours/tests/example.qml is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 import QtQuick 2.14
0008 import QtQuick.Layouts 1.1
0009 import QtQuick.Controls 2.1 as QQC2
0010 import org.kde.kirigami 2.0 as Kirigami
0011 import org.kde.kholidays 1.0 as KHolidays
0012 import org.kde.kopeninghours 1.0
0013 
0014 Kirigami.ApplicationWindow {
0015     title: "Opening Hours Demo"
0016     reachableModeEnabled: false
0017 
0018     width: 640
0019     height: 480
0020 
0021     pageStack.initialPage: mainPage
0022 
0023     Component {
0024         id: openingHoursDelegate
0025         Item {
0026             id: delegateRoot
0027             property var dayData: model
0028             implicitHeight: row.implicitHeight
0029             Row {
0030                 id: row
0031                 QQC2.Label {
0032                     text: dayData.shortDayName
0033                     width: delegateRoot.ListView.view.labelWidth + Kirigami.Units.smallSpacing
0034                     Component.onCompleted: delegateRoot.ListView.view.labelWidth = Math.max(delegateRoot.ListView.view.labelWidth, implicitWidth)
0035                     font.italic: dayData.isToday
0036                 }
0037                 Repeater {
0038                     model: dayData.intervals
0039                     Rectangle {
0040                         id: intervalBox
0041                         property var interval: modelData
0042                         color: {
0043                             switch (interval.state) {
0044                                 case Interval.Open: return Kirigami.Theme.positiveBackgroundColor;
0045                                 case Interval.Closed: return Kirigami.Theme.negativeBackgroundColor;
0046                                 case Interval.Unknown: return Kirigami.Theme.neutralBackgroundColor;
0047                             }
0048                             return "transparent";
0049                         }
0050                         width: {
0051                             var ratio = (interval.estimatedEnd - interval.begin) / (24 * 60 * 60 * 1000);
0052                             return ratio * (delegateRoot.ListView.view.width - delegateRoot.ListView.view.labelWidth - Kirigami.Units.smallSpacing);
0053                         }
0054                         height: Kirigami.Units.gridUnit
0055                         gradient: Gradient {
0056                             orientation: Gradient.Horizontal
0057                             GradientStop { position: 0.0; color: intervalBox.color }
0058                             GradientStop { position: (interval.end - interval.begin) / (interval.estimatedEnd - interval.begin); color: intervalBox.color }
0059                             GradientStop { position: 1.0; color: interval.hasOpenEndTime ? Kirigami.Theme.negativeBackgroundColor : intervalBox.color }
0060                         }
0061 
0062                         QQC2.Label {
0063                             id: commentLabel
0064                             text: interval.comment
0065                             anchors.centerIn: parent
0066                             visible: commentLabel.implicitWidth < intervalBox.width
0067                             font.italic: true
0068                         }
0069                     }
0070                 }
0071             }
0072             Rectangle {
0073                 id: nowMarker
0074                 property double position: (Date.now() - dayData.dayBegin) / (24 * 60 * 60 * 1000)
0075                 visible: position >= 0.0 && position < 1.0
0076                 color: Kirigami.Theme.textColor
0077                 width: 2
0078                 height: Kirigami.Units.gridUnit
0079                 x: position * (delegateRoot.ListView.view.width - delegateRoot.ListView.view.labelWidth - Kirigami.Units.smallSpacing)
0080                     + delegateRoot.ListView.view.labelWidth + Kirigami.Units.smallSpacing
0081             }
0082         }
0083     }
0084 
0085     Component {
0086         id: mainPage
0087         Kirigami.Page {
0088             id: page
0089             title: "OSM Opening Hours Expression Evaluator Demo"
0090             property var oh: {
0091                 var v = OpeningHoursParser.parse(expression.text);
0092                 v.setLocation(52.5, 13.0);
0093                 return v;
0094             }
0095 
0096             function evaluateCurrentState() {
0097                 var tmp = OpeningHoursParser.parse(expression.text, intervalMode.checked ? OpeningHours.IntervalMode : OpeningHours.PointInTimeMode);
0098                 tmp.setLocation(latitude.text, longitude.text);
0099                 tmp.region = region.currentText;
0100                 page.oh = tmp;
0101                 currentState.text = Qt.binding(function() { return intervalModel.currentState; });
0102             }
0103 
0104             ColumnLayout {
0105                 anchors.fill: parent
0106 
0107                 QQC2.TextField {
0108                     id: expression
0109                     Layout.fillWidth: true
0110                     text: "Mo-Fr 08:00-12:00,13:00-17:30; Sa 08:00-12:00; Su unknown \"on appointment\""
0111                     onTextChanged: evaluateCurrentState();
0112                 }
0113 
0114                 RowLayout {
0115                     QQC2.RadioButton {
0116                         id: intervalMode
0117                         text: "Interval Mode"
0118                         checked: true
0119                         onCheckedChanged: evaluateCurrentState()
0120                     }
0121                     QQC2.RadioButton {
0122                         id: pointInTimeMode
0123                         text: "Point in Time Mode"
0124                         onCheckedChanged: evaluateCurrentState()
0125                     }
0126                 }
0127 
0128                 RowLayout {
0129                     QQC2.Label { text: "Latitude:" }
0130                     QQC2.TextField {
0131                         id: latitude
0132                         text: "52.5"
0133                         onTextChanged: evaluateCurrentState();
0134                     }
0135                     QQC2.Label { text: "Longitude:" }
0136                     QQC2.TextField {
0137                         id: longitude
0138                         text: "13.0"
0139                         onTextChanged: evaluateCurrentState();
0140                     }
0141                 }
0142 
0143                 KHolidays.HolidayRegionsModel { id: regionModel }
0144                 QQC2.ComboBox {
0145                     id: region
0146                     model: regionModel
0147                     textRole: "region"
0148                     onCurrentIndexChanged: evaluateCurrentState();
0149                 }
0150 
0151                 QQC2.Label {
0152                     text: {
0153                         switch (oh.error) {
0154                             case OpeningHours.NoError:
0155                                 return "Expression is valid.";
0156                             case OpeningHours.SyntaxError:
0157                                 return "Syntax error!";
0158                             case OpeningHours.MissingRegion:
0159                                 return "Expression needs to know the ISO 3166-2 region it is evaluated for.";
0160                             case OpeningHours.MissingLocation:
0161                                 return "Expression needs to know the geo coordinates of the location it is evaluated for.";
0162                             case OpeningHours.UnsupportedFeature:
0163                                 return "Expression uses a feature that is not supported/implemented yet.";
0164                             case OpeningHours.IncompatibleMode:
0165                                 return "Expression uses an incompatible evaluation mode.";
0166                             case OpeningHours.EvaluationError:
0167                                 return "Runtime error."
0168                         }
0169                     }
0170                 }
0171                 QQC2.Label {
0172                     text: "Normalized: " + oh.normalizedExpression
0173                     visible: oh.error != OpeningHours.SyntaxError && oh.normalizedExpression != expression.text
0174                 }
0175 
0176                 QQC2.Label {
0177                     id: currentState
0178                 }
0179 
0180                 IntervalModel {
0181                     id: intervalModel
0182                     openingHours: page.oh
0183                     beginDate: intervalModel.beginOfWeek(new Date())
0184                     endDate: new Date(intervalModel.beginDate.getTime() + 7 * 24 * 3600 * 1000)
0185                 }
0186 
0187                 ListView {
0188                     id: intervalView
0189                     visible: intervalMode.checked
0190                     model: intervalModel
0191                     delegate: openingHoursDelegate
0192                     property int labelWidth: 0
0193                     Layout.fillWidth: true
0194                     Layout.fillHeight: true
0195                     spacing: Kirigami.Units.smallSpacing
0196                     clip: true
0197                     header: Row {
0198                         id: intervalHeader
0199                         property int itemWidth: (intervalHeader.ListView.view.width -  intervalHeader.ListView.view.labelWidth - Kirigami.Units.smallSpacing) / 8
0200                         x: intervalHeader.ListView.view.labelWidth + Kirigami.Units.smallSpacing + intervalHeader.itemWidth/2
0201                         Repeater {
0202                             model: [3, 6, 9, 12, 15, 18, 21]
0203                             QQC2.Label {
0204                                 text: intervalModel.formatTimeColumnHeader(modelData, 0);
0205                                 width: intervalHeader.itemWidth
0206                                 horizontalAlignment: Qt.AlignHCenter
0207                             }
0208                         }
0209                     }
0210                 }
0211 
0212                 Timer {
0213                     interval: 60000
0214                     repeat: true
0215                     running: true
0216                     onTriggered: evaluateCurrentState()
0217                 }
0218 
0219                 Component.onCompleted: evaluateCurrentState()
0220             }
0221         }
0222     }
0223 }
0224