summaryrefslogtreecommitdiffstats
path: root/kpercentage/kpercentage/kpercentmain.cpp
blob: 67f4a687c41bfb005b720f098ac180821b84090a (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/***************************************************************************
                         kpercentmain.cpp  -  description
                            -------------------
   begin                : Fri Nov 16 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 <time.h>

// Qt includes
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqprogressbar.h>
#include <tqpushbutton.h>
#include <tqtooltip.h>

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

// local includes
#include "kpercentmain.h"
#include "kpercentage.h"


KPercentMain::KPercentMain( TQWidget *parent, const char *name ) :
        KDialog( parent, name, TRUE )  // TRUE for modal dialog
{
    setFont( parent->font() );
    /** load and set the background pixmap */
    TQPixmap bgp( locate( "data", TQApplication::reverseLayout() ? "kpercentage/pics/kpercentmain_bg_rtl.png" : "kpercentage/pics/kpercentmain_bg.png" ) );
//    resize( 520, 220 );
    setFixedSize( TQSize( 520, 275 ) );
    setBackgroundPixmap( bgp );
    setBackgroundOrigin( TQDialog::ParentOrigin );
    KIntValidator *validLine = new KIntValidator( this );

    line_edit_percentage = new TQLineEdit( this, "line_edit_percentage" );
    line_edit_percentage->setAlignment( int( TQLineEdit::AlignHCenter ) );
    line_edit_percentage->setValidator( validLine );

    line_edit_basevalue = new TQLineEdit( this, "line_edit_basevalue" );
    line_edit_basevalue->setAlignment( int( TQLineEdit::AlignHCenter ) );
    line_edit_basevalue->setValidator( validLine );

    line_edit_percentvalue = new TQLineEdit( this, "line_edit_percentvalue" );
    line_edit_percentvalue->setAlignment( int( TQLineEdit::AlignHCenter ) );
    line_edit_percentvalue->setValidator( validLine );

    progress_bar_count = new TQProgressBar( this, "progress_bar_count" );
    progress_bar_count->setFrameShape( TQProgressBar::Panel );
    progress_bar_count->setFrameShadow( TQProgressBar::Sunken );
//    progress_bar_count->setFixedWidth( 250 );

    progress_bar_right_wrong = new TQProgressBar( this, "progress_bar_right" );
    progress_bar_right_wrong->setFrameShape( TQProgressBar::Panel );
    progress_bar_right_wrong->setFrameShadow( TQProgressBar::Sunken );
//    progress_bar_right_wrong->setFixedWidth( 250 );
    progress_bar_right_wrong->setPercentageVisible( FALSE );
    progress_bar_right_wrong->setPaletteBackgroundColor( TQColor( "red" ) );

    label_right = new TQLabel( this, "" );
    label_right->setBackgroundPixmap( bgp );
    label_right->setBackgroundOrigin( TQDialog::ParentOrigin );
    label_wrong = new TQLabel( this, "" );
    label_wrong->setBackgroundPixmap( bgp );
    label_wrong->setBackgroundOrigin( TQDialog::ParentOrigin );

    push_button_apply = new TQPushButton( this, "push_button_apply" );
    push_button_apply->setText( KStdGuiItem::apply().text() );
    push_button_apply->setDefault( TRUE );
    push_button_apply->setBackgroundPixmap( bgp);
    push_button_apply->setBackgroundOrigin( TQPushButton::ParentOrigin );

    push_button_cancel = new TQPushButton( this, "push_button_cancel" );
    push_button_cancel->setText( KStdGuiItem::cancel().text() );

    KIconLoader icon_loader;
    push_button_apply->setIconSet( TQIconSet( icon_loader.loadIcon( "button_ok", KIcon::NoGroup, 32 ) ) );
    push_button_cancel->setIconSet( TQIconSet( icon_loader.loadIcon( "button_cancel", KIcon::NoGroup, 32 ) ) );

    label_1 = new TQLabel( this, "label_1" );
    label_1->setText( i18n( " % of " ) );
    label_1->setBackgroundPixmap( bgp );
    label_1->setBackgroundOrigin( TQDialog::ParentOrigin );

    label_2 = new TQLabel( this, "label_2" );
    label_2->setText( i18n( " = " ) );
    label_2->setBackgroundPixmap( bgp );
    label_2->setBackgroundOrigin( TQDialog::ParentOrigin );

    label_number = new TQLabel( this, "label_number" );
    label_number->setBackgroundPixmap( bgp );
    label_number->setBackgroundOrigin( TQDialog::ParentOrigin );
    label_number->setText( i18n( "Task no. MM:" ) ); // for the right width

    label_count = new TQLabel( this, "label_count" );
    label_count->setText( i18n( "You got MM of MM." ) ); // for the right width
    label_count->setBackgroundPixmap( bgp );
    label_count->setBackgroundOrigin( TQDialog::ParentOrigin );

    //////
    // begin layouting
    //////
    TQVBoxLayout *main_layout = new TQVBoxLayout ( this, 0, -1, "main_layout" );
    main_layout->setMargin( 20 );
    main_layout->setSpacing( 10 );

    TQHBoxLayout *label_number_layout = new TQHBoxLayout ( main_layout, -1, "label_number_layout" );
    label_number_layout->addWidget( label_number );
    label_number_layout->addStretch();

    TQHBoxLayout *line_edit_layout = new TQHBoxLayout ( main_layout, -1, "line_edit_layout" );

    line_edit_layout->addWidget( line_edit_percentage );
    line_edit_layout->addWidget( label_1 );
    line_edit_layout->addWidget( line_edit_basevalue );
    line_edit_layout->addWidget( label_2 );
    line_edit_layout->addWidget( line_edit_percentvalue );
    line_edit_layout->addStretch();

    main_layout->addStretch();

    TQHBoxLayout *bottom_layout = new TQHBoxLayout( main_layout, -1, "bottom_layout" );

    TQVBoxLayout *current_state_layout = new TQVBoxLayout ( bottom_layout, -1, "current_state_layout" );

    TQHBoxLayout *label_count_layout = new TQHBoxLayout ( current_state_layout, -1, "label_count_layout" );
    label_count_layout->addWidget( label_count );
    label_count_layout->addStretch();

    current_state_layout->addSpacing( 10 );
    current_state_layout->addWidget( progress_bar_count );

    TQHBoxLayout *right_wrong_layout = new TQHBoxLayout( current_state_layout, -1, "right_wrong_layout" );

    right_wrong_layout->addWidget( label_right );
    right_wrong_layout->addWidget( progress_bar_right_wrong );
    right_wrong_layout->addWidget( label_wrong );

    bottom_layout->addSpacing( 40 );

    TQVBoxLayout *button_layout = new TQVBoxLayout ( bottom_layout, -1, "button_layout" );

    button_layout->addWidget( push_button_apply );
    button_layout->addSpacing( 10 );
    button_layout->addWidget( push_button_cancel );

    // END LAYOUT
    // --------------------------------------------------------

    // tab order
    setTabOrder( line_edit_percentvalue, push_button_apply );
    setTabOrder( push_button_apply, push_button_cancel );

	///////////
	// ToolTips

	TQToolTip::add( progress_bar_count, i18n( "Number of managed exercises" ) );
	TQToolTip::add( progress_bar_right_wrong, i18n( "Relation of right to wrong inputs" ) );
	TQToolTip::add( push_button_apply, i18n( "Check your answer" ) );
	TQToolTip::add( push_button_cancel, i18n( "Back to the main window" ) );

    // signals and slots connections
    connect( push_button_apply, TQT_SIGNAL( clicked() ), this, TQT_SLOT( slotApplyInput() ) );
    connect( push_button_cancel, TQT_SIGNAL( clicked() ), this, TQT_SLOT( accept() ) );

    // build up the answer window
    answer = new KAnswer( this );


    /** this is to tell rand() to be randomizing */
    srandom( time( 0 ) );

}

/** No descriptions */
void KPercentMain::newTask()
{
    // no Editline can be focused
    line_edit_percentage->setFocusPolicy( NoFocus );
    line_edit_basevalue->setFocusPolicy( NoFocus );
    line_edit_percentvalue->setFocusPolicy( NoFocus );

    // set the number of managed tasks into a label
    label_count->setText( TQString( i18n("You got %1 of %2 exercises.").arg(count).arg(number) ) ); // update Label for the Progress

//    progress_bar_count->setProgress( count*100 ); // updating Progrssbar
    startTimer( 10 );
    // set string which task is just workin on (Task x of y)
    TQString taskNumber( i18n( "Exercise no. %1:" ).arg(count+1) );

    label_number->setText( taskNumber );
    //    label_number->adjustSize();

    int selection; // which task is to be solved ?
     if ( count == number )  // all tasks managed ?
     {
//         finished();
     }
    else
    {
        if ( selected_type == SEL_RANDOM )
        {
            selection = rand() % 3;  // select one task by random
        }
        else
        {
            selection = selected_type; // or take the preselected one
        }
        // set the right line_edit_layout for input
        switch ( selection )
        {
        case SEL_PERCENTAGE:
            line_edit_input = line_edit_percentage;
            break;
        case SEL_BASEVALUE:
            line_edit_input = line_edit_basevalue;
            break;
        case SEL_PERCENTVALUE:
            line_edit_input = line_edit_percentvalue;
            break;
        }
        // show the numbers
        showNumbers();
        // delete this one
        line_edit_input->clear();
        // make the line_edit_layout accessable
        line_edit_input->setFocusPolicy( StrongFocus );
        // set the focus on it
        line_edit_input->setFocus();
    }
}

