moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kbruch / src / statisticsview.cpp
blobb19779f63114d1abd2c26b3de721beb4afe27b74
1 /***************************************************************************
2 statisticsview.cpp - the statistic window
3 -------------------
4 begin : Tue Mar 08 17:20:00 CET 2002
5 copyright : (C) 2001 - 2004 by Sebastian Stein, Eva Brucherseifer
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 ***************************************************************************/
18 #include "statisticsview.h"
19 #include "statisticsview.moc"
21 /* the includes are needed for Qt support */
22 #include <qlabel.h>
23 #include <qlayout.h>
24 #include <qpushbutton.h>
25 #include <qtooltip.h>
26 #include <qwhatsthis.h>
28 #include <klocale.h>
29 #include <kdebug.h>
31 #include "settingsclass.h"
33 /* constructor */
34 StatisticsView::StatisticsView(QWidget * parent, const char * name):
35 QWidget(parent, name), m_count(0), m_correct(0)
37 #ifdef DEBUG
38 kdDebug() << "constructor StatisticsView()" << endl;
39 #endif
41 // load statistics from config file
42 m_count = SettingsClass::count();
43 m_correct = SettingsClass::correct();
45 QPalette pal;
46 QColorGroup cg;
48 /* set the caption of the window */
49 // setCaption(i18n("Statistics"));
51 /* add a layout as a base */
52 layout1 = new QVBoxLayout(this);
53 layout1->setSpacing(6);
54 layout1->setMargin(6);
56 /* now add a v-spacer */
57 QSpacerItem * v_spacer = new QSpacerItem(1, 1);
58 layout1->addItem(v_spacer);
60 /* create a grid to show the labels */
61 labelGrid = new QGridLayout(layout1, 3, 2);
63 /* add 6 labels to the grid */
64 info1Label = new QLabel(this);
65 info1Label->setText(i18n("Tasks so far:"));
66 labelGrid->addWidget(info1Label, 1, 0);
68 result1Label = new QLabel(this);
69 labelGrid->addWidget(result1Label, 1, 1);
70 QToolTip::add(result1Label,
71 i18n("This is the current total number of solved tasks."));
73 info2Label = new QLabel(this);
74 info2Label->setText(i18n("Correct:"));
75 labelGrid->addWidget(info2Label, 2, 0);
77 result2Label = new QLabel(this);
79 /* set green text color for this label */
80 pal = result2Label->palette();
81 cg = pal.active();
82 cg.setColor(QColorGroup::Foreground, QColor(6, 179, 0));
83 pal.setActive(cg);
84 cg = pal.inactive();
85 cg.setColor(QColorGroup::Foreground, QColor(6, 179, 0));
86 pal.setInactive(cg);
87 result2Label->setPalette(pal);
89 labelGrid->addWidget(result2Label, 2, 1);
90 QToolTip::add(result2Label,
91 i18n("This is the current total number of correctly solved tasks."));
93 info3Label = new QLabel(this);
94 info3Label->setText(i18n("Incorrect:"));
95 labelGrid->addWidget(info3Label, 3, 0);
97 result3Label = new QLabel(this);
99 /* set red text color for this label */
100 pal = result3Label->palette();
101 cg = pal.active();
102 cg.setColor(QColorGroup::Foreground, QColor(red));
103 pal.setActive(cg);
104 cg = pal.inactive();
105 cg.setColor(QColorGroup::Foreground, QColor(red));
106 pal.setInactive(cg);
107 result3Label->setPalette(pal);
109 labelGrid->addWidget(result3Label, 3, 1);
110 QToolTip::add(result3Label,
111 i18n("This is the current total number of unsolved tasks."));
113 /* now add a v-spacer */
114 v_spacer = new QSpacerItem(1, 1);
115 layout1->addItem(v_spacer);
117 /* the Reset button */
118 buttonLayout = new QHBoxLayout(layout1);
119 resetBtn = new QPushButton(i18n("&Reset"), this);
120 QObject::connect(resetBtn, SIGNAL(clicked()), this, SLOT(resetStatistics()));
121 buttonLayout->addWidget(resetBtn);
122 QToolTip::add(resetBtn, i18n("Press the button to reset the statistics."));
123 QSpacerItem* spacer = new QSpacerItem(0,0);
124 buttonLayout->addItem(spacer);
126 /* calculate the statistics */
127 (void) calc();
129 // add tooltip and qwhatsthis help to the widget
130 QToolTip::add(this, i18n("This part of the window shows the statistics."));
131 QWhatsThis::add(this, i18n("This part of the window shows the statistics. Each exercise you do is counted. You can reset the statistics by clicking on the button below. Also, if you do not want to see the statistics, use the vertical bar on the left to reduce the size of this window part."));
134 /* destructor */
135 StatisticsView::~StatisticsView()
137 #ifdef DEBUG
138 kdDebug() << "destructor StatisticsView()" << endl;
139 #endif
140 // save statistics for next run
141 SettingsClass::setCount(m_count);
142 SettingsClass::setCorrect(m_correct);
143 SettingsClass::writeConfig();
145 /* no need to delete any child widgets, Qt does it by itself */
148 /* called, if a task solved correctly */
149 void StatisticsView::addCorrect()
151 ++m_count;
152 ++m_correct;
153 (void) calc(); /* repaint the statistics */
156 /* called, if a task was solved wrong */
157 void StatisticsView::addWrong()
159 ++m_count;
160 (void) calc(); /* repaint the statistics */
164 /* ------ private member functions ------ */
166 /* recalculates the statistics and changes the corresponding labels */
167 void StatisticsView::calc()
169 QString new_text;
170 QString number;
172 new_text = QString("<b>%1</b>").arg(m_count);
173 result1Label->setText(new_text);
175 /* we have to be careful with division by 0 */
176 if (m_count == 0)
178 result2Label->setText("- (- %)");
179 result3Label->setText("- (- %)");
180 } else {
181 /* set the correct label */
182 new_text = QString("%1 (%2 %)").arg(m_correct).arg(int(double(m_correct) / m_count * 100));
183 result2Label->setText(new_text);
185 /* set the incorrect label */
186 new_text = QString("%1 (%2 %)").arg(m_count - m_correct).arg(int(double(m_count - m_correct) / m_count * 100));
187 result3Label->setText(new_text);
191 /* ------ private slots ------ */
193 /* called by the reset button */
194 void StatisticsView::resetStatistics()
196 m_count = 0;
197 m_correct = 0;
198 (void) calc();