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