forked from cristianadam/llama.qtcreator
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtoolsettingswidget.cpp
More file actions
205 lines (175 loc) · 6.85 KB
/
toolsettingswidget.cpp
File metadata and controls
205 lines (175 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "toolsettingswidget.h"
#include "llamasettings.h"
#include "llamatr.h"
#include "tools/factory.h"
#include <utils/qtcassert.h>
#include <QHeaderView>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
using namespace Utils;
namespace LlamaCpp {
ToolsSettingsWidget::ToolsSettingsWidget()
{
m_view = new QTreeView(this);
m_view->setUniformRowHeights(true);
m_view->setHeaderHidden(false);
m_detailEdit = new QTextEdit(this);
m_detailEdit->setReadOnly(true);
m_detailEdit->setWordWrapMode(QTextOption::NoWrap);
m_detailEdit->setPlaceholderText(Tr::tr("Select a tool to view its JSON definition"));
// model
m_model = new TreeModel<>(m_view);
m_model->setHeader({Tr::tr("Tool"), Tr::tr("Description")});
fillModel();
m_view->setModel(m_model);
m_view->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
m_view->header()->setSectionResizeMode(1, QHeaderView::Stretch);
// layout
using namespace Layouting;
Column{m_view, m_detailEdit}.attachTo(this);
// keep model in sync when user toggles a check‑box
connect(m_model, &TreeModel<>::dataChanged, this, [this] { updateEnabledToolsFromModel(); });
connect(m_view->selectionModel(),
&QItemSelectionModel::currentChanged,
this,
&ToolsSettingsWidget::showToolDefinition);
}
void ToolsSettingsWidget::fillModel()
{
m_model->clear();
const QStringList allTools = ToolFactory::instance().creatorsList();
for (const QString &toolName : allTools) {
std::unique_ptr<Tool> tmp = ToolFactory::instance().create(toolName);
const QString json = tmp ? tmp->toolDefinition() : QString();
QString description;
QString tooltip = json; // default tooltip = whole JSON
if (!json.isEmpty()) {
QJsonParseError err;
const QJsonDocument doc = QJsonDocument::fromJson(json.toUtf8(), &err);
if (!doc.isNull() && doc.isObject()) {
const QJsonObject rootObj = doc.object();
// The schema we use is:
// {
// "type": "function",
// "function": { "description": "...", ... }
// }
const QJsonObject functionObj = rootObj.value(QStringLiteral("function")).toObject();
if (!functionObj.isEmpty()) {
description = functionObj.value(QStringLiteral("description")).toString();
}
}
}
if (description.isEmpty())
description = Tr::tr("No description");
// Store both the short description and the full JSON (tooltip).
m_model->rootItem()->appendChild(new ToolItem(toolName, description, tooltip));
}
// Initialise the check‑states from the stored settings
updateModelFromEnabledTools();
}
void ToolsSettingsWidget::showToolDefinition(const QModelIndex ¤t,
const QModelIndex & /*previous*/)
{
if (!current.isValid()) {
m_detailEdit->clear();
return;
}
// The model stores the full JSON string in the ToolItem (as “tooltip”).
// We can retrieve it via the custom role we already use – Qt::ToolTipRole.
// This works even if the tooltip is not shown to the user.
const QString json = current.data(Qt::ToolTipRole).toString();
m_detailEdit->setPlainText(json);
}
ToolsSettingsWidget::ToolItem::ToolItem(const QString &toolName,
const QString &description,
const QString &tooltip)
: m_name(toolName)
, m_description(description)
, m_tooltip(tooltip)
{
// default to unchecked – the UI will later set the correct state
m_checkState = Qt::Unchecked;
}
QVariant ToolsSettingsWidget::ToolItem::data(int column, int role) const
{
// Column 0 – name + check‑box
if (column == 0) {
if (role == Qt::DisplayRole)
return m_name;
if (role == Qt::CheckStateRole)
return m_checkState;
if (role == Qt::ToolTipRole)
return m_tooltip; // show the full JSON as tooltip
return QVariant();
}
// Column 1 – description (read‑only)
if (column == 1) {
if (role == Qt::DisplayRole)
return m_description;
if (role == Qt::ToolTipRole)
return m_tooltip; // also show tooltip on the description column
return QVariant();
}
return QVariant();
}
/* flags() – only column 0 is user‑checkable */
Qt::ItemFlags ToolsSettingsWidget::ToolItem::flags(int column) const
{
if (column == 0)
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
/* setData() – handle changes of the check‑state */
bool ToolsSettingsWidget::ToolItem::setData(int column, const QVariant &value, int role)
{
Q_UNUSED(column);
if (role == Qt::CheckStateRole) {
const Qt::CheckState newState = static_cast<Qt::CheckState>(value.toInt());
if (newState != m_checkState) {
m_checkState = newState;
// Returning true tells TreeModel<> to emit dataChanged for us.
return true;
}
}
return false;
}
void ToolsSettingsWidget::updateEnabledToolsFromModel()
{
// Walk through all rows and collect the names whose check‑state is Checked.
QStringList enabled;
for (int row = 0; row < m_model->rowCount(); ++row) {
const QModelIndex idx = m_model->index(row, 0);
const Qt::CheckState cs = static_cast<Qt::CheckState>(idx.data(Qt::CheckStateRole).toInt());
if (cs == Qt::Checked) {
const QString name = idx.data(Qt::DisplayRole).toString();
enabled << name;
}
}
// Write back to the global settings object.
settings().enabledToolsList.setValue(enabled);
}
void ToolsSettingsWidget::updateModelFromEnabledTools()
{
const QStringList enabled = settings().enabledToolsList();
for (int row = 0; row < m_model->rowCount(); ++row) {
const QModelIndex idx = m_model->index(row, 0);
const QString name = idx.data(Qt::DisplayRole).toString();
const Qt::CheckState cs = enabled.contains(name) ? Qt::Checked : Qt::Unchecked;
m_model->setData(idx, cs, Qt::CheckStateRole);
}
}
void ToolsSettingsWidget::apply()
{
// Ensure the latest UI state is persisted.
updateEnabledToolsFromModel();
// The settings object already knows its value, we just need to write it to disk.
settings().writeSettings(); // writes all changed aspects, including enabledTools
}
void ToolsSettingsWidget::cancel()
{
// Re‑load the stored value – this discards any UI changes.
settings().readSettings(); // reload from .ini
updateModelFromEnabledTools(); // reflect the stored state in the UI
}
} // namespace LlamaCpp