##// END OF EJS Templates
Creates dialog to rename a variable
Alexandre Leroux -
r634:db1f763a3aa8
parent child
Show More
@@ -0,0 +1,31
1 #ifndef SCIQLOP_RENAMEVARIABLEDIALOG_H
2 #define SCIQLOP_RENAMEVARIABLEDIALOG_H
3
4 #include <QDialog>
5
6 namespace Ui {
7 class RenameVariableDialog;
8 } // Ui
9
10 /**
11 * @brief The RenameVariableDialog class represents the dialog to rename a variable
12 */
13 class RenameVariableDialog : public QDialog {
14 public:
15 explicit RenameVariableDialog(const QString &defaultName,
16 const QVector<QString> &forbiddenNames,
17 QWidget *parent = nullptr);
18 virtual ~RenameVariableDialog() noexcept;
19
20 QString name() const noexcept;
21
22 public slots:
23 void accept() override;
24
25 private:
26 Ui::RenameVariableDialog *ui;
27 QString m_DefaultName;
28 QVector<QString> m_ForbiddenNames;
29 };
30
31 #endif // SCIQLOP_RENAMEVARIABLEDIALOG_H
@@ -0,0 +1,63
1 #include "Variable/RenameVariableDialog.h"
2
3 #include <ui_RenameVariableDialog.h>
4
5 RenameVariableDialog::RenameVariableDialog(const QString &defaultName,
6 const QVector<QString> &forbiddenNames, QWidget *parent)
7 : QDialog{parent},
8 ui{new Ui::RenameVariableDialog},
9 m_DefaultName{defaultName},
10 m_ForbiddenNames{forbiddenNames}
11 {
12 ui->setupUi(this);
13
14 connect(ui->nameLineEdit, &QLineEdit::textChanged, [this]() { ui->errorLabel->hide(); });
15
16 ui->nameLineEdit->setText(defaultName);
17 ui->nameLineEdit->selectAll();
18 ui->nameLineEdit->setFocus();
19 }
20
21 RenameVariableDialog::~RenameVariableDialog() noexcept
22 {
23 delete ui;
24 }
25
26 QString RenameVariableDialog::name() const noexcept
27 {
28 return ui->nameLineEdit->text();
29 }
30
31 void RenameVariableDialog::accept()
32 {
33 auto invalidateInput = [this](const auto &error) {
34 ui->nameLineEdit->selectAll();
35 ui->nameLineEdit->setFocus();
36 ui->errorLabel->setText(error);
37 ui->errorLabel->show();
38 };
39
40 // Empty name
41 auto name = ui->nameLineEdit->text();
42 if (name.isEmpty()) {
43 invalidateInput(tr("A variable name must be specified"));
44 return;
45 }
46
47 // Same name when opening dialog
48 if (name.compare(m_DefaultName, Qt::CaseInsensitive) == 0) {
49 reject();
50 return;
51 }
52
53 // Forbidden name
54 auto isForbidden
55 = [&name](const auto &it) { return name.compare(it, Qt::CaseInsensitive) == 0; };
56 if (std::any_of(m_ForbiddenNames.cbegin(), m_ForbiddenNames.cend(), isForbidden)) {
57 invalidateInput(tr("'%1' is already used").arg(name));
58 return;
59 }
60
61 // Valid name
62 QDialog::accept();
63 }
@@ -0,0 +1,90
1 <?xml version="1.0" encoding="UTF-8"?>
2 <ui version="4.0">
3 <class>RenameVariableDialog</class>
4 <widget class="QDialog" name="RenameVariableDialog">
5 <property name="geometry">
6 <rect>
7 <x>0</x>
8 <y>0</y>
9 <width>379</width>
10 <height>109</height>
11 </rect>
12 </property>
13 <property name="windowTitle">
14 <string>Rename variable</string>
15 </property>
16 <layout class="QGridLayout" name="gridLayout">
17 <item row="2" column="0">
18 <spacer name="verticalSpacer">
19 <property name="orientation">
20 <enum>Qt::Vertical</enum>
21 </property>
22 <property name="sizeHint" stdset="0">
23 <size>
24 <width>20</width>
25 <height>40</height>
26 </size>
27 </property>
28 </spacer>
29 </item>
30 <item row="3" column="0">
31 <widget class="QDialogButtonBox" name="buttonBox">
32 <property name="orientation">
33 <enum>Qt::Horizontal</enum>
34 </property>
35 <property name="standardButtons">
36 <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
37 </property>
38 </widget>
39 </item>
40 <item row="0" column="0">
41 <widget class="QLineEdit" name="nameLineEdit"/>
42 </item>
43 <item row="1" column="0">
44 <widget class="QLabel" name="errorLabel">
45 <property name="styleSheet">
46 <string notr="true">color: rgb(255, 0, 0);</string>
47 </property>
48 <property name="text">
49 <string>Error label</string>
50 </property>
51 </widget>
52 </item>
53 </layout>
54 </widget>
55 <resources/>
56 <connections>
57 <connection>
58 <sender>buttonBox</sender>
59 <signal>accepted()</signal>
60 <receiver>RenameVariableDialog</receiver>
61 <slot>accept()</slot>
62 <hints>
63 <hint type="sourcelabel">
64 <x>248</x>
65 <y>254</y>
66 </hint>
67 <hint type="destinationlabel">
68 <x>157</x>
69 <y>274</y>
70 </hint>
71 </hints>
72 </connection>
73 <connection>
74 <sender>buttonBox</sender>
75 <signal>rejected()</signal>
76 <receiver>RenameVariableDialog</receiver>
77 <slot>reject()</slot>
78 <hints>
79 <hint type="sourcelabel">
80 <x>316</x>
81 <y>260</y>
82 </hint>
83 <hint type="destinationlabel">
84 <x>286</x>
85 <y>274</y>
86 </hint>
87 </hints>
88 </connection>
89 </connections>
90 </ui>
General Comments 0
You need to be logged in to leave comments. Login now