File indexing completed on 2022-11-29 18:40:54

0001 /*
0002     SPDX-FileCopyrightText: 2001-2003 Nicolas Hadacek <hadacek@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #include "kexthighscore_gui.h"
0008 
0009 #include <QApplication>
0010 #include <QFileDialog>
0011 #include <QGridLayout>
0012 #include <QGroupBox>
0013 #include <QHBoxLayout>
0014 #include <QHeaderView>
0015 #include <QIcon>
0016 #include <QLabel>
0017 #include <QPushButton>
0018 #include <QTabWidget>
0019 #include <QTemporaryFile>
0020 #include <QTextStream>
0021 #include <QVBoxLayout>
0022 
0023 #include <KIO/OpenUrlJob>
0024 #include <kio_version.h>
0025 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0)
0026 #include <KIO/JobUiDelegateFactory>
0027 #else
0028 #include <KIO/JobUiDelegate>
0029 #endif
0030 #include <KGuiItem>
0031 #include <KIO/StatJob>
0032 #include <KIO/CopyJob>
0033 #include <KJobWidgets>
0034 #include <KMessageBox>
0035 #include <KUrlLabel>
0036 
0037 #include "kexthighscore_internal.h"
0038 #include "kexthighscore_tab.h"
0039 
0040 
0041 namespace KExtHighscore
0042 {
0043 
0044 //-----------------------------------------------------------------------------
0045 ShowItem::ShowItem(QTreeWidget *list, bool highlight)
0046     : QTreeWidgetItem(list), _highlight(highlight)
0047 {
0048 //   kDebug(11001) ;
0049   if (_highlight) {
0050     for (int i=0; i < columnCount();i++) {
0051       setForeground(i, Qt::red);
0052     }
0053   }
0054 }
0055 
0056 //-----------------------------------------------------------------------------
0057 ScoresList::ScoresList(QWidget *parent)
0058     : QTreeWidget(parent)
0059 {
0060 //   kDebug(11001) ;
0061     setSelectionMode(QTreeWidget::NoSelection);
0062 //     setItemMargin(3);
0063     setAllColumnsShowFocus(true);
0064 //     setSorting(-1);
0065     header()->setSectionsClickable(false);
0066     header()->setSectionsMovable(false);
0067 }
0068 
0069 void ScoresList::addHeader(const ItemArray &items)
0070 {
0071 //   kDebug(11001) ;
0072     addLineItem(items, 0, nullptr);
0073 }
0074 
0075 QTreeWidgetItem *ScoresList::addLine(const ItemArray &items,
0076                                    uint index, bool highlight)
0077 {
0078 //   kDebug(11001) ;
0079     QTreeWidgetItem *item = new ShowItem(this, highlight);
0080     addLineItem(items, index, item);
0081     return item;
0082 }
0083 
0084 void ScoresList::addLineItem(const ItemArray &items,
0085                              uint index, QTreeWidgetItem *line)
0086 {
0087 //   kDebug(11001) ;
0088     uint k = 0;
0089     for (int i=0; i<items.size(); i++) {
0090         const ItemContainer& container = *items[i];
0091         if ( !container.item()->isVisible() ) {
0092       continue;
0093     }
0094         if (line) {
0095       line->setText(k, itemText(container, index));
0096       line->setTextAlignment(k, container.item()->alignment());
0097     }
0098         else {
0099         headerItem()->setText(k, container.item()->label() );
0100             headerItem()->setTextAlignment(k, container.item()->alignment());
0101         }
0102         k++;
0103     }
0104     update();
0105 }
0106 
0107 //-----------------------------------------------------------------------------
0108 HighscoresList::HighscoresList(QWidget *parent)
0109     : ScoresList(parent)
0110 {
0111 //   kDebug(11001) ;
0112 }
0113 
0114 QString HighscoresList::itemText(const ItemContainer &item, uint row) const
0115 {
0116 //   kDebug(11001) ;
0117     return item.pretty(row);
0118 }
0119 
0120 void HighscoresList::load(const ItemArray &items, int highlight)
0121 {
0122 //     kDebug(11001) ;
0123     clear();
0124     QTreeWidgetItem *line = nullptr;
0125     for (int j=items.nbEntries()-1; j>=0; j--) {
0126         QTreeWidgetItem *item = addLine(items, j, j==highlight);
0127         if ( j==highlight ) line = item;
0128     }
0129     scrollTo(indexFromItem(line));
0130 }
0131 
0132 //-----------------------------------------------------------------------------
0133 HighscoresWidget::HighscoresWidget(QWidget *parent)
0134     : QWidget(parent)
0135 {
0136 //     kDebug(11001) << ": HighscoresWidget";
0137 
0138     setObjectName( QStringLiteral("show_highscores_widget" ));
0139     const ScoreInfos &s = internal->scoreInfos();
0140     const PlayerInfos &p = internal->playerInfos();
0141 
0142     QVBoxLayout *vbox = new QVBoxLayout(this);
0143     vbox->setSpacing(QApplication::fontMetrics().lineSpacing());
0144 
0145     _tw = new QTabWidget(this);
0146     connect(_tw, &QTabWidget::currentChanged, this, &HighscoresWidget::handleTabChanged);
0147     vbox->addWidget(_tw);
0148 
0149     // scores tab
0150     _scoresList = new HighscoresList(nullptr);
0151     _scoresList->addHeader(s);
0152     _tw->addTab(_scoresList, i18n("Best &Scores"));
0153 
0154     // players tab
0155     _playersList = new HighscoresList(nullptr);
0156     _playersList->addHeader(p);
0157     _tw->addTab(_playersList, i18n("&Players"));
0158 
0159     // statistics tab
0160     if ( internal->showStatistics ) {
0161         _statsTab = new StatisticsTab(nullptr);
0162         _tw->addTab(_statsTab, i18n("Statistics"));
0163     }
0164 
0165     // histogram tab
0166     if ( p.histogram().size()!=0 ) {
0167         _histoTab = new HistogramTab(nullptr);
0168         _tw->addTab(_histoTab, i18n("Histogram"));
0169     }
0170 
0171     // url labels
0172     if ( internal->isWWHSAvailable() ) {
0173         QUrl url = internal->queryUrl(ManagerPrivate::Scores);
0174         _scoresUrl = new KUrlLabel(url.url(),
0175                                    i18n("View world-wide highscores"), this);
0176         connect(_scoresUrl, qOverload<>(&KUrlLabel::leftClickedUrl), this,
0177                 &HighscoresWidget::handleUrlClicked);
0178         vbox->addWidget(_scoresUrl);
0179 
0180         url = internal->queryUrl(ManagerPrivate::Players);
0181         _playersUrl = new KUrlLabel(url.url(),
0182                                     i18n("View world-wide players"), this);
0183         connect(_playersUrl, qOverload<>(&KUrlLabel::leftClickedUrl), this,
0184                 &HighscoresWidget::handleUrlClicked);
0185         vbox->addWidget(_playersUrl);
0186     }
0187     load(-1);
0188 }
0189 
0190 void HighscoresWidget::changeTab(int i)
0191 {
0192 //   kDebug(11001) ;
0193     if ( i!=_tw->currentIndex() )
0194         _tw->setCurrentIndex(i);
0195 }
0196 
0197 void HighscoresWidget::handleUrlClicked()
0198 {
0199     auto* label = qobject_cast<KUrlLabel*>(sender());
0200     if (!label) {
0201         return;
0202     }
0203 //   kDebug(11001) ;
0204     auto *job = new KIO::OpenUrlJob(QUrl(label->url()));
0205 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0)
0206     job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this));
0207 #else
0208     job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this));
0209 #endif
0210     job->start();
0211 }
0212 
0213 void HighscoresWidget::load(int rank)
0214 {
0215 //   kDebug(11001) << rank;
0216     _scoresList->load(internal->scoreInfos(), rank);
0217     _playersList->load(internal->playerInfos(), internal->playerInfos().id());
0218     if (_scoresUrl)
0219         _scoresUrl->setUrl(internal->queryUrl(ManagerPrivate::Scores).url());
0220     if (_playersUrl)
0221         _playersUrl->setUrl(internal->queryUrl(ManagerPrivate::Players).url());
0222     if (_statsTab) _statsTab->load();
0223     if (_histoTab) _histoTab->load();
0224 }
0225 
0226 //-----------------------------------------------------------------------------
0227 HighscoresDialog::HighscoresDialog(int rank, QWidget *parent)
0228     : KPageDialog(parent), _rank(rank), _tab(0)
0229 {
0230 //     kDebug(11001) << ": HighscoresDialog";
0231 
0232     setWindowTitle(i18nc("@title:window", "Highscores"));
0233 // TODO    setButtons( Close|User1|User2 );
0234 // TODO    setDefaultButton( Close );
0235     if ( internal->nbGameTypes()>1 )
0236         setFaceType( KPageDialog::Tree );
0237     else
0238         setFaceType( KPageDialog::Plain );
0239 // TODO    setButtonGuiItem( User1, KGuiItem(i18n("Configure..."), QLatin1String( "configure" )) );
0240 // TODO    setButtonGuiItem( User2, KGuiItem(i18n("Export...")) );
0241 
0242     for (uint i=0; i<internal->nbGameTypes(); i++) {
0243         QString title = internal->manager.gameTypeLabel(i, Manager::I18N);
0244         QString icon = internal->manager.gameTypeLabel(i, Manager::Icon);
0245         HighscoresWidget *hsw = new HighscoresWidget(nullptr);
0246         KPageWidgetItem *pageItem = new KPageWidgetItem( hsw, title);
0247         const int iconSize = style()->pixelMetric(QStyle::PM_ToolBarIconSize);
0248         pageItem->setIcon(QIcon::fromTheme(icon).pixmap(iconSize));
0249         addPage( pageItem );
0250         _pages.append(pageItem);
0251         connect(hsw, &HighscoresWidget::tabChanged, this, &HighscoresDialog::tabChanged);
0252     }
0253 
0254     connect(this, &KPageDialog::currentPageChanged,
0255             this, &HighscoresDialog::highscorePageChanged);
0256     setCurrentPage(_pages[internal->gameType()]);
0257 
0258     setStandardButtons(QDialogButtonBox::Close);
0259 }
0260 
0261 void HighscoresDialog::highscorePageChanged(KPageWidgetItem* page, KPageWidgetItem* pageold)
0262 {
0263     Q_UNUSED(pageold);
0264 //   kDebug(11001) ;
0265     int idx = _pages.indexOf( page );
0266     Q_ASSERT(idx != -1);
0267 
0268     internal->hsConfig().readCurrentConfig();
0269     uint type = internal->gameType();
0270     bool several = ( internal->nbGameTypes()>1 );
0271     if (several)
0272         internal->setGameType(idx);
0273     HighscoresWidget *hsw = static_cast<HighscoresWidget*>(page->widget());
0274     hsw->load(uint(idx)==type ? _rank : -1);
0275     if (several) setGameType(type);
0276     hsw->changeTab(_tab);
0277 }
0278 
0279 void HighscoresDialog::slotUser1()
0280 {
0281 //   kDebug(11001) ;
0282     if ( KExtHighscore::configure(this) )
0283         highscorePageChanged(currentPage(), nullptr);//update data
0284 }
0285 
0286 void HighscoresDialog::slotUser2()
0287 {
0288 //   kDebug(11001) ;
0289     QUrl url = QFileDialog::getSaveFileUrl(this, tr("HighscoresDialog"), QUrl(), QString());
0290     if ( url.isEmpty() ) return;
0291     auto job = KIO::statDetails(url, KIO::StatJob::SourceSide, KIO::StatNoDetails);
0292     KJobWidgets::setWindow(job, this);
0293     job->exec();
0294     if (!job->error()) {
0295         KGuiItem gi = KStandardGuiItem::save();
0296         gi.setText(i18n("Overwrite"));
0297         int res = KMessageBox::warningContinueCancel(this,
0298                                  i18n("The file already exists. Overwrite?"),
0299                                  i18n("Export"), gi);
0300         if ( res==KMessageBox::Cancel ) return;
0301     }
0302     QTemporaryFile tmp;
0303     tmp.open();
0304     QTextStream stream(&tmp);
0305     internal->exportHighscores(stream);
0306     stream.flush();
0307 //     KIO::NetAccess::upload(tmp.fileName(), url, this);
0308     auto copyJob = KIO::copy(QUrl::fromLocalFile(tmp.fileName()), url);
0309     copyJob->exec();
0310 }
0311 
0312 //-----------------------------------------------------------------------------
0313 LastMultipleScoresList::LastMultipleScoresList(
0314                             const QVector<Score> &scores, QWidget *parent)
0315     : ScoresList(parent), _scores(scores)
0316 {
0317 //     kDebug(11001) << ": LastMultipleScoresList";
0318 
0319     const ScoreInfos &s = internal->scoreInfos();
0320     addHeader(s);
0321     for (int i=0; i<scores.size(); i++) addLine(s, i, false);
0322 }
0323 
0324 void LastMultipleScoresList::addLineItem(const ItemArray &si,
0325                                          uint index, QTreeWidgetItem *line)
0326 {
0327 //   kDebug(11001) ;
0328     uint k = 1; // skip "id"
0329     for (int i=0; i<si.size()-2; i++) {
0330         if ( i==3 ) k = 5; // skip "date"
0331         const ItemContainer& container = *si[k];
0332         k++;
0333         if (line) {
0334       line->setText(i, itemText(container, index));
0335       line->setTextAlignment(i, container.item()->alignment());
0336     }
0337         else {
0338         headerItem()->setText(i, container.item()->label() );
0339             headerItem()->setTextAlignment(i, container.item()->alignment());
0340         }
0341     }
0342 }
0343 
0344 QString LastMultipleScoresList::itemText(const ItemContainer &item,
0345                                          uint row) const
0346 {
0347 //   kDebug(11001) ;
0348     QString name = item.name();
0349     if ( name==QLatin1String( "rank" ) )
0350         return (_scores[row].type()==Won ? i18n("Winner") : QString());
0351     QVariant v = _scores[row].data(name);
0352     if ( name==QLatin1String( "name" ) ) return v.toString();
0353     return item.item()->pretty(row, v);
0354 }
0355 
0356 //-----------------------------------------------------------------------------
0357 TotalMultipleScoresList::TotalMultipleScoresList(
0358                             const QVector<Score> &scores, QWidget *parent)
0359     : ScoresList(parent), _scores(scores)
0360 {
0361 //     kDebug(11001) << ": TotalMultipleScoresList";
0362     const ScoreInfos &s = internal->scoreInfos();
0363     addHeader(s);
0364     for (int i=0; i<scores.size(); i++) addLine(s, i, false);
0365 }
0366 
0367 void TotalMultipleScoresList::addLineItem(const ItemArray &si,
0368                                           uint index, QTreeWidgetItem *line)
0369 {
0370 //   kDebug(11001) ;
0371     const PlayerInfos &pi = internal->playerInfos();
0372     uint k = 1; // skip "id"
0373     for (uint i=0; i<4; i++) { // skip additional fields
0374         const ItemContainer *container;
0375         if ( i==2 ) container = pi.item(QStringLiteral( "nb games" ));
0376         else if ( i==3 ) container = pi.item(QStringLiteral( "mean score" ));
0377         else {
0378             container = si[k];
0379             k++;
0380         }
0381 
0382         if (line) {
0383       line->setText(i, itemText(*container, index));
0384       line->setTextAlignment(i, container->item()->alignment());
0385     }
0386         else {
0387             QString label =
0388                 (i==2 ? i18n("Won Games") : container->item()->label());
0389         headerItem()->setText(i, label );
0390             headerItem()->setTextAlignment(i, container->item()->alignment());
0391         }
0392     }
0393 }
0394 
0395 QString TotalMultipleScoresList::itemText(const ItemContainer &item,
0396                                           uint row) const
0397 {
0398 //   kDebug(11001) ;
0399     QString name = item.name();
0400     if ( name==QLatin1String( "rank" ) ) return QString::number(_scores.size()-row);
0401     else if ( name==QLatin1String( "nb games" ) )
0402         return QString::number( _scores[row].data(QStringLiteral( "nb won games" )).toUInt() );
0403     QVariant v = _scores[row].data(name);
0404     if ( name==QLatin1String( "name" ) ) return v.toString();
0405     return item.item()->pretty(row, v);
0406 }
0407 
0408 
0409 //-----------------------------------------------------------------------------
0410 ConfigDialog::ConfigDialog(QWidget *parent)
0411     : QDialog(parent),
0412       _saved(false), _WWHEnabled(nullptr)
0413 {
0414 //     kDebug(11001) << ": ConfigDialog";
0415     
0416     setWindowTitle(i18nc("@title:window", "Configure Highscores"));
0417     setModal( true );
0418     
0419     QWidget *page = nullptr;
0420     QTabWidget *tab = nullptr;
0421     
0422     QVBoxLayout *layout = new QVBoxLayout;
0423     setLayout(layout);
0424     
0425     if ( internal->isWWHSAvailable() ) {
0426         tab = new QTabWidget(this);
0427         layout->addWidget(tab);
0428         page = new QWidget;
0429         tab->addTab(page, i18n("Main"));
0430     } 
0431     
0432     else {
0433         page = new QWidget(this);
0434         layout->addWidget(page);
0435     }
0436 
0437     QGridLayout *pageTop =
0438         new QGridLayout(page);
0439     //pageTop->setMargin(QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing));
0440     //pageTop->setSpacing(QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing));
0441     
0442     layout->addLayout(pageTop);
0443 
0444     QLabel *label = new QLabel(i18n("Nickname:"), page);
0445     pageTop->addWidget(label, 0, 0);
0446     _nickname = new QLineEdit(page);
0447     connect(_nickname, &QLineEdit::textChanged,
0448             this, &ConfigDialog::modifiedSlot);
0449     connect(_nickname, &QLineEdit::textChanged,
0450             this, &ConfigDialog::nickNameChanged);
0451 
0452     _nickname->setMaxLength(16);
0453     pageTop->addWidget(_nickname, 0, 1);
0454 
0455     label = new QLabel(i18n("Comment:"), page);
0456     pageTop->addWidget(label, 1, 0);
0457     _comment = new QLineEdit(page);
0458     connect(_comment, &QLineEdit::textChanged,
0459             this, &ConfigDialog::modifiedSlot);
0460     _comment->setMaxLength(50);
0461     pageTop->addWidget(_comment, 1, 1);
0462 
0463     if (tab) {
0464         _WWHEnabled
0465             = new QCheckBox(i18n("World-wide highscores enabled"), page);
0466         connect(_WWHEnabled, &QAbstractButton::toggled,
0467                 this, &ConfigDialog::modifiedSlot);
0468         pageTop->addWidget(_WWHEnabled, 2, 0, 1, 2 );
0469 
0470         // advanced tab
0471         QWidget *page = new QWidget;
0472         tab->addTab(page, i18n("Advanced"));
0473         QVBoxLayout *pageTop = new QVBoxLayout(page);
0474         //pageTop->setMargin(QApplication::style()->pixelMetric(QStyle::PM_DefaultChildMargin));
0475         //pageTop->setSpacing(QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing));
0476 
0477         QGroupBox *group = new QGroupBox(page);
0478         group->setTitle( i18n("Registration Data") );
0479         pageTop->addWidget(group);
0480         QGridLayout *groupLayout = new QGridLayout(group);
0481         //groupLayout->setSpacing(QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing));
0482 
0483         label = new QLabel(i18n("Nickname:"), group);
0484         groupLayout->addWidget(label, 0, 0);
0485         _registeredName = new QLineEdit(group);
0486         _registeredName->setReadOnly(true);
0487         groupLayout->addWidget(_registeredName, 0, 1);
0488 
0489         label = new QLabel(i18n("Key:"), group);
0490         groupLayout->addWidget(label, 1, 0);
0491         _key = new QLineEdit(group);
0492         _key->setReadOnly(true);
0493         groupLayout->addWidget(_key, 1, 1);
0494 
0495         KGuiItem gi = KStandardGuiItem::clear();
0496         gi.setText(i18n("Remove"));
0497         _removeButton = new QPushButton(group);
0498     KGuiItem::assign(_removeButton, gi);
0499     groupLayout->addWidget(_removeButton, 2, 0);
0500         connect(_removeButton, &QAbstractButton::clicked, this, &ConfigDialog::removeSlot);
0501     }
0502     
0503     buttonBox = new QDialogButtonBox(this);
0504     
0505     buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Apply | QDialogButtonBox::Cancel); 
0506     connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0507     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0508     // TODO mapping for Apply button
0509     pageTop->addWidget(buttonBox);
0510 
0511     load();
0512     buttonBox->button(QDialogButtonBox::Ok)->setEnabled( !_nickname->text().isEmpty() );
0513     buttonBox->button(QDialogButtonBox::Apply)->setEnabled( false );
0514     
0515 }
0516 
0517 void ConfigDialog::nickNameChanged(const QString &text)
0518 {
0519     buttonBox->button(QDialogButtonBox::Ok)->setEnabled( !text.isEmpty() );
0520 }
0521 
0522 
0523 void ConfigDialog::modifiedSlot()
0524 {
0525     buttonBox->button(QDialogButtonBox::Apply)->setEnabled(true && !_nickname->text().isEmpty() );
0526 }
0527 
0528 void ConfigDialog::accept()
0529 {
0530     if ( save() ) {
0531         QDialog::accept();
0532         KSharedConfig::openConfig()->sync(); // safer
0533     }
0534 }
0535 
0536 void ConfigDialog::removeSlot()
0537 {
0538     KGuiItem gi = KStandardGuiItem::clear();
0539     gi.setText(i18n("Remove"));
0540     int res = KMessageBox::warningContinueCancel(this,
0541                                i18n("This will permanently remove your "
0542                                "registration key. You will not be able to use "
0543                                "the currently registered nickname anymore."),
0544                                QString(), gi);
0545     if ( res==KMessageBox::Continue ) {
0546         internal->playerInfos().removeKey();
0547         _registeredName->clear();
0548         _key->clear();
0549         _removeButton->setEnabled(false);
0550         _WWHEnabled->setChecked(false);
0551         modifiedSlot();
0552     }
0553 }
0554 
0555 void ConfigDialog::load()
0556 {
0557     internal->hsConfig().readCurrentConfig();
0558     const PlayerInfos &infos = internal->playerInfos();
0559     _nickname->setText(infos.isAnonymous() ? QString() : infos.name());
0560     _comment->setText(infos.comment());
0561     if (_WWHEnabled) {
0562         _WWHEnabled->setChecked(infos.isWWEnabled());
0563         if ( !infos.key().isEmpty() ) {
0564             _registeredName->setText(infos.registeredName());
0565             _registeredName->home(false);
0566             _key->setText(infos.key());
0567             _key->home(false);
0568         }
0569         _removeButton->setEnabled(!infos.key().isEmpty());
0570     }
0571 }
0572 
0573 bool ConfigDialog::save()
0574 {
0575     bool enabled = (_WWHEnabled ? _WWHEnabled->isChecked() : false);
0576 
0577     // do not bother the user with "nickname empty" if he has not
0578     // messed with nickname settings ...
0579     QString newName = _nickname->text();
0580     if ( newName.isEmpty() && !internal->playerInfos().isAnonymous()
0581          && !enabled ) return true;
0582 
0583     if ( newName.isEmpty() ) {
0584         KMessageBox::error(this, i18n("Please choose a non empty nickname."));
0585         return false;
0586     }
0587     if ( internal->playerInfos().isNameUsed(newName) ) {
0588         KMessageBox::error(this, i18n("Nickname already in use. Please "
0589                                       "choose another one"));
0590         return false;
0591     }
0592 
0593     int res =
0594         internal->modifySettings(newName, _comment->text(), enabled, this);
0595     if (res) {
0596         load(); // needed to update view when "apply" is clicked
0597         buttonBox->button(QDialogButtonBox::Apply)->setEnabled(false);
0598     }
0599     _saved = true;
0600     return res;
0601 }
0602 
0603 //-----------------------------------------------------------------------------
0604 AskNameDialog::AskNameDialog(QWidget *parent)
0605     : QDialog(parent)
0606 {
0607 //     kDebug(11001) << ": AskNameDialog";
0608 
0609     setWindowTitle(i18nc("@title:window", "Enter Your Nickname"));
0610 
0611     internal->hsConfig().readCurrentConfig();
0612     QVBoxLayout *top = new QVBoxLayout;
0613     //top->setMargin( QApplication::style()->pixelMetric(QStyle::PM_DefaultChildMargin) );
0614     //top->setSpacing( QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing) );
0615     setLayout(top);
0616         
0617     QLabel *label =
0618         new QLabel(i18n("Congratulations, you have won!"), this);
0619     top->addWidget(label);
0620 
0621     QHBoxLayout *hbox = new QHBoxLayout;
0622     top->addLayout(hbox);
0623     label = new QLabel(i18n("Enter your nickname:"), this);
0624     hbox->addWidget(label);
0625     _edit = new QLineEdit(this);
0626     _edit->setFocus();
0627     connect(_edit, &QLineEdit::textChanged, this, &AskNameDialog::nameChanged);
0628     hbox->addWidget(_edit);
0629 
0630     //top->addSpacing(QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing));
0631     _checkbox = new QCheckBox(i18n("Do not ask again."),  this);
0632     top->addWidget(_checkbox);
0633     
0634     _buttonBox = new QDialogButtonBox(this);
0635     
0636     _buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0637     connect(_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0638     connect(_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0639     top->addWidget(_buttonBox);
0640 
0641     nameChanged();
0642 }
0643 
0644 void AskNameDialog::nameChanged()
0645 {
0646     _buttonBox->button(QDialogButtonBox::Ok)->setEnabled( !name().isEmpty()
0647                       && !internal->playerInfos().isNameUsed(name())); 
0648 }
0649 
0650 } // namespace