~vitimiti/cheetahcs/trunk

« back to all changes in this revision

Viewing changes to backend/modules/CheetaHCS/cheetahcs.cpp

  • Committer: Víctor Matía Rodríguez
  • Date: 2014-11-02 12:50:00 UTC
  • Revision ID: vmatir@gmail.com-20141102125000-l5n7c6bs3o1yycko
Added the possibility to change the settings. Changed the way the
settings dialog OK button behaves so as to reduce the functions granting
the same functionality. Combined the complaint dialogs to only two files
instead of four. Added several backend functions that all work together
to handle and read the configuration file as needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
#include "cheetahcs.h"
24
24
 
 
25
// The constructor
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
28
29
{
 
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");
 
35
 
 
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";
35
40
}
36
41
 
 
42
// The destructor
37
43
CheetaHCS::~CheetaHCS()
38
44
{
39
45
 
40
46
}
41
47
 
 
48
/*
 
49
 * Whether the config file exists or not, used to prompt with the configuration
 
50
 * menu
 
51
 */
42
52
bool CheetaHCS::configExists()
43
53
{
44
54
    return m_configExists;
45
55
}
46
56
 
 
57
// Change the state of the property
47
58
void CheetaHCS::setConfigExists(bool configExists)
48
59
{
49
60
    if (configExists != m_configExists)
50
61
    {
51
62
        m_configExists = configExists;
52
 
        Q_EMIT configExistsChanged();
 
63
        Q_EMIT configExistsChanged(); // Emit the corresponding signal
53
64
    }
54
65
}
55
66
 
 
67
// The hospital that has been chosen
56
68
QString CheetaHCS::hospital()
57
69
{
58
70
    return m_hospital;
59
71
}
60
72
 
 
73
// Change the state of the property
61
74
void CheetaHCS::setHospital(QString hospital)
62
75
{
63
76
    if (hospital != m_hospital)
64
77
    {
65
78
        m_hospital = hospital;
66
 
        Q_EMIT hospitalChanged();
 
79
        Q_EMIT hospitalChanged(); // Emit the corresponding signal
67
80
    }
68
81
}
69
82
 
 
83
// The primary center that has been chosen
70
84
QString CheetaHCS::primaryCenter()
71
85
{
72
86
    return m_primaryCenter;
73
87
}
74
88
 
 
89
// Change the state of the property
75
90
void CheetaHCS::setPrimaryCenter(QString primaryCenter)
76
91
{
77
92
    if (primaryCenter != m_primaryCenter)
78
93
    {
79
94
        m_primaryCenter = primaryCenter;
80
 
        Q_EMIT primaryCenterChanged();
 
95
        Q_EMIT primaryCenterChanged(); // Emit the corresponding signal
81
96
    }
82
97
}
83
98
 
 
99
// The hospitals that are available
84
100
QStringList CheetaHCS::hospitals()
85
101
{
86
102
    return m_hospitals;
87
103
}
88
104
 
 
105
// Change the state of the property
89
106
void CheetaHCS::setHospitals(QStringList hospitals)
90
107
{
91
108
    if (hospitals != m_hospitals)
92
109
    {
93
110
        m_hospitals = hospitals;
94
 
        Q_EMIT hospitalsChanged();
 
111
        Q_EMIT hospitalsChanged(); // Emit the corresponding signal
95
112
    }
96
113
}
97
114
 
 
115
// The primary centers that are available
98
116
QStringList CheetaHCS::primaryCenters()
99
117
{
100
118
    return m_primaryCenters;
101
119
}
102
120
 
 
121
// Change the state of the property
103
122
void CheetaHCS::setPrimaryCenters(QStringList primaryCenters)
104
123
{
105
124
    if (primaryCenters != m_primaryCenters)
106
125
    {
107
126
        m_primaryCenters = primaryCenters;
108
 
        Q_EMIT primaryCentersChanged();
 
127
        Q_EMIT primaryCentersChanged(); // Emit the correspongin signal
109
128
    }
110
129
}
111
130
 
 
131
// Run the start checks and read the configuration file if available
112
132
void CheetaHCS::startChecks()
113
133
{
 
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);
116
137
 
 
138
    // Check if the config file exists and create it if not
