1 /***************************************************************************
2 mainqtwidget.cpp - The main Qt/KDE window
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 /***************************************************************************
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 ***************************************************************************/
17 #include "mainqtwidget.h"
22 #include <kapplication.h>
25 #include <kiconloader.h>
26 #include <kjanuswidget.h>
27 #include <kconfigdialog.h>
30 #include <qsplitter.h>
33 #include <qwhatsthis.h>
38 #include "exercisecompare.h"
39 #include "exerciseconvert.h"
40 #include "exercisefactorize.h"
42 #include "taskvieweroptionsbase.h"
43 #include "statisticsview.h"
45 #include "settingsclass.h"
47 /* ------ public member functions ------ */
49 MainQtWidget::MainQtWidget()
52 kdDebug() << "constructor MainQtWidget" << endl
;
58 // creating KActions, used by the kbruchui.rc file
63 // we split the main view into 2 parts, one for the tasks, one for the
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");
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)
112 resize(QSize(QMAX(toolBar()->sizeHint().width(), sizeHint().width()), sizeHint().height()));
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
)
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()
157 m_NewTaskAction
= new KAction(i18n("&New"), "filenew", KStdAccel::shortcut(KStdAccel::New
),
158 this, SLOT(NewTask()),
159 actionCollection(), "NewTask");
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);
205 case 20 : m_MaxMainDenominatorBox
->setCurrentItem(1);
207 case 30 : m_MaxMainDenominatorBox
->setCurrentItem(2);
209 case 50 : m_MaxMainDenominatorBox
->setCurrentItem(3);
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();
253 /* ------ private slots ------ */
255 void MainQtWidget::NewTask()
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
;
264 // check which page should generate a new task
265 switch (m_exercises
->activePageIndex())
268 m_taskview
->forceNewTask();
271 m_exerciseCompare
->forceNewTask();
274 m_exerciseConvert
->forceNewTask();
277 m_exerciseFactorize
->forceNewTask();
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();
288 if (m_exercises->activePageIndex() == m_exercises->pageIndex(m_exerciseCompare))
290 m_exerciseCompare->forceNewTask();
295 /* this even do not compile, but I don't know why
297 switch (m_exercises->activePageIndex())
299 case m_exercises->pageIndex(m_taskview):
301 case m_exercises->pageIndex(m_exerciseCompare):
302 m_exerciseCompare->forceNewTask();
310 void MainQtWidget::NrOfTermsBoxSlot()
313 kdDebug() << "MainQtWidget::NrOfTermsBoxSlot()" << endl
;
315 QString curr_nr
= m_NrOfTermsBox
->currentText();
316 m_MaxMainDenominatorBox
->clear();
318 if (m_mulDiv
== true)
322 m_MaxMainDenominatorBox
->insertItem("10");
323 m_MaxMainDenominatorBox
->insertItem("20");
324 m_MaxMainDenominatorBox
->insertItem("30");
325 m_MaxMainDenominatorBox
->insertItem("50");
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");
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");
339 m_maxMainDenominator
= 20;
341 m_MaxMainDenominatorBox
->insertItem("50");
343 m_maxMainDenominator
= 50;
345 m_MaxMainDenominatorBox
->setCurrentItem(0);
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");
354 else if (curr_nr
== "3")
356 else if (curr_nr
== "4")
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()
369 kdDebug() << "MainQtWidget::MaxMainDenominatorBoxSlot()" << endl
;
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()
384 kdDebug() << "MainQtWidget::OperationBoxSlot()" << endl
;
387 int index
= m_OperationBox
->currentItem(); // get selected item
389 // user has selected the operations for the next task, so store it in the
396 /* set the number of terms box and max main denominator box correctly */
398 } else if (index
== 1) {
402 /* set the number of terms box and max main denominator box correctly */
408 /* set the number of terms box and max main denominator box correctly */
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"))
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()));
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();
458 void MainQtWidget::slotAboutToShowPage(QWidget
* page
)
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
;
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);
475 m_NrOfTermsBox
->setEnabled(false);
476 m_MaxMainDenominatorBox
->setEnabled(false);
477 m_OperationBox
->setEnabled(false);
483 bool MainQtWidget::queryExit()
489 #include "mainqtwidget.moc"