1/*
2 * Copyright (C) 2004 Bart Vanhauwaert <bvh-cplusplus@irule.be>
3 * Copyright (C) 2005-2008 Stephan Kulow <coolo@kde.org>
4 * Copyright (C) 2008-2009 Parker Coates <coates@kde.org>
5 *
6 * License of original code:
7 * -------------------------------------------------------------------------
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation for any purpose and without fee is hereby granted,
10 * provided that the above copyright notice appear in all copies and that
11 * both that copyright notice and this permission notice appear in
12 * supporting documentation.
13 *
14 * This file is provided AS IS with no warranties of any kind. The author
15 * shall have no liability with respect to the infringement of copyrights,
16 * trade secrets or any patents by this file or any part thereof. In no
17 * event will the author be liable for any lost revenue or profits or
18 * other special, indirect and consequential damages.
19 * -------------------------------------------------------------------------
20 *
21 * License of modifications/additions made after 2009-01-01:
22 * -------------------------------------------------------------------------
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License as
25 * published by the Free Software Foundation; either version 2 of
26 * the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 * -------------------------------------------------------------------------
36 */
37
38#include "statisticsdialog.h"
39
40#include "dealerinfo.h"
41#include "version.h"
42
43#include <KConfigGroup>
44#include <KDebug>
45#include <KGlobal>
46#include <KLocale>
47
48#include <QtCore/QList>
49
50
51StatisticsDialog::StatisticsDialog(QWidget* aParent)
52 : KDialog(aParent),
53 indexToIdMap()
54{
55 QWidget* widget = new QWidget(this);
56 ui = new Ui::GameStats();
57 ui->setupUi(widget);
58
59 setWindowTitle(i18n("Statistics"));
60 setMainWidget(widget);
61 setButtons(KDialog::Reset | KDialog::Close);
62 setDefaultButton(KDialog::Close);
63
64
65 QMap<QString,int> nameToIdMap;
66 foreach ( DealerInfo * game, DealerInfoList::self()->games() )
67 foreach ( int id, game->distinctIds() )
68 nameToIdMap.insert( game->nameForId( id ), id );
69
70 QMap<QString,int>::const_iterator it = nameToIdMap.constBegin();
71 QMap<QString,int>::const_iterator end = nameToIdMap.constEnd();
72 for (; it != end; ++it)
73 {
74 // Map combobox indices to game IDs
75 indexToIdMap[ui->GameType->count()] = it.value();
76 ui->GameType->addItem(it.key());
77 }
78
79 ui->GameType->setFocus();
80 ui->GameType->setMaxVisibleItems(indexToIdMap.size());
81
82 showGameType(indexToIdMap[0]);
83
84 connect(ui->GameType, SIGNAL(activated(int)), SLOT(selectionChanged(int)));
85 connect(this, SIGNAL(resetClicked()), SLOT(resetStats()));
86}
87
88StatisticsDialog::~StatisticsDialog()
89{
90 delete ui;
91}
92
93void StatisticsDialog::selectionChanged(int comboIndex)
94{
95 int gameIndex = indexToIdMap[comboIndex];
96 setGameType(gameIndex);
97}
98
99void StatisticsDialog::showGameType(int gameIndex)
100{
101 int comboIndex = indexToIdMap.key(gameIndex);
102 ui->GameType->setCurrentIndex(comboIndex);
103 setGameType(gameIndex);
104}
105
106void StatisticsDialog::setGameType(int gameIndex)
107{
108 KConfigGroup cg(KGlobal::config(), scores_group);
109 unsigned int t = cg.readEntry(QString("total%1").arg(gameIndex),0);
110 ui->Played->setText(QString::number(t));
111 unsigned int w = cg.readEntry(QString("won%1").arg(gameIndex),0);
112 if (t)
113 ui->Won->setText(i18n("%1 (%2%)", w, w*100/t));
114 else
115 ui->Won->setText( QString::number(w));
116 ui->WinStreak->setText( QString::number( cg.readEntry(QString("maxwinstreak%1").arg(gameIndex), 0)));
117 ui->LoseStreak->setText( QString::number( cg.readEntry(QString("maxloosestreak%1").arg(gameIndex), 0)));
118 unsigned int l = cg.readEntry(QString("loosestreak%1").arg(gameIndex),0);
119 if (l)
120 ui->CurrentStreak->setText( i18np("1 loss", "%1 losses", l) );
121 else
122 ui->CurrentStreak->setText( i18np("1 win", "%1 wins",
123 cg.readEntry(QString("winstreak%1").arg(gameIndex),0)) );
124}
125
126void StatisticsDialog::resetStats()
127{
128 int gameIndex = indexToIdMap[ui->GameType->currentIndex()];
129 Q_ASSERT(gameIndex >= 0);
130 KConfigGroup cg(KGlobal::config(), scores_group);
131 cg.writeEntry(QString("total%1").arg(gameIndex),0);
132 cg.writeEntry(QString("won%1").arg(gameIndex),0);
133 cg.writeEntry(QString("maxwinstreak%1").arg(gameIndex),0);
134 cg.writeEntry(QString("maxloosestreak%1").arg(gameIndex),0);
135 cg.writeEntry(QString("loosestreak%1").arg(gameIndex),0);
136 cg.writeEntry(QString("winstreak%1").arg(gameIndex),0);
137 cg.sync();
138
139 setGameType(gameIndex);
140}
141
142#include "statisticsdialog.moc"
143
144// kate: replace-tabs off; replace-tabs-save off
145