summaryrefslogtreecommitdiffstats
path: root/kwordquiz/src/wqscore.cpp
blob: 420a002d2882bca5dd3e1aea21b8924c14ac6864 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/***************************************************************************
                          wqscore.cpp  -  description
                             -------------------
   copyright            : (C) 2003 by Peter Hedlund
   email                : peter.hedlund@kdemail.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "wqscore.h"

WQScore::WQScore()
{
  m_questionCount = 0;
  m_percent = false;
  m_error = 0;
  m_correct = 0;
  m_answerCount = 0;
}


WQScore::~WQScore()
{}


void WQScore::setAsPercent(bool p)
{
  m_percent = p;
}

void WQScore::setQuestionCount(int c)
{
  m_questionCount = c;
  //m_percent = false;
  m_error = 0;
  m_correct = 0;
  m_answerCount = 0;
}

void WQScore::countIncrement(CountDirection d)
{

  if (d != cdNone)
    m_answerCount++;
  if (d == cdCorrect)
    m_correct++;
  if (d == cdError)
    m_error++;
}

/*!
    \fn WQScore::answerText()
 */
QString WQScore::answerText()
{
  QString s;
  if (m_percent)
  {
    float f = ((m_answerCount * 100.0) / m_questionCount);
    if (m_answerCount > 0)
    {
      if (f < 1)
        s = s.setNum(f, 'f', 1) + "%"; //for long lists (i.e. each question is less than 1%) we show one decimal 
      else
        s = s.setNum(f, 'f', 0) + "%";
    }
    else
      s= valueToString(m_answerCount);
  }
  else
    s = valueToString(m_answerCount);
  return s;
}

/*!
    \fn WQScore::correctText()
 */
QString WQScore::correctText()
{
  QString s;
  if (m_percent)
  {
    if (m_correct > 0)
    {
      float f = ((m_correct * 100.0) / m_answerCount);
      s = s.setNum(f, 'f', 0) + "%";
    }
    else
      s = valueToString(m_correct);

  }
  else
    s = valueToString(m_correct);
  return s;
}

/*!
    \fn WQScore::errorText()
 */
QString WQScore::errorText()
{
  QString s;
  if (m_percent)
  {
    if (m_error > 0)
    {
      float f = ((m_error * 100.0) / m_answerCount);
      s = s.setNum(f, 'f', 0) + "%";
    }
    else
      s = valueToString(m_error);

  }
  else
    s = valueToString(m_error);
  return s;
}

/*!
    \fn WQScore::valueToString(int i)
 */
QString WQScore::valueToString(int i)
{
  QString s = ""; //return empty string for 0

  if (i > 0)
    s = s.setNum(i, 10);

  return s;
}