Spectral/matrix/roomlistmodel.cpp

88 lines
2.3 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-03-04 12:05:09 +00:00
#include "user.h"
2018-02-28 09:10:42 +00:00
2018-03-02 11:58:55 +00:00
RoomListModel::RoomListModel() {
}
RoomListModel::~RoomListModel() {
}
void RoomListModel::setConnection(QMatrixClient::Connection *conn) {
m_connection = conn;
2018-02-28 13:11:42 +00:00
beginResetModel();
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) {
2018-02-28 09:10:42 +00:00
return m_rooms.at(row);
}
void RoomListModel::addRoom(QMatrixClient::Room* room) {
2018-02-28 09:10:42 +00:00
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
}
int RoomListModel::rowCount(const QModelIndex& parent) const {
2018-02-28 13:11:42 +00:00
if( parent.isValid() )
return 0;
return m_rooms.count();
}
QVariant RoomListModel::data(const QModelIndex& index, int role) const {
2018-02-28 13:11:42 +00:00
if(!index.isValid())
return QVariant();
if(index.row() >= m_rooms.count()) {
2018-02-28 13:11:42 +00:00
qDebug() << "UserListModel: something wrong here...";
return QVariant();
}
QMatrixClient::Room* room = m_rooms.at(index.row());
if(role == NameRole) {
2018-02-28 13:11:42 +00:00
return room->displayName();
}
if(role == ValueRole) {
2018-02-28 13:11:42 +00:00
return room->topic();
}
2018-03-02 08:56:36 +00:00
if(role == AvatarRole) {
2018-03-04 12:05:09 +00:00
if(room->avatarUrl().toString() != "") {
return room->avatarUrl();
} else if(room->users().length() == 2) {
QMatrixClient::User* user = room->users().at(0);
return user->avatarUrl();
}
}
2018-02-28 13:11:42 +00:00
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";
2018-03-02 08:56:36 +00:00
roles[AvatarRole] = "avatar";
2018-02-28 13:11:42 +00:00
return roles;
}
void RoomListModel::namesChanged(QMatrixClient::Room* room) {
2018-02-28 13:11:42 +00:00
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 09:10:42 +00:00
}