From 19dbbb2c6cffdf0697635f08acf3af9138aed7b9 Mon Sep 17 00:00:00 2001 From: Jedi18 Date: Tue, 16 Feb 2021 00:47:17 +0530 Subject: [PATCH] add rooms model, add room delegate for completer --- CMakeLists.txt | 2 ++ resources/qml/Completer.qml | 29 +++++++++++++++++ resources/qml/MessageInput.qml | 3 ++ src/RoomsModel.cpp | 57 ++++++++++++++++++++++++++++++++++ src/RoomsModel.h | 33 ++++++++++++++++++++ src/timeline/InputBar.cpp | 6 ++++ 6 files changed, 130 insertions(+) create mode 100644 src/RoomsModel.cpp create mode 100644 src/RoomsModel.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 72190947..54fbd7b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -326,6 +326,7 @@ set(SRC_FILES src/UserInfoWidget.cpp src/UserSettingsPage.cpp src/UsersModel.cpp + src/RoomsModel.cpp src/Utils.cpp src/WebRTCSession.cpp src/WelcomePage.cpp @@ -537,6 +538,7 @@ qt5_wrap_cpp(MOC_HEADERS src/UserInfoWidget.h src/UserSettingsPage.h src/UsersModel.h + src/RoomsModel.h src/WebRTCSession.h src/WelcomePage.h src/popups/PopupItem.h diff --git a/resources/qml/Completer.qml b/resources/qml/Completer.qml index 27322172..f77f50e9 100644 --- a/resources/qml/Completer.qml +++ b/resources/qml/Completer.qml @@ -154,6 +154,35 @@ Popup { } + DelegateChoice { + roleValue: "room" + + RowLayout { + id: del + + anchors.centerIn: parent + + Avatar { + height: 24 + width: 24 + url: model.avatarUrl.replace("mxc://", "image://MxcImage/") + onClicked: popup.completionClicked(completer.completionAt(model.index)) + } + + Label { + text: model.roomName + color: model.index == popup.currentIndex ? colors.highlightedText : colors.text + } + + Label { + text: "(" + model.roomAlias + ")" + color: model.index == popup.currentIndex ? colors.highlightedText : colors.buttonText + } + + } + + } + } } diff --git a/resources/qml/MessageInput.qml b/resources/qml/MessageInput.qml index b5c96660..7ecaf81a 100644 --- a/resources/qml/MessageInput.qml +++ b/resources/qml/MessageInput.qml @@ -183,6 +183,9 @@ Rectangle { } else if (event.key == Qt.Key_Colon) { messageInput.openCompleter(cursorPosition, "emoji"); popup.open(); + } else if (event.key == Qt.Key_NumberSign) { + messageInput.openCompleter(cursorPosition, "room"); + popup.open(); } else if (event.key == Qt.Key_Escape && popup.opened) { completerTriggeredAt = -1; popup.completerName = ""; diff --git a/src/RoomsModel.cpp b/src/RoomsModel.cpp new file mode 100644 index 00000000..f79c5515 --- /dev/null +++ b/src/RoomsModel.cpp @@ -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 +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 {}; +} diff --git a/src/RoomsModel.h b/src/RoomsModel.h new file mode 100644 index 00000000..88cb5c68 --- /dev/null +++ b/src/RoomsModel.h @@ -0,0 +1,33 @@ +#pragma once + +#include "Cache.h" + +#include +#include + +class RoomsModel : public QAbstractListModel +{ +public: + enum Roles + { + AvatarUrl = Qt::UserRole, + RoomAlias, + RoomID, + RoomName, + }; + + RoomsModel(QObject *parent = nullptr); + QHash 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 rooms_; + std::vector roomids; + std::vector roomAliases; + std::map roomInfos; +}; diff --git a/src/timeline/InputBar.cpp b/src/timeline/InputBar.cpp index 08cbd15b..49fa5249 100644 --- a/src/timeline/InputBar.cpp +++ b/src/timeline/InputBar.cpp @@ -23,6 +23,7 @@ #include "TimelineViewManager.h" #include "UserSettingsPage.h" #include "UsersModel.h" +#include "RoomsModel.h" #include "Utils.h" #include "dialogs/PreviewUploadOverlay.h" #include "emoji/EmojiModel.h" @@ -186,6 +187,11 @@ InputBar::completerFor(QString completerName) auto proxy = new CompletionProxyModel(emojiModel); emojiModel->setParent(proxy); return proxy; + } else if (completerName == "room") { + auto roomModel = new RoomsModel(); + auto proxy = new CompletionProxyModel(roomModel); + roomModel->setParent(proxy); + return proxy; } return nullptr; }