mirror of https://github.com/Nheko-Reborn/nheko
parent
734fb7e286
commit
19dbbb2c6c
@ -0,0 +1,57 @@ |
||||
#include "RoomsModel.h" |
||||
|
||||
#include "Cache_p.h" |
||||
#include "CompletionModelRoles.h" |
||||
|
||||
RoomsModel::RoomsModel(QObject *parent) |
||||
: QAbstractListModel(parent) |
||||
{ |
||||
rooms_ = cache::joinedRooms(); |
||||
roomInfos = cache::getRoomInfo(rooms_); |
||||
|
||||
for (const auto &r : rooms_) { |
||||
auto roomAliasesList = cache::client()->getRoomAliases(r); |
||||
|
||||
if (roomAliasesList) { |
||||
roomAliases.push_back(QString::fromStdString(roomAliasesList->alias)); |
||||
roomids.push_back(QString::fromStdString(r)); |
||||
} |
||||
} |
||||
} |
||||
|
||||
QHash<int, QByteArray> |
||||
RoomsModel::roleNames() const |
||||
{ |
||||
return {{CompletionModel::CompletionRole, "completionRole"}, |
||||
{CompletionModel::SearchRole, "searchRole"}, |
||||
{CompletionModel::SearchRole2, "searchRole2"}, |
||||
{Roles::RoomAlias, "roomAlias"}, |
||||
{Roles::AvatarUrl, "avatarUrl"}, |
||||
{Roles::RoomID, "roomid"}, |
||||
{Roles::RoomName, "roomName"}}; |
||||
} |
||||
|
||||
QVariant |
||||
RoomsModel::data(const QModelIndex &index, int role) const |
||||
{ |
||||
if (hasIndex(index.row(), index.column(), index.parent())) { |
||||
switch (role) { |
||||
case CompletionModel::CompletionRole: |
||||
return QString("%1").arg(roomAliases[index.row()]); |
||||
case CompletionModel::SearchRole: |
||||
case Qt::DisplayRole: |
||||
case Roles::RoomAlias: |
||||
return roomAliases[index.row()]; |
||||
case CompletionModel::SearchRole2: |
||||
return roomAliases[index.row()]; |
||||
case Roles::AvatarUrl: |
||||
return QString::fromStdString( |
||||
roomInfos.at(roomids[index.row()]).avatar_url); |
||||
case Roles::RoomID: |
||||
return roomids[index.row()]; |
||||
case Roles::RoomName: |
||||
return QString::fromStdString(roomInfos.at(roomids[index.row()]).name); |
||||
} |
||||
} |
||||
return {}; |
||||
} |
@ -0,0 +1,33 @@ |
||||
#pragma once |
||||
|
||||
#include "Cache.h" |
||||
|
||||
#include <QAbstractListModel> |
||||
#include <QString> |
||||
|
||||
class RoomsModel : public QAbstractListModel |
||||
{ |
||||
public: |
||||
enum Roles |
||||
{ |
||||
AvatarUrl = Qt::UserRole, |
||||
RoomAlias, |
||||
RoomID, |
||||
RoomName, |
||||
}; |
||||
|
||||
RoomsModel(QObject *parent = nullptr); |
||||
QHash<int, QByteArray> roleNames() const override; |
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override |
||||
{ |
||||
(void)parent; |
||||
return (int)roomAliases.size(); |
||||
} |
||||
QVariant data(const QModelIndex &index, int role) const override; |
||||
|
||||
private: |
||||
std::vector<std::string> rooms_; |
||||
std::vector<QString> roomids; |
||||
std::vector<QString> roomAliases; |
||||
std::map<QString, RoomInfo> roomInfos; |
||||
}; |
Loading…
Reference in new issue