/** checks, if the task is solved right */
bool KPercentMain::validateTask()
{
    // here is some mathematics :)
    return ( getPercentage() * getBasevalue() == 100 * getPercentvalue() );
}

/** gets the numbers from the line_edit_layouts */
int KPercentMain::getPercentage()
{
    return line_edit_percentage->text().toInt(); // check for valid input is omitted (slotApplyInput)
}
int KPercentMain::getBasevalue()
{
    return line_edit_basevalue->text().toInt(); // check for valid input is omitted (slotApplyInput)
}
int KPercentMain::getPercentvalue()
{
    return line_edit_percentvalue->text().toInt(); // check for valid input is omitted (slotApplyInput)
}

/* checks, if the input is a val. number
 * calls validateTask then and if succeeds causes a newTask */
void KPercentMain::slotApplyInput()
{
    bool ok; // conversion to int successful ?
    line_edit_input->text().toInt( &ok );
    if ( !ok )
    {
        showAnswer( 3 );
    }
    else
    {
        if ( validateTask() )  // are the numbers right ?
        {
            showAnswer( 1 );
            count++; // one more solved
            right_answers++; // one more right answer
            total_answers++;
            progress_bar_right_wrong->setTotalSteps( total_answers );
            progress_bar_right_wrong->setProgress( right_answers );
            label_right->setText( i18n( "%1%\nright" ).arg( (100*right_answers)/total_answers ) );
            label_wrong->setText( i18n( "%1%\nwrong" ).arg( 100-(100*right_answers)/total_answers ) );

            newTask(); // next task, perhaps
        }
        else
        {
            showAnswer( 2 );
            wrong_answers++; // one more right answer
            total_answers++;
            progress_bar_right_wrong->setTotalSteps( total_answers );
            progress_bar_right_wrong->setProgress( right_answers );
            label_right->setText( i18n( "%1%\nright" ).arg( (100*right_answers)/total_answers ) );
            label_wrong->setText( i18n( "%1%\nwrong" ).arg( 100-(100*right_answers)/total_answers ) );
        }
    }
}

/** inserts the numbers of a new task in the line_edit_layouts */
void KPercentMain::showNumbers()
{
    line_edit_percentage->setText( TQString::number( percentage[ count ] ) );
    line_edit_basevalue->setText( TQString::number( basevalue[ count ] ) );
    line_edit_percentvalue->setText( TQString::number( percentvalue[ count ] ) );
}

