summaryrefslogtreecommitdiffstats
path: root/kwordquiz/src/wqscore.cpp
blob: 485363b02345aa28be0c6a2bf5e123e1519282e7 (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"

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


WTQScore::~WTQScore()
{}


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

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

void WTQScore::countIncrement(CountDirection d)
{

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

/*!
    \fn WTQScore::answerText()
 */
TQString WTQScore::answerText()
{
  TQString 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 WTQScore::correctText()
 */
TQString WTQScore::correctText()
{
  TQString 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 WTQScore::errorText()
 */
TQString WTQScore::errorText()
{
  TQString 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 WTQScore::valueToString(int i)
 */
TQString WTQScore::valueToString(int i)
{
  TQString s = ""; //return empty string for 0

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

  return s;
}