From fd5afa267c54d782475244cc14cdca1dcf5e2500 Mon Sep 17 00:00:00 2001 From: Black Hat Date: Tue, 27 Feb 2018 19:37:53 +0800 Subject: [PATCH] Tweak UI. --- matrix/controller.cpp | 46 +++++++++++++++---------------- matrix/controller.h | 13 ++++++++- qml/Login.qml | 64 ++++++++++++++++--------------------------- qml/main.qml | 6 ++-- 4 files changed, 59 insertions(+), 70 deletions(-) diff --git a/matrix/controller.cpp b/matrix/controller.cpp index b2f74e9..382d902 100644 --- a/matrix/controller.cpp +++ b/matrix/controller.cpp @@ -3,39 +3,30 @@ #include "libqmatrixclient/connection.h" Controller::Controller(QObject *parent) : QObject(parent) { - + connect(connection, &Connection::connected, this, &Controller::connected); + connect(connection, &Connection::resolveError, this, &Controller::reconnect); + connect(connection, &Connection::syncError, this, &Controller::reconnect); + connect(connection, &Connection::syncDone, this, &Controller::resync); } Controller::~Controller() { } -void Controller::init() { - connect(connection, &Connection::connected, - [=](){ - qInfo() << "Matrix connected."; - setUserID(connection->userId()); - setToken(connection->accessToken()); - } - ); - - connect(connection, &Connection::resolveError, this, &Controller::reconnect); - connect(connection, &Connection::syncError, this, &Controller::reconnect); - connect(connection, &Connection::syncDone, this, &Controller::resync); -} - void Controller::login(QString home, QString user, QString pass) { - qInfo() << "UserID:" << userID; - qInfo() << "Token:" << token; - qInfo() << "Home:" << home; - qInfo() << "User:" << user; - qInfo() << "Pass:" << pass; - if(!userID.isEmpty() && !token.isEmpty()) { - qInfo() << "Using token."; - connection->connectWithToken(userID, token, ""); + if(home.isEmpty()) home = "matrix.org"; + qDebug() << "UserID:" << userID; + qDebug() << "Token:" << token; + qDebug() << "Home:" << home; + qDebug() << "User:" << user; + qDebug() << "Pass:" << pass; + + if(!userID.isEmpty() && !token.isEmpty()) { + qDebug() << "Using token."; + connection->connectWithToken(userID, token, ""); } else if(!user.isEmpty() && !pass.isEmpty()) { - qInfo() << "Using given credential."; + qDebug() << "Using given credential."; connection->connectToServer("@"+user+":"+home, pass, ""); } } @@ -43,6 +34,13 @@ void Controller::login(QString home, QString user, QString pass) { void Controller::logout() { userID = ""; token = ""; + setIsLogin(false); +} + +void Controller::connected() { + setUserID(connection->userId()); + setToken(connection->accessToken()); + setIsLogin(true); } void Controller::resync() { diff --git a/matrix/controller.h b/matrix/controller.h index 5420e6b..a0e8314 100644 --- a/matrix/controller.h +++ b/matrix/controller.h @@ -11,6 +11,7 @@ class Controller : public QObject { Q_OBJECT + Q_PROPERTY(bool isLogin READ getIsLogin WRITE setIsLogin NOTIFY isLoginChanged) Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged) Q_PROPERTY(QByteArray token READ getToken WRITE setToken NOTIFY tokenChanged) public: @@ -18,13 +19,21 @@ public: ~Controller(); // All the Q_INVOKABLEs. - Q_INVOKABLE void init(); Q_INVOKABLE void login(QString, QString, QString); Q_INVOKABLE void logout(); // All the non-Q_INVOKABLE functions. // All the Q_PROPERTYs. + bool isLogin = false; + bool getIsLogin() { return isLogin; } + void setIsLogin(bool n) { + if(n != isLogin) { + isLogin = n; + emit isLoginChanged(); + } + } + QString userID; QString getUserID() { return userID; } void setUserID(QString n) { @@ -45,10 +54,12 @@ public: private: QMatrixClient::Connection *connection = new QMatrixClient::Connection(); + void connected(); void resync(); void reconnect(); signals: + void isLoginChanged(); void userIDChanged(); void tokenChanged(); void homeServerChanged(); diff --git a/qml/Login.qml b/qml/Login.qml index 20dd3a9..c21b0d2 100644 --- a/qml/Login.qml +++ b/qml/Login.qml @@ -80,53 +80,35 @@ Page { anchors.horizontalCenter: parent.horizontalCenter } - RowLayout { + TextField { + id: serverField width: parent.width height: 48 - spacing: 0 + placeholderText: "Server" + leftPadding: 16 + topPadding: 0 + bottomPadding: 0 - Text { - text: "@" - horizontalAlignment: Text.AlignHCenter - Layout.preferredWidth: parent.width * 0.05 + background: Rectangle { + color: "#eaeaea" + border.color: parent.activeFocus ? Material.accent : "transparent" + border.width: 2 } + } - TextField { - id: usernameField - Layout.preferredWidth: parent.width * 0.45 - Layout.fillHeight: true - placeholderText: "Username" - leftPadding: 16 - topPadding: 0 - bottomPadding: 0 + TextField { + id: usernameField + width: parent.width + height: 48 + placeholderText: "Username" + leftPadding: 16 + topPadding: 0 + bottomPadding: 0 - background: Rectangle { - color: "#eaeaea" - border.color: parent.activeFocus ? Material.accent : "transparent" - border.width: 2 - } - } - - Text { - text: ":" - horizontalAlignment: Text.AlignHCenter - Layout.preferredWidth: parent.width * 0.05 - } - - TextField { - id: serverField - Layout.preferredWidth: parent.width * 0.45 - Layout.fillHeight: true - placeholderText: "Server" - leftPadding: 16 - topPadding: 0 - bottomPadding: 0 - - background: Rectangle { - color: "#eaeaea" - border.color: parent.activeFocus ? Material.accent : "transparent" - border.width: 2 - } + background: Rectangle { + color: "#eaeaea" + border.color: parent.activeFocus ? Material.accent : "transparent" + border.width: 2 } } diff --git a/qml/main.qml b/qml/main.qml index f47600e..2ac6f6b 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -19,6 +19,8 @@ ApplicationWindow { Controller { id: controller + + onIsLoginChanged: console.log("Status:", isLogin) } Settings { @@ -136,8 +138,4 @@ ApplicationWindow { } } - - Component.onCompleted: { - controller.init() - } }