• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdegames
 

libtdegames

  • libtdegames
  • highscore
kscoredialog.cpp
1/****************************************************************
2Copyright (c) 1998 Sandro Sigala <ssigala@globalnet.it>.
3Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
4All rights reserved.
5
6Permission to use, copy, modify, and distribute this software
7and its documentation for any purpose and without fee is hereby
8granted, provided that the above copyright notice appear in all
9copies and that both that the copyright notice and this
10permission notice and warranty disclaimer appear in supporting
11documentation, and that the name of the author not be used in
12advertising or publicity pertaining to distribution of the
13software without specific, written prior permission.
14
15The author disclaim all warranties with regard to this
16software, including all implied warranties of merchantability
17and fitness. In no event shall the author be liable for any
18special, indirect or consequential damages or any damages
19whatsoever resulting from loss of use, data or profits, whether
20in an action of contract, negligence or other tortious action,
21arising out of or in connection with the use or performance of
22this software.
23****************************************************************/
24
25#include "config.h"
26
27#include <tqlabel.h>
28#include <tqlayout.h>
29#include <tqlineedit.h>
30#include <tqwidgetstack.h>
31#include <tqtimer.h>
32#include <tqevent.h>
33#include <tqptrvector.h>
34
35#include <tdeapplication.h>
36#include <tdeconfig.h>
37#include <tdelocale.h>
38#include <kseparator.h>
39
40#include "kscoredialog.h"
41
42class KScoreDialog::KScoreDialogPrivate
43{
44public:
45 TQPtrList<FieldInfo> scores;
46 TQWidget *page;
47 TQGridLayout *layout;
48 TQLineEdit *edit;
49 TQPtrVector<TQWidgetStack> stack;
50 TQPtrVector<TQLabel> labels;
51 TQLabel *commentLabel;
52 TQString comment;
53 int fields;
54 int newName;
55 int latest;
56 int nrCols;
57 bool loaded;
58 TQString configGroup;
59
60 TQMap<int, int> col;
61 TQMap<int, TQString> header;
62 TQMap<int, TQString> key;
63 TQString player;
64};
65
66
67KScoreDialog::KScoreDialog(int fields, TQWidget *parent, const char *oname)
68 : KDialogBase(parent, oname, true, i18n("High Scores"), Ok, Ok, true)
69{
70 d = new KScoreDialogPrivate();
71 d->edit = 0;
72 d->fields = fields;
73 d->newName = -1;
74 d->latest = -1;
75 d->loaded = false;
76 d->nrCols = 0;
77 d->configGroup = "High Score";
78
79 d->scores.setAutoDelete(true);
80 d->header[Name] = i18n("Name");
81 d->key[Name] = "Name";
82
83 d->header[Date] = i18n("Date");
84 d->key[Date] = "Date";
85
86 d->header[Level] = i18n("Level");
87 d->key[Level] = "Level";
88
89 d->header[Score] = i18n("Score");
90 d->key[Score] = "Score";
91 d->page = makeMainWidget();
92
93 connect(this, TQ_SIGNAL(okClicked()), TQ_SLOT(slotGotName()));
94}
95
96KScoreDialog::~KScoreDialog()
97{
98 delete d;
99}
100
101void KScoreDialog::setConfigGroup(const TQString &group)
102{
103 d->configGroup = group;
104 d->loaded = false;
105}
106
107void KScoreDialog::setComment(const TQString &comment)
108{
109 d->comment = comment;
110}
111
112void KScoreDialog::addField(int field, const TQString &header, const TQString &key)
113{
114 d->fields |= field;
115 d->header[field] = header;
116 d->key[field] = key;
117}
118
119void KScoreDialog::setupDialog()
120{
121 d->nrCols = 1;
122
123 for(int field = 1; field < d->fields; field = field * 2)
124 {
125 if (d->fields & field)
126 d->col[field] = d->nrCols++;
127 }
128
129 d->layout = new TQGridLayout(d->page, 15, d->nrCols, marginHint() + 20, spacingHint());
130 d->layout->addRowSpacing(4, 15);
131
132 d->commentLabel = new TQLabel(d->page);
133 d->commentLabel->setAlignment(AlignVCenter | AlignHCenter);
134 d->layout->addMultiCellWidget(d->commentLabel, 1, 1, 0, d->nrCols-1);
135
136 TQFont bold = font();
137 bold.setBold(true);
138
139 TQLabel *label;
140 d->layout->addColSpacing(0, 50);
141 label = new TQLabel(i18n("Rank"), d->page);
142 d->layout->addWidget(label, 3, 0);
143 label->setFont(bold);
144
145 for(int field = 1; field < d->fields; field = field * 2)
146 {
147 if (d->fields & field)
148 {
149 d->layout->addColSpacing(d->col[field], 50);
150
151 label = new TQLabel(d->header[field], d->page);
152 d->layout->addWidget(label, 3, d->col[field], field <= Name ? AlignLeft : AlignRight);
153 label->setFont(bold);
154 }
155 }
156
157 KSeparator *sep = new KSeparator(TQt::Horizontal, d->page);
158 d->layout->addMultiCellWidget(sep, 4, 4, 0, d->nrCols-1);
159
160 d->labels.resize(d->nrCols * 10);
161 d->stack.resize(10);
162
163 TQString num;
164 for (int i = 1; i <= 10; ++i) {
165 TQLabel *label;
166 num.setNum(i);
167 label = new TQLabel(i18n("#%1").arg(num), d->page);
168 d->labels.insert((i-1)*d->nrCols + 0, label);
169 d->layout->addWidget(label, i+4, 0);
170 if (d->fields & Name)
171 {
172 TQWidgetStack *stack = new TQWidgetStack(d->page);
173 d->stack.insert(i-1, stack);
174 d->layout->addWidget(stack, i+4, d->col[Name]);
175 label = new TQLabel(d->page);
176 d->labels.insert((i-1)*d->nrCols + d->col[Name], label);
177 stack->addWidget(label);
178 stack->raiseWidget(label);
179 }
180 for(int field = Name * 2; field < d->fields; field = field * 2)
181 {
182 if (d->fields & field)
183 {
184 label = new TQLabel(d->page);
185 d->labels.insert((i-1)*d->nrCols + d->col[field], label);
186 d->layout->addWidget(label, i+4, d->col[field], AlignRight);
187 }
188 }
189 }
190}
191
192void KScoreDialog::aboutToShow()
193{
194 if (!d->loaded)
195 loadScores();
196
197 if (!d->nrCols)
198 setupDialog();
199
200 d->commentLabel->setText(d->comment);
201 if (d->comment.isEmpty())
202 {
203 d->commentLabel->setMinimumSize(TQSize(1,1));
204 d->commentLabel->hide();
205 d->layout->addRowSpacing(0, -15);
206 d->layout->addRowSpacing(2, -15);
207 }
208 else
209 {
210 d->commentLabel->setMinimumSize(d->commentLabel->sizeHint());
211 d->commentLabel->show();
212 d->layout->addRowSpacing(0, -10);
213 d->layout->addRowSpacing(2, 10);
214 }
215 d->comment = TQString();
216
217 TQFont normal = font();
218 TQFont bold = normal;
219 bold.setBold(true);
220
221 TQString num;
222 for (int i = 1; i <= 10; ++i) {
223 TQLabel *label;
224 num.setNum(i);
225 FieldInfo *score = d->scores.at(i-1);
226 label = d->labels[(i-1)*d->nrCols + 0];
227 if (i == d->latest)
228 label->setFont(bold);
229 else
230 label->setFont(normal);
231
232 if (d->fields & Name)
233 {
234 if (d->newName == i)
235 {
236 TQWidgetStack *stack = d->stack[i-1];
237 d->edit = new TQLineEdit(d->player, stack);
238 d->edit->setMinimumWidth(40);
239 stack->addWidget(d->edit);
240 stack->raiseWidget(d->edit);
241 d->edit->setFocus();
242 connect(d->edit, TQ_SIGNAL(returnPressed()),
243 this, TQ_SLOT(slotGotReturn()));
244 }
245 else
246 {
247 label = d->labels[(i-1)*d->nrCols + d->col[Name]];
248 if (i == d->latest)
249 label->setFont(bold);
250 else
251 label->setFont(normal);
252 label->setText((*score)[Name]);
253 }
254
255 }
256 for(int field = Name * 2; field < d->fields; field = field * 2)
257 {
258 if (d->fields & field)
259 {
260 label = d->labels[(i-1)*d->nrCols + d->col[field]];
261 if (i == d->latest)
262 label->setFont(bold);
263 else
264 label->setFont(normal);
265 label->setText((*score)[field]);
266 }
267 }
268 }
269 d->latest = -1;
270 setFixedSize(minimumSizeHint());
271}
272
273void KScoreDialog::loadScores()
274{
275 TQString key, value;
276 d->loaded = true;
277 d->scores.clear();
278 TDEConfigGroup config(kapp->config(), d->configGroup.utf8());
279
280 d->player = config.readEntry("LastPlayer");
281
282 TQString num;
283 for (int i = 1; i <= 10; ++i) {
284 num.setNum(i);
285 FieldInfo *score = new FieldInfo();
286 for(int field = 1; field < d->fields; field = field * 2)
287 {
288 if (d->fields & field)
289 {
290 key = "Pos" + num + d->key[field];
291 (*score)[field] = config.readEntry(key, "-");
292 }
293 }
294 d->scores.append(score);
295 }
296}
297
298void KScoreDialog::saveScores()
299{
300 TQString key, value;
301 TDEConfigGroup config(kapp->config(), d->configGroup.utf8());
302
303 config.writeEntry("LastPlayer", d->player);
304
305 TQString num;
306 for (int i = 1; i <= 10; ++i) {
307 num.setNum(i);
308 FieldInfo *score = d->scores.at(i-1);
309 for(int field = 1; field < d->fields; field = field * 2)
310 {
311 if (d->fields & field)
312 {
313 key = "Pos" + num + d->key[field];
314 config.writeEntry(key, (*score)[field]);
315 }
316 }
317 }
318 kapp->config()->sync();
319}
320
321int KScoreDialog::addScore(int newScore, const FieldInfo &newInfo, bool askName)
322{
323 return addScore(newScore, newInfo, askName, false);
324}
325
326int KScoreDialog::addScore(int newScore, const FieldInfo &newInfo, bool askName, bool lessIsMore)
327{
328 if (!d->loaded)
329 loadScores();
330 FieldInfo *score = d->scores.first();
331 int i = 1;
332 for(; score; score = d->scores.next(), i++)
333 {
334 bool ok;
335 int num_score = (*score)[Score].toLong(&ok);
336 if (lessIsMore && !ok)
337 num_score = 1 << 30;
338 if (((newScore > num_score) && !lessIsMore) ||
339 ((newScore < num_score) && lessIsMore))
340 {
341 score = new FieldInfo(newInfo);
342 (*score)[Score].setNum(newScore);
343 d->scores.insert(i-1, score);
344 d->scores.remove(10);
345 d->latest = i;
346 if (askName)
347 d->newName = i;
348 else
349 saveScores();
350 if (i == 1)
351 d->comment = i18n("Excellent!\nYou have a new high score!");
352 else
353 d->comment = i18n("Well done!\nYou made it to the high score list!");
354 return i;
355 }
356 }
357 return 0;
358}
359
360void KScoreDialog::show()
361{
362 aboutToShow();
363 KDialogBase::show();
364}
365
366void KScoreDialog::slotGotReturn()
367{
368 TQTimer::singleShot(0, this, TQ_SLOT(slotGotName()));
369}
370
371void KScoreDialog::slotGotName()
372{
373 if (d->newName == -1) return;
374
375 d->player = d->edit->text();
376
377 (*d->scores.at(d->newName-1))[Name] = d->player;
378 saveScores();
379
380 TQFont bold = font();
381 bold.setBold(true);
382
383 TQLabel *label = d->labels[(d->newName-1)*d->nrCols + d->col[Name]];
384 label->setFont(bold);
385 label->setText(d->player);
386 d->stack[(d->newName-1)]->raiseWidget(label);
387 delete d->edit;
388 d->edit = 0;
389 d->newName = -1;
390}
391
392int KScoreDialog::highScore()
393{
394 if (!d->loaded)
395 loadScores();
396
397 return (*d->scores.first())[Score].toInt();
398}
399
400void KScoreDialog::keyPressEvent( TQKeyEvent *ev)
401{
402 if ((d->newName != -1) && (ev->key() == Key_Return))
403 {
404 ev->ignore();
405 return;
406 }
407 KDialogBase::keyPressEvent(ev);
408}
409
410
411#include "kscoredialog.moc"
KScoreDialog::KScoreDialog
KScoreDialog(int fields, TQWidget *parent=0, const char *name=0)
Definition: kscoredialog.cpp:67
KScoreDialog::highScore
int highScore()
Returns the current best score.
Definition: kscoredialog.cpp:392
KScoreDialog::addField
void addField(int field, const TQString &header, const TQString &key)
Define an extra FieldInfo entry.
Definition: kscoredialog.cpp:112
KScoreDialog::addScore
int addScore(int newScore, const FieldInfo &newInfo, bool askName, bool lessIsMore)
Adds a new score to the list.
Definition: kscoredialog.cpp:326
KScoreDialog::setComment
void setComment(const TQString &comment)
Definition: kscoredialog.cpp:107
KScoreDialog::setConfigGroup
void setConfigGroup(const TQString &group)
Definition: kscoredialog.cpp:101

libtdegames

Skip menu "libtdegames"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libtdegames

Skip menu "libtdegames"
  • libtdegames
Generated for libtdegames by doxygen 1.9.4
This website is maintained by Timothy Pearson.