Init sync and listmodel function.

This commit is contained in:
Black Hat 2018-02-28 21:11:42 +08:00
parent a4b00f823e
commit c3367543bf
12 changed files with 143 additions and 124 deletions

View File

@ -15,11 +15,11 @@ int main(int argc, char *argv[])
QGuiApplication app(argc, argv); QGuiApplication app(argc, argv);
// Enable this if you need proxy. // Enable this if you need proxy.
// QNetworkProxy proxy; QNetworkProxy proxy;
// proxy.setType(QNetworkProxy::HttpProxy); proxy.setType(QNetworkProxy::HttpProxy);
// proxy.setHostName("localhost"); proxy.setHostName("localhost");
// proxy.setPort(1082); proxy.setPort(1082);
// QNetworkProxy::setApplicationProxy(proxy); QNetworkProxy::setApplicationProxy(proxy);
qmlRegisterType<Controller>("Matrique", 0, 1, "Controller"); qmlRegisterType<Controller>("Matrique", 0, 1, "Controller");
qmlRegisterType<RoomListModel>("Matrique", 0, 1, "RoomListModel"); qmlRegisterType<RoomListModel>("Matrique", 0, 1, "RoomListModel");

View File

@ -46,9 +46,11 @@ void Controller::connected() {
} }
void Controller::resync() { void Controller::resync() {
qDebug() << "Syncing Matrix.";
m_connection->sync(30000); m_connection->sync(30000);
} }
void Controller::reconnect() { void Controller::reconnect() {
qDebug() << "Connection lost. Reconnecting...";
m_connection->connectWithToken(userID, token, ""); m_connection->connectWithToken(userID, token, "");
} }

View File

@ -29,7 +29,7 @@ public:
// All the non-Q_INVOKABLE functions. // All the non-Q_INVOKABLE functions.
// All the Q_PROPERTYs. // All the Q_PROPERTYs.
RoomListModel *roomListModel = new RoomListModel(this); RoomListModel *roomListModel = new RoomListModel();
RoomListModel* getRoomListModel() { return roomListModel; } RoomListModel* getRoomListModel() { return roomListModel; }
bool isLogin = false; bool isLogin = false;

View File

@ -1,24 +1,30 @@
#include <QtGui/QBrush>
#include <QtGui/QColor>
#include "roomlistmodel.h" #include "roomlistmodel.h"
#include "controller.h" #include "controller.h"
RoomListModel::RoomListModel(QObject *parent) : QObject(parent) RoomListModel::RoomListModel(QObject *parent)
{ {
} }
RoomListModel::~RoomListModel() {
}
void RoomListModel::init(QMatrixClient::Connection *conn) { void RoomListModel::init(QMatrixClient::Connection *conn) {
qDebug() << "Registering connection."; qDebug() << "Registering connection.";
beginResetModel();
m_connection = conn; m_connection = conn;
m_rooms.clear();
connect(m_connection, &QMatrixClient::Connection::newRoom, this, &RoomListModel::addRoom); connect(m_connection, &QMatrixClient::Connection::newRoom, this, &RoomListModel::addRoom);
for(QMatrixClient::Room* room: m_connection->roomMap().values()) { for(QMatrixClient::Room* room: m_connection->roomMap().values()) {
connect(room, &QMatrixClient::Room::namesChanged, this, &RoomListModel::namesChanged); connect(room, &QMatrixClient::Room::namesChanged, this, &RoomListModel::namesChanged);
m_rooms.append(room); m_rooms.append(room);
} }
} endResetModel();
RoomListModel::~RoomListModel() {
} }
QMatrixClient::Room* RoomListModel::roomAt(int row) QMatrixClient::Room* RoomListModel::roomAt(int row)
@ -29,16 +35,60 @@ QMatrixClient::Room* RoomListModel::roomAt(int row)
void RoomListModel::addRoom(QMatrixClient::Room* room) void RoomListModel::addRoom(QMatrixClient::Room* room)
{ {
qDebug() << "Adding room."; qDebug() << "Adding room.";
beginInsertRows(QModelIndex(), m_rooms.count(), m_rooms.count());
connect(room, &QMatrixClient::Room::namesChanged, this, &RoomListModel::namesChanged ); connect(room, &QMatrixClient::Room::namesChanged, this, &RoomListModel::namesChanged );
m_rooms.append(room); 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<int, QByteArray> RoomListModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[ValueRole] = "value";
return roles;
} }
void RoomListModel::namesChanged(QMatrixClient::Room* room) void RoomListModel::namesChanged(QMatrixClient::Room* room)
{ {
int row = m_rooms.indexOf(room);
emit dataChanged(index(row), index(row));
} }
void RoomListModel::unreadMessagesChanged(QMatrixClient::Room* room) void RoomListModel::unreadMessagesChanged(QMatrixClient::Room* room)
{ {
} }
RoomModel::RoomModel(QString name, QString value) {
m_name = name;
m_value = value;
}

