libkdegames Library API Documentation

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., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include <config.h>
00021 
00022 #include "kexthighscore_tab.h"
00023 #include "kexthighscore_tab.moc"
00024 
00025 #include <qlayout.h>
00026 #include <qlabel.h>
00027 #include <qvgroupbox.h>
00028 #include <qgrid.h>
00029 #include <qheader.h>
00030 
00031 #include <kdialogbase.h>
00032 #include <klistview.h>
00033 #include <kdebug.h>
00034 #include <kglobal.h>
00035 
00036 #include "kexthighscore.h"
00037 #include "kexthighscore_internal.h"
00038 
00039 
00040 namespace KExtHighscore
00041 {
00042 
00043 //-----------------------------------------------------------------------------
00044 PlayersCombo::PlayersCombo(QWidget *parent, const char *name)
00045     : QComboBox(parent, name)
00046 {
00047     const PlayerInfos &p = internal->playerInfos();
00048     for (uint i = 0; i<p.nbEntries(); i++)
00049         insertItem(p.prettyName(i));
00050     insertItem(QString("<") + i18n("all") + '>');
00051     connect(this, SIGNAL(activated(int)), SLOT(activatedSlot(int)));
00052 }
00053 
00054 void PlayersCombo::activatedSlot(int i)
00055 {
00056     const PlayerInfos &p = internal->playerInfos();
00057     if ( i==(int)p.nbEntries() ) emit allSelected();
00058     else if ( i==(int)p.nbEntries()+1 ) emit noneSelected();
00059     else emit playerSelected(i);
00060 }
00061 
00062 void PlayersCombo::load()
00063 {
00064     const PlayerInfos &p = internal->playerInfos();
00065     for (uint i = 0; i<p.nbEntries(); i++)
00066         changeItem(p.prettyName(i), i);
00067 }
00068 
00069 //-----------------------------------------------------------------------------
00070 AdditionalTab::AdditionalTab(QWidget *parent, const char *name)
00071     : QWidget(parent, name)
00072 {
00073     QVBoxLayout *top = new QVBoxLayout(this, KDialogBase::marginHint(),
00074                                        KDialogBase::spacingHint());
00075 
00076     QHBoxLayout *hbox = new QHBoxLayout(top);
00077     QLabel *label = new QLabel(i18n("Select player:"), this);
00078     hbox->addWidget(label);
00079     _combo = new PlayersCombo(this);
00080     connect(_combo, SIGNAL(playerSelected(uint)),
00081             SLOT(playerSelected(uint)));
00082     connect(_combo, SIGNAL(allSelected()), SLOT(allSelected()));
00083     hbox->addWidget(_combo);
00084     hbox->addStretch(1);
00085 }
00086 
00087 void AdditionalTab::init()
00088 {
00089     uint id = internal->playerInfos().id();
00090     _combo->setCurrentItem(id);
00091     playerSelected(id);
00092 }
00093 
00094 void AdditionalTab::allSelected()
00095 {
00096     display(internal->playerInfos().nbEntries());
00097 }
00098 
00099 QString AdditionalTab::percent(uint n, uint total, bool withBraces)
00100 {
00101     if ( n==0 || total==0 ) return QString::null;
00102     QString s =  QString("%1%").arg(100.0 * n / total, 0, 'f', 1);
00103     return (withBraces ? QString("(") + s + ")" : s);
00104 }
00105 
00106 void AdditionalTab::load()
00107 {
00108     _combo->load();
00109 }
00110 
00111 
00112 //-----------------------------------------------------------------------------
00113 const char *StatisticsTab::COUNT_LABELS[Nb_Counts] = {
00114     I18N_NOOP("Total:"), I18N_NOOP("Won:"), I18N_NOOP("Lost:"),
00115     I18N_NOOP("Draw:")
00116 };
00117 const char *StatisticsTab::TREND_LABELS[Nb_Trends] = {
00118     I18N_NOOP("Current:"), I18N_NOOP("Max won:"), I18N_NOOP("Max lost:")
00119 };
00120 
00121 StatisticsTab::StatisticsTab(QWidget *parent)
00122     : AdditionalTab(parent, "statistics_tab")
00123 {
00124     // construct GUI
00125     QVBoxLayout *top = static_cast<QVBoxLayout *>(layout());
00126 
00127     QHBoxLayout *hbox = new QHBoxLayout(top);
00128     QVBoxLayout *vbox = new QVBoxLayout(hbox);
00129     QVGroupBox *group = new QVGroupBox(i18n("Game Counts"), this);
00130     vbox->addWidget(group);
00131     QGrid *grid = new QGrid(3, group);
00132     grid->setSpacing(KDialogBase::spacingHint());
00133     for (uint k=0; k<Nb_Counts; k++) {
00134         if ( Count(k)==Draw && !internal->showDrawGames ) continue;
00135         (void)new QLabel(i18n(COUNT_LABELS[k]), grid);
00136         _nbs[k] = new QLabel(grid);
00137         _percents[k] = new QLabel(grid);
00138     }
00139 
00140     group = new QVGroupBox(i18n("Trends"), this);
00141     vbox->addWidget(group);
00142     grid = new QGrid(2, group);
00143     grid->setSpacing(KDialogBase::spacingHint());
00144     for (uint k=0; k<Nb_Trends; k++) {
00145         (void)new QLabel(i18n(TREND_LABELS[k]), grid);
00146         _trends[k] = new QLabel(grid);
00147     }
00148 
00149     hbox->addStretch(1);
00150     top->addStretch(1);
00151 }
00152 
00153 void StatisticsTab::load()
00154 {
00155     AdditionalTab::load();
00156     const PlayerInfos &pi = internal->playerInfos();
00157     uint nb = pi.nbEntries();
00158     _data.resize(nb+1);
00159     for (uint i=0; i<_data.size()-1; i++) {
00160         _data[i].count[Total] = pi.item("nb games")->read(i).toUInt();
00161         _data[i].count[Lost] = pi.item("nb lost games")->read(i).toUInt()
00162                        + pi.item("nb black marks")->read(i).toUInt(); // legacy
00163         _data[i].count[Draw] = pi.item("nb draw games")->read(i).toUInt();
00164         _data[i].count[Won] = _data[i].count[Total] - _data[i].count[Lost]
00165                               - _data[i].count[Draw];
00166         _data[i].trend[CurrentTrend] =
00167             pi.item("current trend")->read(i).toInt();
00168         _data[i].trend[WonTrend] = pi.item("max won trend")->read(i).toUInt();
00169         _data[i].trend[LostTrend] =
00170             -(int)pi.item("max lost trend")->read(i).toUInt();
00171     }
00172 
00173     for (uint k=0; k<Nb_Counts; k++) _data[nb].count[k] = 0;
00174     for (uint k=0; k<Nb_Trends; k++) _data[nb].trend[k] = 0;
00175     for (uint i=0; i<_data.size()-1; i++) {
00176         for (uint k=0; k<Nb_Counts; k++)
00177             _data[nb].count[k] += _data[i].count[k];
00178         for (uint k=0; k<Nb_Trends; k++)
00179             _data[nb].trend[k] += _data[i].trend[k];
00180     }
00181     for (uint k=0; k<Nb_Trends; k++)
00182         _data[nb].trend[k] /= (_data.size()-1);
00183 
00184     init();
00185 }
00186 
00187 QString StatisticsTab::percent(const Data &d, Count count) const
00188 {
00189     if ( count==Total ) return QString::null;
00190     return AdditionalTab::percent(d.count[count], d.count[Total], true);
00191 }
00192 
00193 void StatisticsTab::display(uint i)
00194 {
00195     const Data &d = _data[i];
00196     for (uint k=0; k<Nb_Counts; k++) {
00197         if ( Count(k) && !internal->showDrawGames ) continue;
00198         _nbs[k]->setText(QString::number(d.count[k]));
00199         _percents[k]->setText(percent(d, Count(k)));
00200     }
00201     for (uint k=0; k<Nb_Trends; k++) {
00202         QString s;
00203         if ( d.trend[k]>0 ) s = '+';
00204         int prec = (i==internal->playerInfos().nbEntries() ? 1 : 0);
00205         _trends[k]->setText(s + QString::number(d.trend[k], 'f', prec));
00206     }
00207 }
00208 
00209 //-----------------------------------------------------------------------------
00210 HistogramTab::HistogramTab(QWidget *parent)
00211     : AdditionalTab(parent, "histogram_tab")
00212 {
00213     // construct GUI
00214     QVBoxLayout *top = static_cast<QVBoxLayout *>(layout());
00215 
00216     _list = new KListView(this);
00217     _list->setSelectionMode(QListView::NoSelection);
00218     _list->setItemMargin(3);
00219     _list->setAllColumnsShowFocus(true);
00220     _list->setSorting(-1);
00221     _list->header()->setClickEnabled(false);
00222     _list->header()->setMovingEnabled(false);
00223     top->addWidget(_list);
00224 
00225     _list->addColumn(i18n("From"));
00226     _list->addColumn(i18n("To"));
00227     _list->addColumn(i18n("Count"));
00228     _list->addColumn(i18n("Percent"));
00229     for (uint i=0; i<4; i++) _list->setColumnAlignment(i, AlignRight);
00230     _list->addColumn(QString::null);
00231 
00232     const Item *sitem = internal->scoreInfos().item("score")->item();
00233     const PlayerInfos &pi = internal->playerInfos();
00234     const QMemArray<uint> &sh = pi.histogram();
00235     for (uint k=1; k<pi.histoSize(); k++) {
00236         QString s1 = sitem->pretty(0, sh[k-1]);
00237         QString s2;
00238         if ( k==sh.size() ) s2 = "...";
00239         else if ( sh[k]!=sh[k-1]+1 ) s2 = sitem->pretty(0, sh[k]);
00240         (void)new KListViewItem(_list, s1, s2);
00241     }
00242 }
00243 
00244 void HistogramTab::load()
00245 {
00246     AdditionalTab::load();
00247     const PlayerInfos &pi = internal->playerInfos();
00248     uint n = pi.nbEntries();
00249     uint s = pi.histoSize() - 1;
00250     _counts.resize((n+1) * s);
00251     _data.fill(0, n+1);
00252     for (uint k=0; k<s; k++) {
00253         _counts[n*s + k] = 0;
00254         for (uint i=0; i<n; i++) {
00255             uint nb = pi.item(pi.histoName(k+1))->read(i).toUInt();
00256             _counts[i*s + k] = nb;
00257             _counts[n*s + k] += nb;
00258             _data[i] += nb;
00259             _data[n] += nb;
00260         }
00261     }
00262 
00263     init();
00264 }
00265 
00266 void HistogramTab::display(uint i)
00267 {
00268     const PlayerInfos &pi = internal->playerInfos();
00269     QListViewItem *item = _list->firstChild();
00270     uint s = pi.histoSize() - 1;
00271     for (int k=s-1; k>=0; k--) {
00272         uint nb = _counts[i*s + k];
00273         item->setText(2, QString::number(nb));
00274         item->setText(3, percent(nb, _data[i]));
00275         uint width = (_data[i]==0 ? 0 : qRound(150.0 * nb / _data[i]));
00276         QPixmap pixmap(width, 10);
00277         pixmap.fill(blue);
00278         item->setPixmap(4, pixmap);
00279         item = item->nextSibling();
00280     }
00281 }
00282 
00283 } // namespace
KDE Logo
This file is part of the documentation for libkdegames Library Version 3.4.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 9 09:38:12 2005 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003