moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kbruch / src / mainqtwidget.cpp
blob069a4d839943279720423f9985f32d6e41657b5a
1 /***************************************************************************
2 mainqtwidget.cpp - The main Qt/KDE window
3 -------------------
4 begin : Tue Mar 16 00:00:00 CET 2003
5 copyright : (C) 2003-2004 by Sebastian Stein
6 email : seb.kde@hpfsc.de
7 ***************************************************************************/
9 /***************************************************************************
10 * *
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. *
15 * *
16 ***************************************************************************/
17 #include "mainqtwidget.h"
19 #include <config.h>
21 #include <kaccel.h>
22 #include <kapplication.h>
23 #include <kaction.h>
24 #include <kdebug.h>
25 #include <kiconloader.h>
26 #include <kjanuswidget.h>
27 #include <kconfigdialog.h>
28 #include <klocale.h>
30 #include <qsplitter.h>
31 #include <qlabel.h>
32 #include <qtooltip.h>
33 #include <qwhatsthis.h>
34 #include <qwidget.h>
36 #include <math.h>
38 #include "exercisecompare.h"
39 #include "exerciseconvert.h"
40 #include "exercisefactorize.h"
41 #include "taskview.h"
42 #include "taskvieweroptionsbase.h"
43 #include "statisticsview.h"
45 #include "settingsclass.h"
47 /* ------ public member functions ------ */
49 MainQtWidget::MainQtWidget()
51 #ifdef DEBUG
52 kdDebug() << "constructor MainQtWidget" << endl;
53 #endif
55 // get the settings
56 readOptions();
58 // creating KActions, used by the kbruchui.rc file
59 setupActions();
61 createGUI(0L);
63 // we split the main view into 2 parts, one for the tasks, one for the
64 // statistics
65 QSplitter* splitter = new QSplitter(QSplitter::Horizontal, this,"QSplitter");
66 setCentralWidget(splitter);
68 // the iconlist, where the user can choose the different exercises
69 m_exercises = new KJanusWidget(splitter, "KJanusWidget", KJanusWidget::IconList);
70 QToolTip::add(m_exercises, i18n("Choose another exercise by clicking on an icon."));
71 QWhatsThis::add(m_exercises, i18n("Click on the different icons to choose another exercise. The exercises help you to practice different aspects of calculating with fractions."));
73 // create the statistic view
74 m_statview = new StatisticsView(splitter, "StatisticsView");
76 // add the pages
78 // we have the exercise to solve fraction tasks
79 QVBox * page = m_exercises->addVBoxPage(i18n("Fraction Task"), "", DesktopIcon("kbruch_exercise_common"));
80 m_taskview = new TaskView((QWidget *) page, "TaskView", m_addSub, m_mulDiv, m_nrRatios, m_maxMainDenominator);
82 // we have the exercise to compare ratios
83 page = m_exercises->addVBoxPage(i18n("Comparison"), "", DesktopIcon("kbruch_exercise_compare"));
84 m_exerciseCompare = new ExerciseCompare((QWidget *) page, "ExerciseCompare");
86 // we have the exercise to convert rational numbers into ratios
87 page = m_exercises->addVBoxPage(i18n("Conversion"), "", DesktopIcon("kbruch_exercise_conversion"));
88 m_exerciseConvert = new ExerciseConvert((QWidget *) page, "ExerciseConvert");
90 // we have the exercise to factorize a given number into prime factors
91 page = m_exercises->addVBoxPage(i18n("Factorization"), "", DesktopIcon("kbruch_exercise_factorisation"));
92 m_exerciseFactorize = new ExerciseFactorize((QWidget *) page, "ExerciseFactorize");
94 splitter->setResizeMode(m_statview, QSplitter::FollowSizeHint);
96 // we must change the status of the menubar before another page is shown
97 QObject::connect(m_exercises, SIGNAL(aboutToShowPage(QWidget *)), this, SLOT(slotAboutToShowPage(QWidget *)));
99 // connect signals of the exercises and StatisticView, so that StatisticView
100 // gets informed about how the user solved a given task (wrong or correct)
101 QObject::connect(m_taskview, SIGNAL(signalTaskSolvedCorrect()), m_statview, SLOT(addCorrect()));
102 QObject::connect(m_taskview, SIGNAL(signalTaskSolvedWrong()), m_statview, SLOT(addWrong()));
103 QObject::connect(m_exerciseCompare, SIGNAL(signalExerciseSolvedCorrect()), m_statview, SLOT(addCorrect()));
104 QObject::connect(m_exerciseCompare, SIGNAL(signalExerciseSolvedWrong()), m_statview, SLOT(addWrong()));
105 QObject::connect(m_exerciseConvert, SIGNAL(signalExerciseSolvedCorrect()), m_statview, SLOT(addCorrect()));
106 QObject::connect(m_exerciseConvert, SIGNAL(signalExerciseSolvedWrong()), m_statview, SLOT(addWrong()));
107 QObject::connect(m_exerciseFactorize, SIGNAL(signalExerciseSolvedCorrect()), m_statview, SLOT(addCorrect()));
108 QObject::connect(m_exerciseFactorize, SIGNAL(signalExerciseSolvedWrong()), m_statview, SLOT(addWrong()));
110 #if (KDE_VERSION_MINOR>=3) && (KDE_VERSION_MAJOR>=3)
111 #else
112 resize(QSize(QMAX(toolBar()->sizeHint().width(), sizeHint().width()), sizeHint().height()));
113 #endif
114 // now show the last exercise
115 m_exercises->showPage(SettingsClass::activeExercise());
116 slotAboutToShowPage(m_exercises->pageWidget(m_exercises->activePageIndex()));
119 MainQtWidget::~MainQtWidget()
124 /* ------ private member functions ------ */
126 void MainQtWidget::readOptions()
128 m_addSub = SettingsClass::addsub();
129 m_mulDiv = SettingsClass::muldiv();
130 m_nrRatios = SettingsClass::number_ratios();
131 m_maxMainDenominator = SettingsClass::max_main_denominator();
133 /* make sure that we can load config files with corrupted values */
134 if (m_mulDiv == true && pow(2, m_nrRatios) > m_maxMainDenominator)
136 m_nrRatios = 2;
137 m_maxMainDenominator = 10;
141 void MainQtWidget::writeOptions()
143 SettingsClass::setActiveExercise(m_exercises->activePageIndex());
145 // save settings for exercise solve task with fractions
146 SettingsClass::setAddsub(m_addSub);
147 SettingsClass::setMuldiv(m_mulDiv);
148 SettingsClass::setNumber_ratios(m_nrRatios);
149 SettingsClass::setMax_main_denominator(m_maxMainDenominator);
151 SettingsClass::writeConfig();
154 void MainQtWidget::setupActions()
156 // new task action
157 m_NewTaskAction = new KAction(i18n("&New"), "filenew", KStdAccel::shortcut(KStdAccel::New),
158 this, SLOT(NewTask()),
159 actionCollection(), "NewTask");
161 // quit action
162 KStdAction::quit(kapp, SLOT(quit()), actionCollection());
165 KStdAction::preferences(this, SLOT( slotPrefs() ), actionCollection());
167 // a label just describing the Number of terms ComboBox
168 m_NrOfTermsLabel = new QLabel(i18n("Terms:"), 0, "kde toolbar widget");
169 m_NrOfTermsLabelAction = new KWidgetAction(m_NrOfTermsLabel, i18n("Terms:"), ALT+Key_E,
170 this, SLOT(NrOfTermsBoxSlot()),
171 actionCollection(), "NrOfTermsLabelAction");
173 // the ComboBox holding possible values for term number
174 m_NrOfTermsBox = new QComboBox();
175 m_NrOfTermsBox->insertItem("2");
176 m_NrOfTermsBox->insertItem("3");
177 m_NrOfTermsBox->insertItem("4");
178 m_NrOfTermsBox->insertItem("5");
179 m_NrOfTermsBox->setCurrentItem(m_nrRatios - 2);
180 QToolTip::add( m_NrOfTermsBox, i18n( "The number of terms you want" ) );
181 QWhatsThis::add( m_NrOfTermsBox, i18n( "Choose the number of terms (2, 3, 4 or 5) you want for calculating fractions." ) );
182 m_NrOfTermsBoxAction = new KWidgetAction(m_NrOfTermsBox, i18n("Number of Terms"), ALT+Key_E, this, SLOT(NrOfTermsBoxSlot()), actionCollection(), "NrOfTermsBoxAction");
184 // now connect the ComboBox's signal textChanged() to the slot function
185 QObject::connect(m_NrOfTermsBox, SIGNAL(activated(int)), this, SLOT(NrOfTermsBoxSlot()));
187 // a label just describing the max. main denominator ComboBox
188 m_MaxMainDenominatorLabel = new QLabel(i18n("Max. main denominator:"), 0, "kde toolbar widget");
189 m_MaxMainDenominatorLabelAction = new KWidgetAction(m_MaxMainDenominatorLabel, i18n("Max. main denominator:"), ALT+Key_D,
190 this, SLOT(MaxMainDenominatorBoxSlot()),
191 actionCollection(), "MaxMainDenominatorLabelAction");
193 // the ComboBox holding possible values for the max. main denominator
194 m_MaxMainDenominatorBox = new QComboBox(this);
195 m_MaxMainDenominatorBox->insertItem("10");
196 m_MaxMainDenominatorBox->insertItem("20");
197 m_MaxMainDenominatorBox->insertItem("30");
198 m_MaxMainDenominatorBox->insertItem("50");
199 QToolTip::add( m_MaxMainDenominatorBox, i18n( "The maximum number you can have as main denominator" ) );
200 QWhatsThis::add( m_MaxMainDenominatorBox, i18n( "Choose the number which will be the maximum for the main denominator: 10, 20, 30, 40 or 50." ) );
201 switch (m_maxMainDenominator)
203 case 10 : m_MaxMainDenominatorBox->setCurrentItem(0);
204 break;
205 case 20 : m_MaxMainDenominatorBox->setCurrentItem(1);
206 break;
207 case 30 : m_MaxMainDenominatorBox->setCurrentItem(2);
208 break;
209 case 50 : m_MaxMainDenominatorBox->setCurrentItem(3);
210 break;
212 m_MaxMainDenominatorBoxAction = new KWidgetAction(m_MaxMainDenominatorBox, i18n("Maximal Main Denominator"), ALT+Key_D, this, SLOT(MaxMainDenominatorBoxSlot()), actionCollection(), "MaxMainDenominatorBoxAction");
214 // now connect the ComboBox's signal textChanged() to the slot function
215 QObject::connect(m_MaxMainDenominatorBox, SIGNAL(activated(int)),
216 this, SLOT(MaxMainDenominatorBoxSlot()));
218 // a label just describing the operation ComboBox
219 m_OperationLabel = new QLabel(i18n("Operations:"), 0, "kde toolbar widget");
220 m_OperationLabelAction = new KWidgetAction(m_OperationLabel, i18n("Operations:"), ALT+Key_O,
221 this, SLOT(OperationBoxSlot()),
222 actionCollection(), "OperationLabelAction");
224 // the ComboBox holding possible combinations for operations
225 m_OperationBox = new QComboBox(this);
226 m_OperationBox->insertItem(i18n("Addition/Subtraction"));
227 m_OperationBox->insertItem(i18n("Multiplication/Division"));
228 m_OperationBox->insertItem(i18n("All Operations Mixed"));
229 if (m_addSub == true && m_mulDiv == false)
231 m_OperationBox->setCurrentItem(0);
232 } else if (m_addSub == false && m_mulDiv == true) {
233 m_OperationBox->setCurrentItem(1);
234 } else if (m_addSub == true && m_mulDiv == true) {
235 m_OperationBox->setCurrentItem(2);
237 QToolTip::add( m_OperationBox, i18n( "The operations you want" ) );
238 QWhatsThis::add( m_OperationBox, i18n( "Choose the type of operations you want for calculating fractions: Addition/Substraction, Multiplication/Division or All Operations Mixed. If you choose All Operations Mixed, the program will randomly choose addition, substraction, multiplication and/or division." ) );
239 m_OperationBoxAction = new KWidgetAction(m_OperationBox, i18n("Operations:"), ALT+Key_O, this, SLOT(OperationBoxSlot()), actionCollection(), "OperationBoxAction");
241 // now connect the ComboBox's signal textChanged() to the slot function
242 QObject::connect(m_OperationBox, SIGNAL(activated(int)), this, SLOT(OperationBoxSlot()));
244 #if (KDE_VERSION_MINOR>=3) && (KDE_VERSION_MAJOR>=3)
245 if (!initialGeometrySet())
246 resize( QSize(725, 330).expandedTo(minimumSizeHint()));
247 setupGUI(ToolBar | Keys | StatusBar | Create);
248 setAutoSaveSettings();
249 #endif
253 /* ------ private slots ------ */
255 void MainQtWidget::NewTask()
257 #ifdef DEBUG
258 kdDebug() << "NewTask MainQtWidget" << endl;
259 kdDebug() << "pageIndex(m_taskview): " << m_exercises->pageIndex(m_taskview) << endl;
260 kdDebug() << "pageIndex(m_exerciseCompare): " << m_exercises->pageIndex(m_exerciseCompare) << endl;
261 kdDebug() << "pageIndex(m_exerciseConvert): " << m_exercises->pageIndex(m_exerciseConvert) << endl;
262 #endif
264 // check which page should generate a new task
265 switch (m_exercises->activePageIndex())
267 case 0 :
268 m_taskview->forceNewTask();
269 break;
270 case 1 :
271 m_exerciseCompare->forceNewTask();
272 break;
273 case 2 :
274 m_exerciseConvert->forceNewTask();
275 break;
276 case 3 :
277 m_exerciseFactorize->forceNewTask();
278 break;
281 /* this doesn't seem to work, because pageIndex always returns 0
283 if (m_exercises->activePageIndex() == m_exercises->pageIndex(m_taskview))
285 m_taskview->forceNewTask();
286 return;
288 if (m_exercises->activePageIndex() == m_exercises->pageIndex(m_exerciseCompare))
290 m_exerciseCompare->forceNewTask();
291 return;
295 /* this even do not compile, but I don't know why
297 switch (m_exercises->activePageIndex())
299 case m_exercises->pageIndex(m_taskview):
300 break;
301 case m_exercises->pageIndex(m_exerciseCompare):
302 m_exerciseCompare->forceNewTask();
303 break;
307 return;
310 void MainQtWidget::NrOfTermsBoxSlot()
312 #ifdef DEBUG
313 kdDebug() << "MainQtWidget::NrOfTermsBoxSlot()" << endl;
314 #endif
315 QString curr_nr = m_NrOfTermsBox->currentText();
316 m_MaxMainDenominatorBox->clear();
318 if (m_mulDiv == true)
320 if (curr_nr == "2")
322 m_MaxMainDenominatorBox->insertItem("10");
323 m_MaxMainDenominatorBox->insertItem("20");
324 m_MaxMainDenominatorBox->insertItem("30");
325 m_MaxMainDenominatorBox->insertItem("50");
326 m_nrRatios = 2;
327 m_maxMainDenominator = 10;
328 } else if (curr_nr == "3") {
329 m_MaxMainDenominatorBox->insertItem("20");
330 m_MaxMainDenominatorBox->insertItem("30");
331 m_MaxMainDenominatorBox->insertItem("50");
332 m_nrRatios = 3;
333 m_maxMainDenominator = 20;
334 } else if (curr_nr == "4") {
335 m_MaxMainDenominatorBox->insertItem("20");
336 m_MaxMainDenominatorBox->insertItem("30");
337 m_MaxMainDenominatorBox->insertItem("50");
338 m_nrRatios = 4;
339 m_maxMainDenominator = 20;
340 } else {
341 m_MaxMainDenominatorBox->insertItem("50");
342 m_nrRatios = 5;
343 m_maxMainDenominator = 50;
345 m_MaxMainDenominatorBox->setCurrentItem(0);
346 } else {
347 /* no multiplication or division allowed, so we add the default values */
348 m_MaxMainDenominatorBox->insertItem("10");
349 m_MaxMainDenominatorBox->insertItem("20");
350 m_MaxMainDenominatorBox->insertItem("30");
351 m_MaxMainDenominatorBox->insertItem("50");
352 if (curr_nr == "2")
353 m_nrRatios = 2;
354 else if (curr_nr == "3")
355 m_nrRatios = 3;
356 else if (curr_nr == "4")
357 m_nrRatios = 4;
358 else
359 m_nrRatios = 5;
360 } // if (m_mulDiv == true)
362 // set the new task parameters
363 (void) m_taskview->setTaskParameters(m_addSub, m_mulDiv, m_nrRatios, m_maxMainDenominator);
366 void MainQtWidget::MaxMainDenominatorBoxSlot()
368 #ifdef DEBUG
369 kdDebug() << "MainQtWidget::MaxMainDenominatorBoxSlot()" << endl;
370 #endif
372 // get the max. size from the ComboBox, convert it to a number and store
373 // it in the private member
374 QString curr_md = m_MaxMainDenominatorBox->currentText();
375 m_maxMainDenominator = curr_md.toUInt();
377 // set the new task parameters
378 (void) m_taskview->setTaskParameters(m_addSub, m_mulDiv, m_nrRatios, m_maxMainDenominator);
381 void MainQtWidget::OperationBoxSlot()
383 #ifdef DEBUG
384 kdDebug() << "MainQtWidget::OperationBoxSlot()" << endl;
385 #endif
387 int index = m_OperationBox->currentItem(); // get selected item
389 // user has selected the operations for the next task, so store it in the
390 // private members
391 if (index == 0)
393 m_addSub = true;
394 m_mulDiv = false;
396 /* set the number of terms box and max main denominator box correctly */
397 NrOfTermsBoxSlot();
398 } else if (index == 1) {
399 m_addSub = false;
400 m_mulDiv = true;
402 /* set the number of terms box and max main denominator box correctly */
403 NrOfTermsBoxSlot();
404 } else {
405 m_addSub = true;
406 m_mulDiv = true;
408 /* set the number of terms box and max main denominator box correctly */
409 NrOfTermsBoxSlot();
412 // set the new task parameters
413 (void) m_taskview->setTaskParameters(m_addSub, m_mulDiv, m_nrRatios, m_maxMainDenominator);
416 void MainQtWidget::slotPrefs()
418 // do not show dialog twice
419 if (KConfigDialog::showDialog("settings"))
420 return;
422 //KConfigDialog didn't find an instance of this dialog, so lets create it :
423 KConfigDialog* configDialog = new KConfigDialog( this, "settings", SettingsClass::self() );
426 TaskViewerOptionsBase * taskViewerOptions = new TaskViewerOptionsBase(0, "TaskViewerOptionsBase");
427 configDialog->addPage(taskViewerOptions, i18n("Task Viewer Settings"), "colorize");
429 // User edited the configuration - update your local copies of the
430 // configuration data
431 connect(configDialog, SIGNAL(settingsChanged()), this, SLOT(slotApplySettings()) );
433 configDialog->show();
435 SettingsDialog * dlg = new SettingsDialog(this);
436 connect(dlg, SIGNAL(configChanged()), this, SLOT(slotApplySettings()));
438 dlg->exec();
440 delete dlg;
441 dlg = NULL;
444 return;
447 void MainQtWidget::slotApplySettings()
449 // update the task view
450 m_taskview->update();
451 m_exerciseCompare->update();
452 m_exerciseConvert->update();
453 m_exerciseFactorize->update();
455 return;
458 void MainQtWidget::slotAboutToShowPage(QWidget * page)
460 #ifdef DEBUG
461 kdDebug() << "slotAboutToShowPage MainQtWidget" << endl;
462 kdDebug() << "pageIndex(m_taskview): " << m_exercises->pageIndex(m_taskview) << endl;
463 kdDebug() << "pageIndex(m_exerciseCompare): " << m_exercises->pageIndex(m_exerciseCompare) << endl;
464 kdDebug() << "pageIndex(m_exerciseConvert): " << m_exercises->pageIndex(m_exerciseConvert) << endl;
465 #endif
467 // check which page to show
468 if (m_exercises->pageIndex(page) == m_exercises->pageIndex(m_taskview))
470 // exercise solve task with fraction (taskview.h)
471 m_NrOfTermsBox->setEnabled(true);
472 m_MaxMainDenominatorBox->setEnabled(true);
473 m_OperationBox->setEnabled(true);
474 } else {
475 m_NrOfTermsBox->setEnabled(false);
476 m_MaxMainDenominatorBox->setEnabled(false);
477 m_OperationBox->setEnabled(false);
480 return;
483 bool MainQtWidget::queryExit()
485 writeOptions();
486 return true;
489 #include "mainqtwidget.moc"