summaryrefslogtreecommitdiffstats
path: root/libkdegames/highscore/kscoredialog.cpp
blob: 0a3710f4b8994cd70db7003ccaa1dbbd06611d3b (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
/****************************************************************
Copyright (c) 1998 Sandro Sigala <ssigala@globalnet.it>.
Copyright (c) 2001 Waldo Bastian <bastian@kde.org>
All rights reserved.

Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness.  In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
****************************************************************/

#include "config.h"

#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlineedit.h>
#include <tqwidgetstack.h>
#include <tqtimer.h>
#include <tqevent.h>
#include <tqptrvector.h>

#include <kapplication.h>
#include <kconfig.h>
#include <klocale.h>
#include <kseparator.h>

#include "kscoredialog.h"

class KScoreDialog::KScoreDialogPrivate
{ 
public:  
   TQPtrList<FieldInfo> scores;
   TQWidget *page;
   TQGridLayout *tqlayout;
   TQLineEdit *edit;
   TQPtrVector<TQWidgetStack> stack;
   TQPtrVector<TQLabel> labels;
   TQLabel *commentLabel;
   TQString comment;
   int fields;
   int newName;
   int latest;
   int nrCols;
   bool loaded;
   TQString configGroup;
   
   TQMap<int, int> col;
   TQMap<int, TQString> header;
   TQMap<int, TQString> key;
   TQString player;
};


KScoreDialog::KScoreDialog(int fields, TQWidget *tqparent, const char *oname)
        : KDialogBase(tqparent, oname, true, i18n("High Scores"), Ok, Ok, true)
{
   d = new KScoreDialogPrivate();
   d->edit = 0;
   d->fields = fields;
   d->newName = -1;
   d->latest = -1;
   d->loaded = false;
   d->nrCols = 0;
   d->configGroup = "High Score";
   
   d->scores.setAutoDelete(true);
   d->header[Name] = i18n("Name");
   d->key[Name] = "Name";
   
   d->header[Date] = i18n("Date");
   d->key[Date] = "Date";
   
   d->header[Level] = i18n("Level");
   d->key[Level] = "Level";
   
   d->header[Score] = i18n("Score");
   d->key[Score] = "Score";
   d->page = makeMainWidget();
   
   connect(this, TQT_SIGNAL(okClicked()), TQT_SLOT(slotGotName()));
}

KScoreDialog::~KScoreDialog()
{
   delete d;
}

void KScoreDialog::setConfigGroup(const TQString &group)
{
   d->configGroup = group;
   d->loaded = false;
}

void KScoreDialog::setComment(const TQString &comment)
{
   d->comment = comment;
}

void KScoreDialog::addField(int field, const TQString &header, const TQString &key)
{
   d->fields |= field;
   d->header[field] = header;
   d->key[field] = key;
}

void KScoreDialog::setupDialog()
{
   d->nrCols = 1;
   
   for(int field = 1; field < d->fields; field = field * 2)
   {
      if (d->fields & field)
         d->col[field] = d->nrCols++;
   }
      
   d->tqlayout = new TQGridLayout(d->page, 15, d->nrCols, marginHint() + 20, spacingHint());
   d->tqlayout->addRowSpacing(4, 15);

   d->commentLabel = new TQLabel(d->page);
   d->commentLabel->tqsetAlignment(AlignVCenter | AlignHCenter);
   d->tqlayout->addMultiCellWidget(d->commentLabel, 1, 1, 0, d->nrCols-1);

   TQFont bold = font();
   bold.setBold(true);

   TQLabel *label;
   d->tqlayout->addColSpacing(0, 50);
   label = new TQLabel(i18n("Rank"), d->page);
   d->tqlayout->addWidget(label, 3, 0);
   label->setFont(bold);

   for(int field = 1; field < d->fields; field = field * 2)
   {
      if (d->fields & field)
      {
         d->tqlayout->addColSpacing(d->col[field], 50);

         label = new TQLabel(d->header[field], d->page);
         d->tqlayout->addWidget(label, 3, d->col[field], field <= Name ? AlignLeft : AlignRight);
         label->setFont(bold);
      }
   }

   KSeparator *sep = new KSeparator(Qt::Horizontal, d->page);
   d->tqlayout->addMultiCellWidget(sep, 4, 4, 0, d->nrCols-1);

   d->labels.resize(d->nrCols * 10);
   d->stack.resize(10);

   TQString num;
   for (int i = 1; i <= 10; ++i) {
      TQLabel *label;
      num.setNum(i);
      label = new TQLabel(i18n("#%1").tqarg(num), d->page);
      d->labels.insert((i-1)*d->nrCols + 0, label);
      d->tqlayout->addWidget(label, i+4, 0);
      if (d->fields & Name)
      {
         TQWidgetStack *stack = new TQWidgetStack(d->page);
         d->stack.insert(i-1, stack);
         d->tqlayout->addWidget(stack, i+4, d->col[Name]);
         label = new TQLabel(d->page);
         d->labels.insert((i-1)*d->nrCols + d->col[Name], label);
         stack->addWidget(label);
         stack->raiseWidget(label);
      }
      for(int field = Name * 2; field < d->fields; field = field * 2)
      {
         if (d->fields & field)
         {
           label = new TQLabel(d->page);
           d->labels.insert((i-1)*d->nrCols + d->col[field], label);
           d->tqlayout->addWidget(label, i+4, d->col[field], AlignRight);
         }
      }
   }
}

void KScoreDialog::aboutToShow()
{
   if (!d->loaded)
      loadScores();
      
   if (!d->nrCols)
      setupDialog();

   d->commentLabel->setText(d->comment);
   if (d->comment.isEmpty())
   {
      d->commentLabel->setMinimumSize(TQSize(1,1));
      d->commentLabel->hide();    
      d->tqlayout->addRowSpacing(0, -15);
      d->tqlayout->addRowSpacing(2, -15);
   } 
   else
   {
      d->commentLabel->setMinimumSize(d->commentLabel->tqsizeHint());
      d->commentLabel->show();
      d->tqlayout->addRowSpacing(0, -10);
      d->tqlayout->addRowSpacing(2, 10);
   }
   d->comment = TQString();

   TQFont normal = font();
   TQFont bold = normal;
   bold.setBold(true);

   TQString num;
   for (int i = 1; i <= 10; ++i) {
      TQLabel *label;
      num.setNum(i);
      FieldInfo *score = d->scores.at(i-1);
      label = d->labels[(i-1)*d->nrCols + 0];
      if (i == d->latest) 
         label->setFont(bold);
      else
         label->setFont(normal);

      if (d->fields & Name)
      {
         if (d->newName == i)
         {
           TQWidgetStack *stack = d->stack[i-1];
           d->edit = new TQLineEdit(d->player, stack);
           d->edit->setMinimumWidth(40);
           stack->addWidget(d->edit);
           stack->raiseWidget(d->edit);
           d->edit->setFocus();
           connect(d->edit, TQT_SIGNAL(returnPressed()), 
                 this, TQT_SLOT(slotGotReturn()));
         }
         else
         {
           label = d->labels[(i-1)*d->nrCols + d->col[Name]];
           if (i == d->latest) 
             label->setFont(bold);
           else
             label->setFont(normal);
           label->setText((*score)[Name]);
         }
         
      }
      for(int field = Name * 2; field < d->fields; field = field * 2)
      {
         if (d->fields & field)
         {
           label = d->labels[(i-1)*d->nrCols + d->col[field]];
           if (i == d->latest) 
             label->setFont(bold);
           else
             label->setFont(normal);
           label->setText((*score)[field]);
         }
      }
   }
   d->latest = -1;
   setFixedSize(tqminimumSizeHint());
}

void KScoreDialog::loadScores()
{
   TQString key, value;
   d->loaded = true;
   d->scores.clear();
   KConfigGroup config(kapp->config(), d->configGroup.utf8());

   d->player = config.readEntry("LastPlayer");

   TQString num;
   for (int i = 1; i <= 10; ++i) {
      num.setNum(i);
      FieldInfo *score = new FieldInfo();
      for(int field = 1; field < d->fields; field = field * 2)
      {
         if (d->fields & field)
         {
            key = "Pos" + num + d->key[field];
            (*score)[field] = config.readEntry(key, "-");
         }
      }
      d->scores.append(score);
   }
}

void KScoreDialog::saveScores()
{
   TQString key, value;
   KConfigGroup config(kapp->config(), d->configGroup.utf8());

   config.writeEntry("LastPlayer", d->player);

   TQString num;
   for (int i = 1; i <= 10; ++i) {
      num.setNum(i);
      FieldInfo *score = d->scores.at(i-1);
      for(int field = 1; field < d->fields; field = field * 2)
      {
         if (d->fields & field)
         {
            key = "Pos" + num + d->key[field];
            config.writeEntry(key, (*score)[field]);
         }
      }
   }
   kapp->config()->sync();
}

int KScoreDialog::addScore(int newScore, const FieldInfo &newInfo, bool askName)
{
   return addScore(newScore, newInfo, askName, false);
}

int KScoreDialog::addScore(int newScore, const FieldInfo &newInfo, bool askName, bool lessIsMore)
{
   if (!d->loaded)
      loadScores();
   FieldInfo *score = d->scores.first();
   int i = 1;
   for(; score; score = d->scores.next(), i++)
   {
      bool ok;
      int num_score = (*score)[Score].toLong(&ok);
      if (lessIsMore && !ok)
         num_score = 1 << 30;
      if (((newScore > num_score) && !lessIsMore) ||
          ((newScore < num_score) && lessIsMore))
      {
        score = new FieldInfo(newInfo);
        (*score)[Score].setNum(newScore);
        d->scores.insert(i-1, score);
        d->scores.remove(10);
        d->latest = i;
        if (askName)
          d->newName = i;
        else
          saveScores();
        if (i == 1)
          d->comment = i18n("Excellent!\nYou have a new high score!");
        else
          d->comment = i18n("Well done!\nYou made it to the high score list!");
        return i;
      }
   }
   return 0;
}

void KScoreDialog::show()
{
   aboutToShow();
   KDialogBase::show();
}

void KScoreDialog::slotGotReturn()
{
   TQTimer::singleShot(0, this, TQT_SLOT(slotGotName()));
}

void KScoreDialog::slotGotName()
{
   if (d->newName == -1) return;
   
   d->player = d->edit->text();
   
   (*d->scores.at(d->newName-1))[Name] = d->player;
   saveScores();

   TQFont bold = font();
   bold.setBold(true);
   
   TQLabel *label = d->labels[(d->newName-1)*d->nrCols + d->col[Name]];
   label->setFont(bold);
   label->setText(d->player);
   d->stack[(d->newName-1)]->raiseWidget(label);
   delete d->edit;
   d->edit = 0;
   d->newName = -1;
}

int KScoreDialog::highScore()
{
   if (!d->loaded)
      loadScores();

   return (*d->scores.first())[Score].toInt();
}

void KScoreDialog::keyPressEvent( TQKeyEvent *ev)
{
   if ((d->newName != -1) && (ev->key() == Key_Return))
   {
       ev->ignore();
       return;
   }
   KDialogBase::keyPressEvent(ev);
}


#include "kscoredialog.moc"