|
| 1 | +import QtQuick |
| 2 | +import QtQuick.Layouts |
| 3 | +import qs.Commons |
| 4 | +import qs.Widgets |
| 5 | + |
| 6 | +Item { |
| 7 | + id: root |
| 8 | + |
| 9 | + // https://stackoverflow.com/questions/4156434/javascript-get-the-first-day-of-the-week-from-current-date |
| 10 | + function getMonday(d) { |
| 11 | + d = new Date(d); |
| 12 | + var day = d.getDay(), diff = d.getDate() - day + (day == 0 ? -6 : 1); // adjust when day is sunday |
| 13 | + return new Date(d.setDate(diff)); |
| 14 | + } |
| 15 | + |
| 16 | + function getWeekFormat(d) { |
| 17 | + const date = d.getUTCDate().toString().padStart(2, '0') + "/" + (d.getUTCMonth() + 1).toString().padStart(2, '0') + "/" + d.getUTCFullYear(); |
| 18 | + return pluginApi?.tr("panel.week-format", { |
| 19 | + date: date |
| 20 | + }); |
| 21 | + } |
| 22 | + |
| 23 | + function getMenuUrl(d) { |
| 24 | + const day = d.getUTCDate().toString().padStart(2, '0'); |
| 25 | + const month = (d.getUTCMonth() + 1).toString().padStart(2, '0'); |
| 26 | + const year = d.getUTCFullYear().toString().slice(-2); |
| 27 | + return "https://www.uclouvain.be/fr/system/files/uclouvain_assetmanager/groups/cms-editors-resto-u/" + day + month + year + ".jpeg"; |
| 28 | + } |
| 29 | + |
| 30 | + // Plugin API (injected by PluginPanelSlot) |
| 31 | + property var pluginApi: null |
| 32 | + |
| 33 | + // SmartPanel properties (required for panel behavior) |
| 34 | + readonly property var geometryPlaceholder: panelContainer |
| 35 | + readonly property bool allowAttach: true |
| 36 | + |
| 37 | + // Preferred dimensions |
| 38 | + property real contentPreferredWidth: 500 * Style.uiScaleRatio |
| 39 | + property real contentPreferredHeight: 540 * Style.uiScaleRatio |
| 40 | + |
| 41 | + property date currentMonday: getMonday(new Date()) |
| 42 | + property string dateString: getWeekFormat(currentMonday) |
| 43 | + |
| 44 | + anchors.fill: parent |
| 45 | + |
| 46 | + Rectangle { |
| 47 | + id: panelContainer |
| 48 | + anchors.fill: parent |
| 49 | + color: "transparent" |
| 50 | + |
| 51 | + ColumnLayout { |
| 52 | + id: dateSelector |
| 53 | + |
| 54 | + anchors { |
| 55 | + fill: parent |
| 56 | + margins: Style.marginM |
| 57 | + } |
| 58 | + spacing: Style.marginM |
| 59 | + Rectangle { |
| 60 | + id: headerRect |
| 61 | + radius: Style.marginM |
| 62 | + Layout.preferredWidth: parent.width |
| 63 | + Layout.preferredHeight: 40 * Style.uiScaleRatio |
| 64 | + color: "transparent" |
| 65 | + border.color: Color.mPrimary |
| 66 | + |
| 67 | + NButton { |
| 68 | + anchors.left: parent.left |
| 69 | + anchors.leftMargin: Style.marginM |
| 70 | + anchors.verticalCenter: parent.verticalCenter |
| 71 | + |
| 72 | + width: 20 * Style.uiScaleRatio |
| 73 | + height: 20 * Style.uiScaleRatio |
| 74 | + |
| 75 | + NIcon { |
| 76 | + icon: "caret-left" |
| 77 | + anchors.centerIn: parent |
| 78 | + } |
| 79 | + |
| 80 | + onClicked: { |
| 81 | + currentMonday = getMonday(new Date(currentMonday.setDate(currentMonday.getDate() - 7))); |
| 82 | + dateString = getWeekFormat(currentMonday); |
| 83 | + menuImage.loaded = false; |
| 84 | + menuImage.source = getMenuUrl(currentMonday); |
| 85 | + loadingOverlay.visible = true; |
| 86 | + errorOverlay.visible = false; |
| 87 | + rotationAnimation.running = true; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + NText { |
| 92 | + anchors.centerIn: parent |
| 93 | + text: dateString |
| 94 | + } |
| 95 | + |
| 96 | + NButton { |
| 97 | + anchors.right: parent.right |
| 98 | + anchors.rightMargin: Style.marginM |
| 99 | + anchors.verticalCenter: parent.verticalCenter |
| 100 | + |
| 101 | + width: 20 * Style.uiScaleRatio |
| 102 | + height: 20 * Style.uiScaleRatio |
| 103 | + |
| 104 | + NIcon { |
| 105 | + icon: "caret-right" |
| 106 | + anchors.centerIn: parent |
| 107 | + } |
| 108 | + |
| 109 | + onClicked: { |
| 110 | + currentMonday = getMonday(new Date(currentMonday.setDate(currentMonday.getDate() + 7))); |
| 111 | + dateString = getWeekFormat(currentMonday); |
| 112 | + menuImage.loaded = false; |
| 113 | + menuImage.source = getMenuUrl(currentMonday); |
| 114 | + loadingOverlay.visible = true; |
| 115 | + errorOverlay.visible = false; |
| 116 | + rotationAnimation.running = true; |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + Item { |
| 122 | + id: imageContainer |
| 123 | + Layout.fillWidth: true |
| 124 | + Layout.fillHeight: true |
| 125 | + |
| 126 | + NBox { |
| 127 | + id: loadingOverlay |
| 128 | + anchors.centerIn: parent |
| 129 | + visible: !menuImage.loaded |
| 130 | + color: "transparent" |
| 131 | + |
| 132 | + NIcon { |
| 133 | + icon: "refresh" |
| 134 | + font.pixelSize: 48 |
| 135 | + anchors.centerIn: parent |
| 136 | + RotationAnimation on rotation { |
| 137 | + id: rotationAnimation |
| 138 | + loops: Animation.Infinite |
| 139 | + duration: 1000 |
| 140 | + from: 0 |
| 141 | + to: 360 |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + NBox { |
| 147 | + id: errorOverlay |
| 148 | + anchors.centerIn: parent |
| 149 | + visible: false |
| 150 | + color: "transparent" |
| 151 | + |
| 152 | + NText { |
| 153 | + text: pluginApi?.tr("panel.error.no-menu") |
| 154 | + anchors.centerIn: parent |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + Image { |
| 159 | + id: menuImage |
| 160 | + property bool loaded: false |
| 161 | + anchors.fill: parent |
| 162 | + fillMode: Image.PreserveAspectFit |
| 163 | + source: getMenuUrl(currentMonday) |
| 164 | + cache: true |
| 165 | + asynchronous: true |
| 166 | + |
| 167 | + onStatusChanged: { |
| 168 | + if (status === Image.Ready) { |
| 169 | + loaded = true; |
| 170 | + rotationAnimation.running = false; |
| 171 | + loadingOverlay.visible = false; |
| 172 | + errorOverlay.visible = false; |
| 173 | + } else if (status === Image.Error) { |
| 174 | + loaded = false; |
| 175 | + rotationAnimation.running = false; |
| 176 | + loadingOverlay.visible = false; |
| 177 | + errorOverlay.visible = true; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments