Spectral/matrix/roomlistmodel.cpp

95 lines
2.1 KiB
C++
Raw Normal View History

2018-02-28 13:11:42 +00:00
#include <QtGui/QBrush>
#include <QtGui/QColor>
2018-02-28 09:10:42 +00:00
#include "roomlistmodel.h"
#include "controller.h"
2018-02-28 13:11:42 +00:00
RoomListModel::RoomListModel(QObject *parent)
2018-02-28 09:10:42 +00:00
{
}
2018-02-28 13:11:42 +00:00
RoomListModel::~RoomListModel() {
}
2018-02-28 09:10:42 +00:00
void RoomListModel::init(QMatrixClient::Connection *conn) {
qDebug() << "Registering connection.";
2018-02-28 13:11:42 +00:00
beginResetModel();
2018-02-28 09:10:42 +00:00
m_connection = conn;
2018-02-28 13:11:42 +00:00
m_rooms.clear();
2018-02-28 09:10:42 +00:00
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);
}
2018-02-28 13:11:42 +00:00
endResetModel();
2018-02-28 09:10:42 +00:00
}
QMatrixClient::Room* RoomListModel::roomAt(int row)
{
return m_rooms.at(row);
}
void RoomListModel::addRoom(QMatrixClient::Room* room)
{
qDebug() << "Adding room.";
2018-02-28 13:11:42 +00:00
beginInsertRows(QModelIndex(), m_rooms.count(), m_rooms.count());
2018-02-28 09:10:42 +00:00
connect(room, &QMatrixClient::Room::namesChanged, this, &RoomListModel::namesChanged );
m_rooms.append(room);
2018-02-28 13:11:42 +00:00
endInsertRows();
2018-02-28 09:10:42 +00:00
}
2018-02-28 13:11:42 +00:00
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
2018-02-28 09:10:42 +00:00
{
2018-02-28 13:11:42 +00:00
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();
}
2018-02-28 09:10:42 +00:00
2018-02-28 13:11:42 +00:00
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));
2018-02-28 09:10:42 +00:00
}
void RoomListModel::unreadMessagesChanged(QMatrixClient::Room* room)
{
}
2018-02-28 13:11:42 +00:00
RoomModel::RoomModel(QString name, QString value) {
m_name = name;
m_value = value;
}