From db1f763a3aa88b19b9dc7cb37c37ff30ae478432 2017-09-01 07:10:56 From: Alexandre Leroux Date: 2017-09-01 07:10:56 Subject: [PATCH] Creates dialog to rename a variable --- diff --git a/gui/include/Variable/RenameVariableDialog.h b/gui/include/Variable/RenameVariableDialog.h new file mode 100644 index 0000000..e3f503c --- /dev/null +++ b/gui/include/Variable/RenameVariableDialog.h @@ -0,0 +1,31 @@ +#ifndef SCIQLOP_RENAMEVARIABLEDIALOG_H +#define SCIQLOP_RENAMEVARIABLEDIALOG_H + +#include + +namespace Ui { +class RenameVariableDialog; +} // Ui + +/** + * @brief The RenameVariableDialog class represents the dialog to rename a variable + */ +class RenameVariableDialog : public QDialog { +public: + explicit RenameVariableDialog(const QString &defaultName, + const QVector &forbiddenNames, + QWidget *parent = nullptr); + virtual ~RenameVariableDialog() noexcept; + + QString name() const noexcept; + +public slots: + void accept() override; + +private: + Ui::RenameVariableDialog *ui; + QString m_DefaultName; + QVector m_ForbiddenNames; +}; + +#endif // SCIQLOP_RENAMEVARIABLEDIALOG_H diff --git a/gui/src/Variable/RenameVariableDialog.cpp b/gui/src/Variable/RenameVariableDialog.cpp new file mode 100644 index 0000000..1af8af5 --- /dev/null +++ b/gui/src/Variable/RenameVariableDialog.cpp @@ -0,0 +1,63 @@ +#include "Variable/RenameVariableDialog.h" + +#include + +RenameVariableDialog::RenameVariableDialog(const QString &defaultName, + const QVector &forbiddenNames, QWidget *parent) + : QDialog{parent}, + ui{new Ui::RenameVariableDialog}, + m_DefaultName{defaultName}, + m_ForbiddenNames{forbiddenNames} +{ + ui->setupUi(this); + + connect(ui->nameLineEdit, &QLineEdit::textChanged, [this]() { ui->errorLabel->hide(); }); + + ui->nameLineEdit->setText(defaultName); + ui->nameLineEdit->selectAll(); + ui->nameLineEdit->setFocus(); +} + +RenameVariableDialog::~RenameVariableDialog() noexcept +{ + delete ui; +} + +QString RenameVariableDialog::name() const noexcept +{ + return ui->nameLineEdit->text(); +} + +void RenameVariableDialog::accept() +{ + auto invalidateInput = [this](const auto &error) { + ui->nameLineEdit->selectAll(); + ui->nameLineEdit->setFocus(); + ui->errorLabel->setText(error); + ui->errorLabel->show(); + }; + + // Empty name + auto name = ui->nameLineEdit->text(); + if (name.isEmpty()) { + invalidateInput(tr("A variable name must be specified")); + return; + } + + // Same name when opening dialog + if (name.compare(m_DefaultName, Qt::CaseInsensitive) == 0) { + reject(); + return; + } + + // Forbidden name + auto isForbidden + = [&name](const auto &it) { return name.compare(it, Qt::CaseInsensitive) == 0; }; + if (std::any_of(m_ForbiddenNames.cbegin(), m_ForbiddenNames.cend(), isForbidden)) { + invalidateInput(tr("'%1' is already used").arg(name)); + return; + } + + // Valid name + QDialog::accept(); +} diff --git a/gui/ui/Variable/RenameVariableDialog.ui b/gui/ui/Variable/RenameVariableDialog.ui new file mode 100644 index 0000000..987f5ba --- /dev/null +++ b/gui/ui/Variable/RenameVariableDialog.ui @@ -0,0 +1,90 @@ + + + RenameVariableDialog + + + + 0 + 0 + 379 + 109 + + + + Rename variable + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + + color: rgb(255, 0, 0); + + + Error label + + + + + + + + + buttonBox + accepted() + RenameVariableDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + RenameVariableDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + +