-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplacedata_dialog.cpp
More file actions
168 lines (149 loc) · 5.08 KB
/
replacedata_dialog.cpp
File metadata and controls
168 lines (149 loc) · 5.08 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
/***********************************************
gSafe document editor
Author:
(C) 2026 Deák Péter (hyper80@gmail.com)
*/
#include "replacedata_dialog.h"
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QHeaderView>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QShortcut>
#include <QKeySequence>
#include <QAbstractItemDelegate>
#include <QLineEdit>
ReplaceDataDialog::ReplaceDataDialog(const QMap<QString, QString> &initial, QWidget *parent)
: QDialog(parent)
{
table = new QTableWidget(this);
table->setColumnCount(2);
table->setHorizontalHeaderLabels({tr("Key"), tr("Value")});
table->horizontalHeader()->setStretchLastSection(true);
table->setSelectionBehavior(QAbstractItemView::SelectRows);
table->setSelectionMode(QAbstractItemView::SingleSelection);
table->setStyleSheet("QTableView::item:selected { background-color: #5378c9; }");
for (auto it = initial.constBegin(); it != initial.constEnd(); ++it)
{
int row = table->rowCount();
table->insertRow(row);
table->setItem(row, 0, new QTableWidgetItem(it.key()));
table->setItem(row, 1, new QTableWidgetItem(it.value()));
}
// Install a shortcut so Enter commits the editor and moves to next cell.
QShortcut *enterShortcut = new QShortcut(QKeySequence(Qt::Key_Return), table);
connect(enterShortcut, &QShortcut::activated, this, &ReplaceDataDialog::handleEnterKey);
QPushButton *addBtn = new QPushButton(tr("New line"), this);
QPushButton *removeBtn = new QPushButton(tr("Delete"), this);
connect(addBtn, &QPushButton::clicked, this, &ReplaceDataDialog::addRow);
connect(removeBtn, &QPushButton::clicked, this, &ReplaceDataDialog::removeSelectedRows);
QHBoxLayout *btnLayout = new QHBoxLayout;
btnLayout->addWidget(addBtn);
btnLayout->addWidget(removeBtn);
btnLayout->addStretch();
QDialogButtonBox *box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
connect(box, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(box, &QDialogButtonBox::rejected, this, &QDialog::reject);
QVBoxLayout *main = new QVBoxLayout(this);
main->addWidget(table);
main->addLayout(btnLayout);
main->addWidget(box);
setWindowTitle(tr("Edit Replacement Data"));
resize(500, 400);
}
QMap<QString, QString> ReplaceDataDialog::results() const
{
QMap<QString, QString> map;
for (int r = 0; r < table->rowCount(); ++r)
{
QTableWidgetItem *k = table->item(r, 0);
QTableWidgetItem *v = table->item(r, 1);
if (k && !k->text().isEmpty())
map.insert(k->text(), v ? v->text() : QString());
}
return map;
}
void ReplaceDataDialog::addRow()
{
int row = table->rowCount();
table->insertRow(row);
table->setItem(row, 0, new QTableWidgetItem(QString()));
table->setItem(row, 1, new QTableWidgetItem(QString()));
table->setCurrentCell(row, 0);
table->setFocus();
QTableWidgetItem *it = table->item(row, 0);
if (it)
table->editItem(it);
}
void ReplaceDataDialog::removeSelectedRows()
{
QList<QTableWidgetSelectionRange> ranges = table->selectedRanges();
for (const auto &range : ranges)
{
for (int r = range.bottomRow(); r >= range.topRow(); --r)
table->removeRow(r);
}
}
void ReplaceDataDialog::handleEnterKey()
{
QWidget *ed = table->focusWidget();
QModelIndex idx = table->currentIndex();
int row = idx.row();
int col = idx.column();
QString entered;
if (QLineEdit *le = qobject_cast<QLineEdit*>(ed))
{
entered = le->text();
}
else
{
// fallback: try to commit via delegate
if (ed)
{
QAbstractItemDelegate *del = table->itemDelegate();
if (del)
{
del->setModelData(ed, table->model(), idx);
del->destroyEditor(ed, idx);
}
}
QTableWidgetItem *it = table->item(row, col);
if (it) entered = it->text();
}
// ensure item exists and set text
QTableWidgetItem *curItem = table->item(row, col);
if (!curItem)
{
curItem = new QTableWidgetItem(QString());
table->setItem(row, col, curItem);
}
curItem->setText(entered);
int newRow = row;
int newCol = col;
if (col < table->columnCount() - 1)
{
// move to next column in same row
newCol = col + 1;
}
else
{
// last column: move to next row's first column
newCol = 0;
newRow = row + 1;
if (newRow >= table->rowCount())
{
int r = table->rowCount();
table->insertRow(r);
// ensure new row has items
table->setItem(r, 0, new QTableWidgetItem(QString()));
table->setItem(r, 1, new QTableWidgetItem(QString()));
newRow = r;
}
}
if (!table->item(newRow, newCol))
table->setItem(newRow, newCol, new QTableWidgetItem(QString()));
table->setCurrentCell(newRow, newCol);
table->editItem(table->item(newRow, newCol));
}