-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilehandlers.cpp
More file actions
142 lines (118 loc) · 3.48 KB
/
filehandlers.cpp
File metadata and controls
142 lines (118 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "filehandlers.h"
#include "moldocument.h"
#include <QString>
#include <QList>
#include <QFileInfo>
//#include <functional>
//#include <optional>
namespace FileHandlers {
struct FileTypeInfo
{
QString name;
QStringList extensions;
bool canOpen = false;
bool canSave = false;
// std::function<MolDocument(QString)> openFunction;
// std::function<bool(QString, MolDocument)> saveFunction;
};
const QList<FileTypeInfo> &getFileTypes()
{
static QList<FileTypeInfo> fileTypes;
if (!fileTypes.isEmpty())
return fileTypes;
FileTypeInfo cvProj;
cvProj.name = "ChemView Project";
cvProj.extensions = QStringList{"cvproj"};
cvProj.canOpen = true;
cvProj.canSave = true;
fileTypes.push_back(cvProj);
FileTypeInfo gaussianCube;
gaussianCube.name = "Gaussian Cube";
gaussianCube.extensions = QStringList{"cube"};
gaussianCube.canOpen = true;
fileTypes.push_back(gaussianCube);
FileTypeInfo molFile;
molFile.name = "MOL/SDF";
molFile.extensions = QStringList{"mol","sdf"};
molFile.canOpen = true;
molFile.canSave = true;
fileTypes.push_back(molFile);
FileTypeInfo nwchemOutput;
nwchemOutput.name = "NWChem Output";
nwchemOutput.extensions = QStringList{"out", "nwout", "log"};
nwchemOutput.canOpen = true;
fileTypes.push_back(nwchemOutput);
FileTypeInfo nwchemInput;
nwchemInput.name = "NWChem Input";
nwchemInput.extensions = QStringList{"nw"};
nwchemInput.canSave = true;
fileTypes.push_back(nwchemInput);
FileTypeInfo xyzFile;
xyzFile.name = "XYZ (Cartesian)";
xyzFile.extensions = QStringList{"xyz"};
xyzFile.canOpen = true;
xyzFile.canSave = true;
fileTypes.push_back(xyzFile);
return fileTypes;
}
bool canOpenPath(QString path)
{
QString suffix = QFileInfo(path).suffix();
for (const auto &filetype: getFileTypes())
{
if (filetype.canOpen && filetype.extensions.contains(suffix))
return true;
}
return false;
}
bool canSavePath(QString path)
{
QString suffix = QFileInfo(path).suffix();
for (const auto &filetype: getFileTypes())
{
if (filetype.canSave && filetype.extensions.contains(suffix))
return true;
}
return false;
}
const QString &getOpenFilters()
{
static QString openFilters;
if (openFilters.isEmpty())
{
QStringList filterList({"All files (*.*)"});
for (const auto &filetype: getFileTypes())
{
if (filetype.canOpen)
{
QStringList extensionList;
for (const auto &extension: filetype.extensions)
extensionList.append(QStringLiteral("*.") + extension);
filterList.append(filetype.name + " (" + extensionList.join(" ") + ")");
}
}
openFilters = filterList.join(";;");
}
return openFilters;
}
const QString &getSaveFilters()
{
static QString saveFilters;
if (saveFilters.isEmpty())
{
QStringList filterList;
for (const auto &filetype: getFileTypes())
{
if (filetype.canSave)
{
QStringList extensionList;
for (const auto &extension: filetype.extensions)
extensionList.append(QStringLiteral("*.") + extension);
filterList.append(filetype.name + " (" + extensionList.join(" ") + ")");
}
}
saveFilters = filterList.join(";;");
}
return saveFilters;
}
}