BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
ProjectUtils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/mainwindow/ProjectUtils.cpp
6 //! @brief Implements ProjectUtils 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 
21 #include <QDateTime>
22 #include <QDebug>
23 #include <QDir>
24 #include <QFileInfo>
25 
26 QString ProjectUtils::projectName(const QString& projectFileName)
27 {
28  QFileInfo info(projectFileName);
29  return info.baseName();
30 }
31 
32 QString ProjectUtils::projectDir(const QString& projectFileName)
33 {
34  QFileInfo info(projectFileName);
35  return info.path();
36 }
37 
39 {
40  return "autosave";
41 }
42 
43 //! From '/projects/Untitled2/Untitled2.pro' returns '/projects/Untitled2/autosave'.
44 
45 QString ProjectUtils::autosaveDir(const QString& projectFileName)
46 {
47  return ProjectUtils::projectDir(projectFileName) + "/" + autosaveSubdir();
48 }
49 
50 //! From '/projects/Untitled2/Untitled2.pro' returns '/projects/Untitled2/autosave/Untitled2.pro'.
51 
52 QString ProjectUtils::autosaveName(const QString& projectFileName)
53 {
54  return ProjectUtils::autosaveDir(projectFileName) + "/"
56 }
57 
58 bool ProjectUtils::exists(const QString& fileName)
59 {
60  QFileInfo info(fileName);
61  return info.exists();
62 }
63 
64 bool ProjectUtils::hasAutosavedData(const QString& projectFileName)
65 {
66  return exists(projectFileName) && exists(autosaveName(projectFileName));
67 }
68 
69 QString ProjectUtils::lastModified(const QString& fileName)
70 {
71  QFileInfo info(fileName);
72  return info.lastModified().toString("hh:mm:ss, MMMM d, yyyy");
73 }
74 
75 QStringList ProjectUtils::nonXMLDataInDir(const QString& dirname)
76 {
77  QDir dir(dirname);
78 
79  if (!dir.exists())
80  throw GUIHelpers::Error("ProjectUtils::nonXMLDataInDir() -> Error. Non existing "
81  "directory '"
82  + dirname + "'.");
83 
84  return dir.entryList(ItemFileNameUtils::nonXMLFileNameFilters());
85 }
86 
87 bool ProjectUtils::removeRecursively(const QString& dirname)
88 {
89  QDir dir(dirname);
90 
91  if (!dir.exists())
92  throw GUIHelpers::Error("ProjectUtils::removeRecursively() -> Error. Non existing "
93  "directory '"
94  + dirname + "'.");
95 
96  return dir.removeRecursively();
97 }
98 
99 bool ProjectUtils::removeFile(const QString& dirname, const QString& filename)
100 {
101  QString name = dirname + "/" + filename;
102  QFile fin(name);
103 
104  if (!fin.exists())
105  throw GUIHelpers::Error("ProjectUtils::removeFile() -> Error. Non existing "
106  "file '"
107  + name + "'.");
108 
109  return fin.remove();
110 }
111 
112 bool ProjectUtils::removeFiles(const QString& dirname, const QStringList& filenames)
113 {
114  bool success(true);
115 
116  for (auto& name : filenames)
117  success &= removeFile(dirname, name);
118 
119  return success;
120 }
121 
122 QStringList ProjectUtils::substract(const QStringList& lhs, const QStringList& rhs)
123 {
124 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
125  auto lhs_set = QSet<QString>{lhs.begin(), lhs.end()};
126  auto rhs_set = QSet<QString>{rhs.begin(), rhs.end()};
127  QSet<QString> diff = lhs_set.subtract(rhs_set);
128  return diff.values();
129 #else
130  QSet<QString> diff = lhs.toSet().subtract(rhs.toSet());
131  return diff.toList();
132 #endif
133 }
134 
135 QString ProjectUtils::readTextFile(const QString& fileName)
136 {
137  QFile file(fileName);
138  if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
139  throw GUIHelpers::Error("ProjectUtils::readTextFile -> Error. Can't open the file '"
140  + fileName + "' for reading.");
141  QTextStream in(&file);
142  return in.readAll();
143 }
144 
146 {
148 }
Defines class AppSvc.
Defines class GUIHelpers functions.
Defines class ProjectManager.
Defines auxiliary functions in ItemFileNameUtils namespace.
Defines ProjectUtils namespace.
static ProjectManager * projectManager()
Definition: AppSvc.cpp:18
static QString projectFileExtension()
QString userExportDir() const
Returns directory name suitable for saving plots.
std::string filename(const std::string &path)
Returns path without directory part ("Foo/Bar/Doz.int.gz" -> "Doz.int.gz")
QStringList nonXMLFileNameFilters()
Returns list of fileName filters related to nonXML data stored by JobModel and RealDataModel.
QStringList substract(const QStringList &lhs, const QStringList &rhs)
Returns list of string from lhs, which are not in rhs.
QStringList nonXMLDataInDir(const QString &dirname)
Returns list of files on disk representing nonXML data.
bool exists(const QString &fileName)
Returns true if file exists.
bool removeRecursively(const QString &dirname)
Removes recursively directory with given name.
bool removeFiles(const QString &dirname, const QStringList &filenames)
QString lastModified(const QString &fileName)
Returns a string representing modification time of given file.
QString autosaveName(const QString &projectFileName)
Returns name of project for autoSave from given project file name.
bool removeFile(const QString &dirname, const QString &filename)
Remove file from given directory.
QString projectName(const QString &projectFileName)
Returns project name deduced from project file name.
bool hasAutosavedData(const QString &projectFileName)
Returns true if project with given projectFileName contains autosaved data.
QString projectDir(const QString &projectFileName)
Returns project directory deduced from project file name.
QString autosaveDir(const QString &projectFileName)
Returns name of autosave directory for project with given project file name.
QString userExportDir()
Returns directory which user is normally using to export files.
QString readTextFile(const QString &fileName)
Returns multi-lione string representing content of text file.
QString autosaveSubdir()
Returns fixed name for autosave sub-directory.
QString const & name(EShape k)
Definition: particles.cpp:21
Defines class ProjectDocument.