117
139
    if (startChecksFunction.configExists(m_file) == false)
118
140
        setConfigExists(false);
119
 
}
120
 
 
121
 
void CheetaHCS::changeHospital(int hospital)
122
 
{
123
 
    QString hosp;
124
 
 
125
 
    for (int i = 0; i < m_hospitals.length(); i++)
126
 
    {
127
 
        if (hospital == i)
128
 
            hosp = m_hospitals[i];
129
 
    }
130
 
 
131
 
    setHospital(hosp);
132
 
}
133
 
 
134
 
void CheetaHCS::changePrimaryCenter(int primaryCenter)
135
 
{
136
 
    QString pCenter;
137
 
 
138
 
    for (int i = 0; i < m_primaryCenters.length(); i++)
139
 
    {
140
 
        if (primaryCenter == i)
141
 
            pCenter = m_primaryCenters[i];
142
 
    }
143
 
 
144
 
    setPrimaryCenter(pCenter);
145
 
}
146
 
 
147
 
void CheetaHCS::configure(int hospital, int primaryCenter)
148
 
{
149
 
    QFile file(m_file);
150
 
 
151
 
    QString hosp;
152
 
    QString pCenter;
153
 
 
154
 
    for (int i = 0; i < m_hospitals.length(); i++)
155
 
    {
156
 
        if (hospital == i)
157
 
            hosp = m_hospitals[i];
158
 
    }
159
 
 
160
 
    for (int i = 0; i < m_primaryCenters.length(); i++)
161
 
    {
162
 
        if (primaryCenter == i)
163
 
            pCenter = m_primaryCenters[i];
164
 
    }
165
 
 
 
141
 
 
142
    // If the config file exists, read it
 
143
    if (QFile(m_file).exists())
 
144
        readConfiguration(m_file);
 
145
}
 
146
 
 
147
// Create the configuration file
 
148
void CheetaHCS::configure(int hospital, int primaryCenter) /* int for the
 
149
                                                            * selectedIndex
 
150
                                                            * of each */
 
151
{
 
152
    QFile file(m_file); // The config file
 
153
 
 
154
    QString hosp; // The hospital string
 
155
    QString pCenter; // The primary center string
 
156
 
 
157
    // For every hospital in the list
 
158
    for (int i = 0; i < m_hospitals.length(); i++)
 
159
    {
 
160
        // If the selected index is the one of the list
 
161
        if (hospital == i)
 
162
            hosp = m_hospitals[i]; // Set the string
 
163
    }
 
164
 
 
165
    // For every primary center in the list
 
166
    for (int i = 0; i < m_primaryCenters.length(); i++)
 
167
    {
 
168
        // If the selected index is the one of the list
 
169
        if (primaryCenter == i)
 
170
            pCenter = m_primaryCenters[i]; // Set the string
 
171
    }
 
172
 
 
173
    // Remove the file if it exists and rewrite it
 
174
    if (file.exists())
 
175
        QFile().remove(m_file);
 
176
 
 
177
    // If the config file can be open
166
178
    if (file.open(QIODevice::ReadWrite))
167
179
    {
 
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=" <<
170
184
                  pCenter << endl;
171
185
    }
172
186
}
 
187
 
 
188
// Read the configuration file
 
189
void CheetaHCS::readConfiguration(QString configFile)
 
190
{
 
191
    QFile file(configFile); // The file itself
 
192
 
 
193
    // If it can be open
 
194
    if (file.open(QIODevice::ReadOnly))
 
195
    {
 
196
        // Create a stream in the file
 
197
        QTextStream stream(&file);
 
198
 
 
199
        // While there are lines to read
 
200
        while (!stream.atEnd())
 
201
        {
 
202
            QString line = stream.readLine(); // Get the line
 
203
            QStringList fields = line.split("="); // Split it
 
204
 
 
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]);
 
210
        }
 
211
    }
 
212
 
 
213
    // Close the file
 
214
    file.close();
 
215
}
 
216
 
 
217
void CheetaHCS::readConfig()
 
218
{
 
219
    readConfiguration(m_file);
 
220
}