diff --git a/chatview/BaseChatWidget.cpp b/chatview/BaseChatWidget.cpp index 427503d..bb16c20 100644 --- a/chatview/BaseChatWidget.cpp +++ b/chatview/BaseChatWidget.cpp @@ -24,9 +24,9 @@ namespace QodeAssist::Chat { -BaseChatWidget::BaseChatWidget(QWidget *parent) +BaseChatWidget::BaseChatWidget(QWidget *parent) : QQuickWidget(parent) { - setSource(QUrl("qrc:/ChatView/qml/ChatView.qml")); + setSource(QUrl("qrc:/ChatView/qml/RootItem.qml")); setResizeMode(QQuickWidget::SizeRootObjectToView); engine()->rootContext()->setContextObject(this); diff --git a/chatview/BaseChatWidget.hpp b/chatview/BaseChatWidget.hpp index 79f5b7e..e255672 100644 --- a/chatview/BaseChatWidget.hpp +++ b/chatview/BaseChatWidget.hpp @@ -26,6 +26,7 @@ namespace QodeAssist::Chat { class BaseChatWidget : public QQuickWidget { Q_OBJECT + public: explicit BaseChatWidget(QWidget *parent = nullptr); ~BaseChatWidget() = default; diff --git a/chatview/CMakeLists.txt b/chatview/CMakeLists.txt index be590c8..14812bb 100644 --- a/chatview/CMakeLists.txt +++ b/chatview/CMakeLists.txt @@ -6,9 +6,10 @@ qt_add_qml_module(QodeAssistChatView URI ChatView VERSION 1.0 QML_FILES - qml/ChatView.qml + qml/RootItem.qml SOURCES BaseChatWidget.hpp BaseChatWidget.cpp + ChatModel.hpp ChatModel.cpp ) target_link_libraries(QodeAssistChatView diff --git a/chatview/ChatModel.cpp b/chatview/ChatModel.cpp new file mode 100644 index 0000000..67eda66 --- /dev/null +++ b/chatview/ChatModel.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2024 Petr Mironychev + * + * This file is part of QodeAssist. + * + * QodeAssist is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * QodeAssist is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QodeAssist. If not, see . + */ + +#include "ChatModel.hpp" + +namespace QodeAssist::Chat { + +ChatModel::ChatModel(QObject *parent) + : QAbstractListModel(parent) +{ +} + +int ChatModel::rowCount(const QModelIndex &parent) const +{ + return m_messages.size(); +} + +QVariant ChatModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid() || index.row() >= m_messages.size()) + return QVariant(); + + const Message &message = m_messages[index.row()]; + switch (static_cast(role)) { + case Roles::RoleType: + return QVariant::fromValue(message.role); + case Roles::Content: + return message.content; + default: + return QVariant(); + } +} + +QHash ChatModel::roleNames() const +{ + QHash roles; + roles[Roles::RoleType] = "roleType"; + roles[Roles::Content] = "content"; + return roles; +} + +void ChatModel::addMessage(const QString &content, ChatRole role) +{ + beginInsertRows(QModelIndex(), m_messages.size(), m_messages.size()); + m_messages.append({role, content}); + endInsertRows(); +} + +void ChatModel::clear() +{ + beginResetModel(); + m_messages.clear(); + endResetModel(); +} + +} // namespace QodeAssist::Chat diff --git a/chatview/ChatModel.hpp b/chatview/ChatModel.hpp new file mode 100644 index 0000000..2058d34 --- /dev/null +++ b/chatview/ChatModel.hpp @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2024 Petr Mironychev + * + * This file is part of QodeAssist. + * + * QodeAssist is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * QodeAssist is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with QodeAssist. If not, see . + */ + +#pragma once + +#include + +namespace QodeAssist::Chat { + +enum class ChatRole { System, User, Assistant }; + +struct Message +{ + ChatRole role; + QString content; +}; + +class ChatModel : public QAbstractListModel +{ + Q_OBJECT + +public: + enum Roles { RoleType = Qt::UserRole, Content }; + + explicit ChatModel(QObject *parent = nullptr); + + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + QHash roleNames() const override; + + Q_INVOKABLE void addMessage(const QString &content, ChatRole role); + Q_INVOKABLE void clear(); + +private: + QVector m_messages; +}; + +} // namespace QodeAssist::Chat + +Q_DECLARE_METATYPE(QodeAssist::Chat::ChatRole) diff --git a/chatview/qml/ChatView.qml b/chatview/qml/RootItem.qml similarity index 100% rename from chatview/qml/ChatView.qml rename to chatview/qml/RootItem.qml