• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkdegames/highscore

kexthighscore_tab.cpp

Go to the documentation of this file.
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>
00024 #include <QLabel>
00025 #include <QPixmap>
00026 #include <QVector>
00027 #include <QGroupBox>
00028 #include <QTreeWidget>
00029 #include <QHeaderView>
00030 
00031 #include <kdialog.h>
00032 #include <kdebug.h>
00033 #include <kglobal.h>
00034 
00035 #include "kexthighscore.h"
00036 #include "kexthighscore_internal.h"
00037 
00038 
00039 namespace KExtHighscore
00040 {
00041 
00042 //-----------------------------------------------------------------------------
00043 PlayersCombo::PlayersCombo(QWidget *parent)
00044     : QComboBox(parent)
00045 {
00046     const PlayerInfos &p = internal->playerInfos();
00047     for (uint i = 0; i<p.nbEntries(); i++)
00048         addItem(p.prettyName(i));
00049     addItem(QString("<") + i18n("all") + '>');
00050     connect(this, SIGNAL(activated(int)), SLOT(activatedSlot(int)));
00051 }
00052 
00053 void PlayersCombo::activatedSlot(int i)
00054 {
00055     const PlayerInfos &p = internal->playerInfos();
00056     if ( i==(int)p.nbEntries() ) emit allSelected();
00057     else if ( i==(int)p.nbEntries()+1 ) emit noneSelected();
00058     else emit playerSelected(i);
00059 }
00060 
00061 void PlayersCombo::load()
00062 {
00063     const PlayerInfos &p = internal->playerInfos();
00064     for (uint i = 0; i<p.nbEntries(); i++)
00065         setItemText(i, p.prettyName(i));
00066 }
00067 
00068 //-----------------------------------------------------------------------------
00069 AdditionalTab::AdditionalTab(QWidget *parent)
00070     : QWidget(parent)
00071 {
00072     QVBoxLayout *top = new QVBoxLayout(this);
00073     top->setMargin( KDialog::marginHint() );
00074     top->setSpacing( KDialog::spacingHint() );
00075 
00076     QHBoxLayout *hbox = new QHBoxLayout;
00077     top->addLayout(hbox);
00078     QLabel *label = new QLabel(i18n("Select player:"), this);
00079     hbox->addWidget(label);
00080     _combo = new PlayersCombo(this);
00081     connect(_combo, SIGNAL(playerSelected(uint)),
00082             SLOT(playerSelected(uint)));
00083     connect(_combo, SIGNAL(allSelected()), SLOT(allSelected()));
00084     hbox->addWidget(_combo);
00085     hbox->addStretch(1);
00086 }
00087 
00088 void AdditionalTab::init()
00089 {
00090     uint id = internal->playerInfos().id();
00091     _combo->setCurrentIndex(id);
00092     playerSelected(id);
00093 }
00094 
00095 void AdditionalTab::allSelected()
00096 {
00097     display(internal->playerInfos().nbEntries());
00098 }
00099 
00100 QString AdditionalTab::percent(uint n, uint total, bool withBraces)
00101 {
00102     if ( n==0 || total==0 ) return QString();
00103     QString s =  QString("%1%").arg(100.0 * n / total, 0, 'f', 1);
00104     return (withBraces ? QString('(') + s + ')' : s);
00105 }
00106 
00107 void AdditionalTab::load()
00108 {
00109     _combo->load();
00110 }
00111 
00112 
00113 //-----------------------------------------------------------------------------
00114 const char *StatisticsTab::COUNT_LABELS[Nb_Counts] = {
00115     I18N_NOOP("Total:"), I18N_NOOP("Won:"), I18N_NOOP("Lost:"),
00116     I18N_NOOP("Draw:")
00117 };
00118 const char *StatisticsTab::TREND_LABELS[Nb_Trends] = {
00119     I18N_NOOP("Current:"), I18N_NOOP("Max won:"), I18N_NOOP("Max lost:")
00120 };
00121 
00122 StatisticsTab::StatisticsTab(QWidget *parent)
00123     : AdditionalTab(parent)
00124 {
00125     setObjectName("statistics_tab");
00126     // construct GUI
00127     QVBoxLayout *top = static_cast<QVBoxLayout *>(layout());
00128 
00129     QHBoxLayout *hbox = new QHBoxLayout;
00130     QVBoxLayout *vbox = new QVBoxLayout;
00131     hbox->addLayout(vbox);
00132     top->addLayout(hbox);
00133 
00134     QGroupBox *group = new QGroupBox(i18n("Game Counts"), this);
00135     vbox->addWidget(group);
00136     QGridLayout *gridLay = new QGridLayout(group);
00137     gridLay->setSpacing(KDialog::spacingHint());
00138     for (uint k=0; k<Nb_Counts; k++) {
00139         if ( Count(k)==Draw && !internal->showDrawGames ) continue;
00140         gridLay->addWidget(new QLabel(i18n(COUNT_LABELS[k]), group), k, 0);
00141         _nbs[k] = new QLabel(group);
00142         gridLay->addWidget(_nbs[k], k, 1);
00143         _percents[k] = new QLabel(group);
00144         gridLay->addWidget(_percents[k], k, 2);
00145     }
00146 
00147     group = new QGroupBox(i18n("Trends"), this);
00148     vbox->addWidget(group);
00149     gridLay = new QGridLayout(group);
00150     gridLay->setSpacing(KDialog::spacingHint());
00151     for (uint k=0; k<Nb_Trends; k++) {
00152         gridLay->addWidget(new QLabel(i18n(TREND_LABELS[k]), group), k, 0);
00153         _trends[k] = new QLabel(group);
00154         gridLay->addWidget(_trends[k], k, 1);
00155     }
00156 
00157     hbox->addStretch(1);
00158     top->addStretch(1);
00159 }
00160 
00161 void StatisticsTab::load()
00162 {
00163     AdditionalTab::load();
00164     const PlayerInfos &pi = internal->playerInfos();
00165     uint nb = pi.nbEntries();
00166     _data.resize(nb+1);
00167     for (int i=0; i<_data.size()-1; i++) {
00168         _data[i].count[Total] = pi.item("nb games")->read(i).toUInt();
00169         _data[i].count[Lost] = pi.item("nb lost games")->read(i).toUInt()
00170                        + pi.item("nb black marks")->read(i).toUInt(); // legacy
00171         _data[i].count[Draw] = pi.item("nb draw games")->read(i).toUInt();
00172         _data[i].count[Won] = _data[i].count[Total] - _data[i].count[Lost]
00173                               - _data[i].count[Draw];
00174         _data[i].trend[CurrentTrend] =
00175             pi.item("current trend")->read(i).toInt();
00176         _data[i].trend[WonTrend] = pi.item("max won trend")->read(i).toUInt();
00177         _data[i].trend[LostTrend] =
00178             -(int)pi.item("max lost trend")->read(i).toUInt();
00179     }
00180 
00181     for (int k=0; k<Nb_Counts; k++) _data[nb].count[k] = 0;
00182     for (int k=0; k<Nb_Trends; k++) _data[nb].trend[k] = 0;
00183     for (int i=0; i<_data.size()-1; i++) {
00184         for (uint k=0; k<Nb_Counts; k++)
00185             _data[nb].count[k] += _data[i].count[k];
00186         for (uint k=0; k<Nb_Trends; k++)
00187             _data[nb].trend[k] += _data[i].trend[k];
00188     }
00189     for (uint k=0; k<Nb_Trends; k++)
00190         _data[nb].trend[k] /= (_data.size()-1);
00191 
00192     init();
00193 }
00194 
00195 QString StatisticsTab::percent(const Data &d, Count count) const
00196 {
00197     if ( count==Total ) return QString();
00198     return AdditionalTab::percent(d.count[count], d.count[Total], true);
00199 }
00200 
00201 void StatisticsTab::display(uint i)
00202 {
00203     const Data &d = _data[i];
00204     for (uint k=0; k<Nb_Counts; k++) {
00205         if ( Count(k) && !internal->showDrawGames ) continue;
00206         _nbs[k]->setText(QString::number(d.count[k]));
00207         _percents[k]->setText(percent(d, Count(k)));
00208     }
00209     for (uint k=0; k<Nb_Trends; k++) {
00210         QString s;
00211         if ( d.trend[k]>0 ) s = '+';
00212         int prec = (i==internal->playerInfos().nbEntries() ? 1 : 0);
00213         _trends[k]->setText(s + QString::number(d.trend[k], 'f', prec));
00214     }
00215 }
00216 
00217 //-----------------------------------------------------------------------------
00218 HistogramTab::HistogramTab(QWidget *parent)
00219     : AdditionalTab(parent)
00220 {
00221     setObjectName("histogram_tab");
00222     // construct GUI
00223     QVBoxLayout *top = static_cast<QVBoxLayout *>(layout());
00224 
00225     _list = new QTreeWidget(this);
00226     _list->setSelectionMode(QAbstractItemView::NoSelection);
00228 //     _list->setItemMargin(3);
00229     _list->setAllColumnsShowFocus(true);
00230     _list->setSortingEnabled(false);
00231     _list->header()->setClickable(false);
00232     _list->header()->setMovable(false);
00233     top->addWidget(_list);
00234 
00235     _list->headerItem()->setText(0,i18n("From")); 
00236     _list->headerItem()->setText(1,i18n("To")); 
00237     _list->headerItem()->setText(2,i18n("Count")); 
00238     _list->headerItem()->setText(3,i18n("Percent")); 
00239     for (int i=0; i<4; i++) _list->headerItem()->setTextAlignment(i, Qt::AlignRight);
00240     _list->headerItem()->setText(4,QString()); 
00241 
00242     const Item *sitem = internal->scoreInfos().item("score")->item();
00243     const PlayerInfos &pi = internal->playerInfos();
00244     const QVector<uint> &sh = pi.histogram();
00245     for (int k=1; k<( int )pi.histoSize(); k++) {
00246         QString s1 = sitem->pretty(0, sh[k-1]);
00247         QString s2;
00248         if ( k==sh.size() ) s2 = "...";
00249         else if ( sh[k]!=sh[k-1]+1 ) s2 = sitem->pretty(0, sh[k]);
00250     QStringList items; items << s1 << s2;
00251         (void)new QTreeWidgetItem(_list, items);
00252     }
00253 }
00254 
00255 void HistogramTab::load()
00256 {
00257     AdditionalTab::load();
00258     const PlayerInfos &pi = internal->playerInfos();
00259     uint n = pi.nbEntries();
00260     uint s = pi.histoSize() - 1;
00261     _counts.resize((n+1) * s);
00262     _data.fill(0, n+1);
00263     for (uint k=0; k<s; k++) {
00264         _counts[n*s + k] = 0;
00265         for (uint i=0; i<n; i++) {
00266             uint nb = pi.item(pi.histoName(k+1))->read(i).toUInt();
00267             _counts[i*s + k] = nb;
00268             _counts[n*s + k] += nb;
00269             _data[i] += nb;
00270             _data[n] += nb;
00271         }
00272     }
00273 
00274     init();
00275 }
00276 
00277 void HistogramTab::display(uint i)
00278 {
00279     const PlayerInfos &pi = internal->playerInfos();
00280     uint itemNum = 0;
00281     QTreeWidgetItem *item = _list->topLevelItem(itemNum);
00282     uint s = pi.histoSize() - 1;
00283     for (int k=s-1; k>=0; k--) {
00284         uint nb = _counts[i*s + k];
00285         item->setText(2, QString::number(nb));
00286         item->setText(3, percent(nb, _data[i]));
00287         uint width = (_data[i]==0 ? 0 : qRound(150.0 * nb / _data[i]));
00288         QPixmap pixmap(width, 10);
00289         pixmap.fill(Qt::blue);
00290         item->setData(4, Qt::DecorationRole, pixmap);
00291     itemNum++;
00292         item = _list->topLevelItem(itemNum);
00293     }
00294 }
00295 
00296 } // namespace

libkdegames/highscore

Skip menu "libkdegames/highscore"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal