summaryrefslogtreecommitdiffstats
path: root/kpercentage/kpercentage/kanswer.cpp
blob: 7e12bafc5b54dfc11d214e3b4f12eb3c7d062175 (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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/***************************************************************************
                       kanswer.cpp  -  description
                          -------------------
 begin                : Fri Nov 30 2001
 copyright            : (C) 2001 by Matthias Messmer &
                                    Carsten Niehaus &
                                    Robert Gogolok
 email                : bmlmessmer@web.de &
                        cniehaus@gmx.de &
                        mail@robert-gogolok.de
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

// C/C++ includes
#include <stdlib.h>
#include <assert.h>

// TQt includes
#include <tqfile.h>
#include <tqlabel.h>
#include <tqlayout.h>

// KDE includes
#include <kdebug.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kpushbutton.h>
#include <kstandarddirs.h>
#include <kstdguiitem.h>

// local includes
#include "kanimation.h"
#include "kanswer.h"

KAnswer::KAnswer( TQWidget *tqparent ): KDialog( tqparent, "answer", TRUE )
{
    setFont( tqparent->font() );
    // fix size for the background pixmap
    setFixedSize( TQSize( 430, 190 ) );

    // load background pixmap
    TQPixmap bgp( locate( "data", "kpercentage/pics/kanswer_bg.png" ) );
    setBackgroundPixmap( bgp );

    setupSprite();

    ButtonOK = new KPushButton( KStdGuiItem::ok(), this );
    // for the bottons edges
    ButtonOK->setBackgroundOrigin( TQPushButton::ParentOrigin );
    ButtonOK->setIconSet( TQIconSet( DesktopIcon( "button_ok" ) ) );
//    ButtonOK->hide();

    TextLabelAnswer = new TQLabel( this );
    // make the label transparent
    TextLabelAnswer->setBackgroundOrigin( TQLabel::ParentOrigin );
    TextLabelAnswer->setBackgroundPixmap( bgp );

    canvas->tqsetBackgroundPixmap( bgp );
    canvas_view->setBackgroundOrigin( TQCanvasView::ParentOrigin );
    canvas_view->setBackgroundPixmap( bgp );

    ///////
    // begin tqlayouting
    ///////
    mainLayout = new TQHBoxLayout( this );
    mainLayout->setDirection(TQBoxLayout::LeftToRight);

//    mainLayout->addSpacing( 20 );

    TQVBoxLayout *leftLayout = new TQVBoxLayout( mainLayout, -1, "main" );
//    leftLayout->addSpacing( 20 );
    leftLayout->addWidget( canvas_view );
    leftLayout->addSpacing( 20 );
    leftLayout->addStretch();

    mainLayout->addSpacing( 60 );

    TQVBoxLayout *rightLayout = new TQVBoxLayout( mainLayout, -1, "right" );

    rightLayout->addStretch( 2 );

    TQHBoxLayout *topLayout = new TQHBoxLayout( rightLayout, -1, "top" );
    topLayout->setDirection(TQBoxLayout::LeftToRight);
    topLayout->addStretch();
    topLayout->addWidget( TextLabelAnswer );
    topLayout->addStretch();

    rightLayout->addStretch( 2 );

    TQHBoxLayout *bottomLayout = new TQHBoxLayout( rightLayout, -1, "bottom" );
    bottomLayout->setDirection(TQBoxLayout::LeftToRight);
    bottomLayout->addStretch();
    bottomLayout->addWidget( ButtonOK );
    bottomLayout->addSpacing( 20 );

    rightLayout->addSpacing( 20 );
    ///////
    // end tqlayouting
    ///////

    connect( ButtonOK, TQT_SIGNAL( clicked() ), this, TQT_SLOT( accept() ) );
    loadAnswers();
}

void KAnswer::loadAnswers()
{
    // helping to manage the IO
    TQString s;
    TQFile f;
    TQTextStream t;

    //reading proper answers from file
    f.setName( locate( "data", "kpercentage/right.txt" ) );
    if ( f.open( IO_ReadOnly ) )        // file opened successfully
    {
        TQTextStream t( &f );        // use a text stream
        while ( !t.eof() )            // until end of file...
        {
            s = t.readLine();       // line of text excluding '\n'
            rightAnswerList.append( s );
        }
    }
    f.close();

    // reading proper answers from file
    f.setName( locate( "data", "kpercentage/wrong.txt" ) );
    if ( f.open( IO_ReadOnly ) )        // file opened successfully
    {
        TQTextStream t( &f );        // use a text stream
        while ( !t.eof() )            // until end of file...
        {
            s = t.readLine();       // line of text excluding '\n'
            wrongAnswerList.append( s );
        }
    }
    f.close();
}

void KAnswer::setAnswer( int modus )
{
    bool correct;
    switch ( modus )
    {
    case 1:
        setCaption( i18n( "Congratulations!" ) );
        TextLabelAnswer->setText( getRightAnswer() );
        correct = true;
        break;
    case 2:
        setCaption( i18n( "Error!" ) );
        TextLabelAnswer->setText( getWrongAnswer() );
        correct = false;
        break;
    case 3:
        setCaption( i18n( "Oops!" ) );
        TextLabelAnswer->setText( i18n( "Mistyped!" ) );
        correct = false;
        break;
    case 4:
        setCaption( i18n( "Congratulations!" ) );
        TextLabelAnswer->setText( i18n( "Great!\nYou managed all\nthe exercises!" ) );
        correct = true;
	break;
    default:
	assert(false);
	correct = false;
    }
    
    if (correct)
    {
        wrong_animation->hide();
        right_animation->show();
    }
    else
    {
        wrong_animation->show();
        right_animation->hide();
    }
    
    wrong_animation->setToStart();
    wrong_animation->setAnimated( !correct );
    right_animation->setToStart();
    right_animation->setAnimated( correct );
    canvas->setAllChanged();
    canvas->update();
    canvas->setAdvancePeriod( advPer );
    
	 // this seems to be needed for proper showing TextLabelAnswer :-(
    resize( width(), height() );
}

// reads one answer out of the list by chance
TQString KAnswer::getRightAnswer()
{
    return i18n( rightAnswerList[ random() % rightAnswerList.count() ].utf8() );
}

// reads one answer out of the list by chance
TQString KAnswer::getWrongAnswer()
{
    return i18n( wrongAnswerList[ random() % wrongAnswerList.count() ].utf8() );
}


// Animation stuff

void KAnswer::setupSprite()
{

    advPer = 80;
    
    canvas = new TQCanvas( TQT_TQOBJECT(this) );
    canvas->resize( size().width(), size().height() );
    pixs = new TQCanvasPixmapArray( locate( "data", "kpercentage/pics/" )+"smily%1.png", 7 );

    right_animation = new KAnimation(  locate( "data", "kpercentage/story/right.story" ), pixs , canvas );
    right_animation->setAnimated( TRUE );
    wrong_animation = new KAnimation(  locate( "data", "kpercentage/story/wrong.story" ), pixs , canvas );
    wrong_animation->setAnimated( TRUE );

    canvas_view = new TQCanvasView( canvas, this );
    canvas_view->resize( size() );
    canvas_view->setVScrollBarMode( TQCanvasView::AlwaysOff );
    canvas_view->setHScrollBarMode( TQCanvasView::AlwaysOff );
    canvas_view->setFrameStyle( TQCanvasView::NoFrame );
}


void KAnswer::timerEvent( TQTimerEvent *event )
{
    accept();
}

int KAnswer::exec()
{
    TQT_TQOBJECT(this)->killTimers();
    startTimer( 3000 ); // 5 seconds
    return KDialog::exec();
}

void KAnswer::accept()
{
    canvas->setAdvancePeriod( -1 ); // stop all animation
    KDialog::accept();
}

#include "kanswer.moc"