forked from hundlab/LongQt-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settingsIO.cpp
123 lines (108 loc) · 3.84 KB
/
settingsIO.cpp
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
#include "settingsIO.h"
#include "cellutils.h"
#include <QFile>
#include <QDebug>
#include <QDir>
SettingsIO* SettingsIO::__instance = 0;
SettingsIO* SettingsIO::getInstance() {
if(!__instance) {
__instance = new SettingsIO();
}
return __instance;
}
void SettingsIO::writeSettings(shared_ptr<Protocol> proto, QString filename) {
QFile ofile(filename);
string name;
if(!ofile.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Truncate)){
qCritical() << "SettingsIO: Error opening " << filename;
return;
}
QXmlStreamWriter xml(&ofile);
xml.setAutoFormatting(true);
xml.writeStartDocument();
xml.writeStartElement("file");
xml.writeTextElement("protocolType",proto->type());
proto->writepars(xml);
proto->measureMgr().writeMVarsFile(xml);
this->writedvars(proto, xml);
proto->pvars().writePvars(xml);
xml.writeEndElement();
}
bool SettingsIO::readProtoType(shared_ptr<Protocol>& proto, QXmlStreamReader& xml) {
if(CellUtils::readNext(xml, "protocolType")) {
xml.readNext();
QString type = xml.text().toString();
if(proto->type() != type) {
if(!allowProtoChange) {
qWarning("SettingsIO: Changing protocol type is disabled");
throw std::invalid_argument("SettingsIO: Changing protocol type is disabled");
return false;
}
try {
QDir datadir = proto->datadir;
proto = CellUtils::protoMap.at(type.toStdString())();
proto->datadir = datadir;
emit ProtocolChanged(proto);
} catch (const std::out_of_range&) {
qWarning("SettingsIO: %s not in protocol map", type.toStdString().c_str());
return true;
}
}
} else {
qWarning("SettingsIO: Settings file does not contain protocol type");
return false;
}
return true;
}
void SettingsIO::readSettings(shared_ptr<Protocol> proto, QString filename) {
QFile ifile(filename);
if(!ifile.open(QIODevice::ReadOnly)){
qCritical() << "SettingsIO: Error opening " << filename;
return;
}
QXmlStreamReader xml(&ifile);
try {
this->readProtoType(proto,xml);
proto->readpars(xml);
/*
try {
if(proto->pars.at("writeCellState").get() == "true") {
QDir fileDir(fileName);
fileDir.cdUp();
proto->pars.at("cellStateDir").set(fileDir.path().toStdString());
proto->pars.at("readCellState").set("true");
proto->pars.at("writeCellState").set("false");
}
} catch(const std::out_of_range& e) {
qDebug("SimvarMenu: par not in proto: %s", e.what());
}*/
// proto->datadir = working_dir.absolutePath().toStdString();
proto->measureMgr().readMvarsFile(xml);
this->readdvars(proto, xml);
proto->pvars().readPvars(xml);
} catch (const std::invalid_argument&) {
return;
}
this->lastProto = proto;
ifile.close();
}
void SettingsIO::writedvars(shared_ptr<Protocol> proto, QXmlStreamWriter& xml) {
set<string> selection = proto->cell()->getVariableSelection();
xml.writeStartElement("dvars");
for(auto it = selection.begin(); it != selection.end(); it++){
xml.writeTextElement("dvar",it->c_str());
}
xml.writeEndElement();
}
void SettingsIO::readdvars(shared_ptr<Protocol> proto, QXmlStreamReader& xml) {
set<string> selection;
if(!CellUtils::readNext(xml, "dvars")) return;
while(!xml.atEnd() && xml.readNextStartElement()){
xml.readNext();
selection.insert(xml.text().toString().toStdString());
xml.readNext();
}
if(!proto->cell()->setVariableSelection(selection)) {
qWarning("SettingsIO: Some dvars were not vars in cell");
}
}