kexthighscore_tab.cpp

00001 /*
00002     This file is part of the KDE games library
00003     Copyright (C) 2002 Nicolas Hadacek (hadacek@kde.org)
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License version 2 as published by the Free Software Foundation.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kexthighscore_tab.h"
00021 #include "kexthighscore_tab.moc"
00022 
00023 #include <qlayout.h>
00024 #include <qlabel.h>
00025 #include <qvgroupbox.h>
00026 #include <qgrid.h>
00027 #include <qheader.h>
00028 
00029 #include <kdialogbase.h>
00030 #include <klistview.h>
00031 #include <kdebug.h>
00032 #include <kglobal.h>
00033 
00034 #include "kexthighscore.h"
00035 #include "kexthighscore_internal.h"
00036 
00037 
00038 namespace KExtHighscore
00039 {
00040 
00041 //-----------------------------------------------------------------------------
00042 PlayersCombo::PlayersCombo(QWidget *parent, const char *name)
00043     : QComboBox(parent, name)
00044 {
00045     const PlayerInfos &p = internal->playerInfos();
00046     for (uint i = 0; i<p.nbEntries(); i++)
00047         insertItem(p.prettyName(i));
00048     insertItem(QString("<") + i18n("all") + '>');
00049     connect(this, SIGNAL(activated(int)), SLOT(activatedSlot(int)));
00050 }
00051 
00052 void PlayersCombo::activatedSlot(int i)
00053 {
00054     const PlayerInfos &p = internal->playerInfos();
00055     if ( i==(int)p.nbEntries() ) emit allSelected();
00056     else if ( i==(int)p.nbEntries()+1 ) emit noneSelected();
00057     else emit playerSelected(i);
00058 }
00059 
00060 void PlayersCombo::load()
00061 {
00062     const PlayerInfos &p = internal->playerInfos();
00063     for (uint i = 0; i<p.nbEntries(); i++)
00064         changeItem(p.prettyName(i), i);
00065 }
00066 
00067 //-----------------------------------------------------------------------------
00068 AdditionalTab::AdditionalTab(QWidget *parent, const char *name)
00069     : QWidget(parent, name)
00070 {
00071     QVBoxLayout *top = new QVBoxLayout(this, KDialogBase::marginHint(),
00072                                        KDialogBase::spacingHint());
00073 
00074     QHBoxLayout *hbox = new QHBoxLayout(top);
00075     QLabel *label = new QLabel(i18n("Select player:"), this);
00076     hbox->addWidget(label);
00077     _combo = new PlayersCombo(this);
00078     connect(_combo, SIGNAL(playerSelected(uint)),
00079             SLOT(playerSelected(uint)));
00080     connect(_combo, SIGNAL(allSelected()), SLOT(allSelected()));
00081     hbox->addWidget(_combo);
00082     hbox->addStretch(1);
00083 }
00084 
00085 void AdditionalTab::init()
00086 {
00087     uint id = internal->playerInfos().id();
00088     _combo->setCurrentItem(id);
00089     playerSelected(id);
00090 }
00091 
00092 void AdditionalTab::allSelected()
00093 {
00094     display(internal->playerInfos().nbEntries());
00095 }
00096 
00097 QString AdditionalTab::percent(uint n, uint total, bool withBraces)
00098 {
00099     if ( n==0 || total==0 ) return QString::null;
00100     QString s =  QString("%1%").arg(100.0 * n / total, 0, 'f', 1);
00101     return (withBraces ? QString("(") + s + ")" : s);
00102 }
00103 
00104 void AdditionalTab::load()
00105 {
00106     _combo->load();
00107 }
00108 
00109 
00110 //-----------------------------------------------------------------------------
00111 const char *StatisticsTab::COUNT_LABELS[Nb_Counts] = {
00112     I18N_NOOP("Total:"), I18N_NOOP("Won:"), I18N_NOOP("Lost:"),
00113     I18N_NOOP("Draw:")
00114 };
00115 const char *StatisticsTab::TREND_LABELS[Nb_Trends] = {
00116     I18N_NOOP("Current:"), I18N_NOOP("Max won:"), I18N_NOOP("Max lost:")
00117 };
00118 
00119 StatisticsTab::StatisticsTab(QWidget *parent)
00120     : AdditionalTab(parent, "statistics_tab")
00121 {
00122     // construct GUI
00123     QVBoxLayout *top = static_cast<QVBoxLayout *>(layout());
00124 
00125     QHBoxLayout *hbox = new QHBoxLayout(top);
00126     QVBoxLayout *vbox = new QVBoxLayout(hbox);
00127     QVGroupBox *group = new QVGroupBox(i18n("Game Counts"), this);
00128     vbox->addWidget(group);
00129     QGrid *grid = new QGrid(3, group);
00130     grid->setSpacing(KDialogBase::spacingHint());
00131     for (uint k=0; k<Nb_Counts; k++) {
00132         if ( Count(k)==Draw && !internal->showDrawGames ) continue;
00133         (void)new QLabel(i18n(COUNT_LABELS[k]), grid);
00134         _nbs[k] = new QLabel(grid);
00135         _percents[k] = new QLabel(grid);
00136     }
00137 
00138     group = new QVGroupBox(i18n("Trends"), this);
00139     vbox->addWidget(group);
00140     grid = new QGrid(2, group);
00141     grid->setSpacing(KDialogBase::spacingHint());
00142     for (uint k=0; k<Nb_Trends; k++) {
00143         (void)new QLabel(i18n(TREND_LABELS[k]), grid);
00144         _trends[k] = new QLabel(grid);
00145     }
00146 
00147     hbox->addStretch(1);
00148     top->addStretch(1);
00149 }
00150 
00151 void StatisticsTab::load()
00152 {
00153     AdditionalTab::load();
00154     const PlayerInfos &pi = internal->playerInfos();
00155     uint nb = pi.nbEntries();
00156     _data.resize(nb+1);
00157     for (uint i=0; i<_data.size()-1; i++) {
00158         _data[i].count[Total] = pi.item("nb games")->read(i).toUInt();
00159         _data[i].count[Lost] = pi.item("nb lost games")->read(i).toUInt()
00160                        + pi.item("nb black marks")->read(i).toUInt(); // legacy
00161         _data[i].count[Draw] = pi.item("nb draw games")->read(i).toUInt();
00162         _data[i].count[Won] = _data[i].count[Total] - _data[i].count[Lost]
00163                               - _data[i].count[Draw];
00164         _data[i].trend[CurrentTrend] =
00165             pi.item("current trend")->read(i).toInt();
00166         _data[i].trend[WonTrend] = pi.item("max won trend")->read(i).toUInt();
00167         _data[i].trend[LostTrend] =
00168             -(int)pi.item("max lost trend")->read(i).toUInt();
00169     }
00170 
00171     for (uint k=0; k<Nb_Counts; k++) _data[nb].count[k] = 0;
00172     for (uint k=0; k<Nb_Trends; k++) _data[nb].trend[k] = 0;
00173     for (uint i=0; i<_data.size()-1; i++) {
00174         for (uint k=0; k<Nb_Counts; k++)
00175             _data[nb].count[k] += _data[i].count[k];
00176         for (uint k=0; k<Nb_Trends; k++)
00177             _data[nb].trend[k] += _data[i].trend[k];
00178     }
00179     for (uint k=0; k<Nb_Trends; k++)
00180         _data[nb].trend[k] /= (_data.size()-1);
00181 
00182     init();
00183 }
00184 
00185 QString StatisticsTab::percent(const Data &d, Count count) const
00186 {
00187     if ( count==Total ) return QString::null;
00188     return AdditionalTab::percent(d.count[count], d.count[Total], true);
00189 }
00190 
00191 void StatisticsTab::display(uint i)
00192 {
00193     const Data &d = _data[i];
00194     for (uint k=0; k<Nb_Counts; k++) {
00195         if ( Count(k) && !internal->showDrawGames ) continue;
00196         _nbs[k]->setText(QString::number(d.count[k]));
00197         _percents[k]->setText(percent(d, Count(k)));
00198     }
00199     for (uint k=0; k<Nb_Trends; k++) {
00200         QString s;
00201         if ( d.trend[k]>0 ) s = return data("score").toUInt(); }
00210 
00216     void setScore(uint score) { setData("score", score); }
00217 
00222     bool isTheWorst() const;
00223 
00229     bool operator <(const Score &score);
00230 
00231  private:
00232     ScoreType  _type;
00233     QMap<QString, QVariant> _data;
00234 
00235     class ScorePrivate;
00236     ScorePrivate *d;
00237 
00238     friend class MultiplayerScores;
00239 
00240     friend QDataStream &operator <<(QDataStream &stream, const Score &score);
00241     friend QDataStream &operator >>(QDataStream &stream, Score &score);
00242 };
00243 
00244 KDE_EXPORT QDataStream &operator <<(QDataStream &stream, const Score &score);
00245 KDE_EXPORT QDataStream &operator >>(QDataStream &stream, Score &score);
00246 
00267 class KDE_EXPORT MultiplayerScores
00268 {
00269  public:
00270     MultiplayerScores();
00271 
00272     ~MultiplayerScores();
00273 
00277     void setPlayerCount(uint nb);
00278 
00282     void setName(uint player, const QString &name);
00283 
00287     void addScore(uint player, const Score &score);
00288 
00292     void clear();
00293 
00297     void show(QWidget *parent);
00298 
00299  private:
00300     QValueVector<uint>  _nbGames;
00301     QValueVector<Score> _scores;
00302 
00303     class MultiplayerScoresPrivate;
00304     MultiplayerScoresPrivate *d;
00305 
00306     friend QDataStream &operator <<(QDataStream &stream,
00307                                     const MultiplayerScores &score);
00308     friend QDataStream &operator >>(QDataStream &stream,
00309                                     MultiplayerScores &score);
00310 };
00311 
00312 KDE_EXPORT QDataStream &operator <<(QDataStream &stream, const MultiplayerScores &score);
00313 KDE_EXPORT QDataStream &operator >>(QDataStream &stream, MultiplayerScores &score);
00314 
00315 } // namespace
00316 
00317 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys
./usr/share/doc/kde/HTML/en/kdegames-apidocs/libkdegames/html/kexthighscore__tab_8cpp-source.html0000644000000000000000000006051210374307104033132 0ustar rootroot00000000000000 libkdegames: kexthighscore_tab.cpp Source File (libkdegames)