From 4349dec7b49caed7959d9a66b5c0011f41c09220 Mon Sep 17 00:00:00 2001 From: Lubos Date: Mon, 18 Dec 2023 10:15:56 +0100 Subject: [PATCH] Added Switch item based on AbstractEditor --- app/qml/components/MMSwitch.qml | 12 +++++- app/qml/inputs/MMPasswordEditor.qml | 2 +- app/qml/inputs/MMSwitchEditor.qml | 58 +++++++++++++++++++++++++++++ gallery/qml.qrc | 1 + gallery/qml/pages/EditorsPage.qml | 10 ++++- 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 app/qml/inputs/MMSwitchEditor.qml diff --git a/app/qml/components/MMSwitch.qml b/app/qml/components/MMSwitch.qml index 0da197df9..f9426a470 100644 --- a/app/qml/components/MMSwitch.qml +++ b/app/qml/components/MMSwitch.qml @@ -16,9 +16,19 @@ Switch { property string textOn property string textOff + property bool visibleText: true contentItem: Text { - text: (control.textOn.length > 0 && control.textOff.length > 0) ? (control.checked ? control.textOn : control.textOff) : control.text + text: { + if(control.visibleText) { + if(control.textOn.length > 0 && control.textOff.length > 0) { + if(control.checked) + return control.textOn + return control.textOff + } + return control.text + } + } font: __style.p5 color: control.enabled ? __style.forestColor : __style.mediumGreenColor verticalAlignment: Text.AlignVCenter diff --git a/app/qml/inputs/MMPasswordEditor.qml b/app/qml/inputs/MMPasswordEditor.qml index 5385c9da4..22d548145 100644 --- a/app/qml/inputs/MMPasswordEditor.qml +++ b/app/qml/inputs/MMPasswordEditor.qml @@ -18,7 +18,7 @@ MMAbstractEditor { id: root property alias placeholderText: textField.placeholderText - readonly property alias text: textField.text + property alias text: textField.text hasFocus: textField.activeFocus diff --git a/app/qml/inputs/MMSwitchEditor.qml b/app/qml/inputs/MMSwitchEditor.qml new file mode 100644 index 000000000..e8351e444 --- /dev/null +++ b/app/qml/inputs/MMSwitchEditor.qml @@ -0,0 +1,58 @@ +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +import QtQuick +import QtQuick.Controls +import QtQuick.Controls.Basic +import QtQuick.Layouts +import Qt5Compat.GraphicalEffects +import "../components" + +MMAbstractEditor { + id: root + + property var parentValue: parent.value ?? false + property bool parentValueIsNull: parent.valueIsNull ?? false + property bool isReadOnly: parent.readOnly ?? false + + property string textOn: qsTr("True") + property string textOff: qsTr("False") + property alias checked: rightSwitch.checked + + signal editorValueChanged( var newValue, var isNull ) + + hasFocus: rightSwitch.focus + + content: Text { + id: textField + + width: parent.width + rightSwitch.x + anchors.verticalCenter: parent.verticalCenter + + text: root.checked ? root.textOn : root.textOff + color: root.enabled ? __style.nightColor : __style.mediumGreenColor + font: __style.p5 + elide: Text.ElideRight + } + + rightAction: MMSwitch { + id: rightSwitch + + width: 50 + height: parent.height + x: -30 * __dp + + checked: root.checked + textOn: root.textOn + textOff: root.textOff + visibleText: false + + onCheckedChanged: focus = true + } +} diff --git a/gallery/qml.qrc b/gallery/qml.qrc index 63e48064a..fc778d98a 100644 --- a/gallery/qml.qrc +++ b/gallery/qml.qrc @@ -69,5 +69,6 @@ ../app/qml/components/MMBackButton.qml ../app/qml/components/MMIconCheckBoxHorizontal.qml ../app/qml/components/MMIconCheckBoxVertical.qml + ../app/qml/inputs/MMSwitchEditor.qml diff --git a/gallery/qml/pages/EditorsPage.qml b/gallery/qml/pages/EditorsPage.qml index fcc8fcfef..a82e30f14 100644 --- a/gallery/qml/pages/EditorsPage.qml +++ b/gallery/qml/pages/EditorsPage.qml @@ -72,12 +72,20 @@ ScrollView { MMPasswordEditor { title: "MMPasswordEditor" - parentValue: "Password" + text: "Password" //regexp: '(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9])(?=.{6,})' errorMsg: "Password must contain at least 6 characters\nMinimum 1 number, uppercase and lowercase letter and special character" enabled: checkbox.checked width: parent.width } + + MMSwitchEditor { + title: "MMSwitchEditor" + checked: true + warningMsg: checked ? "" : "Should be checked :)" + enabled: checkbox.checked + width: parent.width + } } }