1 /***************************************************************************
5 copyright : (C) 2004 by Sebastian Stein
6 email : seb.kde@hpfsc.de
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
18 #include "exercisecompare.h"
19 #include "exercisecompare.moc"
21 /* these includes are needed for KDE support */
22 #include <kapplication.h>
25 /* these includes are needed for Qt support */
28 #include <qpushbutton.h>
30 #include <qwhatsthis.h>
32 /* standard C++ library includes */
35 /* ----- public member functions ----- */
38 ExerciseCompare::ExerciseCompare(QWidget
* parent
, const char * name
):
39 ExerciseBase(parent
, name
)
42 kdDebug() << "constructor ExerciseCompare()" << endl
;
45 /* create a new task */
46 QApplication::setOverrideCursor(waitCursor
); /* show the sand clock */
48 QApplication::restoreOverrideCursor(); /* show the normal cursor */
50 // the next thing to do on a button click would be to check the entered
52 m_currentState
= _CHECK_TASK
;
54 baseWidget
= new QWidget(this, "baseWidget");
55 baseGrid
= new QGridLayout(this, 1, 1, 0, -1, "baseGrid");
56 baseGrid
->addWidget(baseWidget
, 0, 0);
59 realLayout
= new QVBoxLayout(baseWidget
, 5, 5, "realLayout");
61 // add a spacer at the top of the VBox
62 QSpacerItem
* v_spacer
= new QSpacerItem(1, 1);
63 realLayout
->addItem(v_spacer
);
65 // now a line holding the task, input fields and result
66 QHBoxLayout
* taskLineHBoxLayout
= new QHBoxLayout(5, "taskLineHBoxLayout");
67 realLayout
->addLayout(taskLineHBoxLayout
);
70 v_spacer
= new QSpacerItem(1, 1);
71 taskLineHBoxLayout
->addItem(v_spacer
);
73 // first the first ratio widget
74 m_firstRatioWidget
= new RatioWidget(baseWidget
, "m_firstRatioWidget", m_firstRatio
);
75 taskLineHBoxLayout
->addWidget(m_firstRatioWidget
);
78 v_spacer
= new QSpacerItem(1, 1);
79 taskLineHBoxLayout
->addItem(v_spacer
);
81 // now the button where the user has to choose the comparison sign
82 m_signButton
= new QPushButton(baseWidget
, "m_signButton");
83 m_signButton
->setText("<");
84 m_signButtonState
= lessThen
;
85 taskLineHBoxLayout
->addWidget(m_signButton
);
86 QObject::connect(m_signButton
, SIGNAL(clicked()), this, SLOT(slotSignButtonClicked()));
87 QToolTip::add(m_signButton
, i18n("Click on this button to change the comparison sign."));
90 v_spacer
= new QSpacerItem(1, 1);
91 taskLineHBoxLayout
->addItem(v_spacer
);
93 // now the second ratio widget
94 m_secondRatioWidget
= new RatioWidget(baseWidget
, "m_secondRatioWidget", m_secondRatio
);
95 taskLineHBoxLayout
->addWidget(m_secondRatioWidget
);
98 v_spacer
= new QSpacerItem(1, 1);
99 taskLineHBoxLayout
->addItem(v_spacer
);
101 // at the right end we have a label just showing CORRECT or WRONG
102 result_label
= new QLabel(baseWidget
, "result_label");
103 result_label
->setText(i18n("WRONG"));
104 taskLineHBoxLayout
->addWidget(result_label
);
105 result_label
->hide();
107 // --- that is the end of the horizontal line ---
109 // add another spacer in the middle of the VBox
110 v_spacer
= new QSpacerItem(1, 1);
111 realLayout
->addItem(v_spacer
);
113 // the lower part of the VBox holds just a right aligned button
114 QHBoxLayout
* lowerHBox
= new QHBoxLayout(1, "lowerHBox");
115 realLayout
->addLayout(lowerHBox
);
116 lowerHBox
->addStretch(100);
118 // the right aligned button
119 m_checkButton
= new QPushButton( baseWidget
, "m_checkButton" );
120 m_checkButton
->setText(i18n("&Check Task"));
121 m_checkButton
->setDefault(true); // is the default button of the dialog
122 QToolTip::add(m_checkButton
, i18n("Click on this button to check your result."));
123 lowerHBox
->addWidget(m_checkButton
, 1, Qt::AlignRight
);
124 QObject::connect(m_checkButton
, SIGNAL(clicked()), this, SLOT(slotCheckButtonClicked()));
126 // that the user can start typing without moving the focus
127 m_signButton
->setFocus();
129 // show the whole layout
132 // add tooltip and qwhatsthis help to the widget
133 QToolTip::add(this, i18n("In this exercise you have to compare 2 given fractions."));
134 QWhatsThis::add(this, i18n("In this exercise you have to compare 2 given fractions by choosing the correct comparison sign. You can change the comparison sign by just clicking on the button showing the sign."));
138 ExerciseCompare::~ExerciseCompare()
141 kdDebug() << "destructor ExerciseCompare()" << endl
;
144 /* no need to delete any child widgets, Qt does it by itself */
147 /** resets the current state, creates a new task and count the last task as
148 * wrong, if it wasn't solved (in _NEXT_TASK state) yet
149 * mainly used after changing the task parameters */
150 void ExerciseCompare::forceNewTask()
153 kdDebug() << "forceNewTask ExerciseCompare()" << endl
;
156 if (m_currentState
== _CHECK_TASK
)
158 // emit the signal for wrong
159 signalExerciseSolvedWrong();
161 m_currentState
= _CHECK_TASK
;
162 m_checkButton
->setText(i18n("&Check Task"));
164 // generate next task
169 /* ------ public slots ------ */
171 void ExerciseCompare::update()
173 // call update of components
174 m_firstRatioWidget
->updateAndRepaint();
175 m_secondRatioWidget
->updateAndRepaint();
178 ((QWidget
*) this)->update();
182 /* ------ private member functions ------ */
184 void ExerciseCompare::createTask()
186 // generate the first ratio
187 m_firstRatio
= ratio(int((double(rand()) / RAND_MAX
) * 10 + 1), int((double(rand()) / RAND_MAX
) * 10 + 1));
189 // now the second ratio, but make sure, the second ratio is different from
193 m_secondRatio
= ratio(int((double(rand()) / RAND_MAX
) * 10 + 1), int((double(rand()) / RAND_MAX
) * 10 + 1));
194 } while (m_firstRatio
== m_secondRatio
);
199 /** - checks, if the user solved the task correctly
200 - emits signals if task was solved correctly or wrong */
201 void ExerciseCompare::showResult()
205 bool result
= m_firstRatio
< m_secondRatio
;
207 // change the tooltip of the check button
208 QToolTip::add(m_checkButton
, i18n("Click on this button to get to the next task."));
210 // disable sign button
211 m_signButton
->setEnabled(false);
213 if ((m_signButtonState
== lessThen
&& result
== true) ||
214 (m_signButtonState
== greaterThen
&& result
== false))
216 // emit the signal for correct
217 signalExerciseSolvedCorrect();
219 /* yes, the user entered the correct result */
220 result_label
->setText(i18n("CORRECT"));
221 pal
= result_label
->palette(); /* set green font color */
223 cg
.setColor(QColorGroup::Foreground
, QColor(6, 179, 0));
226 cg
.setColor(QColorGroup::Foreground
, QColor(6, 179, 0));
228 result_label
->setPalette(pal
);
229 result_label
->show(); /* show the result at the end of the task */
231 // emit the signal for wrong
232 signalExerciseSolvedWrong();
234 /* no, the user entered the wrong result */
235 result_label
->setText(i18n("WRONG"));
236 pal
= result_label
->palette(); /* set red font color */
238 cg
.setColor(QColorGroup::Foreground
, QColor(red
));
241 cg
.setColor(QColorGroup::Foreground
, QColor(red
));
243 result_label
->setPalette(pal
);
245 result_label
->show(); /* show the result at the end of the task */
246 } /* if (entered_result == result) */
251 /** generate the next task and show it to the user */
252 void ExerciseCompare::nextTask()
254 // change the tooltip of the check button
255 QToolTip::add(m_checkButton
, i18n("Click on this button to check your result."));
257 // enable sign button
258 m_signButton
->setEnabled(true);
260 result_label
->hide(); /* do not show the result at the end of the task */
262 // reset the signButton
263 m_signButton
->setText("<");
264 m_signButtonState
= lessThen
;
266 /* create a new task */
267 QApplication::setOverrideCursor(waitCursor
); /* show the sand clock */
269 QApplication::restoreOverrideCursor(); /* show the normal cursor */
271 // set the ratio widgets with the new ratios
272 m_firstRatioWidget
->setRatio(m_firstRatio
);
273 m_secondRatioWidget
->setRatio(m_secondRatio
);
278 /* ------ private slots ------ */
280 void ExerciseCompare::slotCheckButtonClicked()
282 if (m_currentState
== _CHECK_TASK
)
284 m_currentState
= _NEXT_TASK
;
285 m_checkButton
->setText(i18n("N&ext Task"));
288 m_currentState
= _CHECK_TASK
;
289 m_checkButton
->setText(i18n("&Check Task"));
296 void ExerciseCompare::slotSignButtonClicked()
298 if (m_signButtonState
== lessThen
)
300 m_signButton
->setText(">");
301 m_signButtonState
= greaterThen
;
303 m_signButton
->setText("<");
304 m_signButtonState
= lessThen
;