BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
String.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Util/String.cpp
6 //! @brief Implements functions from Utils namespace
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
15 #include "GUI/Util/String.h"
16 #include <QDir>
17 
18 QStringList GUI::Util::String::fromStdStrings(const std::vector<std::string>& container)
19 {
20  QStringList result;
21  for (const std::string& str : container)
22  result.append(QString::fromStdString(str));
23  return result;
24 }
25 
26 QString GUI::Util::String::suggestName(const QStringList& existingNames, const QString& name)
27 {
28  int newNumber = -1;
29  QRegExp regexp(R"(\‍((\d+)\))");
30 
31  QString baseName = name;
32  if (regexp.indexIn(baseName) >= 0) {
33  newNumber = regexp.cap(1).toInt();
34  baseName.replace(regexp.cap(0), "");
35  baseName = baseName.trimmed();
36  }
37 
38  for (const auto& name : existingNames) {
39  if (name == baseName)
40  newNumber = std::max(newNumber, 2);
41  else if (regexp.indexIn(name) >= 0)
42  newNumber = std::max(newNumber, regexp.cap(1).toInt() + 1);
43  }
44 
45  if (newNumber == -1)
46  return baseName;
47 
48  return QString("%1 (%2)").arg(baseName).arg(newNumber);
49 }
Defines functions from namespace GUI::Util::String.
QString const & name(EShape k)
Definition: particles.cpp:20
QString baseName(const QString &fileName)
Returns base name of file.
Definition: Path.cpp:133
QStringList fromStdStrings(const std::vector< std::string > &container)
Definition: String.cpp:18
QString suggestName(const QStringList &existingNames, const QString &name)
Returns a name suggestion based on the given name.
Definition: String.cpp:26