diff --git a/main.cpp b/main.cpp index 4c96d3a..2f64a99 100644 --- a/main.cpp +++ b/main.cpp @@ -15,11 +15,11 @@ int main(int argc, char *argv[]) QGuiApplication app(argc, argv); // Enable this if you need proxy. -// QNetworkProxy proxy; -// proxy.setType(QNetworkProxy::HttpProxy); -// proxy.setHostName("localhost"); -// proxy.setPort(1082); -// QNetworkProxy::setApplicationProxy(proxy); + QNetworkProxy proxy; + proxy.setType(QNetworkProxy::HttpProxy); + proxy.setHostName("localhost"); + proxy.setPort(1082); + QNetworkProxy::setApplicationProxy(proxy); qmlRegisterType("Matrique", 0, 1, "Controller"); qmlRegisterType("Matrique", 0, 1, "RoomListModel"); diff --git a/matrix/controller.cpp b/matrix/controller.cpp index a43d2f3..41800ad 100644 --- a/matrix/controller.cpp +++ b/matrix/controller.cpp @@ -46,9 +46,11 @@ void Controller::connected() { } void Controller::resync() { + qDebug() << "Syncing Matrix."; m_connection->sync(30000); } void Controller::reconnect() { + qDebug() << "Connection lost. Reconnecting..."; m_connection->connectWithToken(userID, token, ""); } diff --git a/matrix/controller.h b/matrix/controller.h index f4c971a..4b7157f 100644 --- a/matrix/controller.h +++ b/matrix/controller.h @@ -29,7 +29,7 @@ public: // All the non-Q_INVOKABLE functions. // All the Q_PROPERTYs. - RoomListModel *roomListModel = new RoomListModel(this); + RoomListModel *roomListModel = new RoomListModel(); RoomListModel* getRoomListModel() { return roomListModel; } bool isLogin = false; diff --git a/matrix/roomlistmodel.cpp b/matrix/roomlistmodel.cpp index ed27011..d574079 100644 --- a/matrix/roomlistmodel.cpp +++ b/matrix/roomlistmodel.cpp @@ -1,24 +1,30 @@ +#include +#include + #include "roomlistmodel.h" #include "controller.h" -RoomListModel::RoomListModel(QObject *parent) : QObject(parent) +RoomListModel::RoomListModel(QObject *parent) { } +RoomListModel::~RoomListModel() { + +} + void RoomListModel::init(QMatrixClient::Connection *conn) { qDebug() << "Registering connection."; + beginResetModel(); m_connection = conn; + m_rooms.clear(); connect(m_connection, &QMatrixClient::Connection::newRoom, this, &RoomListModel::addRoom); for(QMatrixClient::Room* room: m_connection->roomMap().values()) { connect(room, &QMatrixClient::Room::namesChanged, this, &RoomListModel::namesChanged); m_rooms.append(room); } -} - -RoomListModel::~RoomListModel() { - + endResetModel(); } QMatrixClient::Room* RoomListModel::roomAt(int row) @@ -29,16 +35,60 @@ QMatrixClient::Room* RoomListModel::roomAt(int row) void RoomListModel::addRoom(QMatrixClient::Room* room) { qDebug() << "Adding room."; + beginInsertRows(QModelIndex(), m_rooms.count(), m_rooms.count()); connect(room, &QMatrixClient::Room::namesChanged, this, &RoomListModel::namesChanged ); m_rooms.append(room); + endInsertRows(); +} + +int RoomListModel::rowCount(const QModelIndex& parent) const +{ + if( parent.isValid() ) + return 0; + return m_rooms.count(); +} + +QVariant RoomListModel::data(const QModelIndex& index, int role) const +{ + if(!index.isValid()) + return QVariant(); + + if(index.row() >= m_rooms.count()) + { + qDebug() << "UserListModel: something wrong here..."; + return QVariant(); + } + QMatrixClient::Room* room = m_rooms.at(index.row()); + if( role == NameRole ) + { + return room->displayName(); + } + if( role == ValueRole ) + { + return room->topic(); + } + return QVariant(); +} + +QHash RoomListModel::roleNames() const { + QHash roles; + roles[NameRole] = "name"; + roles[ValueRole] = "value"; + return roles; } void RoomListModel::namesChanged(QMatrixClient::Room* room) { - + int row = m_rooms.indexOf(room); + emit dataChanged(index(row), index(row)); } void RoomListModel::unreadMessagesChanged(QMatrixClient::Room* room) { } + +RoomModel::RoomModel(QString name, QString value) { + m_name = name; + m_value = value; +} diff --git a/matrix/roomlistmodel.h b/matrix/roomlistmodel.h index d490338..21d85ea 100644 --- a/matrix/roomlistmodel.h +++ b/matrix/roomlistmodel.h @@ -2,6 +2,7 @@ #define ROOMLISTMODEL_H #include +#include #include "libqmatrixclient/connection.h" #include "libqmatrixclient/room.h" @@ -11,17 +12,49 @@ namespace QMatrixClient { class Room; } -class RoomListModel : public QObject +class RoomModel : public QObject { Q_OBJECT + + Q_PROPERTY(QString name READ getName) + Q_PROPERTY(QString value READ getValue) + +public: + explicit RoomModel(QString name, QString value); + + QString getName() { return m_name; } + QString getValue() { return m_value; } + +signals: + void nameChanged(); + void valueChanged(); + +private: + QString m_name; + QString m_value; +}; + +class RoomListModel : public QAbstractListModel +{ + Q_OBJECT + public: explicit RoomListModel(QObject *parent = nullptr); ~RoomListModel(); + enum RoomModelRoles { + NameRole, ValueRole + }; + + QHash roleNames() const; + void init(QMatrixClient::Connection*); Q_INVOKABLE QMatrixClient::Room* roomAt(int row); + QVariant data(const QModelIndex& index, int role) const override; + Q_INVOKABLE int rowCount(const QModelIndex& parent=QModelIndex()) const override; + signals: public slots: diff --git a/qml/Home.qml b/qml/Home.qml deleted file mode 100644 index c61556c..0000000 --- a/qml/Home.qml +++ /dev/null @@ -1,17 +0,0 @@ -import QtQuick 2.10 -import QtQuick.Controls 2.3 -import "qrc:/qml/form" - -Page { - RoomListForm { - id: roomListForm - height: parent.height - width: 320 - } - - RoomForm { - id: roomForm - anchors.fill: parent - anchors.leftMargin: roomListForm.width - } -} diff --git a/qml/Room.qml b/qml/Room.qml index a3a893f..2580cc1 100644 --- a/qml/Room.qml +++ b/qml/Room.qml @@ -6,14 +6,15 @@ Page { property var roomListModel RoomListForm { - id: contactListForm + id: roomListForm height: parent.height width: 320 + listModel: roomListModel } - ContactDetailForm { - id: contactDetailForm + RoomForm { + id: roomForm anchors.fill: parent - anchors.leftMargin: contactListForm.width + anchors.leftMargin: roomListForm.width } } diff --git a/qml/form/ContactDetailForm.qml b/qml/form/ContactDetailForm.qml index 6e8b997..75b80b7 100644 --- a/qml/form/ContactDetailForm.qml +++ b/qml/form/ContactDetailForm.qml @@ -7,6 +7,7 @@ import "qrc:/qml/component" Item { ColumnLayout { anchors.fill: parent + spacing: 0 Pane { Layout.fillWidth: true diff --git a/qml/form/RoomForm.qml b/qml/form/RoomForm.qml index 9b73a59..bec8961 100644 --- a/qml/form/RoomForm.qml +++ b/qml/form/RoomForm.qml @@ -8,6 +8,7 @@ import "qrc:/qml/component" Item { ColumnLayout { anchors.fill: parent + spacing: 0 Pane { z: 10 diff --git a/qml/form/RoomListForm.qml b/qml/form/RoomListForm.qml index 887b6f8..869b79a 100644 --- a/qml/form/RoomListForm.qml +++ b/qml/form/RoomListForm.qml @@ -6,6 +6,7 @@ import QtQuick.Controls.Material 2.3 import "qrc:/qml/component" Item { + property var listModel ColumnLayout { anchors.fill: parent spacing: 0 @@ -74,58 +75,6 @@ Item { color: "#eaeaea" } - ListModel { - id: listModel - ListElement { - name: "Bill Smith" - number: "555 3264" - } - ListElement { - name: "John Brown" - number: "555 8426" - } - ListElement { - name: "Sam Wise" - number: "555 0473" - } - ListElement { - name: "Bill Smith" - number: "555 3264" - } - ListElement { - name: "John Brown" - number: "555 8426" - } - ListElement { - name: "Sam Wise" - number: "555 0473" - } - ListElement { - name: "Bill Smith" - number: "555 3264" - } - ListElement { - name: "John Brown" - number: "555 8426" - } - ListElement { - name: "Sam Wise" - number: "555 0473" - } - ListElement { - name: "Bill Smith" - number: "555 3264" - } - ListElement { - name: "John Brown" - number: "555 8426" - } - ListElement { - name: "Sam Wise" - number: "555 0473" - } - } - ListView { id: listView width: parent.width @@ -145,25 +94,29 @@ Item { height: 80 onClicked: listView.currentIndex = index - contentItem: Item { - Row { - spacing: 16 + contentItem: Row { + width: parent.width - 32 + height: parent.height - 32 + spacing: 16 - ImageStatus { - width: parent.height - height: parent.height - source: "qrc:/asset/img/avatar.png" + ImageStatus { + width: parent.height + height: parent.height + source: "qrc:/asset/img/avatar.png" + } + + Column { + width: parent.width + height: parent.height + Text { + width: parent.width + text: name + color: "#424242" } - - Column { - Text { - text: name - color: "#424242" - } - Text { - text: number - color: "#424242" - } + Text { + width: parent.width + text: value + color: "#424242" } } } diff --git a/qml/main.qml b/qml/main.qml index 6938720..d79fe3d 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -70,22 +70,22 @@ ApplicationWindow { } } +// ButtonDelegate { +// index: 2 + +// contentItem: Text { +// text: "\ue5d2" +// font.pointSize: 16 +// font.family: materialFont.name +// color: "white" +// horizontalAlignment: Text.AlignHCenter +// verticalAlignment: Text.AlignVCenter +// } +// } + ButtonDelegate { index: 2 - contentItem: Text { - text: "\ue5d2" - font.pointSize: 16 - font.family: materialFont.name - color: "white" - horizontalAlignment: Text.AlignHCenter - verticalAlignment: Text.AlignVCenter - } - } - - ButtonDelegate { - index: 3 - contentItem: Text { text: "\ue8b8" font.pointSize: 16 @@ -97,7 +97,7 @@ ApplicationWindow { } ButtonDelegate { - index: 4 + index: 3 contentItem: Text { text: "\ue879" @@ -120,8 +120,8 @@ ApplicationWindow { interactive: false orientation: Qt.Vertical - Home { - + Room { + roomListModel: controller.roomListModel } Login { @@ -130,10 +130,6 @@ ApplicationWindow { controller: controller } - Room { - roomListModel: controller.roomListModel - } - Setting { } diff --git a/res.qrc b/res.qrc index 7a9cc44..6b48577 100644 --- a/res.qrc +++ b/res.qrc @@ -4,7 +4,6 @@ asset/img/avatar.png asset/img/background.jpg asset/font/material.ttf - qml/Home.qml qml/Login.qml qml/main.qml qml/Setting.qml