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