Skip to content
Snippets Groups Projects
Commit e22f9417 authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

ensure that there are no duplicate names in lists

parent 0e18dd08
No related branches found
No related tags found
1 merge request!2404ensure that there are no duplicate names in lists; disable cp/rm actions for empty lists
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/Model/Type/NamedItem.cpp
//! @brief Implements class NamedItem.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2021
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "GUI/Model/Type/NamedItem.h"
#include <QRegularExpression>
#include <QRegularExpressionMatch>
namespace {
QStringList splitName(const QString& s) {
QRegularExpression pattern("(.*)_(\\d+)");
QRegularExpressionMatch match = pattern.match(s);
if (match.hasMatch()) {
QStringList groups;
for (int i = 1; i < match.lastCapturedIndex() + 1; ++i)
groups << match.captured(i);
return groups;
}
return {}; // s does not contain '_\d+'
}
} // namespace
void NamedItem::renumber(const QStringList& extant_names)
{
// Item name consists of a stem and an optional number
QStringList ns = splitName(name());
QString stem = ns.isEmpty() ? name() : ns[0];
// Determine highest number for given stem in extant_items
int imax = 0;
for (QString tname : extant_names) {
QStringList ts = splitName(tname);
if (ts.isEmpty()) {
if (tname == stem)
imax = std::max(imax, 1);
} else {
if (ts[0] == stem)
imax = std::max(imax, ts[1].toInt());
}
}
if (imax != 0)
setName(stem + "_" + QString::number(imax + 1));
if (name().contains(' '))
setName(name().replace(' ', '_'));
}
......@@ -3,7 +3,7 @@
// BornAgain: simulate and fit reflection and scattering
//
//! @file GUI/Model/Type/NamedItem.h
//! @brief Defines and implements class NamedItem.
//! @brief Defines class NamedItem.
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
......@@ -16,6 +16,7 @@
#define BORNAGAIN_GUI_MODEL_TYPE_NAMEDITEM_H
#include <QString>
#include <vector>
//! Base class of items that have a name and a description.
......@@ -33,6 +34,10 @@ public:
QString description() const { return m_description; }
void setDescription(const QString& description) { m_description = description; }
//! Changes name of item to avoid duplication of any name from extant items.
//! Also replaces blank by underscore characters.
void renumber(const QStringList& extant_names);
private:
QString m_name;
QString m_description;
......
......@@ -74,6 +74,7 @@ public:
void add_item(T* t)
{
m_qmodel->beginInsertRows({}, size(), size());
t->renumber(itemNames());
m_vec.push_back(t);
m_idx = m_vec.size() - 1;
m_qmodel->endInsertRows();
......@@ -82,8 +83,10 @@ public:
void add_items(std::vector<T*> v)
{
m_qmodel->beginInsertRows({}, size(), size());
for (T* t : v)
for (T* t : v) {
t->renumber(itemNames());
m_vec.push_back(t);
}
m_idx = m_vec.size() - 1;
m_qmodel->endInsertRows();
emit AbstractSetModel::setChanged();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment