Tweak UI.

This commit is contained in:
Black Hat 2018-02-27 19:37:53 +08:00
parent 5b1047ed98
commit fd5afa267c
4 changed files with 59 additions and 70 deletions

View File

@ -3,39 +3,30 @@
#include "libqmatrixclient/connection.h" #include "libqmatrixclient/connection.h"
Controller::Controller(QObject *parent) : QObject(parent) { 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() { 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) { void Controller::login(QString home, QString user, QString pass) {
qInfo() << "UserID:" << userID; if(home.isEmpty()) home = "matrix.org";
qInfo() << "Token:" << token;
qInfo() << "Home:" << home;
qInfo() << "User:" << user;
qInfo() << "Pass:" << pass;
if(!userID.isEmpty() && !token.isEmpty()) {
qInfo() << "Using token.";
connection->connectWithToken(userID, token, "");
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()) { } else if(!user.isEmpty() && !pass.isEmpty()) {
qInfo() << "Using given credential."; qDebug() << "Using given credential.";
connection->connectToServer("@"+user+":"+home, pass, ""); connection->connectToServer("@"+user+":"+home, pass, "");
} }
} }
@ -43,6 +34,13 @@ void Controller::login(QString home, QString user, QString pass) {
void Controller::logout() { void Controller::logout() {
userID = ""; userID = "";
token = ""; token = "";
setIsLogin(false);
}
void Controller::connected() {
setUserID(connection->userId());
setToken(connection->accessToken());
setIsLogin(true);
} }
void Controller::resync() { void Controller::resync() {

View File

@ -11,6 +11,7 @@ class Controller : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool isLogin READ getIsLogin WRITE setIsLogin NOTIFY isLoginChanged)
Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged) Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged)
Q_PROPERTY(QByteArray token READ getToken WRITE setToken NOTIFY tokenChanged) Q_PROPERTY(QByteArray token READ getToken WRITE setToken NOTIFY tokenChanged)
public: public:
@ -18,13 +19,21 @@ public:
~Controller(); ~Controller();
// All the Q_INVOKABLEs. // All the Q_INVOKABLEs.
Q_INVOKABLE void init();
Q_INVOKABLE void login(QString, QString, QString); Q_INVOKABLE void login(QString, QString, QString);
Q_INVOKABLE void logout(); Q_INVOKABLE void logout();
// All the non-Q_INVOKABLE functions. // All the non-Q_INVOKABLE functions.
// All the Q_PROPERTYs. // 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 userID;
QString getUserID() { return userID; } QString getUserID() { return userID; }
void setUserID(QString n) { void setUserID(QString n) {
@ -45,10 +54,12 @@ public:
private: private:
QMatrixClient::Connection *connection = new QMatrixClient::Connection(); QMatrixClient::Connection *connection = new QMatrixClient::Connection();
void connected();
void resync(); void resync();
void reconnect(); void reconnect();
signals: signals:
void isLoginChanged();
void userIDChanged(); void userIDChanged();
void tokenChanged(); void tokenChanged();
void homeServerChanged(); void homeServerChanged();

View File

@ -80,53 +80,35 @@ Page {
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
} }
RowLayout { TextField {
id: serverField
width: parent.width width: parent.width
height: 48 height: 48
spacing: 0 placeholderText: "Server"
leftPadding: 16
topPadding: 0
bottomPadding: 0
Text { background: Rectangle {
text: "@" color: "#eaeaea"
horizontalAlignment: Text.AlignHCenter border.color: parent.activeFocus ? Material.accent : "transparent"
Layout.preferredWidth: parent.width * 0.05 border.width: 2
} }
}
TextField { TextField {
id: usernameField id: usernameField
Layout.preferredWidth: parent.width * 0.45 width: parent.width
Layout.fillHeight: true height: 48
placeholderText: "Username" placeholderText: "Username"
leftPadding: 16 leftPadding: 16
topPadding: 0 topPadding: 0
bottomPadding: 0 bottomPadding: 0
background: Rectangle { background: Rectangle {
color: "#eaeaea" color: "#eaeaea"
border.color: parent.activeFocus ? Material.accent : "transparent" border.color: parent.activeFocus ? Material.accent : "transparent"
border.width: 2 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
}
} }
} }

View File

@ -19,6 +19,8 @@ ApplicationWindow {
Controller { Controller {
id: controller id: controller
onIsLoginChanged: console.log("Status:", isLogin)
} }
Settings { Settings {
@ -136,8 +138,4 @@ ApplicationWindow {
} }
} }
Component.onCompleted: {
controller.init()
}
} }