From c3d923cdc7d12c76552b7bed505e70214fd8f782 Mon Sep 17 00:00:00 2001 From: Adam Treat Date: Tue, 2 May 2023 07:48:40 -0400 Subject: [PATCH] Don't add new chats willy nilly. --- chatlistmodel.h | 28 +++++++++++++++++++++++----- llm.cpp | 2 +- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/chatlistmodel.h b/chatlistmodel.h index f95b4695..1242cbba 100644 --- a/chatlistmodel.h +++ b/chatlistmodel.h @@ -13,6 +13,8 @@ class ChatListModel : public QAbstractListModel public: explicit ChatListModel(QObject *parent = nullptr) : QAbstractListModel(parent) + , m_currentChat(nullptr) + , m_newChat(nullptr) { } @@ -51,15 +53,22 @@ public: return roles; } - Q_INVOKABLE Chat* addChat() + Q_INVOKABLE void addChat() { - Chat *newChat = new Chat(this); + // Don't add a new chat if the current chat is empty + if (m_newChat) + return; + + // Create a new chat pointer and connect it to determine when it is populated + m_newChat = new Chat(this); + connect(m_newChat->chatModel(), &ChatModel::countChanged, + this, &ChatListModel::newChatCountChanged); + beginInsertRows(QModelIndex(), 0, 0); - m_chats.prepend(newChat); + m_chats.prepend(m_newChat); endInsertRows(); emit countChanged(); - setCurrentChat(newChat); - return newChat; + setCurrentChat(m_newChat); } Q_INVOKABLE void removeChat(Chat* chat) @@ -130,7 +139,16 @@ Q_SIGNALS: void disconnectChat(Chat*); void currentChatChanged(); +private Q_SLOTS: + void newChatCountChanged() + { + Q_ASSERT(m_newChat && m_newChat->chatModel()->count()); + m_newChat->disconnect(this); + m_newChat = nullptr; + } + private: + Chat* m_newChat; Chat* m_currentChat; QList m_chats; }; diff --git a/llm.cpp b/llm.cpp index 6aa17ff3..f624b7a3 100644 --- a/llm.cpp +++ b/llm.cpp @@ -129,5 +129,5 @@ void LLM::connectChat(Chat *chat) void LLM::disconnectChat(Chat *chat) { - disconnect(chat); + chat->disconnect(this); }