KTextAddons

textautogeneratehistorylistview.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 "textautogeneratehistorylistview.h"
8#include "core/textautogeneratechatmodel.h"
9#include "core/textautogeneratehistorymodel.h"
10#include "core/textautogeneratehistorysortfilterproxymodel.h"
11#include "core/textautogeneratemanager.h"
12#include "textautogeneratehistorylistviewdelegate.h"
13#include <KLocalizedString>
14#include <QContextMenuEvent>
15#include <QMenu>
16
17using namespace TextAutogenerateText;
18TextAutogenerateHistoryListView::TextAutogenerateHistoryListView(QWidget *parent)
19 : QListView(parent)
20 , mHistoryProxyModel(new TextAutoGenerateHistorySortFilterProxyModel(this))
21{
22 setDragEnabled(false);
23 setUniformItemSizes(false);
24 setItemDelegate(new TextAutogenerateHistoryListViewDelegate(this));
25
26 auto historyModel = new TextAutoGenerateHistoryModel(this);
27 historyModel->setSourceModel(TextAutogenerateManager::self()->textAutoGenerateChatModel());
28 mHistoryProxyModel->setSourceModel(historyModel);
29 setModel(mHistoryProxyModel);
30
31 connect(this, &TextAutogenerateHistoryListView::clicked, this, &TextAutogenerateHistoryListView::slotClicked);
32}
33
34TextAutogenerateHistoryListView::~TextAutogenerateHistoryListView() = default;
35
36void TextAutogenerateHistoryListView::keyPressEvent(QKeyEvent *event)
37{
38 if (event->key() == Qt::Key_Escape) {
39 event->accept();
40 } else {
42 }
43}
44void TextAutogenerateHistoryListView::slotClicked(const QModelIndex &idx)
45{
46 if (idx.isValid()) {
47 const QByteArray uuid = idx.data(TextAutoGenerateChatModel::UuidRole).toByteArray();
48 if (!uuid.isEmpty()) {
49 Q_EMIT goToDiscussion(uuid);
50 }
51 }
52}
53
54void TextAutogenerateHistoryListView::contextMenuEvent(QContextMenuEvent *event)
55{
56 QMenu menu(this);
57 const QModelIndex index = indexAt(event->pos());
58 if (index.isValid()) {
59 auto renameHistory = new QAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18nc("@action", "Modify…"), &menu);
60 connect(renameHistory, &QAction::triggered, this, [index, this]() {
61 const QByteArray uuid = index.data(TextAutoGenerateChatModel::UuidRole).toByteArray();
62 if (!uuid.isEmpty()) {
63 edit(index);
64 }
65 });
66 menu.addAction(renameHistory);
67
68 menu.addSeparator();
69
70 auto removeHistory = new QAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18nc("@action", "Remove…"), &menu);
71 connect(removeHistory, &QAction::triggered, this, [index]() {
72 const QByteArray uuid = index.data(TextAutoGenerateChatModel::UuidRole).toByteArray();
73 if (!uuid.isEmpty()) {
74 TextAutogenerateManager::self()->textAutoGenerateChatModel()->removeDiscussion(uuid);
75 }
76 });
77 menu.addAction(removeHistory);
78
79 const bool archived = index.data(TextAutoGenerateChatModel::ArchivedRole).toBool();
80 menu.addSeparator();
81 if (archived) {
82 auto restoreArchivedAction = new QAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18nc("@action", "Restore"), &menu);
83 connect(restoreArchivedAction, &QAction::triggered, this, [index]() {
84 /*
85 const QByteArray uuid = index.data(TextAutoGenerateChatModel::UuidRole).toByteArray();
86 if (!uuid.isEmpty()) {
87 TextAutogenerateManager::self()->textAutoGenerateChatModel()->removeDiscussion(uuid);
88 }
89 */
90 });
91 menu.addAction(restoreArchivedAction);
92 } else {
93#if 0
94 auto archiveAction = new QAction(QIcon::fromTheme(QStringLiteral("view-refresh")), i18nc("@action", "Restore"), &menu);
95 connect(archiveAction, &QAction::triggered, this, [index]() {
96 /*
97 const QByteArray uuid = index.data(TextAutoGenerateChatModel::UuidRole).toByteArray();
98 if (!uuid.isEmpty()) {
99 TextAutogenerateManager::self()->textAutoGenerateChatModel()->removeDiscussion(uuid);
100 }
101 */
102 });
103 menu.addAction(archiveAction);
104#endif
105 // TODO
106 }
107 }
108 if (!menu.actions().isEmpty()) {
109 menu.exec(event->globalPos());
110 }
111}
112
113void TextAutogenerateHistoryListView::slotSearchTextChanged(const QString &str)
114{
115 mHistoryProxyModel->setFilterFixedString(str);
116}
117#include "moc_textautogeneratehistorylistview.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void clicked(const QModelIndex &index)
virtual bool edit(const QModelIndex &index, EditTrigger trigger, QEvent *event)
virtual bool event(QEvent *event) override
virtual void keyPressEvent(QKeyEvent *event) override
void triggered(bool checked)
bool isEmpty() const const
QIcon fromTheme(const QString &name)
virtual QModelIndex indexAt(const QPoint &p) const const override
QVariant data(int role) const const
bool isValid() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
Key_Escape
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool toBool() const const
QByteArray toByteArray() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri May 2 2025 12:06:03 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.