summaryrefslogtreecommitdiff
path: root/noncore/games/tetrix/ohighscoredlg.cpp
blob: 504385fe8f1b1190028e4563e2d8b9cc494a52a7 (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
/***************************************************************************
   begin                    : January 2003
   copyright                : ( C ) 2003 by Carsten Niehaus
   email                    : cniehaus@handhelds.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.                                   *
 *                                                                         *
 **************************************************************************/

#include <qdialog.h>
#include <qlayout.h>
#include <qpe/qpeapplication.h>
#include <qpe/config.h>

#include <qstring.h>
#include <qlist.h>
#include <qhbox.h>
#include <qvbox.h>
#include <qlabel.h>
#include <qlistview.h>
#include <qlineedit.h>

#include "ohighscoredlg.h"

OHighscore::OHighscore( int score , int playerLevel ) : playerData()
{
	pLevel = playerLevel;
	getList();
	checkIfItIsANewhighscore( score );
	playerData.setAutoDelete( TRUE );
}

OHighscore::~OHighscore()
{
}

void OHighscore::getList()
{
	Config cfg ( "tetrix" );
	cfg.setGroup( QString::number( 1 ) );
	lowest = cfg.readNumEntry( "Points" );
	playerData.clear();

	int rest = 1;	//for the filling up later

	for ( int i = 1 ; i < 11 ; i++ )
	{
		if ( cfg.hasGroup( QString::number( i ) ) )
		{
			cfg.setGroup( QString::number( i ) );
			int temp = cfg.readNumEntry( "Points" );

			t_playerData *pPlayerData = new t_playerData;
			pPlayerData->sName = cfg.readEntry( "Name" );
			pPlayerData->points = temp;
			pPlayerData->level = cfg.readNumEntry( "Level" );

			playerData.append( pPlayerData );

			if ( (temp < lowest) ) lowest = temp;
			rest++;
		}
	}

	//now I fill up the rest of the list
	if ( rest < 11 ) //only go in this loop if there are less than
					 //10 highscoreentries
	{
		lowest = 0;
		for ( ; rest < 11 ; rest++ )
		{
			t_playerData *pPlayerData = new t_playerData;
			pPlayerData->sName = tr( "empty");
			pPlayerData->points = 0;
			pPlayerData->level = 0;

			playerData.append( pPlayerData );
		}
	}

}

void OHighscore::checkIfItIsANewhighscore( int points)
{
	if ( points > lowest )
		isNewhighscore = true; 
	else 
		isNewhighscore = false;
}

void OHighscore::insertData( QString name , int punkte , int playerLevel )
{
	Config cfg ( "tetrix" );
	t_playerData * Run;
	int index = 0;
	int entryNumber = 1;

        for ( Run=playerData.first(); 
              Run != 0; 
              index ++, Run=playerData.next() ) {

		if ( punkte > Run->points )
		{
			t_playerData* temp = new t_playerData;
			temp->sName = name;
			temp->points = punkte;
			temp->level = playerLevel;

			playerData.insert( index, temp );
			
			//now we have to delete the last entry
			playerData.remove( playerData.count() );
			
		/////////////////////////////////////////
		//this block just rewrites the highscore
                        for ( t_playerData * Run2=playerData.first(); 
                              Run2 != 0; 
                              Run2=playerData.next() ) {
				cfg.setGroup( QString::number( entryNumber ) );
				cfg.writeEntry( "Name" , Run2->sName );
				cfg.writeEntry( "Points" , Run2->points );
				cfg.writeEntry( "Level" , Run2->level );
				entryNumber++;	
			}
		////////////////////////////////////////	

			return;
		}
	} 
}

QString OHighscore::getName()
{
	QString name;
	QDialog *d = new QDialog ( this, 0, true );
	d->setCaption( tr( "Enter your name!" ));
	QLineEdit *ed = new QLineEdit ( d );
	( new QVBoxLayout ( d, 3, 3 ))->addWidget ( ed );
	ed->setFocus ( );

    if ( d->exec() == QDialog::Accepted ) {
        name = ed->text();
    }
    //delete d;
	return name;
}

OHighscoreDialog::OHighscoreDialog(OHighscore *highscore, QWidget *parent, const char *name, bool modal) : QDialog(parent, name, modal)
{
	hs_ = highscore;
	setCaption( tr( "Highscores" ) );
	vbox_layout = new QVBoxLayout( this, 4 , 4 );
	list = new QListView( this );
	list->setSorting( -1 );
	list->addColumn( tr( "#" ));
	list->addColumn( tr( "Name" ));
	list->addColumn( tr( "Points" ));
	list->addColumn( tr( "Level" ));

	createHighscoreListView();
	
	vbox_layout->addWidget( list );
	QPEApplication::showDialog( this );
}

void OHighscoreDialog::createHighscoreListView()
{
	int pos = 10;
	int points_ = 0;
	int level_ = 0;

        for ( t_playerData * Run = hs_->playerData.first(); 
              Run != 0; 
              Run=hs_->playerData.next() )
        {
                QListViewItem *item;

                item = new QListViewItem( list );

		item->setText(  0 , QString::number( pos ) );                   //number
		item->setText(  1 , Run->sName );                       //name
		if ( Run->points  == -1 )
			points_ = 0;
		else points_ =  Run->points;
		if ( Run->level  == -1 )
                    level_ = 0;
		else level_ =  Run->level;
		item->setText(  2 , QString::number( points_ ) );   //points
		item->setText(  3 , QString::number( level_ ) );    //level
		pos--;
	}
}