From 426cd746b2f91edfbdb85c57071600938d18273d Mon Sep 17 00:00:00 2001 From: Toast Date: Thu, 27 Mar 2025 02:10:45 +0100 Subject: [PATCH] Add new countdown dialog --- src/Main.qml | 67 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/src/Main.qml b/src/Main.qml index eaa9940..47ae50e 100644 --- a/src/Main.qml +++ b/src/Main.qml @@ -38,11 +38,7 @@ Kirigami.ApplicationWindow { id: addAction icon.name: "list-add-symbolic" text: i18nc("@action:button", "Add countdown") - onTriggered: countdownModel.append({ - name: "New countdown", - description: "This was added with the add countdown button", - date: 1000 - }) + onTriggered: addDialog.open() } ] Kirigami.CardsListView { @@ -114,4 +110,65 @@ Kirigami.ApplicationWindow { } } } + + Kirigami.Dialog { + id: addDialog + title: i18nc("@title:window", "Add countdown") + standardButtons: Kirigami.Dialog.Ok | Kirigami.Dialog.Cancel + padding: Kirigami.Units.largeSpacing + preferredWidth: Kirigami.Units.gridUnit * 20 + + Kirigami.FormLayout { + Controls.TextField { + id: nameField + Kirigami.FormData.label: i18nc("@label:textbox", "Name*:") + onAccepted: descriptionField.forceActiveFocus() + } + Controls.TextField { + id: descriptionField + Kirigami.FormData.label: i18nc("@label:textbox", "Description:") + placeholderText: i18n("Optional") + onAccepted: dateField.forceActiveFocus() + } + Controls.TextField { + id: dateField + Kirigami.FormData.label: i18nc("@label:textbox", "ISO Date*:") + inputMask: "D999-99-99" + onAccepted: addDialog.onAccepted() + } + Controls.Label { + text: "* = required fields" + } + } + + Component.onCompleted: { + const button = standardButton(Kirigami.Dialog.Ok); + button.enabled = Qt.binding(() => requiredFieldsFilled()); + } + + function requiredFieldsFilled() { + return (nameField.text !== "" && dateField.acceptableInput); + } + + function appendDataToModel() { + countdownModel.append({ + name: nameField.text, + description: descriptionField.text, + date: new Date(dateField.text) + }); + } + function clearFieldsAndClose() { + nameField.text = ""; + descriptionField.text = ""; + dateField.text = ""; + addDialog.close(); + } + + onAccepted: { + if (!addDialog.requiredFieldsFilled()) + return; + appendDataToModel(); + clearFieldsAndClose(); + } + } }