File indexing completed on 2024-04-21 07:27:47

0001 /*
0002     StatisticsView.cpp  -  the statistic window
0003     SPDX-FileCopyrightText: 2001-2004 Sebastian Stein <seb.kde@hpfsc.de>
0004     SPDX-FileCopyrightText: 2001-2004 Eva Brucherseifer
0005     SPDX-FileCopyrightText: 2008 Tadeu Araujo <tadeu.araujo@ltia.fc.unesp.br>
0006     SPDX-FileCopyrightText: 2008 Danilo Balzaque <danilo.balzaque@ltia.fc.unesp.br>
0007     SPDX-FileCopyrightText: 2008 Tiago Porangaba <tiago.porangaba@ltia.fc.unesp.br>
0008 
0009     SPDX-License-Identifier: GPL-2.0-or-later
0010 */
0011 
0012 #include "StatisticsView.h"
0013 
0014 /* the includes are needed for KDE support */
0015 #include <KLocalizedString>
0016 
0017 /* the includes are needed for Qt support */
0018 #include <QGridLayout>
0019 #include <QPushButton>
0020 
0021 #ifdef DEBUG
0022 #include <QDebug>
0023 #endif
0024 
0025 #include "settingsclass.h"
0026 #include "StatisticsBarWidget.h"
0027 
0028 /* constructor */
0029 StatisticsView::StatisticsView(QWidget * parent) :
0030     QFrame(parent), m_count(0), m_correct(0)
0031 {
0032 #ifdef DEBUG
0033     qDebug() << QStringLiteral("constructor StatisticsView()");
0034 #endif
0035     // load statistics from config file
0036     m_count = SettingsClass::count();
0037     m_correct = SettingsClass::correct();
0038     m_skipped = SettingsClass::skipped();
0039 
0040     defaultFont = SettingsClass::taskFont();
0041     defaultFont.setBold(true);
0042     defaultFont.setPointSize(28);
0043 
0044     /* create a grid to show the labels */
0045     labelGrid = new QGridLayout();
0046     setLayout(labelGrid);
0047 
0048     labelGrid->setColumnStretch(0, 1);
0049     labelGrid->setColumnStretch(6, 1);
0050 
0051     labelGrid->setColumnMinimumWidth(5, 220);
0052     labelGrid->setColumnMinimumWidth(2, 30);
0053 
0054     result1Label = new QLabel(this);
0055     labelGrid->addWidget(result1Label, 1, 1, 2, 1);
0056     result1Label->setFont(defaultFont);
0057     result1Label->setAlignment(Qt::AlignCenter);
0058     result1Label->setToolTip(
0059         i18n("This is the current total number of solved tasks."));
0060 
0061     defaultFont.setBold(false);
0062     defaultFont.setPointSize(10);
0063 
0064     info1Label = new QLabel(this);
0065     info1Label->setText(i18n("Questions:"));
0066     info1Label->setFont(defaultFont);
0067     info1Label->setAlignment(Qt::AlignCenter);
0068     labelGrid->addWidget(info1Label, 0, 1);
0069 
0070     info2Label = new QLabel(this);
0071     info2Label->setText(i18nc("@info:status the number of correct answers", "Correct:"));
0072     info2Label->setFont(defaultFont);
0073     info2Label->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
0074     labelGrid->addWidget(info2Label, 0, 3);
0075 
0076     info4Label = new QLabel(this);
0077     info4Label->setText(i18nc("@info:status the number of incorrect answers", "Incorrect:"));
0078     info4Label->setFont(defaultFont);
0079     info4Label->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
0080     labelGrid->addWidget(info4Label, 1, 3);
0081 
0082     info3Label = new QLabel(this);
0083     info3Label->setText(i18nc("@info:status the number of skipped answers", "Skipped:"));
0084     info3Label->setFont(defaultFont);
0085     info3Label->setAlignment(Qt::AlignVCenter | Qt::AlignRight);
0086     labelGrid->addWidget(info3Label, 2, 3);
0087 
0088     defaultFont.setBold(true);
0089 
0090     result2Label = new QLabel(this);
0091     labelGrid->addWidget(result2Label, 0, 4);
0092     result2Label->setFont(defaultFont);
0093     result2Label->setToolTip(
0094         i18n("This is the current total number of correctly solved tasks."));
0095 
0096     result4Label = new QLabel(this);
0097     labelGrid->addWidget(result4Label, 1, 4);
0098     result4Label->setFont(defaultFont);
0099     result4Label->setToolTip(
0100         i18n("This is the current total number of unsolved tasks."));
0101 
0102     result3Label = new QLabel(this);
0103     labelGrid->addWidget(result3Label, 2, 4);
0104     result3Label->setFont(defaultFont);
0105     result3Label->setToolTip(
0106         i18n("This is the current total number of skipped tasks."));
0107 
0108     // add tooltip and qwhatsthis help to the widget
0109     setToolTip(i18n("This part of the window shows the statistics."));
0110     setWhatsThis(i18n("This part of the window shows the statistics. Each exercise you do is counted. You can reset the statistics by clicking on the 'New' button in the toolbar or by selecting 'New' from the 'File' menu"));
0111 
0112     QBoxLayout * cLayout = new QBoxLayout(QBoxLayout::LeftToRight);
0113 
0114     statisticsBar = new StatisticsBarWidget(this);
0115     labelGrid->addWidget(statisticsBar, 0, 5, 3, 1);
0116     labelGrid->addLayout(cLayout, 0, 5, Qt::AlignCenter);
0117 
0118     // add reset button and connect
0119     resetButton = new QPushButton(this);
0120     resetButton->setObjectName(QStringLiteral("resetButton"));
0121     resetButton->setText(i18n("&Reset"));
0122     resetButton->setToolTip(i18n("Click this button to reset the statistics."));
0123     resetButton->setFont(defaultFont);
0124     QObject::connect(resetButton, &QPushButton::clicked, this, &StatisticsView::resetStatistics);
0125     labelGrid->addWidget(resetButton, 5, 5, Qt::AlignRight);
0126 
0127     /* calculate the statistics */
0128     (void) calc();
0129 }
0130 
0131 /* destructor */
0132 StatisticsView::~StatisticsView()
0133 {
0134 #ifdef DEBUG
0135     qDebug() << QStringLiteral("destructor StatisticsView()");
0136 #endif
0137     // save statistics for next run
0138     SettingsClass::setCount(m_count);
0139     SettingsClass::setCorrect(m_correct);
0140     SettingsClass::setSkipped(m_skipped);
0141     SettingsClass::self()->save();
0142 
0143     /* no need to delete any child widgets, Qt does it by itself */
0144 }
0145 
0146 /* called, if a task solved correctly */
0147 void StatisticsView::addCorrect()
0148 {
0149     ++m_count;
0150     ++m_correct;
0151     (void) calc(); /* repaint the statistics */
0152 }
0153 
0154 /* called, if a task is skipped */
0155 void StatisticsView::addSkipped()
0156 {
0157     ++m_count;
0158     ++m_skipped;
0159     (void) calc(); /* repaint the statistics */
0160 }
0161 
0162 /* called, if a task was solved wrong */
0163 void StatisticsView::addWrong()
0164 {
0165     ++m_count;
0166     (void) calc(); /* repaint the statistics */
0167 }
0168 
0169 
0170 /* ------ private member functions ------ */
0171 
0172 /* recalculates the statistics and changes the corresponding labels */
0173 void StatisticsView::calc()
0174 {
0175     statisticsBar->updateBar(m_correct, m_skipped, m_count);
0176 
0177     QString new_text;
0178 
0179     new_text = QStringLiteral("<b>%1</b>").arg(m_count);
0180     result1Label->setText(new_text);
0181 
0182     /* we have to be careful with division by 0 */
0183     if (m_count == 0) {
0184         result2Label->setText(QStringLiteral("0 (0 %)"));
0185         result3Label->setText(QStringLiteral("0 (0 %)"));
0186         result4Label->setText(QStringLiteral("0 (0 %)"));
0187     } else {
0188         /* set the correct label */
0189         new_text = QStringLiteral("%1").arg(m_correct);
0190         result2Label->setText(new_text);
0191 
0192         /* set the correct label */
0193         new_text = QStringLiteral("%1").arg(m_skipped);
0194         result3Label->setText(new_text);
0195 
0196         /* set the incorrect label */
0197         new_text = QStringLiteral("%1").arg(m_count - m_correct - m_skipped);
0198         result4Label->setText(new_text);
0199     }
0200 }
0201 
0202 /* ------ public slots ------ */
0203 
0204 /* called by the reset button */
0205 void StatisticsView::resetStatistics()
0206 {
0207     m_count = 0;
0208     m_correct = 0;
0209     m_skipped = 0;
0210     (void) calc();
0211 }
0212 
0213 #include "moc_StatisticsView.cpp"