Skip to content

Commit 1bcc688

Browse files
committed
Fixup the clipboard copy.
Signed-off-by: Adam Treat <[email protected]>
1 parent e40f055 commit 1bcc688

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

gpt4all-chat/qml/ChatItemView.qml

+1-3
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,7 @@ GridLayout {
534534
name: qsTr("Copy")
535535
source: "qrc:/gpt4all/icons/copy.svg"
536536
onClicked: {
537-
myTextArea.selectAll();
538-
myTextArea.copy();
539-
myTextArea.deselect();
537+
chatModel.copyToClipboard(index);
540538
}
541539
}
542540

gpt4all-chat/src/chatmodel.h

+21
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
#include <fmt/format.h>
1111

12+
#include <QApplication>
1213
#include <QAbstractListModel>
1314
#include <QBuffer>
1415
#include <QByteArray>
16+
#include <QClipboard>
1517
#include <QDataStream>
1618
#include <QJsonDocument>
1719
#include <QHash>
@@ -293,6 +295,15 @@ class ChatItem : public QObject
293295
return code + result;
294296
}
295297

298+
QString clipboardContent() const
299+
{
300+
QStringList clipContent;
301+
for (const ChatItem *item : subItems)
302+
clipContent << item->clipboardContent();
303+
clipContent << content();
304+
return clipContent.join("");
305+
}
306+
296307
QList<ChatItem *> childItems() const
297308
{
298309
// We currently have leaf nodes at depth 3 with nodes at depth 2 as mere containers we don't
@@ -971,6 +982,16 @@ class ChatModel : public QAbstractListModel
971982
emit hasErrorChanged(value);
972983
}
973984

985+
Q_INVOKABLE void copyToClipboard(int index)
986+
{
987+
QMutexLocker locker(&m_mutex);
988+
if (index < 0 || index >= m_chatItems.size())
989+
return;
990+
ChatItem *item = m_chatItems.at(index);
991+
QClipboard *clipboard = QGuiApplication::clipboard();
992+
clipboard->setText(item->clipboardContent(), QClipboard::Clipboard);
993+
}
994+
974995
qsizetype count() const { QMutexLocker locker(&m_mutex); return m_chatItems.size(); }
975996

976997
std::vector<MessageItem> messageItems() const

0 commit comments

Comments
 (0)