Init sync and listmodel function.
This commit is contained in:
@@ -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, "");
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,24 +1,30 @@
|
||||
#include <QtGui/QBrush>
|
||||
#include <QtGui/QColor>
|
||||
|
||||
#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<int, QByteArray> RoomListModel::roleNames() const {
|
||||
QHash<int, QByteArray> 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;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define ROOMLISTMODEL_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QtCore/QAbstractListModel>
|
||||
|
||||
#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<int, QByteArray> 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:
|
||||
|
||||
Reference in New Issue
Block a user