From 02e11e8008865eb9bab76fe43998c76c15481f37 Mon Sep 17 00:00:00 2001 From: James Graham Date: Fri, 4 Apr 2025 12:13:44 +0000 Subject: [PATCH] Remove the relationEventContentModel function from ChatBarCache Remove the relationEventContentModel function from ChatBarCache as ContentProvider makes it unneeded. This means turning ContentProvider into a QML singleton. --- src/chatbar/ChatBar.qml | 2 +- src/chatbarcache.cpp | 18 ------------------ src/chatbarcache.h | 8 -------- src/contentprovider.cpp | 3 ++- src/contentprovider.h | 17 ++++++++++++++--- src/timeline/ChatBarComponent.qml | 2 +- 6 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/chatbar/ChatBar.qml b/src/chatbar/ChatBar.qml index a9f68601e..334a230bc 100644 --- a/src/chatbar/ChatBar.qml +++ b/src/chatbar/ChatBar.qml @@ -374,7 +374,7 @@ QQC2.Control { id: replyComponent replyEventId: _private.chatBarCache.replyId replyAuthor: _private.chatBarCache.relationAuthor - replyContentModel: _private.chatBarCache.relationEventContentModel + replyContentModel: ContentProvider.contentModelForEvent(root.currentRoom, _private.chatBarCache.replyId, true) Message.maxContentWidth: replyLoader.item.width } QQC2.Button { diff --git a/src/chatbarcache.cpp b/src/chatbarcache.cpp index d3b8222d0..56ba38a8e 100644 --- a/src/chatbarcache.cpp +++ b/src/chatbarcache.cpp @@ -162,24 +162,6 @@ QString ChatBarCache::relationMessage() const return {}; } -MessageContentModel *ChatBarCache::relationEventContentModel() -{ - if (parent() == nullptr) { - qWarning() << "ChatBarCache created with no parent, a NeoChatRoom must be set as the parent on creation."; - return nullptr; - } - if (m_relationId.isEmpty()) { - return nullptr; - } - auto room = dynamic_cast(parent()); - if (room == nullptr) { - qWarning() << "ChatBarCache created with incorrect parent, a NeoChatRoom must be set as the parent on creation."; - return nullptr; - } - - return ContentProvider::self().contentModelForEvent(room, m_relationId); -} - bool ChatBarCache::isThreaded() const { return !m_threadId.isEmpty(); diff --git a/src/chatbarcache.h b/src/chatbarcache.h index 0924c11ed..f736b20bb 100644 --- a/src/chatbarcache.h +++ b/src/chatbarcache.h @@ -111,13 +111,6 @@ class ChatBarCache : public QObject */ Q_PROPERTY(QString relationMessage READ relationMessage NOTIFY relationIdChanged) - /** - * @brief The MessageContentModel for the related message. - * - * Will be nullptr if no related message. - */ - Q_PROPERTY(MessageContentModel *relationEventContentModel READ relationEventContentModel NOTIFY relationIdChanged) - /** * @brief Whether the chat bar is replying in a thread. */ @@ -167,7 +160,6 @@ public: Quotient::RoomMember relationAuthor() const; QString relationMessage() const; - MessageContentModel *relationEventContentModel(); bool isThreaded() const; QString threadId() const; diff --git a/src/contentprovider.cpp b/src/contentprovider.cpp index 785b96b72..37b600ecd 100644 --- a/src/contentprovider.cpp +++ b/src/contentprovider.cpp @@ -3,7 +3,8 @@ #include "contentprovider.h" -ContentProvider::ContentProvider() +ContentProvider::ContentProvider(QObject *parent) + : QObject(parent) { } diff --git a/src/contentprovider.h b/src/contentprovider.h index a0217bdbf..542e936d3 100644 --- a/src/contentprovider.h +++ b/src/contentprovider.h @@ -4,6 +4,8 @@ #pragma once #include +#include +#include #include "models/messagecontentmodel.h" #include "models/threadmodel.h" @@ -14,13 +16,22 @@ * * Store and retrieve models for message content. */ -class ContentProvider +class ContentProvider : public QObject { + Q_OBJECT + QML_ELEMENT + QML_SINGLETON + public: /** * Get the global instance of ContentProvider. */ static ContentProvider &self(); + static ContentProvider *create(QQmlEngine *engine, QJSEngine *) + { + engine->setObjectOwnership(&self(), QQmlEngine::CppOwnership); + return &self(); + } /** * @brief Returns the content model for the given event ID. @@ -34,7 +45,7 @@ public: * * @warning Do NOT use for pending events as this function has no way to differentiate. */ - MessageContentModel *contentModelForEvent(NeoChatRoom *room, const QString &evtOrTxnId, bool isReply = false); + Q_INVOKABLE MessageContentModel *contentModelForEvent(NeoChatRoom *room, const QString &evtOrTxnId, bool isReply = false); /** * @brief Returns the content model for the given event. @@ -60,7 +71,7 @@ public: ThreadModel *modelForThread(NeoChatRoom *room, const QString &threadRootId); private: - ContentProvider(); + explicit ContentProvider(QObject *parent = nullptr); QCache m_eventContentModels; QCache m_threadModels; diff --git a/src/timeline/ChatBarComponent.qml b/src/timeline/ChatBarComponent.qml index 85cd1fc3e..54ddc4271 100644 --- a/src/timeline/ChatBarComponent.qml +++ b/src/timeline/ChatBarComponent.qml @@ -219,7 +219,7 @@ QQC2.Control { id: replyComponent replyEventId: root.chatBarCache.replyId replyAuthor: root.chatBarCache.relationAuthor - replyContentModel: root.chatBarCache.relationEventContentModel + replyContentModel: ContentProvider.contentModelForEvent(root.Message.room, root.chatBarCache.replyId, true) Message.maxContentWidth: paneLoader.item.width } QQC2.Button {