##// END OF EJS Templates
Merge branch 'feature/RenameVariable' into develop
Alexandre Leroux -
r687:6af26c25c341 merge
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>
@@ -29,6 +29,7 public:
29 29 const QVariantHash &metadata = {});
30 30
31 31 QString name() const noexcept;
32 void setName(const QString &name) noexcept;
32 33 SqpRange range() const noexcept;
33 34 void setRange(const SqpRange &range) noexcept;
34 35 SqpRange cacheRange() const noexcept;
@@ -45,6 +45,7 public:
45 45
46 46
47 47 std::shared_ptr<Variable> variable(int index) const;
48 std::vector<std::shared_ptr<Variable> > variables() const;
48 49
49 50 void setDataProgress(std::shared_ptr<Variable> variable, double progress);
50 51
@@ -75,6 +75,13 QString Variable::name() const noexcept
75 75 return name;
76 76 }
77 77
78 void Variable::setName(const QString &name) noexcept
79 {
80 impl->lockWrite();
81 impl->m_Name = name;
82 impl->unlock();
83 }
84
78 85 SqpRange Variable::range() const noexcept
79 86 {
80 87 impl->lockRead();
@@ -114,6 +114,11 std::shared_ptr<Variable> VariableModel::variable(int index) const
114 114 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
115 115 }
116 116
117 std::vector<std::shared_ptr<Variable> > VariableModel::variables() const
118 {
119 return impl->m_Variables;
120 }
121
117 122 void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress)
118 123 {
119 124 if (progress > 0.0) {
@@ -1,3 +1,5
1 #include <Variable/RenameVariableDialog.h>
2 #include <Variable/Variable.h>
1 3 #include <Variable/VariableController.h>
2 4 #include <Variable/VariableInspectorWidget.h>
3 5 #include <Variable/VariableMenuHeaderWidget.h>
@@ -172,12 +174,33 void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept
172 174
173 175 // Adds menu-specific actions
174 176 if (!selectedVariables.isEmpty()) {
177 tableMenu.addSeparator();
178
179 // 'Rename' action (only if one variable selected)
180 if (selectedVariables.size() == 1) {
181 auto selectedVariable = selectedVariables.front();
182
183 auto renameFun = [&selectedVariable, &model, this]() {
184 // Generates forbidden names (names associated to existing variables)
185 auto allVariables = model->variables();
186 auto forbiddenNames = QVector<QString>(allVariables.size());
187 std::transform(allVariables.cbegin(), allVariables.cend(), forbiddenNames.begin(),
188 [](const auto &variable) { return variable->name(); });
189
190 RenameVariableDialog dialog{selectedVariable->name(), forbiddenNames, this};
191 if (dialog.exec() == QDialog::Accepted) {
192 selectedVariable->setName(dialog.name());
193 }
194 };
195
196 tableMenu.addAction(tr("Rename..."), renameFun);
197 }
198
175 199 // 'Delete' action
176 200 auto deleteFun = [&selectedVariables]() {
177 201 sqpApp->variableController().deleteVariables(selectedVariables);
178 202 };
179 203
180 tableMenu.addSeparator();
181 204 tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun);
182 205 }
183 206
General Comments 0
You need to be logged in to leave comments. Login now