BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
Path.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Util/Path.cpp
6 //! @brief Implements Helpers functions
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/Path.h"
16 #include "BAVersion.h"
17 #include <QDateTime>
18 #include <QDir>
19 #include <QFileInfo>
20 #include <QMap>
21 #include <QModelIndex>
22 #include <QStandardPaths>
23 #include <QVector>
24 
25 namespace {
26 
27 QMap<QString, QString> initializeCharacterMap()
28 {
29  QMap<QString, QString> result;
30  result["\\"] = "_backslash_";
31  result["/"] = "_slash_";
32  result["\""] = "_quote_";
33  result["<"] = "_lessthan_";
34  result[">"] = "_greaterthan_";
35  result["|"] = "_pipe_";
36  result["?"] = "_questionmark_";
37  return result;
38 }
39 
40 const QMap<QString, QString> invalidCharacterMap = initializeCharacterMap();
41 
42 } // namespace
43 
44 
45 QString GUI::Util::Path::withTildeHomePath(const QString& path)
46 {
47 #ifdef Q_OS_WIN
48  return path;
49 #endif
50 
51  static const QString homePath = QDir::homePath();
52 
53  QFileInfo fi(QDir::cleanPath(path));
54  QString outPath = fi.absoluteFilePath();
55  if (outPath.startsWith(homePath))
56  return QLatin1Char('~') + outPath.mid(homePath.size());
57  return path;
58 }
59 
61 {
62  return QString::fromStdString(BornAgain::GetVersionNumber());
63 }
64 
65 //! Returns valid file name to be saved on disk. This name is constructed from proposed_name
66 //! by replacing all special characters with text representation
67 //! \ backslash
68 //! / slash
69 //! " quote
70 //! < lessthan
71 //! > greaterthan
72 //! | pipe
73 //! ? questionmark
74 QString GUI::Util::Path::getValidFileName(const QString& proposed_name)
75 {
76  QString result = proposed_name;
77  for (auto it = invalidCharacterMap.begin(); it != invalidCharacterMap.end(); ++it)
78  result.replace(it.key(), it.value());
79  return result;
80 }
81 
82 //! parses version string into 3 numbers, returns true in the case of success
83 bool GUI::Util::Path::parseVersion(const QString& version, int& major_num, int& minor_num,
84  int& patch_num)
85 {
86  major_num = minor_num = patch_num = 0;
87  bool success(true);
88  QStringList nums = version.split(".");
89  if (nums.size() != 3)
90  return false;
91 
92  bool ok(false);
93  major_num = nums.at(0).toInt(&ok);
94  success &= ok;
95  minor_num = nums.at(1).toInt(&ok);
96  success &= ok;
97  patch_num = nums.at(2).toInt(&ok);
98  success &= ok;
99 
100  return success;
101 }
102 
103 int GUI::Util::Path::versionCode(const QString& version)
104 {
105  int result(-1);
106 
107  int ba_major(0), ba_minor(0), ba_patch(0);
108  if (!parseVersion(version, ba_major, ba_minor, ba_patch))
109  return result;
110 
111  result = ba_major * 10000 + ba_minor * 100 + ba_patch;
112 
113  return result;
114 }
115 
116 //! Returns true if current BornAgain version match minimal required version
117 bool GUI::Util::Path::isVersionMatchMinimal(const QString& version, const QString& minimal_version)
118 {
119  return versionCode(version) >= versionCode(minimal_version);
120 }
121 
122 //! Returns file directory from the full file path
123 QString GUI::Util::Path::fileDir(const QString& fileName)
124 {
125  QFileInfo info(fileName);
126  if (info.exists())
127  return info.dir().path();
128  return "";
129 }
130 
131 //! Returns base name of file.
132 
133 QString GUI::Util::Path::baseName(const QString& fileName)
134 {
135  QFileInfo info(fileName);
136  return info.baseName();
137 }
138 
140 {
141  return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
142 }
143 
144 QString GUI::Util::Path::getPathFromIndex(const QModelIndex& index)
145 {
146  if (!index.isValid())
147  return "";
148 
149  QStringList namePath;
150  QModelIndex cur = index;
151  while (cur.isValid()) {
152  namePath << cur.data().toString();
153  cur = cur.parent();
154  }
155  std::reverse(namePath.begin(), namePath.end());
156  return namePath.join("/");
157 }
Defines class Helpers functions.
QString appDataFolder()
The folder where persistent application data shall be stored. Under Windows this is the AppData/Roami...
Definition: Path.cpp:139
int versionCode(const QString &version)
Definition: Path.cpp:103
QString baseName(const QString &fileName)
Returns base name of file.
Definition: Path.cpp:133
QString fileDir(const QString &fileName)
Returns file directory from the full file path.
Definition: Path.cpp:123
QString getValidFileName(const QString &proposed_name)
Returns valid file name to be saved on disk. This name is constructed from proposed_name by replacing...
Definition: Path.cpp:74
bool parseVersion(const QString &version, int &major_num, int &minor_num, int &patch_num)
parses version string into 3 numbers, returns true in the case of success
Definition: Path.cpp:83
bool isVersionMatchMinimal(const QString &version, const QString &minimal_version)
Returns true if current BornAgain version match minimal required version.
Definition: Path.cpp:117
QString withTildeHomePath(const QString &path)
Definition: Path.cpp:45
QString getBornAgainVersionString()
Definition: Path.cpp:60
QString getPathFromIndex(const QModelIndex &index)
Definition: Path.cpp:144