cornelius / plutimikation

Math learning game for children.

This URL has Read+Write access

cornelius (author)
Tue Dec 09 12:20:25 -0800 2008
commit  3e95b55b9b792d798ea66874e0b1ed5ecca71ea7
tree    64e4e7eec4cf4d1bfd9e49fde035649ec79f2feb
parent  6f47ad6d256d619e04105d16c5c56bd15e89daf0
plutimikation / src / mainview.cpp
100644 185 lines (143 sloc) 4.966 kb
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
This file is part of KDE.
 
Copyright (c) 2008 Cornelius Schumacher <schumacher@kde.org>
 
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.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
*/
 
#include "mainview.h"
 
#include "resultviewtext.h"
#include "resultviewpic.h"
#include "krandom.h"
 
#include <klocale.h>
#include <kdebug.h>
#include <kmessagebox.h>
 
#include <qlayout.h>
#include <qlineedit.h>
#include <qpushbutton.h>
#include <qlabel.h>
 
MainView::MainView( QWidget *parent )
  : QWidget( parent )
{
  QGridLayout *topLayout = new QGridLayout( this );
  topLayout->setSpacing( 8 );
  topLayout->setMargin( 8 );
 
  QLabel *label = new QLabel( i18n("Question:"), this );
  topLayout->addWidget( label, 0, 0 );
 
  mQuestionLabel = new QLabel( this );
  QFont f = mQuestionLabel->font();
  f.setPointSize( 30 );
  mQuestionLabel->setFont( f );
  mQuestionLabel->setAlignment( AlignCenter );
  topLayout->addWidget( mQuestionLabel, 1, 0 );
  
  label = new QLabel( i18n("Answer:"), this );
  topLayout->addWidget( label, 2, 0 );
  
  mAnswerEdit = new QLineEdit( this );
  topLayout->addWidget( mAnswerEdit, 3, 0 );
  connect( mAnswerEdit, SIGNAL( returnPressed() ), SLOT( checkAnswer() ) );
  mAnswerEdit->setAlignment( AlignCenter );
 
  mOkButton = new QPushButton( i18n("Ok"), this );
  topLayout->addWidget( mOkButton, 4, 0 );
  connect( mOkButton, SIGNAL( clicked() ), SLOT( checkAnswer() ) );
 
  mFeedbackText = new QLabel( this );
  topLayout->addWidget( mFeedbackText, 5, 0 );
  mFeedbackText->setFrameStyle( QFrame::Panel | QFrame::Plain );
  mFeedbackText->setLineWidth( 2 );
  mFeedbackText->setAlignment( AlignCenter );
 
  QFontMetrics fm( mFeedbackText->font() );
  mFeedbackText->setFixedHeight( fm.height() * 3 + 6 );
 
// mResultView = new ResultViewText( this );
  mResultView = new ResultViewPic( this );
  topLayout->addMultiCellWidget( mResultView, 0, 5, 1, 1 );
 
  connect( &mReadyTimer, SIGNAL( timeout() ), SLOT( setReady() ) );
}
 
MainView::~MainView()
{
  clear();
}
 
void MainView::clear()
{
}
 
void MainView::initQuestions( const QuestionSet::List &questionSets )
{
  mQuestions.clear();
 
  QuestionSet::List::ConstIterator it;
  for( it = questionSets.begin(); it != questionSets.end(); ++it ) {
    QuestionSet *set = *it;
    set->initQuestions( mQuestions );
  }
 
  mResultView->initialize( mQuestions );
 
  setReady();
 
  mOkButton->setEnabled( true );
  mAnswerEdit->setEnabled( true );
}
 
void MainView::setReady()
{
  mFeedbackText->setText( i18n("Ready") );
}
 
void MainView::newQuestion()
{
  int i = KRandom::number( mQuestions.count() );
  kdDebug() << "Question number " << i << endl;
  
  mCurrentQuestion = mQuestions.at( i );
  
  mQuestionLabel->setText( (*mCurrentQuestion).question() );
 
  mAnswerEdit->setText( "" );
 
  mReadyTimer.start( 2000, true ); // 2 seconds
 
// mResultView->runAutoResult();
}
 
void MainView::checkAnswer()
{
  if ( mQuestions.count() == 0 ) {
    kdError() << "checkAnswer(): no questions." << endl;
    return;
  }
 
  mReadyTimer.stop();
 
  Question q = *mCurrentQuestion;
 
  QString text = "<qt>";
  if ( mAnswerEdit->text() == q.answer() ) {
    text += i18n("Correct answer:");
    mResultView->incrementCurrentCount();
    mQuestions.remove( mCurrentQuestion );
  } else {
    text += "<font color=\"red\">" + i18n("Wrong answer.") + "</font><br>";
    text += i18n("Correct Answer:");
    mResultView->incrementWrongCount();
  }
  text += "<br>";
  text += q.question() + " = " + q.answer();
  text += "</qt>";
 
  mFeedbackText->setText( text );
  
  if ( mQuestions.count() == 0 ) {
    QString text = "<qt>";
    text += i18n("<b>Congratulation!</b><br/>");
    text += i18n("You answered all questions.<br/>");
    text += "<br/>";
    if ( mResultView->wrongCount() == 0 ) {
      text += i18n("You gave no wrong answers.");
    } else {
      text += i18n("You gave one wrong answer.",
        "You gave %n wrong answers", mResultView->wrongCount() );
    }
    text += "<br/>";
    text += mResultView->rating();
    text += "</qt>";
    
    KMessageBox::information( this, text );
 
    mQuestionLabel->setText( "" );
    mAnswerEdit->setText( "" );
    mAnswerEdit->setEnabled( false );
    mOkButton->setEnabled( false );
    mFeedbackText->setText( i18n("You won.") );
  } else {
    newQuestion();
  }
}
 
#include "mainview.moc"