-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadditionmodule.cpp
More file actions
229 lines (199 loc) · 6.45 KB
/
additionmodule.cpp
File metadata and controls
229 lines (199 loc) · 6.45 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* Copyright (c) 2013 Mike Dusseault
*
* This file is part of QMentat.
*
* QMentat is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QMentat is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QMentat. If not, see <http://www.gnu.org/licenses/>.
*/
#include "additionmodule.h"
#include <QtGui>
#include <QString>
#include <cassert>
#include "additionconfigframe.h"
#include "mainwindow.h"
#include "practicemodule.h"
#include "questiondisplayform.h"
#include "bigfixedpoint.h"
/*! Constructor for AdditionModule.
*
* \param mw MainWindow for module.
*/
AdditionModule::AdditionModule(MainWindow *mw)
{
// Keep a copy for callbacks
mainWindow = mw;
// Read config
QSettings settings;
settings.beginGroup("additionmodule");
BigFixedPoint::setRounding(false);
firstMin = BigFixedPoint(settings.value("firstmin", 2).toString());
firstMax = BigFixedPoint(settings.value("firstmax", 100).toString());
lastMin = BigFixedPoint(settings.value("lastmin", 2).toString());
lastMax = BigFixedPoint(settings.value("lastmax", 100).toString());
largestNumberFirst = settings.value("largestNumberFirst", false).toBool();
settings.endGroup();
// Make config frame
configFrame = new AdditionConfigFrame();
configFrame->setModule(this);
configFrame->setFirstMinimum(firstMin.toString());
configFrame->setFirstMaximum(firstMax.toString());
configFrame->setLastMinimum(lastMin.toString());
configFrame->setLastMaximum(lastMax.toString());
configFrame->setLargestNumberFirst(largestNumberFirst);
// Make display frame
displayFrame = (QuestionDisplay*)(new QuestionDisplayForm());
}
AdditionModule::~AdditionModule()
{
assert(configFrame != 0);
assert(displayFrame != 0);
this->mainWindow->layout()->removeWidget(configFrame);
configFrame->close();
delete configFrame;
configFrame = 0;
delete displayFrame;
displayFrame = 0;
}
/*! \returns Pointer to QFrame containing configuration form for this module.
*/
QFrame* AdditionModule::getConfigFrame()
{
assert(configFrame != 0);
return configFrame;
}
/*! \returns Pointer to QuestionDisplay form which is used to display questions
* for this module.
*/
QuestionDisplay* AdditionModule::getDisplayFrame()
{
assert(displayFrame != 0);
return displayFrame;
}
/*! \returns A QString containing the question to be displayed to the user.
* Must be in appropriate format for our DisplayFrame.
*/
QString AdditionModule::question()
{
firstNumber = BigFixedPoint::random(firstMin, firstMax);
lastNumber = BigFixedPoint::random(lastMin, lastMax);
// Scale to largest number of decimals
int maxDecimals = std::max(firstNumber.getDecimalPlaces(), lastNumber.getDecimalPlaces());
if (firstNumber.getDecimalPlaces() < maxDecimals)
{
firstNumber.scale(maxDecimals);
}
if (lastNumber.getDecimalPlaces() < maxDecimals)
{
lastNumber.scale(maxDecimals);
}
// Swap largest number first, if necessary and set to do so
if (largestNumberFirst && (lastNumber > firstNumber))
{
BigFixedPoint tmp = firstNumber;
firstNumber = lastNumber;
lastNumber = tmp;
}
// Calculate answer
answer = firstNumber + lastNumber;
// Build question string
QString q = QString("%1\n+ %2")
.arg(firstNumber.toString())
.arg(lastNumber.toString());
return q;
}
/*! \param Reference to a QString containing the user's answer to the
* current question.
* \returns true if the user answered correctly, false otherwise.
*/
bool AdditionModule::isCorrect(const QString& answerGiven)
{
BigFixedPoint answerNum(answerGiven);
answerNum.scale(answer.getDecimalPlaces());
if (answerNum == answer)
{
return true;
} else {
return false;
}
}
/*! \returns A QString containing the user viewable textual answer to the
* current question.
*/
QString AdditionModule::getAnswerString()
{
return QString("%1 + %2 = %3")
.arg(firstNumber.toString())
.arg(lastNumber.toString())
.arg(answer.toString());
}
void AdditionModule::setSettings(BigFixedPoint newFirstMin,
BigFixedPoint newFirstMax,
BigFixedPoint newLastMin,
BigFixedPoint newLastMax,
bool newLargestNumberFirst)
{
bool settingsChanged = false;
// RANGE
if ((firstMax != newFirstMax)
|| (firstMax.getDecimalPlaces() != newFirstMax.getDecimalPlaces()))
{
firstMax = newFirstMax;
QSettings settings;
settings.setValue("additionmodule/firstmax", firstMax.toString());
settingsChanged = true;
}
if ((firstMin != newFirstMin)
|| (firstMin.getDecimalPlaces() != newFirstMin.getDecimalPlaces()))
{
firstMin = newFirstMin;
QSettings settings;
settings.setValue("additionmodule/firstmin", firstMin.toString());
settingsChanged = true;
}
if ((lastMax != newLastMax)
|| (lastMax.getDecimalPlaces() != newLastMax.getDecimalPlaces()))
{
lastMax = newLastMax;
QSettings settings;
settings.setValue("additionmodule/lastmax", lastMax.toString());
settingsChanged = true;
}
if ((lastMin != newLastMin)
|| (lastMin.getDecimalPlaces() != newLastMin.getDecimalPlaces()))
{
lastMin = newLastMin;
QSettings settings;
settings.setValue("additionmodule/lastmin", lastMin.toString());
settingsChanged = true;
}
// RESULTS
if (this->largestNumberFirst != newLargestNumberFirst)
{
this->largestNumberFirst = newLargestNumberFirst;
QSettings settings;
settings.setValue("additionmodule/largestNumberFirst", largestNumberFirst);
settingsChanged = true;
}
if (settingsChanged)
{
mainWindow->newQuestion();
}
}
bool AdditionModule::applyConfig()
{
if (configFrame != 0)
{
return configFrame->applyConfig();
}
return false;
}