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 = '+';
00202         int prec = (i==internal->playerInfos().nbEntries() ? 1 : 0);
00203         _trends[k]->setText(s + QString::number(d.trend[k], 'f', prec));
00204     }
00205 }
00206 
00207 //-----------------------------------------------------------------------------
00208 HistogramTab::HistogramTab(QWidget *parent)
00209     : AdditionalTab(parent, "histogram_tab")
00210 {
00211     // construct GUI
00212     QVBoxLayout *top = static_cast<QVBoxLayout *>(layout());
00213 
00214     _list = new KListView(this);
00215     _list->setSelectionMode(QListView::NoSelection);
00216     _list->setItemMargin(3);
00217     _list->setAllColumnsShowFocus(true);
00218     _list->setSorting(-1);
00219     _list->header()->setClickEnabled(false);
00220     _list->header()->setMovingEnabled(false);
00221     top->addWidget(_list);
00222 
00223     _list->addColumn(i18n("From"));
00224     _list->addColumn(i18n("To"));
00225     _list->addColumn(i18n("Count"));
00226     _list->addColumn(i18n("Percent"));
00227     for (uint i=0; i<4; i++) _list->setColumnAlignment(i, AlignRight);
00228     _list->addColumn(QString::null);
00229 
00230     const Item *sitem = internal->scoreInfos().item("score")->item();
00231     const PlayerInfos &pi = internal->playerInfos();
00232     const QMemArray<uint> &sh = pi.histogram();
00233     for (uint k=1; k<pi.histoSize(); k++) {
00234         QString s1 = sitem->pretty(0, sh[k-1]);
00235         QString s2;
00236         if ( k==sh.size() ) s2 = "...";
00237         else if ( sh[k]!=sh[k-1]+1 ) s2 = sitem->pretty(0, sh[k]);
00238         (void)new KListViewItem(_list, s1, s2);
00239     }
00240 }
00241 
00242 void HistogramTab::load()
00243 {
00244     AdditionalTab::load();
00245     const PlayerInfos &pi = internal->playerInfos();
00246     uint n = pi.nbEntries();
00247     uint s = pi.histoSize() - 1;
00248     _counts.resize((n+1) * s);
00249     _data.fill(0, n+1);
00250     for (uint k=0; k<s; k++) {
00251         _counts[n*s + k] = 0;
00252         for (uint i=0; i<n; i++) {
00253             uint nb = pi.item(pi.histoName(k+1))->read(i).toUInt();
00254             _counts[i*s + k] = nb;
00255             _counts[n*s + k] += nb;
00256             _data[i] += nb;
00257             _data[n] += nb;
00258         }
00259     }
00260 
00261     init();
00262 }
00263 
00264 void HistogramTab::display(uint i)
00265 {
00266     const PlayerInfos &pi = internal->playerInfos();
00267     QListViewItem *item = _list->firstChild();
00268     uint s = pi.histoSize() - 1;
00269     for (int k=s-1; k>=0; k--) {
00270         uint nb = _counts[i*s + k];
00271         item->setText(2, QString::number(nb));
00272         item->setText(3, percent(nb, _data[i]));
00273         uint width = (_data[i]==0 ? 0 : qRound(150.0 * nb / _data[i]));
00274         QPixmap pixmap(width, 10);
00275         pixmap.fill(blue);
00276         item->setPixmap(4, pixmap);
00277         item = item->nextSibling();
00278     }
00279 }
00280 
00281 } // namespace
KDE Home | KDE Accessibility Home | Description of Access Keys