Skip to content

Commit

Permalink
Fixup the clipboard copy.
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Treat <treat.adam@gmail.com>
  • Loading branch information
manyoso committed Dec 19, 2024
1 parent 69a74af commit 495973a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
4 changes: 1 addition & 3 deletions gpt4all-chat/qml/ChatItemView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,7 @@ GridLayout {
name: qsTr("Copy")
source: "qrc:/gpt4all/icons/copy.svg"
onClicked: {
myTextArea.selectAll();
myTextArea.copy();
myTextArea.deselect();
chatModel.copyToClipboard(index);
}
}

Expand Down
21 changes: 21 additions & 0 deletions gpt4all-chat/src/chatmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

#include <fmt/format.h>

#include <QApplication>
#include <QAbstractListModel>
#include <QBuffer>
#include <QByteArray>
#include <QClipboard>
#include <QDataStream>
#include <QJsonDocument>
#include <QHash>
Expand Down Expand Up @@ -293,6 +295,15 @@ class ChatItem : public QObject
return code + result;
}

QString clipboardContent() const
{
QStringList clipContent;
for (const ChatItem *item : subItems)
clipContent << item->clipboardContent();
clipContent << content();
return clipContent.join("");
}

QList<ChatItem *> childItems() const
{
// We currently have leaf nodes at depth 3 with nodes at depth 2 as mere containers we don't
Expand Down Expand Up @@ -971,6 +982,16 @@ class ChatModel : public QAbstractListModel
emit hasErrorChanged(value);
}

Q_INVOKABLE void copyToClipboard(int index)
{
QMutexLocker locker(&m_mutex);
if (index < 0 || index >= m_chatItems.size())
return;
ChatItem *item = m_chatItems.at(index);
QClipboard *clipboard = QGuiApplication::clipboard();
clipboard->setText(item->clipboardContent(), QClipboard::Clipboard);
}

qsizetype count() const { QMutexLocker locker(&m_mutex); return m_chatItems.size(); }

std::vector<MessageItem> messageItems() const
Expand Down

0 comments on commit 495973a

Please sign in to comment.