Skip to content

Instantly share code, notes, and snippets.

@segphault
Last active June 19, 2016 05:02
Show Gist options
  • Save segphault/48101fe18f4d5272d077ecbee26b075b to your computer and use it in GitHub Desktop.
Save segphault/48101fe18f4d5272d077ecbee26b075b to your computer and use it in GitHub Desktop.
import QtQuick 2.3
import QtQuick.Window 2.2
import QtGraphicalEffects 1.0
import QtQuick.Controls 1.1
ApplicationWindow {
id: root
width: 540
height: 420
color: "black"
visible: true
visibility: "FullScreen"
property var locale: Qt.locale()
property var currentTime: new Date()
property var nextDeparture: new Date()
property var schedule: null
property int duration: 0
function computeDeparture() {
if (!schedule) return;
currentTime = new Date();
if (nextDeparture == null || nextDeparture < currentTime) {
var day = currentTime.getDay();
var times = day === 0 ? schedule["sunday"] :
day === 6 ? schedule["saturday"] :
schedule["weekday"];
for (var i in times) {
nextDeparture = Date.fromLocaleTimeString(locale, times[i][0], "hh:mm:ss");
if (nextDeparture > currentTime) {
var arrival = Date.fromLocaleTimeString(locale, times[i][1], "hh:mm:ss");
duration = (arrival - nextDeparture) / 1000 / 60;
break;
}
}
}
}
Component.onCompleted: {
var request = new XMLHttpRequest()
request.open('GET', "schedule.json")
request.onreadystatechange = function(event) {
if (request.readyState == XMLHttpRequest.DONE) {
schedule = JSON.parse(request.responseText)
computeDeparture()
}
}
request.send()
}
Text {
id: labelDate
color: "white"
font.pointSize: 38
font.capitalization: Font.AllUppercase
horizontalAlignment: Text.AlignHCenter
text: currentTime.toLocaleString(locale, "ddd MMMM d")
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
anchors.margins: 30
}
Text {
color: "white"
font.pointSize: 76
horizontalAlignment: Text.AlignHCenter
text: currentTime.toLocaleString(locale, "h:mm AP")
anchors.left: parent.left
anchors.right: parent.right
anchors.top: labelDate.bottom
anchors.margins: 30
}
Text {
color: "white"
font.pointSize: 48
text: nextDeparture.toLocaleString(locale, "h:mm AP") + " (" + duration + "M)"
anchors.leftMargin: 25
anchors.left: train.right
anchors.verticalCenter: train.verticalCenter
}
Image {
id: train
source: "train.png"
width: 80
height: 80
anchors.left: parent.left
anchors.bottom: parent.bottom
anchors.margins: 10
}
ColorOverlay {
anchors.fill: train
source: train
color: "#ffffffff"
}
Timer {
interval: 1000
running: true
repeat: true
onTriggered: computeDeparture()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment