20 #include "kexthighscore_gui.h"
21 #include "kexthighscore_gui.moc"
24 #include <tqtextstream.h>
27 #include <tqvgroupbox.h>
29 #include <tdeapplication.h>
30 #include <tdemessagebox.h>
31 #include <kurllabel.h>
32 #include <kopenwith.h>
34 #include <tdefiledialog.h>
35 #include <tdetempfile.h>
36 #include <tdeio/netaccess.h>
37 #include <kiconloader.h>
39 #include "kexthighscore_internal.h"
40 #include "kexthighscore.h"
41 #include "kexthighscore_tab.h"
44 namespace KExtHighscore
48 ShowItem::ShowItem(TQListView *list,
bool highlight)
49 : TDEListViewItem(list), _highlight(highlight)
52 void ShowItem::paintCell(TQPainter *p,
const TQColorGroup &cg,
53 int column,
int width,
int align)
55 TQColorGroup cgrp(cg);
56 if (_highlight) cgrp.setColor(TQColorGroup::Text, red);
57 TDEListViewItem::paintCell(p, cgrp, column, width, align);
61 ScoresList::ScoresList(TQWidget *parent)
64 setSelectionMode(TQListView::NoSelection);
66 setAllColumnsShowFocus(
true);
68 header()->setClickEnabled(
false);
69 header()->setMovingEnabled(
false);
72 void ScoresList::addHeader(
const ItemArray &items)
74 addLineItem(items, 0, 0);
77 TQListViewItem *ScoresList::addLine(
const ItemArray &items,
78 uint index,
bool highlight)
80 TQListViewItem *item =
new ShowItem(
this, highlight);
81 addLineItem(items, index, item);
85 void ScoresList::addLineItem(
const ItemArray &items,
86 uint index, TQListViewItem *line)
89 for (uint i=0; i<items.size(); i++) {
90 const ItemContainer &container = *items[i];
91 if ( !container.item()->isVisible() )
continue;
92 if (line) line->setText(k, itemText(container, index));
94 addColumn( container.item()->label() );
95 setColumnAlignment(k, container.item()->alignment());
102 HighscoresList::HighscoresList(TQWidget *parent)
106 TQString HighscoresList::itemText(
const ItemContainer &item, uint row)
const
108 return item.pretty(row);
111 void HighscoresList::load(
const ItemArray &items,
int highlight)
114 TQListViewItem *line = 0;
115 for (
int j=items.nbEntries()-1; j>=0; j--) {
116 TQListViewItem *item = addLine(items, j, j==highlight);
117 if ( j==highlight ) line = item;
119 if (line) ensureItemVisible(line);
123 HighscoresWidget::HighscoresWidget(TQWidget *parent)
124 : TQWidget(parent,
"show_highscores_widget"),
125 _scoresUrl(0), _playersUrl(0), _statsTab(0), _histoTab(0)
127 const ScoreInfos &s =
internal->scoreInfos();
128 const PlayerInfos &p =
internal->playerInfos();
130 TQVBoxLayout *vbox =
new TQVBoxLayout(
this, KDialogBase::spacingHint());
132 _tw =
new TQTabWidget(
this);
133 connect(_tw, TQ_SIGNAL(currentChanged(TQWidget *)), TQ_SLOT(tabChanged()));
134 vbox->addWidget(_tw);
137 _scoresList =
new HighscoresList(_tw);
138 _scoresList->addHeader(s);
139 _tw->addTab(_scoresList, i18n(
"Best &Scores"));
142 _playersList =
new HighscoresList(_tw);
143 _playersList->addHeader(p);
144 _tw->addTab(_playersList, i18n(
"&Players"));
147 if ( internal->showStatistics ) {
148 _statsTab =
new StatisticsTab(_tw);
149 _tw->addTab(_statsTab, i18n(
"Statistics"));
153 if ( p.histogram().size()!=0 ) {
154 _histoTab =
new HistogramTab(_tw);
155 _tw->addTab(_histoTab, i18n(
"Histogram"));
159 if ( internal->isWWHSAvailable() ) {
160 KURL url =
internal->queryURL(ManagerPrivate::Scores);
161 _scoresUrl =
new KURLLabel(url.url(),
162 i18n(
"View world-wide highscores"),
this);
163 connect(_scoresUrl, TQ_SIGNAL(leftClickedURL(
const TQString &)),
164 TQ_SLOT(showURL(
const TQString &)));
165 vbox->addWidget(_scoresUrl);
167 url =
internal->queryURL(ManagerPrivate::Players);
168 _playersUrl =
new KURLLabel(url.url(),
169 i18n(
"View world-wide players"),
this);
170 connect(_playersUrl, TQ_SIGNAL(leftClickedURL(
const TQString &)),
171 TQ_SLOT(showURL(
const TQString &)));
172 vbox->addWidget(_playersUrl);
176 void HighscoresWidget::changeTab(
int i)
178 if ( i!=_tw->currentPageIndex() )
179 _tw->setCurrentPage(i);
182 void HighscoresWidget::showURL(
const TQString &url)
const
184 (void)
new KRun(KURL(url));
187 void HighscoresWidget::load(
int rank)
189 _scoresList->load(internal->scoreInfos(), rank);
190 _playersList->load(internal->playerInfos(), internal->playerInfos().id());
192 _scoresUrl->setURL(internal->queryURL(ManagerPrivate::Scores).url());
194 _playersUrl->setURL(internal->queryURL(ManagerPrivate::Players).url());
195 if (_statsTab) _statsTab->load();
196 if (_histoTab) _histoTab->load();
200 HighscoresDialog::HighscoresDialog(
int rank, TQWidget *parent)
201 : KDialogBase(internal->nbGameTypes()>1 ? TreeList : Plain,
202 i18n(
"Highscores"), Close|User1|User2, Close,
203 parent,
"show_highscores", true, true,
204 KGuiItem(i18n(
"Configure..."),
"configure"),
205 KGuiItem(i18n(
"Export..."))), _rank(rank), _tab(0)
207 _widgets.resize(internal->nbGameTypes(), 0);
209 if ( internal->nbGameTypes()>1 ) {
210 for (uint i=0; i<
internal->nbGameTypes(); i++) {
211 TQString title =
internal->manager.gameTypeLabel(i, Manager::I18N);
212 TQString icon =
internal->manager.gameTypeLabel(i, Manager::Icon);
213 TQWidget *w = addVBoxPage(title, TQString(),
214 BarIcon(icon, TDEIcon::SizeLarge));
215 if ( i==internal->gameType() ) createPage(w);
218 connect(
this, TQ_SIGNAL(aboutToShowPage(TQWidget *)),
219 TQ_SLOT(createPage(TQWidget *)));
220 showPage(internal->gameType());
222 TQVBoxLayout *vbox =
new TQVBoxLayout(plainPage());
223 createPage(plainPage());
224 vbox->addWidget(_widgets[0]);
225 setMainWidget(_widgets[0]);
229 void HighscoresDialog::createPage(TQWidget *page)
231 internal->hsConfig().readCurrentConfig();
233 bool several = (
internal->nbGameTypes()>1 );
234 int i = (several ? pageIndex(page) : 0);
235 if ( _widgets[i]==0 ) {
236 _widgets[i] =
new HighscoresWidget(page);
237 connect(_widgets[i], TQ_SIGNAL(tabChanged(
int)), TQ_SLOT(tabChanged(
int)));
239 uint type =
internal->gameType();
240 if (several)
internal->setGameType(i);
241 _widgets[i]->load(uint(i)==type ? _rank : -1);
242 if (several) setGameType(type);
243 _widgets[i]->changeTab(_tab);
246 void HighscoresDialog::slotUser1()
248 if ( KExtHighscore::configure(
this) )
249 createPage(_current);
252 void HighscoresDialog::slotUser2()
254 KURL url = KFileDialog::getSaveURL(TQString(), TQString(),
this);
255 if ( url.isEmpty() )
return;
256 if ( TDEIO::NetAccess::exists(url,
true,
this) ) {
257 KGuiItem gi = KStdGuiItem::save();
258 gi.setText(i18n(
"Overwrite"));
259 int res = KMessageBox::warningContinueCancel(
this,
260 i18n(
"The file already exists. Overwrite?"),
262 if ( res==KMessageBox::Cancel )
return;
265 internal->exportHighscores(*tmp.textStream());
267 TDEIO::NetAccess::upload(tmp.name(), url,
this);
272 LastMultipleScoresList::LastMultipleScoresList(
273 const TQValueVector<Score> &scores, TQWidget *parent)
274 : ScoresList(parent), _scores(scores)
276 const ScoreInfos &s =
internal->scoreInfos();
278 for (uint i=0; i<scores.size(); i++) addLine(s, i,
false);
281 void LastMultipleScoresList::addLineItem(
const ItemArray &si,
282 uint index, TQListViewItem *line)
285 for (uint i=0; i<si.size()-2; i++) {
287 const ItemContainer *container = si[k];
289 if (line) line->setText(i, itemText(*container, index));
291 addColumn( container->item()->label() );
292 setColumnAlignment(i, container->item()->alignment());
297 TQString LastMultipleScoresList::itemText(
const ItemContainer &item,
300 TQString name = item.name();
302 return (_scores[row].type()==Won ? i18n(
"Winner") : TQString());
303 TQVariant v = _scores[row].data(name);
304 if ( name==
"name" )
return v.toString();
305 return item.item()->pretty(row, v);
309 TotalMultipleScoresList::TotalMultipleScoresList(
310 const TQValueVector<Score> &scores, TQWidget *parent)
311 : ScoresList(parent), _scores(scores)
313 const ScoreInfos &s =
internal->scoreInfos();
315 for (uint i=0; i<scores.size(); i++) addLine(s, i,
false);
318 void TotalMultipleScoresList::addLineItem(
const ItemArray &si,
319 uint index, TQListViewItem *line)
321 const PlayerInfos &pi =
internal->playerInfos();
323 for (uint i=0; i<4; i++) {
324 const ItemContainer *container;
325 if ( i==2 ) container = pi.item(
"nb games");
326 else if ( i==3 ) container = pi.item(
"mean score");
331 if (line) line->setText(i, itemText(*container, index));
334 (i==2 ? i18n(
"Won Games") : container->item()->label());
336 setColumnAlignment(i, container->item()->alignment());
341 TQString TotalMultipleScoresList::itemText(
const ItemContainer &item,
344 TQString name = item.name();
345 if ( name==
"rank" )
return TQString::number(_scores.size()-row);
346 if ( name==
"nb games" )
347 return TQString::number( _scores[row].data(
"nb won games").toUInt() );
348 TQVariant v = _scores[row].data(name);
349 if ( name==
"name" )
return v.toString();
350 return item.item()->pretty(row, v);
355 ConfigDialog::ConfigDialog(TQWidget *parent)
356 : KDialogBase(Swallow, i18n(
"Configure Highscores"),
357 Ok|Apply|Cancel, Cancel,
358 parent,
"configure_highscores", true, true),
359 _saved(false), _WWHEnabled(0)
362 TQTabWidget *tab = 0;
363 if ( internal->isWWHSAvailable() ) {
364 tab =
new TQTabWidget(
this);
366 page =
new TQWidget(tab);
367 tab->addTab(page, i18n(
"Main"));
369 page =
new TQWidget(
this);
373 TQGridLayout *pageTop =
374 new TQGridLayout(page, 2, 2, spacingHint(), spacingHint());
376 TQLabel *label =
new TQLabel(i18n(
"Nickname:"), page);
377 pageTop->addWidget(label, 0, 0);
378 _nickname =
new TQLineEdit(page);
379 connect(_nickname, TQ_SIGNAL(textChanged(
const TQString &)),
380 TQ_SLOT(modifiedSlot()));
381 connect(_nickname, TQ_SIGNAL(textChanged(
const TQString &)),
382 TQ_SLOT(nickNameChanged(
const TQString &)));
384 _nickname->setMaxLength(16);
385 pageTop->addWidget(_nickname, 0, 1);
387 label =
new TQLabel(i18n(
"Comment:"), page);
388 pageTop->addWidget(label, 1, 0);
389 _comment =
new TQLineEdit(page);
390 connect(_comment, TQ_SIGNAL(textChanged(
const TQString &)),
391 TQ_SLOT(modifiedSlot()));
392 _comment->setMaxLength(50);
393 pageTop->addWidget(_comment, 1, 1);
397 =
new TQCheckBox(i18n(
"World-wide highscores enabled"), page);
398 connect(_WWHEnabled, TQ_SIGNAL(toggled(
bool)),
399 TQ_SLOT(modifiedSlot()));
400 pageTop->addMultiCellWidget(_WWHEnabled, 2, 2, 0, 1);
403 TQWidget *page =
new TQWidget(tab);
404 tab->addTab(page, i18n(
"Advanced"));
405 TQVBoxLayout *pageTop =
406 new TQVBoxLayout(page, spacingHint(), spacingHint());
408 TQVGroupBox *group =
new TQVGroupBox(i18n(
"Registration Data"), page);
409 pageTop->addWidget(group);
410 TQGrid *grid =
new TQGrid(2, group);
411 grid->setSpacing(spacingHint());
413 label =
new TQLabel(i18n(
"Nickname:"), grid);
414 _registeredName =
new KLineEdit(grid);
415 _registeredName->setReadOnly(
true);
417 label =
new TQLabel(i18n(
"Key:"), grid);
418 _key =
new KLineEdit(grid);
419 _key->setReadOnly(
true);
421 KGuiItem gi = KStdGuiItem::clear();
422 gi.setText(i18n(
"Remove"));
423 _removeButton =
new KPushButton(gi, grid);
424 connect(_removeButton, TQ_SIGNAL(clicked()), TQ_SLOT(removeSlot()));
428 enableButtonOK( !_nickname->text().isEmpty() );
429 enableButtonApply(
false);
432 void ConfigDialog::nickNameChanged(
const TQString &text)
434 enableButtonOK( !text.isEmpty() );
438 void ConfigDialog::modifiedSlot()
440 enableButtonApply(
true && !_nickname->text().isEmpty() );
443 void ConfigDialog::accept()
446 KDialogBase::accept();
447 kapp->config()->sync();
451 void ConfigDialog::removeSlot()
453 KGuiItem gi = KStdGuiItem::clear();
454 gi.setText(i18n(
"Remove"));
455 int res = KMessageBox::warningContinueCancel(
this,
456 i18n(
"This will permanently remove your "
457 "registration key. You will not be able to use "
458 "the currently registered nickname anymore."),
460 if ( res==KMessageBox::Continue ) {
461 internal->playerInfos().removeKey();
462 _registeredName->clear();
464 _removeButton->setEnabled(
false);
465 _WWHEnabled->setChecked(
false);
470 void ConfigDialog::load()
472 internal->hsConfig().readCurrentConfig();
473 const PlayerInfos &infos =
internal->playerInfos();
474 _nickname->setText(infos.isAnonymous() ? TQString() : infos.name());
475 _comment->setText(infos.comment());
477 _WWHEnabled->setChecked(infos.isWWEnabled());
478 if ( !infos.key().isEmpty() ) {
479 _registeredName->setText(infos.registeredName());
480 _registeredName->home(
false);
481 _key->setText(infos.key());
484 _removeButton->setEnabled(!infos.key().isEmpty());
488 bool ConfigDialog::save()
490 bool enabled = (_WWHEnabled ? _WWHEnabled->isChecked() :
false);
494 TQString newName = _nickname->text();
495 if ( newName.isEmpty() && !
internal->playerInfos().isAnonymous()
496 && !enabled )
return true;
498 if ( newName.isEmpty() ) {
499 KMessageBox::sorry(
this, i18n(
"Please choose a non empty nickname."));
502 if ( internal->playerInfos().isNameUsed(newName) ) {
503 KMessageBox::sorry(
this, i18n(
"Nickname already in use. Please "
504 "choose another one"));
509 internal->modifySettings(newName, _comment->text(), enabled,
this);
512 enableButtonApply(
false);
519 AskNameDialog::AskNameDialog(TQWidget *parent)
520 : KDialogBase(Plain, i18n(
"Enter Your Nickname"), Ok | Cancel, Ok,
521 parent,
"ask_name_dialog")
523 internal->hsConfig().readCurrentConfig();
526 new TQVBoxLayout(plainPage(), marginHint(), spacingHint());
528 new TQLabel(i18n(
"Congratulations, you have won!"), plainPage());
529 top->addWidget(label);
531 TQHBoxLayout *hbox =
new TQHBoxLayout(top);
532 label =
new TQLabel(i18n(
"Enter your nickname:"), plainPage());
533 hbox->addWidget(label);
534 _edit =
new TQLineEdit(plainPage());
536 connect(_edit, TQ_SIGNAL(textChanged(
const TQString &)), TQ_SLOT(nameChanged()));
537 hbox->addWidget(_edit);
539 top->addSpacing(spacingHint());
540 _checkbox =
new TQCheckBox(i18n(
"Do not ask again."), plainPage());
541 top->addWidget(_checkbox);
546 void AskNameDialog::nameChanged()
548 enableButtonOK( !name().isEmpty()
549 && !internal->playerInfos().isNameUsed(name()) );