KTextAddons

textautogeneratechatmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2025 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "textautogeneratechatmodel.h"
8
9using namespace TextAutogenerateText;
10TextAutoGenerateChatModel::TextAutoGenerateChatModel(QObject *parent)
11 : QAbstractListModel{parent}
12{
13}
14
15TextAutoGenerateChatModel::~TextAutoGenerateChatModel() = default;
16
17int TextAutoGenerateChatModel::rowCount(const QModelIndex &parent) const
18{
19 if (parent.isValid()) {
20 return 0; // flat model
21 }
22 return mMessages.count();
23}
24
25QVariant TextAutoGenerateChatModel::data(const QModelIndex &index, int role) const
26{
27 if (index.row() < 0 || index.row() >= mMessages.count()) {
28 return {};
29 }
30 const auto &message = mMessages[index.row()];
31 switch (role) {
32 case Qt::DisplayRole:
33 case MessageRole:
34 return message.htmlGenerated();
35 case DateTimeRole:
36 return message.dateTime();
37 case SenderRole:
38 return QVariant::fromValue(message.sender());
39 case FinishedRole:
40 return !message.inProgress();
41 case UuidRole:
42 return message.uuid();
43 }
44 return {};
45}
46
47QList<TextAutoGenerateMessage> TextAutoGenerateChatModel::messages() const
48{
49 return mMessages;
50}
51
52void TextAutoGenerateChatModel::setMessages(const QList<TextAutoGenerateMessage> &newMessages)
53{
55 mMessages = newMessages;
57}
58
59void TextAutoGenerateChatModel::resetConversation()
60{
62 mMessages.clear();
64 Q_EMIT conversationCleared();
65}
66
67void TextAutoGenerateChatModel::addMessage(const TextAutoGenerateMessage &msg)
68{
69 beginInsertRows(QModelIndex(), mMessages.count(), mMessages.count());
70 mMessages.append(msg);
72}
73
74void TextAutoGenerateChatModel::replaceLastMessage(const TextAutoGenerateMessage &msg)
75{
76 beginInsertRows(QModelIndex(), mMessages.count(), mMessages.count());
77 mMessages.removeLast();
78 mMessages.append(msg);
79 auto emitChanged = [this](int rowNumber, const QList<int> &roles = QList<int>()) {
80 const QModelIndex index = createIndex(rowNumber, 0);
82 };
83 emitChanged(mMessages.count() - 1, {MessageRole});
85}
86
87TextAutoGenerateMessage TextAutoGenerateChatModel::lastMessage() const
88{
89 if (mMessages.isEmpty()) {
90 return TextAutoGenerateMessage();
91 }
92 return mMessages.last();
93}
94
95void TextAutoGenerateChatModel::removeDiscussion(const QByteArray &uuid)
96{
97 // TODO
98}
99
100#include "moc_textautogeneratechatmodel.cpp"
void beginInsertRows(const QModelIndex &parent, int first, int last)
QModelIndex createIndex(int row, int column, const void *ptr) const const
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const=0
virtual QModelIndex parent(const QModelIndex &index) const const=0
Q_EMITQ_EMIT
DisplayRole
QVariant fromValue(T &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 18 2025 12:00:52 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.