23
23
#include "cheetahcs.h"
25
26
CheetaHCS::CheetaHCS(QObject *parent) :
26
27
QObject(parent), m_configExists(true), m_hospital(""), m_primaryCenter(""),
27
m_hospitals(), m_primaryCenters()
28
m_hospitals(), m_primaryCenters() // Initialize the Q_PROPERTIES
30
// This is the config folder
29
31
m_folder = QString(QStandardPaths::writableLocation(
30
32
QStandardPaths::DataLocation));
33
// And the config file
31
34
m_file = QString(m_folder + "/location.conf");
36
// The hospitals and primary centers lists
32
37
m_hospitals << "El Bierzo" << "Serranía-Ronda";
33
38
m_primaryCenters << "Picotuerto, Ponferrada II" <<
34
39
"Ronda-Sur, Santa Bárbara";
37
43
CheetaHCS::~CheetaHCS()
49
* Whether the config file exists or not, used to prompt with the configuration
42
52
bool CheetaHCS::configExists()
44
54
return m_configExists;
57
// Change the state of the property
47
58
void CheetaHCS::setConfigExists(bool configExists)
49
60
if (configExists != m_configExists)
51
62
m_configExists = configExists;
52
Q_EMIT configExistsChanged();
63
Q_EMIT configExistsChanged(); // Emit the corresponding signal
67
// The hospital that has been chosen
56
68
QString CheetaHCS::hospital()
73
// Change the state of the property
61
74
void CheetaHCS::setHospital(QString hospital)
63
76
if (hospital != m_hospital)
65
78
m_hospital = hospital;
66
Q_EMIT hospitalChanged();
79
Q_EMIT hospitalChanged(); // Emit the corresponding signal
83
// The primary center that has been chosen
70
84
QString CheetaHCS::primaryCenter()
72
86
return m_primaryCenter;
89
// Change the state of the property
75
90
void CheetaHCS::setPrimaryCenter(QString primaryCenter)
77
92
if (primaryCenter != m_primaryCenter)
79
94
m_primaryCenter = primaryCenter;
80
Q_EMIT primaryCenterChanged();
95
Q_EMIT primaryCenterChanged(); // Emit the corresponding signal
99
// The hospitals that are available
84
100
QStringList CheetaHCS::hospitals()
86
102
return m_hospitals;
105
// Change the state of the property
89
106
void CheetaHCS::setHospitals(QStringList hospitals)
91
108
if (hospitals != m_hospitals)
93
110
m_hospitals = hospitals;
94
Q_EMIT hospitalsChanged();
111
Q_EMIT hospitalsChanged(); // Emit the corresponding signal
115
// The primary centers that are available
98
116
QStringList CheetaHCS::primaryCenters()
100
118
return m_primaryCenters;
121
// Change the state of the property
103
122
void CheetaHCS::setPrimaryCenters(QStringList primaryCenters)
105
124
if (primaryCenters != m_primaryCenters)
107
126
m_primaryCenters = primaryCenters;
108
Q_EMIT primaryCentersChanged();
127
Q_EMIT primaryCentersChanged(); // Emit the correspongin signal
131
// Run the start checks and read the configuration file if available
112
132
void CheetaHCS::startChecks()
134
// Check if the folder exists and create it if not
114
135
if (startChecksFunction.folderExists(m_folder) == false)
115
136
QDir().mkpath(m_folder);
138
// Check if the config file exists and create it if not
117
139
if (startChecksFunction.configExists(m_file) == false)
118
140
setConfigExists(false);
121
void CheetaHCS::changeHospital(int hospital)
125
for (int i = 0; i < m_hospitals.length(); i++)
128
hosp = m_hospitals[i];
134
void CheetaHCS::changePrimaryCenter(int primaryCenter)
138
for (int i = 0; i < m_primaryCenters.length(); i++)
140
if (primaryCenter == i)
141
pCenter = m_primaryCenters[i];
144
setPrimaryCenter(pCenter);
147
void CheetaHCS::configure(int hospital, int primaryCenter)
154
for (int i = 0; i < m_hospitals.length(); i++)
157
hosp = m_hospitals[i];
160
for (int i = 0; i < m_primaryCenters.length(); i++)
162
if (primaryCenter == i)
163
pCenter = m_primaryCenters[i];
142
// If the config file exists, read it
143
if (QFile(m_file).exists())
144
readConfiguration(m_file);
147
// Create the configuration file
148
void CheetaHCS::configure(int hospital, int primaryCenter) /* int for the
152
QFile file(m_file); // The config file
154
QString hosp; // The hospital string
155
QString pCenter; // The primary center string
157
// For every hospital in the list
158
for (int i = 0; i < m_hospitals.length(); i++)
160
// If the selected index is the one of the list
162
hosp = m_hospitals[i]; // Set the string
165
// For every primary center in the list
166
for (int i = 0; i < m_primaryCenters.length(); i++)
168
// If the selected index is the one of the list
169
if (primaryCenter == i)
170
pCenter = m_primaryCenters[i]; // Set the string
173
// Remove the file if it exists and rewrite it
175
QFile().remove(m_file);
177
// If the config file can be open
166
178
if (file.open(QIODevice::ReadWrite))
180
// Create a stream of text in the file
168
181
QTextStream stream(&file);
182
// Write the configuration to the file
169
183
stream << "hospital=" << hosp << endl << "primaryCenter=" <<
188
// Read the configuration file
189
void CheetaHCS::readConfiguration(QString configFile)
191
QFile file(configFile); // The file itself
194
if (file.open(QIODevice::ReadOnly))
196
// Create a stream in the file
197
QTextStream stream(&file);
199
// While there are lines to read
200
while (!stream.atEnd())
202
QString line = stream.readLine(); // Get the line
203
QStringList fields = line.split("="); // Split it
205
// Now get the properties for hospital and primary center
206
if (fields[0] == "hospital")
207
setHospital(fields[1]);
208
else if (fields[0] == "primaryCenter")
209
setPrimaryCenter(fields[1]);
217
void CheetaHCS::readConfig()
219
readConfiguration(m_file);