/** called, if number tasks are managed */
void KPercentMain::finished()
{
    showAnswer( 4 );
    accept(); // hides the dialog
}
/** shows the kanswer dialog modal with an proper answer about the success. */
void KPercentMain::showAnswer( int modus )
{
    answer->setAnswer( modus );
    answer->exec();
}
/** sets the total amount of tasks selekted in opening screen */
void KPercentMain::setNumber( int anumber )
{
    number = anumber;
    progress_bar_count->setTotalSteps( number*100 );
}

/** Prepares the exercise of number tasks */
void KPercentMain::initExercise()
{
    count = 0;
    progress_bar_count->setProgress( 0 );
    total_answers = 0;
    right_answers = 0;
    wrong_answers = 0;

    progress_bar_right_wrong->setProgress( 0 );
    label_right->setText( "" );
    label_wrong->setText( "" );
    switch ( selected_level )
    {
        /** easy level */
    case 0:
        {
       	    /** easy percentages to calculate with */
            const int values[] = { 1, 10, 25, 50, 75, 100, 10, 25, 50, 75};
            ::memcpy(percentage, values, sizeof(values));
		}


		int num;
        if ( number < 6 )    /* for only few tasks, chose from the first 6 numbers */
        {
            num = 6;
        }
        else
        {
            num = 10;     /* else take them all! */
        }
        /** swap 20 times in the array */
        for ( int i = 0; i < 20; i++ )
        {
            int m = rand() % num;
            int n = rand() % num;
            if ( n == m )  /* not a swap */
            {
                n = ( n + 1 ) % num;
            }
            int h = percentage[ m ];
            percentage[ m ] = percentage[ n ];
            percentage[ n ] = h;
        }
        for ( int i = 0; i < number; i++ )
        {
            /** basevalue 100..2000 by chance */
            basevalue[ i ] = 100 * ( 1 + rand() % 20 );
            /** calc percentvalue */
            percentvalue[ i ] = percentage[ i ] * basevalue[ i ] / 100;
        }
        break;
    case 1:
        {
            /** some more tricky percentages */
            const int values[] = { 0, 5, 10, 20, 60, 90, 100, 110, 150, 200};
            ::memcpy(percentage, values, sizeof(values));
	    }
		/** swap some times in this array */
        for ( int i = 0; i < 20; i++ )
        {
            int m = rand() % 10;
            int n = rand() % 10;
            if ( n == m )  /** not a swap */
            {
                n = ( n + 1 ) % 10;
            }
            int h = percentage[ m ];
            percentage[ m ] = percentage[ n ];
            percentage[ n ] = h;
        }
        for ( int i = 0; i < number; i++ )
        {
            /** basevalue 0..2000 */
            basevalue[ i ] = 100 * ( rand() % 21 );
            /** calc percentvalue */
            percentvalue[ i ] = percentage[ i ] * basevalue[ i ] / 100;
        }
        break;
    case 2:
        int factor;
        for ( int i = 0; i < number; i++ )
        {
            /** this lines give you 3 integer by chance. percentage will be somewhere
              * between 0 and 200. We use factorisation to have "almost" random numbers
              * and _always_ valid numbers. */
            factor = 1;
            if ( rand() % 2 == 1 )
                factor *= 2;
            if ( rand() % 2 == 1 )
                factor *= 2;
            if ( rand() % 2 == 1 )
                factor *= 5;
            if ( rand() % 2 == 1 )
                factor *= 5;
            percentage[ i ] = rand() % ( 200 / factor );
            basevalue[ i ] = rand() % ( 10 * factor );
            percentvalue[ i ] = percentage[ i ] * basevalue[ i ];
            percentage[ i ] *= factor;
            basevalue[ i ] *= 100 / factor;
        }
        break;
    }
    newTask(); /** prepare the window for the first task */
}

void KPercentMain::timerEvent( TQTimerEvent *event )
{
    if( progress_bar_count->progress()<count*100 )
    {
        progress_bar_count->setProgress(progress_bar_count->progress()+1);
    }
    else
    {
        killTimer( event->timerId() );
    }
    if( progress_bar_count->progress()==number*100 ) finished();
}

void KPercentMain::keyPressEvent( TQKeyEvent * e )
{
	 if ( e->key() == Key_Enter ) slotApplyInput();
    else
        KDialog::keyPressEvent( e );
}

#include "kpercentmain.moc"