File indexing completed on 2024-05-12 08:00:36

0001 /*
0002  * Copyright (C) 2004 Bart Vanhauwaert <bvh-cplusplus@irule.be>
0003  * Copyright (C) 2005-2008 Stephan Kulow <coolo@kde.org>
0004  * Copyright (C) 2008-2009 Parker Coates <coates@kde.org>
0005  *
0006  * License of original code:
0007  * -------------------------------------------------------------------------
0008  *   Permission to use, copy, modify, and distribute this software and its
0009  *   documentation for any purpose and without fee is hereby granted,
0010  *   provided that the above copyright notice appear in all copies and that
0011  *   both that copyright notice and this permission notice appear in
0012  *   supporting documentation.
0013  *
0014  *   This file is provided AS IS with no warranties of any kind.  The author
0015  *   shall have no liability with respect to the infringement of copyrights,
0016  *   trade secrets or any patents by this file or any part thereof.  In no
0017  *   event will the author be liable for any lost revenue or profits or
0018  *   other special, indirect and consequential damages.
0019  * -------------------------------------------------------------------------
0020  *
0021  * License of modifications/additions made after 2009-01-01:
0022  * -------------------------------------------------------------------------
0023  *   This program is free software; you can redistribute it and/or
0024  *   modify it under the terms of the GNU General Public License as
0025  *   published by the Free Software Foundation; either version 2 of
0026  *   the License, or (at your option) any later version.
0027  *
0028  *   This program is distributed in the hope that it will be useful,
0029  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0030  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0031  *   GNU General Public License for more details.
0032  *
0033  *   You should have received a copy of the GNU General Public License
0034  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
0035  * -------------------------------------------------------------------------
0036  */
0037 
0038 #include "statisticsdialog.h"
0039 
0040 // own
0041 #include "dealer.h"
0042 #include "dealerinfo.h"
0043 #include "kpat_debug.h"
0044 // KF
0045 #include <KConfigGroup>
0046 #include <KLocalizedString>
0047 #include <KSharedConfig>
0048 // Qt
0049 #include <QDialogButtonBox>
0050 #include <QPushButton>
0051 #include <QVBoxLayout>
0052 
0053 StatisticsDialog::StatisticsDialog(QWidget *aParent)
0054     : QDialog(aParent)
0055     , indexToIdMap()
0056 {
0057     QWidget *widget = new QWidget(this);
0058     ui = new Ui::GameStats();
0059     ui->setupUi(widget);
0060 
0061     setWindowTitle(i18n("Statistics"));
0062 
0063     QVBoxLayout *mainLayout = new QVBoxLayout;
0064     setLayout(mainLayout);
0065     mainLayout->addWidget(widget);
0066     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close | QDialogButtonBox::Reset);
0067     connect(buttonBox, &QDialogButtonBox::accepted, this, &StatisticsDialog::accept);
0068     connect(buttonBox, &QDialogButtonBox::rejected, this, &StatisticsDialog::reject);
0069     mainLayout->addWidget(buttonBox);
0070     buttonBox->button(QDialogButtonBox::Close)->setDefault(true);
0071 
0072     QMap<QString, int> nameToIdMap;
0073     const auto games = DealerInfoList::self()->games();
0074     for (DealerInfo *game : games) {
0075         const auto ids = game->distinctIds();
0076         for (int id : ids)
0077             nameToIdMap.insert(game->nameForId(id), id);
0078     }
0079 
0080     QMap<QString, int>::const_iterator it = nameToIdMap.constBegin();
0081     QMap<QString, int>::const_iterator end = nameToIdMap.constEnd();
0082     for (; it != end; ++it) {
0083         // Map combobox indices to game IDs
0084         indexToIdMap[ui->GameType->count()] = it.value();
0085         ui->GameType->addItem(it.key());
0086     }
0087 
0088     ui->GameType->setFocus();
0089     ui->GameType->setMaxVisibleItems(indexToIdMap.size());
0090 
0091     showGameType(indexToIdMap[0]);
0092 
0093     connect(ui->GameType, static_cast<void (KComboBox::*)(int)>(&KComboBox::activated), this, &StatisticsDialog::selectionChanged);
0094     connect(buttonBox->button(QDialogButtonBox::Reset), &QPushButton::clicked, this, &StatisticsDialog::resetStats);
0095 }
0096 
0097 StatisticsDialog::~StatisticsDialog()
0098 {
0099     delete ui;
0100 }
0101 
0102 void StatisticsDialog::selectionChanged(int comboIndex)
0103 {
0104     int gameIndex = indexToIdMap[comboIndex];
0105     setGameType(gameIndex);
0106 }
0107 
0108 void StatisticsDialog::showGameType(int gameIndex)
0109 {
0110     int comboIndex = indexToIdMap.key(gameIndex);
0111     ui->GameType->setCurrentIndex(comboIndex);
0112     setGameType(gameIndex);
0113 }
0114 
0115 void StatisticsDialog::setGameType(int gameIndex)
0116 {
0117     KConfigGroup cg(KSharedConfig::openConfig(), scores_group);
0118     unsigned int t = cg.readEntry(QStringLiteral("total%1").arg(gameIndex), 0);
0119     ui->Played->setText(QString::number(t));
0120     unsigned int w = cg.readEntry(QStringLiteral("won%1").arg(gameIndex), 0);
0121     if (t)
0122         ui->Won->setText(i18n("%1 (%2%)", w, w * 100 / t));
0123     else
0124         ui->Won->setText(QString::number(w));
0125     ui->WinStreak->setText(QString::number(cg.readEntry(QStringLiteral("maxwinstreak%1").arg(gameIndex), 0)));
0126     ui->LoseStreak->setText(QString::number(cg.readEntry(QStringLiteral("maxloosestreak%1").arg(gameIndex), 0)));
0127     int minMoves = cg.readEntry(QStringLiteral("minmoves%1").arg(gameIndex), -1);
0128     if (minMoves < 0)
0129         ui->MinMoves->setText(QStringLiteral("∞"));
0130     else
0131         ui->MinMoves->setText(QString::number(minMoves));
0132     unsigned int l = cg.readEntry(QStringLiteral("loosestreak%1").arg(gameIndex), 0);
0133     if (l)
0134         ui->CurrentStreak->setText(i18np("1 loss", "%1 losses", l));
0135     else
0136         ui->CurrentStreak->setText(i18np("1 win", "%1 wins", cg.readEntry(QStringLiteral("winstreak%1").arg(gameIndex), 0)));
0137 }
0138 
0139 void StatisticsDialog::resetStats()
0140 {
0141     int gameIndex = indexToIdMap[ui->GameType->currentIndex()];
0142     Q_ASSERT(gameIndex >= 0);
0143     KConfigGroup cg(KSharedConfig::openConfig(), scores_group);
0144     cg.writeEntry(QStringLiteral("total%1").arg(gameIndex), 0);
0145     cg.writeEntry(QStringLiteral("won%1").arg(gameIndex), 0);
0146     cg.writeEntry(QStringLiteral("maxwinstreak%1").arg(gameIndex), 0);
0147     cg.writeEntry(QStringLiteral("maxloosestreak%1").arg(gameIndex), 0);
0148     cg.writeEntry(QStringLiteral("loosestreak%1").arg(gameIndex), 0);
0149     cg.writeEntry(QStringLiteral("winstreak%1").arg(gameIndex), 0);
0150     cg.writeEntry(QStringLiteral("minmoves%1").arg(gameIndex), -1);
0151     cg.sync();
0152 
0153     setGameType(gameIndex);
0154 }
0155 
0156 #include "moc_statisticsdialog.cpp"
0157 
0158 // kate: replace-tabs off; replace-tabs-save off