21 #include "kexthighscore_gui.moc"
24 #include <QTextStream>
25 #include <QGridLayout>
26 #include <QHBoxLayout>
28 #include <QVBoxLayout>
30 #include <QHeaderView>
31 #include <QtGui/QTabWidget>
33 #include <kmessagebox.h>
34 #include <kurllabel.h>
36 #include <kfiledialog.h>
38 #include <ktemporaryfile.h>
39 #include <kio/netaccess.h>
41 #include <kiconloader.h>
42 #include <klineedit.h>
43 #include <kpushbutton.h>
50 namespace KExtHighscore
59 for (
int i=0; i < columnCount();i++) {
60 setForeground(i, Qt::red);
70 setSelectionMode(QTreeWidget::NoSelection);
72 setAllColumnsShowFocus(
true);
74 header()->setClickable(
false);
75 header()->setMovable(
false);
81 addLineItem(items, 0, 0);
85 uint index,
bool highlight)
89 addLineItem(items, index, item);
93 void ScoresList::addLineItem(
const ItemArray &items,
98 for (
int i=0; i<items.size(); i++) {
104 line->setText(k,
itemText(container, index));
108 headerItem()->setText(k, container.
item()->
label() );
109 headerItem()->setTextAlignment(k, container.
item()->
alignment());
134 for (
int j=items.
nbEntries()-1; j>=0; j--) {
136 if ( j==highlight ) line = item;
138 scrollTo(indexFromItem(line));
144 _scoresUrl(0), _playersUrl(0), _statsTab(0), _histoTab(0)
148 setObjectName( QLatin1String(
"show_highscores_widget" ));
152 QVBoxLayout *vbox =
new QVBoxLayout(
this);
153 vbox->setSpacing(KDialog::spacingHint());
155 _tw =
new QTabWidget(
this);
156 connect(_tw, SIGNAL(currentChanged(
int)), SLOT(
tabChanged()));
157 vbox->addWidget(_tw);
162 _tw->addTab(_scoresList, i18n(
"Best &Scores"));
167 _tw->addTab(_playersList, i18n(
"&Players"));
172 _tw->addTab(_statsTab, i18n(
"Statistics"));
178 _tw->addTab(_histoTab, i18n(
"Histogram"));
184 _scoresUrl =
new KUrlLabel(url.url(),
185 i18n(
"View world-wide highscores"),
this);
186 connect(_scoresUrl, SIGNAL(leftClickedUrl(QString)),
187 SLOT(showURL(QString)));
188 vbox->addWidget(_scoresUrl);
191 _playersUrl =
new KUrlLabel(url.url(),
192 i18n(
"View world-wide players"),
this);
193 connect(_playersUrl, SIGNAL(leftClickedUrl(QString)),
194 SLOT(showURL(QString)));
195 vbox->addWidget(_playersUrl);
203 if ( i!=_tw->currentIndex() )
204 _tw->setCurrentIndex(i);
207 void HighscoresWidget::showURL(
const QString &url)
210 (void)
new KRun(KUrl(url),
this);
222 if (_statsTab) _statsTab->
load();
223 if (_histoTab) _histoTab->
load();
232 setCaption( i18n(
"Highscores") );
233 setButtons( Close|User1|User2 );
234 setDefaultButton( Close );
236 setFaceType( KPageDialog::Tree );
238 setFaceType( KPageDialog::Plain );
239 setButtonGuiItem( User1, KGuiItem(i18n(
"Configure..."), QLatin1String(
"configure" )) );
240 setButtonGuiItem( User2, KGuiItem(i18n(
"Export...")) );
241 connect(
this, SIGNAL(user1Clicked()), SLOT(slotUser1()) );
242 connect(
this, SIGNAL(user2Clicked()), SLOT(slotUser2()) );
244 for (uint i=0; i<
internal->nbGameTypes(); i++) {
245 QString title =
internal->manager.gameTypeLabel(i,
Manager::I18N);
246 QString icon =
internal->manager.gameTypeLabel(i,
Manager::Icon);
248 KPageWidgetItem *pageItem =
new KPageWidgetItem( hsw, title);
249 pageItem->setIcon( KIcon( BarIcon(icon, KIconLoader::SizeLarge) ) );
251 _pages.append(pageItem);
252 connect(hsw, SIGNAL(tabChanged(
int)), SLOT(tabChanged(
int)));
255 connect(
this, SIGNAL(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)),
256 SLOT(highscorePageChanged(KPageWidgetItem*,KPageWidgetItem*)));
260 void HighscoresDialog::highscorePageChanged(KPageWidgetItem* page, KPageWidgetItem* pageold)
264 int idx = _pages.indexOf( page );
267 internal->hsConfig().readCurrentConfig();
268 uint type =
internal->gameType();
269 bool several = (
internal->nbGameTypes()>1 );
271 internal->setGameType(idx);
273 hsw->
load(uint(idx)==type ? _rank : -1);
278 void HighscoresDialog::slotUser1()
282 highscorePageChanged(currentPage(), 0);
285 void HighscoresDialog::slotUser2()
288 KUrl url = KFileDialog::getSaveUrl(KUrl(), QString(),
this);
289 if ( url.isEmpty() )
return;
290 if ( KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide,
this) ) {
291 KGuiItem gi = KStandardGuiItem::save();
292 gi.setText(i18n(
"Overwrite"));
293 int res = KMessageBox::warningContinueCancel(
this,
294 i18n(
"The file already exists. Overwrite?"),
296 if ( res==KMessageBox::Cancel )
return;
300 QTextStream stream(&tmp);
301 internal->exportHighscores(stream);
303 KIO::NetAccess::upload(tmp.fileName(), url,
this);
315 for (
int i=0; i<scores.size(); i++)
addLine(s, i,
false);
318 void LastMultipleScoresList::addLineItem(
const ItemArray &si,
323 for (
int i=0; i<si.size()-2; i++) {
328 line->setText(i, itemText(container, index));
332 headerItem()->setText(i, container.
item()->
label() );
333 headerItem()->setTextAlignment(i, container.
item()->
alignment());
338 QString LastMultipleScoresList::itemText(
const ItemContainer &item,
342 QString name = item.name();
343 if ( name==QLatin1String(
"rank" ) )
344 return (_scores[row].type()==
Won ? i18n(
"Winner") : QString());
345 QVariant v = _scores[row].data(name);
346 if ( name==QLatin1String(
"name" ) )
return v.toString();
347 return item.item()->pretty(row, v);
358 for (
int i=0; i<scores.size(); i++)
addLine(s, i,
false);
361 void TotalMultipleScoresList::addLineItem(
const ItemArray &si,
367 for (uint i=0; i<4; i++) {
369 if ( i==2 ) container = pi.
item(QLatin1String(
"nb games" ));
370 else if ( i==3 ) container = pi.
item(QLatin1String(
"mean score" ));
377 line->setText(i, itemText(*container, index));
382 (i==2 ? i18n(
"Won Games") : container->item()->label());
383 headerItem()->setText(i, label );
384 headerItem()->setTextAlignment(i, container->
item()->
alignment());
389 QString TotalMultipleScoresList::itemText(
const ItemContainer &item,
393 QString name = item.name();
394 if ( name==QLatin1String(
"rank" ) )
return QString::number(_scores.size()-row);
395 else if ( name==QLatin1String(
"nb games" ) )
396 return QString::number( _scores[row].data(QLatin1String(
"nb won games" )).toUInt() );
397 QVariant v = _scores[row].data(name);
398 if ( name==QLatin1String(
"name" ) )
return v.toString();
399 return item.item()->pretty(row, v);
406 _saved(false), _WWHEnabled(0)
409 setCaption( i18n(
"Configure Highscores") );
410 setButtons( Ok|Apply|Cancel );
411 setDefaultButton( Cancel );
416 tab =
new QTabWidget(
this);
419 tab->addTab(page, i18n(
"Main"));
425 QGridLayout *pageTop =
426 new QGridLayout(page);
427 pageTop->setMargin(spacingHint());
428 pageTop->setSpacing(spacingHint());
430 QLabel *label =
new QLabel(i18n(
"Nickname:"), page);
431 pageTop->addWidget(label, 0, 0);
432 _nickname =
new QLineEdit(page);
433 connect(_nickname, SIGNAL(textChanged(QString)),
434 SLOT(modifiedSlot()));
435 connect(_nickname, SIGNAL(textChanged(QString)),
436 SLOT(nickNameChanged(QString)));
438 _nickname->setMaxLength(16);
439 pageTop->addWidget(_nickname, 0, 1);
441 label =
new QLabel(i18n(
"Comment:"), page);
442 pageTop->addWidget(label, 1, 0);
443 _comment =
new QLineEdit(page);
444 connect(_comment, SIGNAL(textChanged(QString)),
445 SLOT(modifiedSlot()));
446 _comment->setMaxLength(50);
447 pageTop->addWidget(_comment, 1, 1);
451 =
new QCheckBox(i18n(
"World-wide highscores enabled"), page);
452 connect(_WWHEnabled, SIGNAL(toggled(
bool)),
453 SLOT(modifiedSlot()));
454 pageTop->addWidget(_WWHEnabled, 2, 0, 1, 2 );
458 tab->addTab(page, i18n(
"Advanced"));
459 QVBoxLayout *pageTop =
new QVBoxLayout(page);
460 pageTop->setMargin(marginHint());
461 pageTop->setSpacing(spacingHint());
463 QGroupBox *group =
new QGroupBox(page);
464 group->setTitle( i18n(
"Registration Data") );
465 pageTop->addWidget(group);
466 QGridLayout *groupLayout =
new QGridLayout(group);
467 groupLayout->setSpacing(spacingHint());
469 label =
new QLabel(i18n(
"Nickname:"), group);
470 groupLayout->addWidget(label, 0, 0);
471 _registeredName =
new KLineEdit(group);
472 _registeredName->setReadOnly(
true);
473 groupLayout->addWidget(_registeredName, 0, 1);
475 label =
new QLabel(i18n(
"Key:"), group);
476 groupLayout->addWidget(label, 1, 0);
477 _key =
new KLineEdit(group);
478 _key->setReadOnly(
true);
479 groupLayout->addWidget(_key, 1, 1);
481 KGuiItem gi = KStandardGuiItem::clear();
482 gi.setText(i18n(
"Remove"));
483 _removeButton =
new KPushButton(gi, group);
484 groupLayout->addWidget(_removeButton, 2, 0);
485 connect(_removeButton, SIGNAL(clicked()), SLOT(removeSlot()));
489 enableButtonOk( !_nickname->text().isEmpty() );
490 enableButtonApply(
false);
493 void ConfigDialog::nickNameChanged(
const QString &text)
495 enableButtonOk( !text.isEmpty() );
499 void ConfigDialog::modifiedSlot()
501 enableButtonApply(
true && !_nickname->text().isEmpty() );
504 void ConfigDialog::accept()
508 KGlobal::config()->sync();
512 void ConfigDialog::removeSlot()
514 KGuiItem gi = KStandardGuiItem::clear();
515 gi.setText(i18n(
"Remove"));
516 int res = KMessageBox::warningContinueCancel(
this,
517 i18n(
"This will permanently remove your "
518 "registration key. You will not be able to use "
519 "the currently registered nickname anymore."),
521 if ( res==KMessageBox::Continue ) {
522 internal->playerInfos().removeKey();
523 _registeredName->clear();
525 _removeButton->setEnabled(
false);
526 _WWHEnabled->setChecked(
false);
531 void ConfigDialog::load()
533 internal->hsConfig().readCurrentConfig();
534 const PlayerInfos &infos =
internal->playerInfos();
535 _nickname->setText(infos.isAnonymous() ? QString() : infos.name());
536 _comment->setText(infos.comment());
538 _WWHEnabled->setChecked(infos.isWWEnabled());
539 if ( !infos.key().isEmpty() ) {
540 _registeredName->setText(infos.registeredName());
541 _registeredName->home(
false);
542 _key->setText(infos.key());
545 _removeButton->setEnabled(!infos.key().isEmpty());
549 bool ConfigDialog::save()
551 bool enabled = (_WWHEnabled ? _WWHEnabled->isChecked() :
false);
555 QString newName = _nickname->text();
556 if ( newName.isEmpty() && !
internal->playerInfos().isAnonymous()
557 && !enabled )
return true;
559 if ( newName.isEmpty() ) {
560 KMessageBox::sorry(
this, i18n(
"Please choose a non empty nickname."));
564 KMessageBox::sorry(
this, i18n(
"Nickname already in use. Please "
565 "choose another one"));
570 internal->modifySettings(newName, _comment->text(), enabled,
this);
573 enableButtonApply(
false);
585 setCaption( i18n(
"Enter Your Nickname") );
586 setButtons( Ok | Cancel );
587 setDefaultButton( Ok );
589 internal->hsConfig().readCurrentConfig();
591 setMainWidget( main );
592 QVBoxLayout *top =
new QVBoxLayout( main );
593 top->setMargin( marginHint() );
594 top->setSpacing( spacingHint() );
597 new QLabel(i18n(
"Congratulations, you have won!"), main);
598 top->addWidget(label);
600 QHBoxLayout *hbox =
new QHBoxLayout;
601 top->addLayout(hbox);
602 label =
new QLabel(i18n(
"Enter your nickname:"), main);
603 hbox->addWidget(label);
604 _edit =
new QLineEdit(main);
606 connect(_edit, SIGNAL(textChanged(QString)), SLOT(nameChanged()));
607 hbox->addWidget(_edit);
609 top->addSpacing(spacingHint());
610 _checkbox =
new QCheckBox(i18n(
"Do not ask again."), main);
611 top->addWidget(_checkbox);
616 void AskNameDialog::nameChanged()
618 enableButtonOk( !
name().isEmpty()
KUrl queryUrl(QueryType type, const QString &newName=QLatin1String("")) const
ScoreInfos & scoreInfos()
QTreeWidgetItem * addLine(const ItemArray &, uint index, bool highlight)
const Item * item() const
ManagerPrivate * internal
ShowItem(QTreeWidget *, bool highlight)
bool isWWHSAvailable() const
LastMultipleScoresList(const QVector< Score > &, QWidget *parent)
const QVector< uint > & histogram() const
ConfigDialog(QWidget *parent)
HighscoresList(QWidget *parent)
PlayerInfos & playerInfos()
QString pretty(uint i) const
void setGameType(uint type)
Set the current game type.
bool configure(QWidget *parent)
Configure the highscores.
bool isNameUsed(const QString &name) const
void load(const ItemArray &, int highlight)
ScoresList(QWidget *parent)
virtual uint nbEntries() const =0
TotalMultipleScoresList(const QVector< Score > &, QWidget *parent)
HighscoresDialog(int rank, QWidget *parent)
QString itemText(const ItemContainer &, uint row) const
const ItemContainer * item(const QString &name) const
virtual QString itemText(const ItemContainer &, uint row) const =0
void addHeader(const ItemArray &)
Manage a bunch of Item which are saved under the same group in KHighscores config file...