Mantid
Loading...
Searching...
No Matches
RenameParDialog.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8
9#include <algorithm>
10#include <utility>
11
12using namespace MantidQt::MantidWidgets;
13
20RenameParDialog::RenameParDialog(std::vector<std::string> old_params, const std::vector<std::string> &new_params,
21 QWidget *parent)
22 : QDialog(parent), m_old_params(std::move(old_params)), m_new_params(new_params) {
23 m_uiForm.setupUi(this);
24 QAbstractItemModel *model = m_uiForm.tableWidget->model();
25 int nparams(static_cast<int>(new_params.size()));
26 model->insertRows(0, nparams);
27 for (int row = 0; row < nparams; ++row) {
28 QString par = QString::fromStdString(new_params[row]);
29 model->setData(model->index(row, 0), par);
30 model->setData(model->index(row, 1), par);
31 }
32 connect(m_uiForm.btnRename, SIGNAL(clicked()), this, SLOT(accept()));
33 connect(m_uiForm.btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
34 connect(m_uiForm.rbAddIndex, SIGNAL(toggled(bool)), this, SLOT(uniqueIndexedNames(bool)));
35 connect(m_uiForm.rbDoNot, SIGNAL(toggled(bool)), this, SLOT(doNotRename(bool)));
36}
37
42bool RenameParDialog::isUnique(const QString &name) const {
43 auto it = std::find(m_old_params.begin(), m_old_params.end(), name.toStdString());
44 if (it != m_old_params.end())
45 return false;
46 QAbstractItemModel *model = m_uiForm.tableWidget->model();
47 for (int row = 0; row < m_uiForm.tableWidget->rowCount(); ++row) {
48 if (model->data(model->index(row, 1)).toString() == name) {
49 return false;
50 }
51 }
52 return true;
53}
54
61QString RenameParDialog::makeUniqueIndexedName(const QString &name) {
62 int index = 1;
63 QString base;
64 int i_ = name.indexOf('_');
65 if (i_ >= 0) {
66 QString old_index = name.mid(i_ + 1);
67 bool ok;
68 int n = old_index.toInt(&ok);
69 // e.g. name = a_3
70 if (ok) {
71 index = n + 1;
72 base = name.mid(0, i_);
73 }
74 // e.g. name = a_b
75 else {
76 base = name;
77 }
78 } else {
79 base = name + "_";
80 }
81 QString tst(base + QString::number(index));
82 while (!isUnique(tst)) {
83 ++index;
84 tst = base + QString::number(index);
85 }
86 return tst;
87}
88
93std::vector<std::string> RenameParDialog::setOutput() const {
94 std::vector<std::string> out;
95 QAbstractItemModel *model = m_uiForm.tableWidget->model();
96 for (int row = 0; row < m_uiForm.tableWidget->rowCount(); ++row) {
97 out.emplace_back(model->data(model->index(row, 1)).toString().toStdString());
98 }
99 return out;
100}
101
103 if (!ok)
104 return;
105 QAbstractItemModel *model = m_uiForm.tableWidget->model();
106 for (int row = 0; row < m_uiForm.tableWidget->rowCount(); ++row) {
107 QString name = model->data(model->index(row, 0)).toString();
108 model->setData(model->index(row, 1), makeUniqueIndexedName(name));
109 }
110}
111
116 if (!ok)
117 return;
118 QAbstractItemModel *model = m_uiForm.tableWidget->model();
119 for (int row = 0; row < m_uiForm.tableWidget->rowCount(); ++row) {
120 QString name = model->data(model->index(row, 0)).toString();
121 model->setData(model->index(row, 1), name);
122 }
123}
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
const std::vector< std::string > m_old_params
std::vector< std::string > setOutput() const
Output the new names to a vector.
RenameParDialog(std::vector< std::string > old_params, const std::vector< std::string > &new_params, QWidget *parent=nullptr)
there has to be a default constructor but you can call it with a pointer to the thing that will take ...
QString makeUniqueIndexedName(const QString &name)
Adds a suffix to the unput parameter name in the form: _n where n is a number.
Ui::RenameParDialog m_uiForm
User interface elements.
void doNotRename(bool)
Do not rename the parameters.
bool isUnique(const QString &name) const
Checks whether a name is unique.
STL namespace.