@@ -1,63 +1,63 | |||
|
1 | 1 | #include "Variable/RenameVariableDialog.h" |
|
2 | 2 | |
|
3 | 3 | #include <ui_RenameVariableDialog.h> |
|
4 | 4 | |
|
5 | 5 | RenameVariableDialog::RenameVariableDialog(const QString &defaultName, |
|
6 | 6 | const QVector<QString> &forbiddenNames, QWidget *parent) |
|
7 | 7 | : QDialog{parent}, |
|
8 | 8 | ui{new Ui::RenameVariableDialog}, |
|
9 | 9 | m_DefaultName{defaultName}, |
|
10 | 10 | m_ForbiddenNames{forbiddenNames} |
|
11 | 11 | { |
|
12 | 12 | ui->setupUi(this); |
|
13 | 13 | |
|
14 | 14 | connect(ui->nameLineEdit, &QLineEdit::textChanged, [this]() { ui->errorLabel->hide(); }); |
|
15 | 15 | |
|
16 | 16 | ui->nameLineEdit->setText(defaultName); |
|
17 | 17 | ui->nameLineEdit->selectAll(); |
|
18 | 18 | ui->nameLineEdit->setFocus(); |
|
19 | 19 | } |
|
20 | 20 | |
|
21 | 21 | RenameVariableDialog::~RenameVariableDialog() noexcept |
|
22 | 22 | { |
|
23 | 23 | delete ui; |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | QString RenameVariableDialog::name() const noexcept |
|
27 | 27 | { |
|
28 | return ui->nameLineEdit->text(); | |
|
28 | return ui->nameLineEdit->text().trimmed(); | |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | void RenameVariableDialog::accept() |
|
32 | 32 | { |
|
33 | 33 | auto invalidateInput = [this](const auto &error) { |
|
34 | 34 | ui->nameLineEdit->selectAll(); |
|
35 | 35 | ui->nameLineEdit->setFocus(); |
|
36 | 36 | ui->errorLabel->setText(error); |
|
37 | 37 | ui->errorLabel->show(); |
|
38 | 38 | }; |
|
39 | 39 | |
|
40 | 40 | // Empty name |
|
41 |
auto name = |
|
|
41 | auto name = this->name(); | |
|
42 | 42 | if (name.isEmpty()) { |
|
43 | 43 | invalidateInput(tr("A variable name must be specified")); |
|
44 | 44 | return; |
|
45 | 45 | } |
|
46 | 46 | |
|
47 | 47 | // Same name when opening dialog |
|
48 | 48 | if (name.compare(m_DefaultName, Qt::CaseInsensitive) == 0) { |
|
49 | 49 | reject(); |
|
50 | 50 | return; |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | // Forbidden name |
|
54 | 54 | auto isForbidden |
|
55 | 55 | = [&name](const auto &it) { return name.compare(it, Qt::CaseInsensitive) == 0; }; |
|
56 | 56 | if (std::any_of(m_ForbiddenNames.cbegin(), m_ForbiddenNames.cend(), isForbidden)) { |
|
57 | 57 | invalidateInput(tr("'%1' is already used").arg(name)); |
|
58 | 58 | return; |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | // Valid name |
|
62 | 62 | QDialog::accept(); |
|
63 | 63 | } |
General Comments 0
You need to be logged in to leave comments.
Login now