View File

@ -2,6 +2,7 @@
#define ROOMLISTMODEL_H #define ROOMLISTMODEL_H
#include <QObject> #include <QObject>
#include <QtCore/QAbstractListModel>
#include "libqmatrixclient/connection.h" #include "libqmatrixclient/connection.h"
#include "libqmatrixclient/room.h" #include "libqmatrixclient/room.h"
@ -11,17 +12,49 @@ namespace QMatrixClient {
class Room; class Room;
} }
class RoomListModel : public QObject class RoomModel : public QObject
{ {
Q_OBJECT 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: public:
explicit RoomListModel(QObject *parent = nullptr); explicit RoomListModel(QObject *parent = nullptr);
~RoomListModel(); ~RoomListModel();
enum RoomModelRoles {
NameRole, ValueRole
};
QHash<int, QByteArray> roleNames() const;
void init(QMatrixClient::Connection*); void init(QMatrixClient::Connection*);
Q_INVOKABLE QMatrixClient::Room* roomAt(int row); 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: signals:
public slots: public slots:

View File

@ -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
}
}

View File

@ -6,14 +6,15 @@ Page {
property var roomListModel property var roomListModel
RoomListForm { RoomListForm {
id: contactListForm id: roomListForm
height: parent.height height: parent.height
width: 320 width: 320
listModel: roomListModel
} }
ContactDetailForm { RoomForm {
id: contactDetailForm id: roomForm
anchors.fill: parent anchors.fill: parent
anchors.leftMargin: contactListForm.width anchors.leftMargin: roomListForm.width
} }
} }

View File

@ -7,6 +7,7 @@ import "qrc:/qml/component"
Item { Item {
ColumnLayout { ColumnLayout {
anchors.fill: parent anchors.fill: parent
spacing: 0
Pane { Pane {
Layout.fillWidth: true Layout.fillWidth: true

View File

@ -8,6 +8,7 @@ import "qrc:/qml/component"
Item { Item {
ColumnLayout { ColumnLayout {
anchors.fill: parent anchors.fill: parent
spacing: 0
Pane { Pane {
z: 10 z: 10

View File

@ -6,6 +6,7 @@ import QtQuick.Controls.Material 2.3
import "qrc:/qml/component" import "qrc:/qml/component"
Item { Item {
property var listModel
ColumnLayout { ColumnLayout {
anchors.fill: parent anchors.fill: parent
spacing: 0 spacing: 0
@ -74,58 +75,6 @@ Item {
color: "#eaeaea" 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 { ListView {
id: listView id: listView
width: parent.width width: parent.width
@ -145,25 +94,29 @@ Item {
height: 80 height: 80
onClicked: listView.currentIndex = index onClicked: listView.currentIndex = index
contentItem: Item { contentItem: Row {
Row { width: parent.width - 32
spacing: 16 height: parent.height - 32
spacing: 16
ImageStatus { ImageStatus {
width: parent.height width: parent.height
height: parent.height height: parent.height
source: "qrc:/asset/img/avatar.png" source: "qrc:/asset/img/avatar.png"
}
Column {
width: parent.width
height: parent.height
Text {
width: parent.width
text: name
color: "#424242"
} }
Text {
Column { width: parent.width
Text { text: value
text: name color: "#424242"
color: "#424242"
}
Text {
text: number
color: "#424242"
}
} }
} }
} }

View File

@ -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 { ButtonDelegate {
index: 2 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 { contentItem: Text {
text: "\ue8b8" text: "\ue8b8"
font.pointSize: 16 font.pointSize: 16
@ -97,7 +97,7 @@ ApplicationWindow {
} }
ButtonDelegate { ButtonDelegate {
index: 4 index: 3
contentItem: Text { contentItem: Text {
text: "\ue879" text: "\ue879"
@ -120,8 +120,8 @@ ApplicationWindow {
interactive: false interactive: false
orientation: Qt.Vertical orientation: Qt.Vertical
Home { Room {
roomListModel: controller.roomListModel
} }
Login { Login {
@ -130,10 +130,6 @@ ApplicationWindow {
controller: controller controller: controller
} }
Room {
roomListModel: controller.roomListModel
}
Setting { Setting {
} }

View File

@ -4,7 +4,6 @@
<file>asset/img/avatar.png</file> <file>asset/img/avatar.png</file>
<file>asset/img/background.jpg</file> <file>asset/img/background.jpg</file>
<file>asset/font/material.ttf</file> <file>asset/font/material.ttf</file>
<file>qml/Home.qml</file>
<file>qml/Login.qml</file> <file>qml/Login.qml</file>
<file>qml/main.qml</file> <file>qml/main.qml</file>
<file>qml/Setting.qml</file> <file>qml/Setting.qml</file>