diff --git a/matrix/controller.cpp b/matrix/controller.cpp index a90581a..7a4af25 100644 --- a/matrix/controller.cpp +++ b/matrix/controller.cpp @@ -29,10 +29,7 @@ void Controller::login(QString home, QString user, QString pass) { } void Controller::setConnection(QMatrixClient::Connection* conn) { - qDebug() << "Setting controller connection."; m_connection = conn; - roomListModel = new RoomListModel(m_connection); - emit roomListModelChanged(); connect(m_connection, &QMatrixClient::Connection::connected, this, &Controller::connected); connect(m_connection, &QMatrixClient::Connection::resolveError, this, &Controller::reconnect); connect(m_connection, &QMatrixClient::Connection::syncError, this, &Controller::reconnect); diff --git a/matrix/controller.h b/matrix/controller.h index 5e23ebf..5c0e7f3 100644 --- a/matrix/controller.h +++ b/matrix/controller.h @@ -14,7 +14,6 @@ class Controller : public QObject { Q_OBJECT - Q_PROPERTY(RoomListModel *roomListModel READ getRoomListModel NOTIFY roomListModelChanged) Q_PROPERTY(QMatrixClient::Connection *connection READ getConnection WRITE setConnection NOTIFY connectionChanged) Q_PROPERTY(bool isLogin READ getIsLogin WRITE setIsLogin NOTIFY isLoginChanged) Q_PROPERTY(QString userID READ getUserID WRITE setUserID NOTIFY userIDChanged) @@ -31,9 +30,6 @@ public: // All the non-Q_INVOKABLE functions. // All the Q_PROPERTYs. - RoomListModel* roomListModel; - RoomListModel* getRoomListModel() { return roomListModel; } - QMatrixClient::Connection* m_connection; QMatrixClient::Connection* getConnection() { return m_connection; } void setConnection(QMatrixClient::Connection* conn); @@ -71,7 +67,6 @@ private: void reconnect(); signals: - void roomListModelChanged(); void connectionChanged(); void isLoginChanged(); void userIDChanged(); diff --git a/matrix/roomlistmodel.cpp b/matrix/roomlistmodel.cpp index 56cc774..c4904cc 100644 --- a/matrix/roomlistmodel.cpp +++ b/matrix/roomlistmodel.cpp @@ -5,7 +5,16 @@ #include "controller.h" -RoomListModel::RoomListModel(QMatrixClient::Connection* m_connection) : m_connection(m_connection) { +RoomListModel::RoomListModel() { + +} + +RoomListModel::~RoomListModel() { + +} + +void RoomListModel::setConnection(QMatrixClient::Connection *conn) { + m_connection = conn; beginResetModel(); m_rooms.clear(); connect(m_connection, &QMatrixClient::Connection::newRoom, this, &RoomListModel::addRoom); @@ -16,10 +25,6 @@ RoomListModel::RoomListModel(QMatrixClient::Connection* m_connection) : m_connec endResetModel(); } -RoomListModel::~RoomListModel() { - -} - QMatrixClient::Room* RoomListModel::roomAt(int row) { return m_rooms.at(row); } diff --git a/matrix/roomlistmodel.h b/matrix/roomlistmodel.h index 21fb553..ec8cb3b 100644 --- a/matrix/roomlistmodel.h +++ b/matrix/roomlistmodel.h @@ -16,14 +16,20 @@ class RoomListModel : public QAbstractListModel { Q_OBJECT + Q_PROPERTY(QMatrixClient::Connection *connection READ getConnection WRITE setConnection NOTIFY connectionChanged) + public: - explicit RoomListModel(QMatrixClient::Connection* m_connection = 0); + explicit RoomListModel(); ~RoomListModel(); enum RoomModelRoles { NameRole, ValueRole, AvatarRole }; + QMatrixClient::Connection* m_connection; + QMatrixClient::Connection* getConnection() { return m_connection; } + void setConnection(QMatrixClient::Connection* conn); + QHash roleNames() const; Q_INVOKABLE QMatrixClient::Room* roomAt(int row); @@ -32,6 +38,7 @@ public: Q_INVOKABLE int rowCount(const QModelIndex& parent=QModelIndex()) const override; signals: + void connectionChanged(); public slots: @@ -41,7 +48,6 @@ private slots: void addRoom(QMatrixClient::Room* room); private: - QMatrixClient::Connection* m_connection; QList m_rooms; }; diff --git a/qml/main.qml b/qml/main.qml index c75b2ab..493c472 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -22,12 +22,17 @@ ApplicationWindow { connection: m_connection } -// Settings { -// id: settings + RoomListModel { + id: roomListModel + connection: m_connection + } -// property var userID -// property var token -// } + Settings { + id: settings + + property alias userID: matrixController.userID + property alias token: matrixController.token + } FontLoader { id: materialFont; source: "qrc:/asset/font/material.ttf" } @@ -50,7 +55,7 @@ ApplicationWindow { page: Room { id: roomPage - roomListModel: matrixController.roomListModel//BUG: It will cause random crash as roomListModel may not be initialized. + roomListModel: roomListModel } }