Spectral/matrix/roomlistmodel.cpp

104 lines
2.5 KiB
C++
Raw Normal View History

2018-02-28 09:10:42 +00:00
#include "roomlistmodel.h"
2018-03-15 09:10:27 +00:00
#include <QtGui/QBrush>
#include <QtGui/QColor>
#include <QtCore/QDebug>
2018-02-28 09:10:42 +00:00
2018-03-14 09:11:45 +00:00
RoomListModel::RoomListModel(QObject* parent)
: QAbstractListModel(parent)
{
2018-03-14 09:11:45 +00:00
}
RoomListModel::~RoomListModel()
2018-03-14 09:11:45 +00:00
{
}
void RoomListModel::setConnection(QMatrixClient::Connection* connection)
2018-03-14 09:11:45 +00:00
{
beginResetModel();
m_connection = connection;
m_rooms.clear();
connect( connection, &QMatrixClient::Connection::newRoom, this, &RoomListModel::addRoom );
for( QMatrixClient::Room* room: connection->roomMap().values() ) {
connect( room, &QMatrixClient::Room::namesChanged, this, &RoomListModel::namesChanged );
m_rooms.append(room);
2018-03-14 09:11:45 +00:00
}
endResetModel();
2018-03-14 09:11:45 +00:00
}
QMatrixClient::Room* RoomListModel::roomAt(int row)
2018-03-14 09:11:45 +00:00
{
return m_rooms.at(row);
2018-02-28 09:10:42 +00:00
}
void RoomListModel::addRoom(QMatrixClient::Room* room)
2018-03-14 09:11:45 +00:00
{
beginInsertRows(QModelIndex(), m_rooms.count(), m_rooms.count());
connect( room, &QMatrixClient::Room::namesChanged, this, &RoomListModel::namesChanged );
m_rooms.append(room);
endInsertRows();
2018-02-28 09:10:42 +00:00
}
2018-03-14 09:11:45 +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();
}
2018-03-14 09:11:45 +00:00
QVariant RoomListModel::data(const QModelIndex& index, int role) const
{
if( !index.isValid() )
2018-02-28 13:11:42 +00:00
return QVariant();
2018-03-14 09:11:45 +00:00
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 == Qt::DisplayRole )
2018-03-14 09:11:45 +00:00
{
return room->displayName();
}
if( role == Qt::ForegroundRole )
{
if( room->highlightCount() > 0 )
return QBrush(QColor("orange"));
return QVariant();
}
if( role == Qt::DecorationRole )
{
2018-07-07 11:06:13 +00:00
if ( room->avatarUrl().toString() != "" ) {
return room->avatarUrl();
2018-03-04 12:05:09 +00:00
}
return QVariant();
}
2018-07-07 11:06:13 +00:00
if ( role == Qt::StatusTipRole )
{
return room->topic();
}
return QVariant();
}
void RoomListModel::namesChanged(QMatrixClient::Room* room)
2018-03-14 09:11:45 +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-03-14 09:11:45 +00:00
{
int row = m_rooms.indexOf(room);
emit dataChanged(index(row), index(row));
2018-03-14 09:11:45 +00:00
}
2018-02-28 09:10:42 +00:00
QHash<int, QByteArray> RoomListModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[Qt::DisplayRole] = "name";
roles[Qt::DecorationRole] = "avatar";
roles[Qt::StatusTipRole] = "topic";
return roles;
2018-02-28 09:10:42 +00:00
}