29#include <tqlineedit.h>
30#include <tqwidgetstack.h>
33#include <tqptrvector.h>
35#include <tdeapplication.h>
38#include <kseparator.h>
40#include "kscoredialog.h"
42class KScoreDialog::KScoreDialogPrivate
45 TQPtrList<FieldInfo> scores;
49 TQPtrVector<TQWidgetStack> stack;
50 TQPtrVector<TQLabel> labels;
51 TQLabel *commentLabel;
61 TQMap<int, TQString> header;
62 TQMap<int, TQString> key;
68 : KDialogBase(parent, oname, true, i18n(
"High Scores"), Ok, Ok, true)
70 d =
new KScoreDialogPrivate();
77 d->configGroup =
"High Score";
79 d->scores.setAutoDelete(
true);
80 d->header[Name] = i18n(
"Name");
81 d->key[Name] =
"Name";
83 d->header[Date] = i18n(
"Date");
84 d->key[Date] =
"Date";
86 d->header[Level] = i18n(
"Level");
87 d->key[Level] =
"Level";
89 d->header[Score] = i18n(
"Score");
90 d->key[Score] =
"Score";
91 d->page = makeMainWidget();
93 connect(
this, TQ_SIGNAL(okClicked()), TQ_SLOT(slotGotName()));
96KScoreDialog::~KScoreDialog()
103 d->configGroup = group;
109 d->comment = comment;
115 d->header[field] = header;
119void KScoreDialog::setupDialog()
123 for(
int field = 1; field < d->fields; field = field * 2)
125 if (d->fields & field)
126 d->col[field] = d->nrCols++;
129 d->layout =
new TQGridLayout(d->page, 15, d->nrCols, marginHint() + 20, spacingHint());
130 d->layout->addRowSpacing(4, 15);
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);
136 TQFont bold = font();
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);
145 for(
int field = 1; field < d->fields; field = field * 2)
147 if (d->fields & field)
149 d->layout->addColSpacing(d->col[field], 50);
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);
157 KSeparator *sep =
new KSeparator(TQt::Horizontal, d->page);
158 d->layout->addMultiCellWidget(sep, 4, 4, 0, d->nrCols-1);
160 d->labels.resize(d->nrCols * 10);
164 for (
int i = 1; i <= 10; ++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)
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);
180 for(
int field = Name * 2; field < d->fields; field = field * 2)
182 if (d->fields & field)
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);
192void KScoreDialog::aboutToShow()
200 d->commentLabel->setText(d->comment);
201 if (d->comment.isEmpty())
203 d->commentLabel->setMinimumSize(TQSize(1,1));
204 d->commentLabel->hide();
205 d->layout->addRowSpacing(0, -15);
206 d->layout->addRowSpacing(2, -15);
210 d->commentLabel->setMinimumSize(d->commentLabel->sizeHint());
211 d->commentLabel->show();
212 d->layout->addRowSpacing(0, -10);
213 d->layout->addRowSpacing(2, 10);
215 d->comment = TQString();
217 TQFont normal = font();
218 TQFont bold = normal;
222 for (
int i = 1; i <= 10; ++i) {
225 FieldInfo *score = d->scores.at(i-1);
226 label = d->labels[(i-1)*d->nrCols + 0];
228 label->setFont(bold);
230 label->setFont(normal);
232 if (d->fields & Name)
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);
242 connect(d->edit, TQ_SIGNAL(returnPressed()),
243 this, TQ_SLOT(slotGotReturn()));
247 label = d->labels[(i-1)*d->nrCols + d->col[Name]];
249 label->setFont(bold);
251 label->setFont(normal);
252 label->setText((*score)[Name]);
256 for(
int field = Name * 2; field < d->fields; field = field * 2)
258 if (d->fields & field)
260 label = d->labels[(i-1)*d->nrCols + d->col[field]];
262 label->setFont(bold);
264 label->setFont(normal);
265 label->setText((*score)[field]);
270 setFixedSize(minimumSizeHint());
273void KScoreDialog::loadScores()
278 TDEConfigGroup config(kapp->config(), d->configGroup.utf8());
280 d->player = config.readEntry(
"LastPlayer");
283 for (
int i = 1; i <= 10; ++i) {
285 FieldInfo *score =
new FieldInfo();
286 for(
int field = 1; field < d->fields; field = field * 2)
288 if (d->fields & field)
290 key =
"Pos" + num + d->key[field];
291 (*score)[field] = config.readEntry(key,
"-");
294 d->scores.append(score);
298void KScoreDialog::saveScores()
301 TDEConfigGroup config(kapp->config(), d->configGroup.utf8());
303 config.writeEntry(
"LastPlayer", d->player);
306 for (
int i = 1; i <= 10; ++i) {
308 FieldInfo *score = d->scores.at(i-1);
309 for(
int field = 1; field < d->fields; field = field * 2)
311 if (d->fields & field)
313 key =
"Pos" + num + d->key[field];
314 config.writeEntry(key, (*score)[field]);
318 kapp->config()->sync();
323 return addScore(newScore, newInfo, askName,
false);
330 FieldInfo *score = d->scores.first();
332 for(; score; score = d->scores.next(), i++)
335 int num_score = (*score)[Score].toLong(&ok);
336 if (lessIsMore && !ok)
338 if (((newScore > num_score) && !lessIsMore) ||
339 ((newScore < num_score) && lessIsMore))
341 score =
new FieldInfo(newInfo);
342 (*score)[Score].setNum(newScore);
343 d->scores.insert(i-1, score);
344 d->scores.remove(10);
351 d->comment = i18n(
"Excellent!\nYou have a new high score!");
353 d->comment = i18n(
"Well done!\nYou made it to the high score list!");
360void KScoreDialog::show()
366void KScoreDialog::slotGotReturn()
368 TQTimer::singleShot(0,
this, TQ_SLOT(slotGotName()));
371void KScoreDialog::slotGotName()
373 if (d->newName == -1)
return;
375 d->player = d->edit->text();
377 (*d->scores.at(d->newName-1))[Name] = d->player;
380 TQFont bold = font();
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);
397 return (*d->scores.first())[Score].toInt();
400void KScoreDialog::keyPressEvent( TQKeyEvent *ev)
402 if ((d->newName != -1) && (ev->key() == Key_Return))
407 KDialogBase::keyPressEvent(ev);
411#include "kscoredialog.moc"
KScoreDialog(int fields, TQWidget *parent=0, const char *name=0)
int highScore()
Returns the current best score.
void addField(int field, const TQString &header, const TQString &key)
Define an extra FieldInfo entry.
int addScore(int newScore, const FieldInfo &newInfo, bool askName, bool lessIsMore)
Adds a new score to the list.
void setComment(const TQString &comment)
void setConfigGroup(const TQString &group)