Repository navigation
Expand file tree
/
Copy pathCreateTableDialog.cpp
More file actions
275 lines (241 loc) · 10.3 KB
/
Copy pathCreateTableDialog.cpp
File metadata and controls
275 lines (241 loc) · 10.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
#include "CreateTableDialog.h"
#include <QCheckBox>
#include <QComboBox>
#include <QDialogButtonBox>
#include <QAbstractItemView>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QLineEdit>
#include <QMessageBox>
#include <QMouseEvent>
#include <QPushButton>
#include <QRegularExpression>
#include <QRegularExpressionValidator>
#include <QSet>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QVBoxLayout>
namespace {
// Only plain identifiers are accepted for table/column names - keeps the
// generated SQL simple and avoids needing to quote or escape anything.
const QRegularExpression kIdentifierPattern("^[A-Za-z_][A-Za-z0-9_]*$");
const QStringList kColumnTypes = {"INTEGER", "TEXT", "REAL", "NUMERIC", "BLOB"};
enum ColumnIndex { NameColumn, TypeColumn, PrimaryKeyColumn, NotNullColumn, ColumnCount };
// PK/NOT NULL cells hold a centered checkbox rather than a plain
// checkable item, so it lines up visually with the combo box column.
// Clicking anywhere in the cell toggles it - the box itself is only ~16px,
// which made it easy to click "next to" it and see nothing happen.
class CheckCell : public QWidget
{
public:
CheckCell()
{
m_box = new QCheckBox(this);
auto* checkLayout = new QHBoxLayout(this);
checkLayout->addWidget(m_box);
checkLayout->setAlignment(Qt::AlignCenter);
checkLayout->setContentsMargins(0, 0, 0, 0);
setCursor(Qt::PointingHandCursor);
}
QCheckBox* checkBox() const { return m_box; }
protected:
// Only reached when the click missed the checkbox's own hit area (a
// click on the box is handled by the box itself), so no double toggle.
void mouseReleaseEvent(QMouseEvent* event) override
{
if (event->button() == Qt::LeftButton && rect().contains(event->pos())) {
m_box->toggle();
}
QWidget::mouseReleaseEvent(event);
}
private:
QCheckBox* m_box = nullptr;
};
QWidget* centeredCheckBox()
{
return new CheckCell();
}
} // namespace
CreateTableDialog::CreateTableDialog(QWidget* parent)
: QDialog(parent)
, m_editing(false)
{
init();
addColumnRow(); // start with one empty row so the dialog isn't blank
}
CreateTableDialog::CreateTableDialog(const Table& existing, QWidget* parent)
: QDialog(parent)
, m_editing(true)
, m_originalTableName(existing.name)
{
init();
m_nameEdit->setText(existing.name);
for (const auto& col : existing.columns) {
addColumnRow(&col);
}
}
void CreateTableDialog::init()
{
setWindowTitle(m_editing ? "Edit Table" : "New Table");
resize(480, 360);
auto* layout = new QVBoxLayout(this);
auto* nameRow = new QHBoxLayout();
nameRow->addWidget(new QLabel("Table name:", this));
m_nameEdit = new QLineEdit(this);
m_nameEdit->setValidator(new QRegularExpressionValidator(kIdentifierPattern, m_nameEdit));
nameRow->addWidget(m_nameEdit, 1);
layout->addLayout(nameRow);
if (m_editing) {
auto* hint = new QLabel(
"You can rename the table and its columns, add columns and remove columns. SQLite cannot "
"change the type, PRIMARY KEY or NOT NULL of an existing column in place, so those are locked.",
this);
hint->setWordWrap(true);
layout->addWidget(hint);
}
m_columnsTable = new QTableWidget(0, ColumnCount, this);
m_columnsTable->setHorizontalHeaderLabels({"Column", "Type", "PK", "NOT NULL"});
m_columnsTable->horizontalHeader()->setSectionResizeMode(NameColumn, QHeaderView::Stretch);
m_columnsTable->horizontalHeader()->setSectionResizeMode(TypeColumn, QHeaderView::ResizeToContents);
m_columnsTable->horizontalHeader()->setSectionResizeMode(PrimaryKeyColumn, QHeaderView::ResizeToContents);
m_columnsTable->horizontalHeader()->setSectionResizeMode(NotNullColumn, QHeaderView::ResizeToContents);
m_columnsTable->verticalHeader()->setVisible(false);
// A single click on a name cell selects it AND opens it for typing,
// so there's no separate "double-click to edit" step to discover.
m_columnsTable->setEditTriggers(QAbstractItemView::AllEditTriggers);
m_columnsTable->setSelectionMode(QAbstractItemView::SingleSelection);
// Explicit colours for the selected cell and the in-cell editor. Without
// them the selection used a default blue that clashed with the theme,
// and the editor was a bare caret on the same colour as the background,
// so it was hard to tell which cell you were typing into.
m_columnsTable->setStyleSheet(
"QTableWidget::item:selected { background-color: #0C447C; color: #E6F1FB; }"
"QTableWidget QLineEdit {"
" background-color: #201F22; color: #F1EFE8;"
" border: 1px solid #378ADD; padding: 0px 4px;"
"}");
layout->addWidget(m_columnsTable, 1);
auto* rowButtons = new QHBoxLayout();
auto* addRowBtn = new QPushButton("+ Add column", this);
auto* removeRowBtn = new QPushButton("- Remove column", this);
rowButtons->addWidget(addRowBtn);
rowButtons->addWidget(removeRowBtn);
rowButtons->addStretch();
layout->addLayout(rowButtons);
auto* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
buttons->button(QDialogButtonBox::Ok)->setText(m_editing ? "Save" : "Create");
layout->addWidget(buttons);
// "+ Add column" also drops the cursor into the new row's name cell.
connect(addRowBtn, &QPushButton::clicked, this, [this]() {
addColumnRow();
const int row = m_columnsTable->rowCount() - 1;
m_columnsTable->setCurrentCell(row, NameColumn);
m_columnsTable->editItem(m_columnsTable->item(row, NameColumn));
});
connect(removeRowBtn, &QPushButton::clicked, this, &CreateTableDialog::removeSelectedRow);
connect(buttons, &QDialogButtonBox::accepted, this, &CreateTableDialog::accept);
connect(buttons, &QDialogButtonBox::rejected, this, &CreateTableDialog::reject);
}
void CreateTableDialog::addColumnRow(const Column* existing)
{
const int row = m_columnsTable->rowCount();
m_columnsTable->insertRow(row);
auto* nameItem = new QTableWidgetItem(existing ? existing->name : QString());
if (existing) {
// Remembered so MainWindow can tell a rename from a drop + add.
nameItem->setData(Qt::UserRole, existing->name);
}
m_columnsTable->setItem(row, NameColumn, nameItem);
auto* typeCombo = new QComboBox();
typeCombo->addItems(kColumnTypes);
if (existing) {
// Existing columns can have any declared type (VARCHAR(50), empty, ...).
int typeIndex = typeCombo->findText(existing->type, Qt::MatchFixedString); // case-insensitive
if (typeIndex < 0) {
typeCombo->addItem(existing->type);
typeIndex = typeCombo->count() - 1;
}
typeCombo->setCurrentIndex(typeIndex);
typeCombo->setEnabled(false);
}
m_columnsTable->setCellWidget(row, TypeColumn, typeCombo);
QWidget* pkCell = centeredCheckBox();
QWidget* notNullCell = centeredCheckBox();
if (existing) {
pkCell->findChild<QCheckBox*>()->setChecked(existing->isPrimaryKey);
notNullCell->findChild<QCheckBox*>()->setChecked(existing->isNotNull);
}
if (m_editing) {
// Disabling the cell widget also stops its click-anywhere-to-toggle.
pkCell->setEnabled(false);
notNullCell->setEnabled(false);
}
m_columnsTable->setCellWidget(row, PrimaryKeyColumn, pkCell);
m_columnsTable->setCellWidget(row, NotNullColumn, notNullCell);
}
void CreateTableDialog::removeSelectedRow()
{
const int row = m_columnsTable->currentRow();
if (row >= 0) {
m_columnsTable->removeRow(row);
}
}
QString CreateTableDialog::tableName() const
{
return m_nameEdit->text().trimmed();
}
QVector<CreateTableDialog::ColumnDef> CreateTableDialog::columns() const
{
QVector<ColumnDef> result;
for (int row = 0; row < m_columnsTable->rowCount(); ++row) {
ColumnDef col;
col.name = m_columnsTable->item(row, NameColumn)->text().trimmed();
col.originalName = m_columnsTable->item(row, NameColumn)->data(Qt::UserRole).toString();
col.type = static_cast<QComboBox*>(m_columnsTable->cellWidget(row, TypeColumn))->currentText();
col.primaryKey = m_columnsTable->cellWidget(row, PrimaryKeyColumn)->findChild<QCheckBox*>()->isChecked();
col.notNull = m_columnsTable->cellWidget(row, NotNullColumn)->findChild<QCheckBox*>()->isChecked();
result.push_back(col);
}
return result;
}
void CreateTableDialog::accept()
{
// A name cell that is still being edited hasn't written its text to the
// item yet. Taking focus away from the editor commits it, so the last
// typed name is never lost when "Create" is pressed.
m_columnsTable->setFocus();
const QString title = windowTitle();
// In edit mode an untouched name is accepted as-is, even if it isn't a
// plain identifier (e.g. a column called "my col" created elsewhere).
const bool tableNameUntouched = m_editing && tableName() == m_originalTableName;
if (!tableNameUntouched && !kIdentifierPattern.match(tableName()).hasMatch()) {
QMessageBox::warning(this, title,
"Table name must start with a letter or underscore, and contain only "
"letters, numbers, and underscores.");
return;
}
const auto cols = columns();
if (cols.isEmpty()) {
QMessageBox::warning(this, title, "Add at least one column.");
return;
}
QSet<QString> seenNames;
for (const auto& col : cols) {
const bool untouched = m_editing && !col.originalName.isEmpty() && col.name == col.originalName;
if (!untouched && !kIdentifierPattern.match(col.name).hasMatch()) {
QMessageBox::warning(this, title,
"Every column name must start with a letter or underscore, and "
"contain only letters, numbers, and underscores.");
return;
}
const QString key = col.name.toLower();
if (seenNames.contains(key)) {
QMessageBox::warning(this, title,
"Column names must be unique: \"" + col.name + "\" is repeated.");
return;
}
seenNames.insert(key);
}
QDialog::accept();
}