File indexing completed on 2023-05-30 10:45:26

0001 /*
0002     SPDX-FileCopyrightText: 2009-2010 Peter Hedlund <peter.hedlund@kdemail.net>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "kwqscorewidget.h"
0007 
0008 #include <QIcon>
0009 
0010 KWQScoreWidget::KWQScoreWidget(QWidget *parent) : QWidget(parent)
0011 {
0012   setupUi(this);
0013   m_percent = false;
0014   clear();
0015 }
0016 
0017 void KWQScoreWidget::clear()
0018 {
0019   m_questionCount = 0;
0020   m_error = 0;
0021   m_correct = 0;
0022   m_answerCount = 0;
0023   picAnswered->clear();
0024   picCorrect->clear();
0025   picError->clear();
0026   update();
0027 }
0028 
0029 void KWQScoreWidget::setAsPercent(bool p)
0030 {
0031   m_percent = p;
0032   update();
0033 }
0034 
0035 void KWQScoreWidget::setQuestionCount(int c)
0036 {
0037   clear();
0038   m_questionCount = c;
0039   update();
0040 }
0041 
0042 void KWQScoreWidget::countIncrement(CountDirection d)
0043 {
0044   m_answerCount++;
0045   if (d == cdCorrect)
0046     m_correct++;
0047   if (d == cdError)
0048     m_error++;
0049   update();
0050 }
0051 
0052 /*!
0053     \fn KWQScoreWidget::answerText()
0054  */
0055 QString KWQScoreWidget::answerText()
0056 {
0057   QString s;
0058   if (m_percent) {
0059     float f = ((m_answerCount * 100.0) / m_questionCount);
0060     if (m_answerCount > 0) {
0061       if (f < 1)
0062         s = s.setNum(f, 'f', 1) + '%'; //for long lists (i.e. each question is less than 1%) we show one decimal
0063       else
0064         s = s.setNum(f, 'f', 0) + '%';
0065     }
0066     else
0067       s = valueToString(m_answerCount);
0068   }
0069   else
0070     s = valueToString(m_answerCount);
0071   return s;
0072 }
0073 
0074 /*!
0075     \fn KWQScoreWidget::correctText()
0076  */
0077 QString KWQScoreWidget::correctText()
0078 {
0079   QString s;
0080   if (m_percent) {
0081     if (m_correct > 0) {
0082       float f = ((m_correct * 100.0) / m_answerCount);
0083       s = s.setNum(f, 'f', 0) + '%';
0084     }
0085     else
0086       s = valueToString(m_correct);
0087   }
0088   else
0089     s = valueToString(m_correct);
0090   return s;
0091 }
0092 
0093 /*!
0094     \fn KWQScoreWidget::errorText()
0095  */
0096 QString KWQScoreWidget::errorText()
0097 {
0098   QString s;
0099   if (m_percent) {
0100     if (m_error > 0) {
0101       float f = ((m_error * 100.0) / m_answerCount);
0102       s = s.setNum(f, 'f', 0) + '%';
0103     }
0104     else
0105       s = valueToString(m_error);
0106   }
0107   else
0108     s = valueToString(m_error);
0109   return s;
0110 }
0111 
0112 /*!
0113     \fn KWQScoreWidget::valueToString(int i)
0114  */
0115 QString KWQScoreWidget::valueToString(int i)
0116 {
0117   QString s;
0118   s.clear(); //return empty string for 0
0119 
0120   if (i > 0)
0121     s = s.setNum(i, 10);
0122 
0123   return s;
0124 }
0125 
0126 void KWQScoreWidget::update()
0127 {
0128   QString s;
0129   s = s.setNum(m_questionCount, 10);
0130   lblScoreCount->setText(s);
0131   picCount->setPixmap(QIcon::fromTheme(QStringLiteral("kwordquiz")).pixmap(32));
0132 
0133   s = answerText();
0134   lblScoreAnswered->setText(s);
0135   if (!s.isEmpty())
0136     picAnswered->setPixmap(QIcon::fromTheme(QStringLiteral("question")).pixmap(32));
0137 
0138   s = correctText();
0139   lblScoreCorrect->setText(s);
0140   if (!s.isEmpty())
0141     picCorrect->setPixmap(QIcon::fromTheme(QStringLiteral("answer-correct")).pixmap(32));
0142 
0143   s = errorText();
0144   lblScoreError->setText(s);
0145   if (s.isEmpty())
0146     picError->setPixmap(QPixmap());
0147   else
0148     picError->setPixmap(QIcon::fromTheme(QStringLiteral("error")).pixmap(32));
0149 }
0150 
0151 void KWQScoreWidget::swapCount()
0152 {
0153   m_error--;
0154   m_correct++;
0155   update();
0156 }