gpt4all/chatlistmodel.h

161 lines
4.0 KiB
C
Raw Normal View History

2023-05-01 21:13:20 +00:00
#ifndef CHATLISTMODEL_H
#define CHATLISTMODEL_H
#include <QAbstractListModel>
#include "chat.h"
class ChatListModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(int count READ count NOTIFY countChanged)
Q_PROPERTY(Chat *currentChat READ currentChat WRITE setCurrentChat NOTIFY currentChatChanged)
public:
explicit ChatListModel(QObject *parent = nullptr)
: QAbstractListModel(parent)
2023-05-02 11:48:40 +00:00
, m_currentChat(nullptr)
, m_newChat(nullptr)
2023-05-01 21:13:20 +00:00
{
}
enum Roles {
IdRole = Qt::UserRole + 1,
NameRole
};
int rowCount(const QModelIndex &parent = QModelIndex()) const override
{
Q_UNUSED(parent)
return m_chats.size();
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
{
if (!index.isValid() || index.row() < 0 || index.row() >= m_chats.size())
return QVariant();
const Chat *item = m_chats.at(index.row());
switch (role) {
case IdRole:
return item->id();
case NameRole:
return item->name();
}
return QVariant();
}
QHash<int, QByteArray> roleNames() const override
{
QHash<int, QByteArray> roles;
roles[IdRole] = "id";
roles[NameRole] = "name";
return roles;
}
2023-05-02 11:48:40 +00:00
Q_INVOKABLE void addChat()
2023-05-01 21:13:20 +00:00
{
2023-05-02 11:48:40 +00:00
// 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);
2023-05-02 11:48:40 +00:00
m_chats.prepend(m_newChat);
2023-05-01 21:13:20 +00:00
endInsertRows();
emit countChanged();
2023-05-02 11:48:40 +00:00
setCurrentChat(m_newChat);
2023-05-01 21:13:20 +00:00
}
Q_INVOKABLE void removeChat(Chat* chat)
{
if (!m_chats.contains(chat)) {
qDebug() << "WARNING: Removing chat failed with id" << chat->id();
return;
}
emit disconnectChat(chat);
2023-05-02 13:07:28 +00:00
if (chat == m_newChat) {
m_newChat->disconnect(this);
m_newChat = nullptr;
}
2023-05-01 21:13:20 +00:00
const int index = m_chats.indexOf(chat);
2023-05-02 00:56:53 +00:00
if (m_chats.count() < 2) {
addChat();
} else {
int nextIndex;
if (index == m_chats.count() - 1)
nextIndex = index - 1;
else
nextIndex = index + 1;
Chat *nextChat = get(nextIndex);
Q_ASSERT(nextChat);
setCurrentChat(nextChat);
}
2023-05-01 21:13:20 +00:00
beginRemoveRows(QModelIndex(), index, index);
m_chats.removeAll(chat);
endRemoveRows();
delete chat;
}
Chat *currentChat() const
{
return m_currentChat;
}
void setCurrentChat(Chat *chat)
{
if (!m_chats.contains(chat)) {
qDebug() << "ERROR: Setting current chat failed with id" << chat->id();
return;
}
if (m_currentChat) {
if (m_currentChat->isModelLoaded())
m_currentChat->unload();
2023-05-01 21:13:20 +00:00
emit disconnect(m_currentChat);
}
emit connectChat(chat);
m_currentChat = chat;
if (!m_currentChat->isModelLoaded())
m_currentChat->reload();
2023-05-01 21:13:20 +00:00
emit currentChatChanged();
}
Q_INVOKABLE Chat* get(int index)
{
if (index < 0 || index >= m_chats.size()) return nullptr;
return m_chats.at(index);
}
int count() const { return m_chats.size(); }
Q_SIGNALS:
void countChanged();
void connectChat(Chat*);
void disconnectChat(Chat*);
2023-05-01 21:13:20 +00:00
void currentChatChanged();
2023-05-02 11:48:40 +00:00
private Q_SLOTS:
void newChatCountChanged()
{
Q_ASSERT(m_newChat && m_newChat->chatModel()->count());
m_newChat->disconnect(this);
m_newChat = nullptr;
}
2023-05-01 21:13:20 +00:00
private:
2023-05-02 11:48:40 +00:00
Chat* m_newChat;
2023-05-01 21:13:20 +00:00
Chat* m_currentChat;
QList<Chat*> m_chats;
};
#endif // CHATITEMMODEL_H