moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / ktouch / src / ktouchtrainingsession.cpp
bloba5a78d813976dbab2330b4486da4ec7f91e77907
1 /***************************************************************************
2 * ktouchtrainingsession.cpp *
3 * ------------------------- *
4 * Copyright (C) 2003 by Andreas Nicolai *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 ***************************************************************************/
11 #include "ktouchtrainingsession.h"
12 #include <qtextstream.h>
13 #include <iterator>
14 #include <algorithm>
15 using std::set;
16 using std::list;
17 using std::copy;
18 using std::back_inserter;
20 KTouchTrainingSession::KTouchTrainingSession(QString str) {
21 // first let's extract the state variables
22 QTextStream textstrm(str, IO_ReadOnly );
23 textstrm >> m_elapsedTime >> m_totalChars >> m_correctChars >> m_words;
24 // and now let's extract the wrong typed chars...
25 int charcode, correctCount, wrongCount;
26 textstrm >> charcode >> correctCount >> wrongCount;
27 while (charcode!=0) {
28 m_charStats.insert( KTouchCharStats( QChar(charcode), correctCount, wrongCount) );
29 textstrm >> charcode >> correctCount >> wrongCount;
33 void KTouchTrainingSession::reset() {
34 m_totalChars=m_correctChars=m_words=m_elapsedTime=0;
35 m_charStats.clear();
36 m_wordBuffer="";
39 QString KTouchTrainingSession::asString() const {
40 // We store the session info as follows: elapsed time, total chars, correct chars, words,
41 QString tmp = QString("%1 %2 %3 %4")
42 .arg(m_elapsedTime).arg(m_totalChars).arg(m_correctChars).arg(m_words);
43 // and then we append for each missed char the char-unicode and the counters
44 for (set<KTouchCharStats>::const_iterator it=m_charStats.begin(); it!=m_charStats.end(); ++it)
45 tmp += QString(" %1 %2 %3").arg(it->m_char.unicode()).arg(it->m_correctCount).arg(it->m_wrongCount);
46 return tmp;
49 void KTouchTrainingSession::addCorrectChar(QChar ch) {
50 ++m_correctChars;
51 ++m_totalChars;
52 // we only count non-space characters
53 if (ch!=QChar(' ')) {
54 set<KTouchCharStats>::iterator it = m_charStats.find( KTouchCharStats(ch,0,0) );
55 if (it==m_charStats.end())
56 m_charStats.insert( KTouchCharStats(ch,1,0) );
57 else
58 ++(const_cast<KTouchCharStats&>(*it).m_correctCount);
59 // NOTE: this const_cast is necessary because the current gcc 3.2 has a small bug in the
60 // implementation of set::find(). The non-const overload is missing and find() always
61 // returns a const object. Maybe in the next release this will be fixed. Until then
62 // the const_cast helps.
64 if (ch==QChar(' ') && !m_wordBuffer.isEmpty() ) {
65 m_wordBuffer="";
66 ++m_words;
68 else
69 m_wordBuffer+=ch;
72 void KTouchTrainingSession::addWrongChar(QChar missedChar) {
73 ++m_totalChars;
74 if (missedChar==QChar(8) || missedChar==QChar(' ')) return; // ignore backspaces and spaces
75 set<KTouchCharStats>::iterator it = m_charStats.find( KTouchCharStats(missedChar,0,0) );
76 if (it==m_charStats.end())
77 m_charStats.insert( KTouchCharStats(missedChar,0,1) );
78 else
79 ++(const_cast<KTouchCharStats&>(*it).m_wrongCount);
82 list<KTouchCharStats> KTouchTrainingSession::sortedCharStats() const {
83 list<KTouchCharStats> tmpList;
84 std::copy(m_charStats.begin(), m_charStats.end(), back_inserter(tmpList) );
85 tmpList.sort(greaterHitMissRatio);
86 return tmpList;