Pārlūkot izejas kodu

Добавлены настройки login и password

Denis V. Dedkov 1 gadu atpakaļ
vecāks
revīzija
5443c573e2

+ 8 - 0
CMakeLists.txt

@@ -5,6 +5,7 @@ project(nooLight VERSION 1.0 LANGUAGES CXX)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
 set(CMAKE_AUTORCC ON)
+include_directories(src)
 
 find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
 
@@ -15,6 +16,13 @@ qt_add_executable(appnooLight
     resources/resources.qrc
 )
 
+qt_add_qml_module(appnooLight
+  URI ru.ded.noolight
+  VERSION 1.0
+  SOURCES
+    src/settings.h src/settings.cpp
+)
+
 if (ANDROID)
     set_property(TARGET appnooLight APPEND PROPERTY QT_ANDROID_EXTRA_LIBS
         ${QT_ANDROID_SSL_DIR}/no-asm/ssl_3/${ANDROID_ABI}/libcrypto_3.so

+ 1 - 1
resources/qml/LightGroup.qml

@@ -69,7 +69,7 @@ Item {
 
                         Label {
                             anchors.horizontalCenter: parent.horizontalCenter
-                            horizontalAlignment: Text.horizontalCenter
+                            horizontalAlignment: Qt.horizontalCenter
                             text: name
                         }
                     }

+ 19 - 6
resources/qml/SettingsForm.qml

@@ -1,6 +1,9 @@
 import QtQuick 2.0
 import QtQuick.Controls 2.2
 
+import ru.ded.noolight 1.0
+import ru.ded.components 1.0
+
 Page {
     id: root
 
@@ -14,6 +17,16 @@ Page {
             title: qsTr("nooLite service URL")
             inputMethodHint: Qt.ImhUrlCharactersOnly
         }
+        ListElement {
+            name: "login"
+            title: qsTr("Login")
+            inputMethodHint: Qt.ImhLatinOnly
+        }
+        ListElement {
+            name: "password"
+            title: qsTr("Password")
+            hideText: true
+        }
     }
 
     ListView {
@@ -24,15 +37,14 @@ Page {
         delegate: SubtitledItemDelegate {
             width: parent.width
             text: model.title
-            subtitle: settings[model.name]
+            subtitle: model.hideText && Settings[model.name] ? qsTr("Hidden") : Settings[model.name]
 
             onClicked: inputDialog.open()
 
             Dialog {
                 id: inputDialog
 
-                x: (parent.width - width) / 2
-                y: (parent.height - height) / 2
+                anchors.centerIn: parent
                 parent: ApplicationWindow.overlay
 
                 focus: true
@@ -49,14 +61,15 @@ Page {
 
                         width: parent.width
                         focus: true
-                        inputMethodHints: Qt.ImhNoAutoUppercase | model.inputMethodHint
+                        inputMethodHints: model.inputMethodHint
+                        echoMode: model.hideText ? TextInput.Password : TextInput.Normal
                         placeholderText: model.title
-                        text: settings[model.name]
+                        text: Settings[model.name]
                     }
                 }
 
                 onAccepted: {
-                    settings[model.name] = textField.text
+                    Settings[model.name] = textField.text
                 }
             }
         }

+ 0 - 24
resources/qml/SubtitledItemDelegate.qml

@@ -1,24 +0,0 @@
-import QtQuick 2.0
-import QtQuick.Controls 2.2
-
-ItemDelegate {
-    id: root
-
-    property string subtitle: ""
-
-    contentItem: Column {
-        Label {
-            id: titleLabel
-
-            width: parent.width
-            text: root.text
-        }
-
-        Label {
-            width: parent.width
-            font.pixelSize: titleLabel.font.pixelSize - 2
-            text: root.subtitle ? root.subtitle : qsTr("undefined")
-            opacity: 0.8
-        }
-    }
-}

+ 3 - 9
resources/qml/main.qml

@@ -1,7 +1,7 @@
 import QtQuick 2.9
 import QtQuick.Controls 2.2
-import Qt.labs.settings 1.0
 
+import ru.ded.noolight 1.0
 import ru.ded.components 1.0
 
 ApplicationWindow {
@@ -12,16 +12,10 @@ ApplicationWindow {
     height: 480
     title: qsTr("Stack")
 
-    Settings {
-        id: settings
-
-        property string serviceUrl: ""
-    }
-
     LightsModel {
         id: lightsModel
 
-        serviceUrl: settings.serviceUrl
+        serviceUrl: Settings.serviceUrl
 
         onError: (text) => stackView.showError(text)
     }
@@ -102,7 +96,7 @@ ApplicationWindow {
         }
     }
 
-    onClosing: {
+    onClosing: (close) => {
         if (stackView.depth > 1) {
             close.accepted = false
             stackView.pop()

+ 0 - 1
resources/resources.qrc

@@ -4,7 +4,6 @@
         <file>qtquickcontrols2.conf</file>
         <file>qml/HomeForm.qml</file>
         <file>qml/SettingsForm.qml</file>
-        <file>qml/SubtitledItemDelegate.qml</file>
         <file>qml/LightsModel.qml</file>
         <file>qml/LightGroup.qml</file>
         <file>qml/GradientButton.qml</file>

+ 54 - 0
src/settings.cpp

@@ -0,0 +1,54 @@
+#include "settings.h"
+
+namespace Keys {
+
+constexpr auto ServiceUrl = "serviceUrl";
+constexpr auto Login = "login";
+constexpr auto Password = "password";
+
+}
+
+QString Settings::serviceUrl() const
+{
+    return m_settings.value(Keys::ServiceUrl, "https://mynoolightservice.org").toString();
+}
+
+void Settings::setServiceUrl(const QString &newServiceUrl)
+{
+    if (serviceUrl() == newServiceUrl) {
+        return;
+    }
+
+    m_settings.setValue(Keys::ServiceUrl, newServiceUrl);
+    emit serviceUrlChanged();
+}
+
+QString Settings::login() const
+{
+    return m_settings.value(Keys::Login).toString();
+}
+
+void Settings::setLogin(const QString &newLogin)
+{
+    if (login() == newLogin) {
+        return;
+    }
+
+    m_settings.setValue(Keys::Login, newLogin);
+    emit loginChanged();
+}
+
+QString Settings::password() const
+{
+    return m_settings.value(Keys::Password).toString();
+}
+
+void Settings::setPassword(const QString &newPassword)
+{
+    if (password() == newPassword) {
+        return;
+    }
+
+    m_settings.setValue(Keys::Password, newPassword);
+    emit passwordChanged();
+}

+ 37 - 0
src/settings.h

@@ -0,0 +1,37 @@
+#ifndef SETTINGS_H
+#define SETTINGS_H
+
+#include <QObject>
+#include <QQmlEngine>
+#include <QSettings>
+
+class Settings : public QObject
+{
+    Q_OBJECT
+    QML_ELEMENT
+    QML_SINGLETON
+
+    Q_PROPERTY(QString serviceUrl READ serviceUrl WRITE setServiceUrl NOTIFY serviceUrlChanged FINAL)
+    Q_PROPERTY(QString login READ login WRITE setLogin NOTIFY loginChanged FINAL)
+    Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged FINAL)
+
+public:
+    QString serviceUrl() const;
+    void setServiceUrl(const QString &newServiceUrl);
+
+    QString login() const;
+    void setLogin(const QString &newLogin);
+
+    QString password() const;
+    void setPassword(const QString &newPassword);
+
+signals:
+    void serviceUrlChanged();
+    void loginChanged();
+    void passwordChanged();
+
+private:
+    QSettings m_settings;
+};
+
+#endif // SETTINGS_H