Repository navigation
Expand file tree
/
Copy pathVectorEditDialog.cpp
More file actions
340 lines (294 loc) · 11.3 KB
/
Copy pathVectorEditDialog.cpp
File metadata and controls
340 lines (294 loc) · 11.3 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "VectorEditDialog.h"
#include "VecDatabase.h"
#include <QBrush>
#include <QDialogButtonBox>
#include <QDoubleSpinBox>
#include <QFormLayout>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QInputDialog>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QPushButton>
#include <QRegularExpression>
#include <QSpinBox>
#include <QTableWidget>
#include <QVBoxLayout>
#include <cmath>
namespace vec {
namespace {
constexpr int kIndexCol = 0;
constexpr int kValueCol = 1;
// 9 significant digits round-trip a float32 exactly.
QString floatText(float v) { return QString::number(double(v), 'g', 9); }
// Locale-independent parse; rejects empty text, NaN and infinity.
bool parseFloat(const QString& text, float& out)
{
bool ok = false;
const double d = QString(text).trimmed().toDouble(&ok);
if (!ok || !std::isfinite(d) || std::fabs(d) > double(3.4e38))
return false;
out = float(d);
return true;
}
} // namespace
VectorEditDialog::VectorEditDialog(const QString& title, std::vector<float> values, QWidget* parent)
: QDialog(parent), m_original(std::move(values))
{
setWindowTitle(title);
resize(520, 640);
const int dims = int(m_original.size());
m_filter = new QLineEdit(this);
m_filter->setPlaceholderText(tr("Filter by index or value…"));
m_filter->setClearButtonEnabled(true);
m_gotoSpin = new QSpinBox(this);
m_gotoSpin->setRange(0, qMax(0, dims - 1));
auto* gotoBtn = new QPushButton(tr("Go to index"), this);
auto* pasteBtn = new QPushButton(tr("Paste list…"), this);
pasteBtn->setToolTip(tr("Replace all %1 values with a pasted list of numbers").arg(dims));
auto* rangeBtn = new QPushButton(tr("Set range…"), this);
rangeBtn->setToolTip(tr("Give the same value to a range of elements"));
auto* resetBtn = new QPushButton(tr("Reset"), this);
resetBtn->setToolTip(tr("Go back to the stored values"));
auto* topRow = new QHBoxLayout;
topRow->addWidget(m_filter, 1);
topRow->addWidget(m_gotoSpin);
topRow->addWidget(gotoBtn);
auto* toolRow = new QHBoxLayout;
toolRow->addWidget(pasteBtn);
toolRow->addWidget(rangeBtn);
toolRow->addWidget(resetBtn);
toolRow->addStretch();
m_table = new QTableWidget(dims, 2, this);
m_table->setHorizontalHeaderLabels({tr("Index"), tr("Value")});
m_table->verticalHeader()->setVisible(false);
m_table->horizontalHeader()->setStretchLastSection(true);
m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
m_table->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::EditKeyPressed
| QAbstractItemView::AnyKeyPressed);
m_populating = true;
for (int i = 0; i < dims; ++i) {
auto* idx = new QTableWidgetItem(QString::number(i));
idx->setFlags(idx->flags() & ~Qt::ItemIsEditable);
idx->setForeground(QBrush(QColor("#888780")));
m_table->setItem(i, kIndexCol, idx);
m_table->setItem(i, kValueCol, new QTableWidgetItem(floatText(m_original[std::size_t(i)])));
}
m_populating = false;
m_info = new QLabel(this);
m_info->setWordWrap(true);
updateInfo();
auto* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
auto* root = new QVBoxLayout(this);
root->addLayout(topRow);
root->addLayout(toolRow);
root->addWidget(m_table, 1);
root->addWidget(m_info);
root->addWidget(buttons);
connect(m_filter, &QLineEdit::textChanged, this, &VectorEditDialog::applyFilter);
connect(gotoBtn, &QPushButton::clicked, this, &VectorEditDialog::goToIndex);
connect(m_gotoSpin, &QSpinBox::editingFinished, this, &VectorEditDialog::goToIndex);
connect(pasteBtn, &QPushButton::clicked, this, &VectorEditDialog::pasteList);
connect(rangeBtn, &QPushButton::clicked, this, &VectorEditDialog::setRange);
connect(resetBtn, &QPushButton::clicked, this, &VectorEditDialog::resetAll);
connect(m_table, &QTableWidget::itemChanged, this, &VectorEditDialog::onItemChanged);
}
bool VectorEditDialog::parseRow(int row, float& out) const
{
const QTableWidgetItem* item = m_table->item(row, kValueCol);
return item && parseFloat(item->text(), out);
}
void VectorEditDialog::markRow(int row)
{
QTableWidgetItem* item = m_table->item(row, kValueCol);
if (!item)
return;
float v = 0.f;
const bool valid = parseFloat(item->text(), v);
const bool changed = valid && v != m_original[std::size_t(row)];
QBrush brush; // default = no background
if (!valid)
brush = QBrush(QColor("#6B1F23")); // red: not a number
else if (changed)
brush = QBrush(QColor("#5C4A12")); // amber: modified
item->setBackground(brush);
}
void VectorEditDialog::updateInfo()
{
int changed = 0, invalid = 0;
for (int i = 0; i < m_table->rowCount(); ++i) {
float v = 0.f;
if (!parseRow(i, v))
++invalid;
else if (v != m_original[std::size_t(i)])
++changed;
}
QString text = tr("%1 dimensions (fixed). Changed: %2.").arg(m_table->rowCount()).arg(changed);
if (invalid > 0)
text += tr(" Invalid: %1 (marked red).").arg(invalid);
m_info->setText(text);
}
void VectorEditDialog::onItemChanged(QTableWidgetItem* item)
{
if (m_populating || !item || item->column() != kValueCol)
return;
markRow(item->row());
updateInfo();
}
void VectorEditDialog::applyFilter(const QString& text)
{
const QString needle = text.trimmed();
for (int i = 0; i < m_table->rowCount(); ++i) {
bool show = needle.isEmpty();
if (!show) {
const QTableWidgetItem* v = m_table->item(i, kValueCol);
show = QString::number(i).contains(needle) || (v && v->text().contains(needle));
}
m_table->setRowHidden(i, !show);
}
}
void VectorEditDialog::goToIndex()
{
const int row = m_gotoSpin->value();
if (row < 0 || row >= m_table->rowCount())
return;
m_filter->clear(); // a filter could hide the target row
m_table->selectRow(row);
m_table->scrollToItem(m_table->item(row, kValueCol), QAbstractItemView::PositionAtCenter);
m_table->setCurrentCell(row, kValueCol);
m_table->setFocus();
}
void VectorEditDialog::pasteList()
{
bool ok = false;
const QString text = QInputDialog::getMultiLineText(
this, tr("Paste list"),
tr("Numbers separated by commas, spaces or new lines (exactly %1 values; [ ] are ignored):")
.arg(m_table->rowCount()),
QString(), &ok);
if (!ok || text.trimmed().isEmpty())
return;
static const QRegularExpression splitter(QStringLiteral("[\\s,;\\[\\]]+"));
const QStringList parts = text.split(splitter, Qt::SkipEmptyParts);
if (parts.size() != m_table->rowCount()) {
QMessageBox::warning(this, tr("Paste list"),
tr("Got %1 values, but this vector has exactly %2 dimensions.")
.arg(parts.size()).arg(m_table->rowCount()));
return;
}
std::vector<float> parsed(parts.size());
for (int i = 0; i < parts.size(); ++i) {
if (!parseFloat(parts[i], parsed[std::size_t(i)])) {
QMessageBox::warning(this, tr("Paste list"),
tr("Value #%1 is not a valid number: \"%2\".").arg(i).arg(parts[i]));
return;
}
}
m_populating = true;
for (int i = 0; i < parts.size(); ++i) {
m_table->item(i, kValueCol)->setText(floatText(parsed[std::size_t(i)]));
markRow(i);
}
m_populating = false;
updateInfo();
}
void VectorEditDialog::setRange()
{
const int last = m_table->rowCount() - 1;
QDialog dlg(this);
dlg.setWindowTitle(tr("Set range"));
auto* from = new QSpinBox(&dlg);
from->setRange(0, last);
auto* to = new QSpinBox(&dlg);
to->setRange(0, last);
to->setValue(last);
auto* value = new QDoubleSpinBox(&dlg);
value->setDecimals(9);
value->setRange(-3.4e38, 3.4e38);
auto* form = new QFormLayout;
form->addRow(tr("From index:"), from);
form->addRow(tr("To index (inclusive):"), to);
form->addRow(tr("Value:"), value);
auto* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, &dlg);
connect(buttons, &QDialogButtonBox::accepted, &dlg, &QDialog::accept);
connect(buttons, &QDialogButtonBox::rejected, &dlg, &QDialog::reject);
auto* lay = new QVBoxLayout(&dlg);
lay->addLayout(form);
lay->addWidget(buttons);
if (dlg.exec() != QDialog::Accepted)
return;
const int a = qMin(from->value(), to->value());
const int b = qMax(from->value(), to->value());
const QString text = floatText(float(value->value()));
m_populating = true;
for (int i = a; i <= b; ++i) {
m_table->item(i, kValueCol)->setText(text);
markRow(i);
}
m_populating = false;
updateInfo();
}
void VectorEditDialog::resetAll()
{
m_populating = true;
for (int i = 0; i < m_table->rowCount(); ++i) {
m_table->item(i, kValueCol)->setText(floatText(m_original[std::size_t(i)]));
markRow(i);
}
m_populating = false;
updateInfo();
}
std::vector<float> VectorEditDialog::values() const
{
std::vector<float> out(std::size_t(m_table->rowCount()), 0.f);
for (int i = 0; i < m_table->rowCount(); ++i)
parseRow(i, out[std::size_t(i)]);
return out;
}
void VectorEditDialog::accept()
{
for (int i = 0; i < m_table->rowCount(); ++i) {
float v = 0.f;
if (!parseRow(i, v)) {
m_filter->clear();
m_table->selectRow(i);
m_table->scrollToItem(m_table->item(i, kValueCol), QAbstractItemView::PositionAtCenter);
QMessageBox::warning(this, windowTitle(),
tr("Element %1 is not a valid number. Fix it (or press Reset) first.").arg(i));
return;
}
}
QDialog::accept();
}
bool VectorEditDialog::editRecord(QWidget* parent, const QString& dbPath, const QString& table, qint64 rowid)
{
Database db;
if (!db.open(dbPath, /*readOnly=*/false, /*create=*/false)) {
QMessageBox::critical(parent, tr("Edit vector"), tr("Cannot open the database:\n%1").arg(db.lastError()));
return false;
}
const auto current = db.embeddingOf(table, rowid);
if (!current) {
QMessageBox::warning(parent, tr("Edit vector"),
tr("No vector found for id %1 in '%2'.").arg(rowid).arg(table));
return false;
}
VectorEditDialog dialog(tr("Edit vector - %1 (id %2)").arg(table).arg(rowid), *current, parent);
if (dialog.exec() != QDialog::Accepted)
return false;
const std::vector<float> edited = dialog.values();
if (edited == *current)
return false; // nothing changed
if (!db.updateEmbedding(table, rowid, edited)) {
QMessageBox::critical(parent, tr("Edit vector"),
tr("The vector could not be saved:\n%1\n\n"
"(This needs a sqlite-vec version that supports UPDATE on vector columns.)")
.arg(db.lastError()));
return false;
}
return true;
}
} // namespace vec