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

libkdegames/highscore

kexthighscore_item.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the KDE games library
00003     Copyright (C) 2001-2003 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_item.h"
00021 
00022 #include <QLayout>
00023 //Added by qt3to4:
00024 #include <QLabel>
00025 #include <QHBoxLayout>
00026 #include <kglobal.h>
00027 #include <kdialog.h>
00028 #include <kdebug.h>
00029 #include <kvbox.h>
00030 #include <kpagedialog.h>
00031 #include "khighscore.h"
00032 #include "kexthighscore_internal.h"
00033 #include "kexthighscore_gui.h"
00034 
00035 
00036 namespace KExtHighscore
00037 {
00038 
00039 //-----------------------------------------------------------------------------
00040 Item::Item(const QVariant &def, const QString &label, Qt::AlignmentFlag alignment)
00041     : _default(def), _label(label), _alignment(alignment),
00042       _format(NoFormat), _special(NoSpecial)
00043 {}
00044 
00045 Item::~Item()
00046 {}
00047 
00048 QVariant Item::read(uint, const QVariant &value) const
00049 {
00050     return value;
00051 }
00052 
00053 void Item::setPrettyFormat(Format format)
00054 {
00055     bool buint = ( _default.type()==QVariant::UInt );
00056     bool bdouble = ( _default.type()==QVariant::Double );
00057     bool bnum = ( buint || bdouble || _default.type()==QVariant::Int );
00058 
00059     switch (format) {
00060     case OneDecimal:
00061     case Percentage:
00062         Q_ASSERT(bdouble);
00063         break;
00064     case MinuteTime:
00065         Q_ASSERT(bnum);
00066         break;
00067     case DateTime:
00068         Q_ASSERT( _default.type()==QVariant::DateTime );
00069     break;
00070     case NoFormat:
00071         break;
00072     }
00073 
00074     _format = format;
00075 }
00076 
00077 void Item::setPrettySpecial(Special special)
00078 {
00079     bool buint = ( _default.type()==QVariant::UInt );
00080     bool bnum = ( buint || _default.type()==QVariant::Double
00081                   || _default.type()==QVariant::Int );
00082 
00083     switch (special) {
00084     case ZeroNotDefined:
00085         Q_ASSERT(bnum);
00086         break;
00087     case NegativeNotDefined:
00088         Q_ASSERT(bnum && !buint);
00089         break;
00090     case DefaultNotDefined:
00091         break;
00092     case Anonymous:
00093         Q_ASSERT( _default.type()==QVariant::String );
00094         break;
00095     case NoSpecial:
00096         break;
00097     }
00098 
00099      _special = special;
00100 }
00101 
00102 QString Item::timeFormat(uint n)
00103 {
00104     Q_ASSERT( n<=3600 && n!=0 );
00105     n = 3600 - n;
00106     return QString::number(n / 60).rightJustified(2, '0') + ':'
00107         + QString::number(n % 60).rightJustified(2, '0');
00108 }
00109 
00110 QString Item::pretty(uint, const QVariant &value) const
00111 {
00112     switch (_special) {
00113     case ZeroNotDefined:
00114         if ( value.toUInt()==0 ) return "--";
00115         break;
00116     case NegativeNotDefined:
00117         if ( value.toInt()<0 ) return "--";
00118         break;
00119     case DefaultNotDefined:
00120         if ( value==_default ) return "--";
00121         break;
00122     case Anonymous:
00123         if ( value.toString()==ItemContainer::ANONYMOUS )
00124             return i18n(ItemContainer::ANONYMOUS_LABEL);
00125         break;
00126     case NoFormat:
00127         break;
00128     }
00129 
00130     switch (_format) {
00131     case OneDecimal:
00132         return QString::number(value.toDouble(), 'f', 1);
00133     case Percentage:
00134         return QString::number(value.toDouble(), 'f', 1) + '%';
00135     case MinuteTime:
00136         return timeFormat(value.toUInt());
00137     case DateTime:
00138         if ( value.toDateTime().isNull() ) return "--";
00139         return KGlobal::locale()->formatDateTime(value.toDateTime());
00140     case NoSpecial:
00141         break;
00142     }
00143 
00144     return value.toString();
00145 }
00146 
00147 //-----------------------------------------------------------------------------
00148 Score::Score(ScoreType type)
00149     : _type(type)
00150 {
00151     const ItemArray &items = internal->scoreInfos();
00152     for (int i=0; i<items.size(); i++)
00153         _data[items[i]->name()] = items[i]->item()->defaultValue();
00154 }
00155 
00156 Score::~Score()
00157 {}
00158 
00159 QVariant Score::data(const QString &name) const
00160 {
00161     Q_ASSERT( _data.contains(name) );
00162     return _data[name];
00163 }
00164 
00165 void Score::setData(const QString &name, const QVariant &value)
00166 {
00167     Q_ASSERT( _data.contains(name) );
00168     Q_ASSERT( _data[name].type()==value.type() );
00169     _data[name] = value;
00170 }
00171 
00172 bool Score::isTheWorst() const
00173 {
00174     Score s;
00175     return score()==s.score();
00176 }
00177 
00178 bool Score::operator <(const Score &score)
00179 {
00180     return internal->manager.isStrictlyLess(*this, score);
00181 }
00182 
00183 QDataStream &operator <<(QDataStream &s, const Score &score)
00184 {
00185     s << (quint8)score.type();
00186     s << score._data;
00187     return s;
00188 }
00189 
00190 QDataStream &operator >>(QDataStream &s, Score &score)
00191 {
00192     quint8 type;
00193     s >> type;
00194     score._type = (ScoreType)type;
00195     s >> score._data;
00196     return s;
00197 }
00198 
00199 //-----------------------------------------------------------------------------
00200 MultiplayerScores::MultiplayerScores()
00201 {}
00202 
00203 MultiplayerScores::~MultiplayerScores()
00204 {}
00205 
00206 void MultiplayerScores::clear()
00207 {
00208     Score score;
00209     for (int i=0; i<_scores.size(); i++) {
00210         _nbGames[i] = 0;
00211         QVariant name = _scores[i].data("name");
00212         _scores[i] = score;
00213         _scores[i].setData("name", name);
00214         _scores[i]._data["mean score"] = double(0);
00215         _scores[i]._data["nb won games"] = uint(0);
00216     }
00217 }
00218 
00219 void MultiplayerScores::setPlayerCount(uint nb)
00220 {
00221     _nbGames.resize(nb);
00222     _scores.resize(nb);
00223     clear();
00224 }
00225 
00226 void MultiplayerScores::setName(uint i, const QString &name)
00227 {
00228     _scores[i].setData("name", name);
00229 }
00230 
00231 void MultiplayerScores::addScore(uint i, const Score &score)
00232 {
00233     QVariant name = _scores[i].data("name");
00234     double mean = _scores[i].data("mean score").toDouble();
00235     uint won = _scores[i].data("nb won games").toUInt();
00236     _scores[i] = score;
00237     _scores[i].setData("name", name);
00238     _nbGames[i]++;
00239     mean += (double(score.score()) - mean) / _nbGames[i];
00240     _scores[i]._data["mean score"] = mean;
00241     if ( score.type()==Won ) won++;
00242     _scores[i]._data["nb won games"] = won;
00243 }
00244 
00245 void MultiplayerScores::show(QWidget *parent)
00246 {
00247     // check consistency
00248     if ( _nbGames.size()<2 ) kWarning(11002) << "less than 2 players";
00249     else {
00250         bool ok = true;
00251         uint nb = _nbGames[0];
00252         for (int i=1; i<_nbGames.size(); i++)
00253             if ( _nbGames[i]!=nb ) ok = false;
00254         if (!ok)
00255            kWarning(11002) << "players have not same number of games";
00256     }
00257 
00258     // order the players according to the number of won games
00259     QVector<Score> ordered;
00260     for (int i=0; i<_scores.size(); i++) {
00261         uint won = _scores[i].data("nb won games").toUInt();
00262         double mean = _scores[i].data("mean score").toDouble();
00263         QVector<Score>::iterator it;
00264         for(it = ordered.begin(); it!=ordered.end(); ++it) {
00265             uint cwon = (*it).data("nb won games").toUInt();
00266             double cmean = (*it).data("mean score").toDouble();
00267             if ( won<cwon || (won==cwon && mean<cmean) ) {
00268                 ordered.insert(it, _scores[i]);
00269                 break;
00270             }
00271         }
00272         if ( it==ordered.end() ) ordered.push_back(_scores[i]);
00273     }
00274 
00275     // show the scores
00276     KPageDialog dialog(parent);
00277     dialog.setCaption(i18n("Multiplayers Scores"));
00278     dialog.setButtons(KDialog::Close);
00279     dialog.setModal(true);
00280     dialog.setFaceType(KPageDialog::Plain);
00281     KPageWidgetItem *page = new KPageWidgetItem( new QLabel(""), "" );
00282     QHBoxLayout *hbox = new QHBoxLayout(page->widget());
00283     hbox->setMargin(KDialog::marginHint());
00284     hbox->setSpacing(KDialog::spacingHint());
00285 
00286     KVBox *vbox = new KVBox(page->widget());
00287     hbox->addWidget(vbox);
00288     if ( _nbGames[0]==0 ) (void)new QLabel(i18n("No game played."), vbox);
00289     else {
00290         (void)new QLabel(i18n("Scores for last game:"), vbox);
00291         (void)new LastMultipleScoresList(ordered, vbox);
00292     }
00293 
00294     if ( _nbGames[0]>1 ) {
00295         vbox = new KVBox(page->widget());
00296         hbox->addWidget(vbox);
00297         (void)new QLabel(i18n("Scores for the last %1 games:",
00298                           _nbGames[0]), vbox);
00299         (void)new TotalMultipleScoresList(ordered, vbox);
00300     }
00301 
00302     //dialog.showButtonSeparator(false);
00303     dialog.addPage(page);
00304     dialog.exec();
00305 }
00306 
00307 QDataStream &operator <<(QDataStream &s, const MultiplayerScores &score)
00308 {
00309     s << score._scores;
00310     s << score._nbGames;
00311     return s;
00312 }
00313 
00314 QDataStream &operator >>(QDataStream &s, MultiplayerScores &score)
00315 {
00316     s >> score._scores;
00317     s >> score._nbGames;
00318     return s;
00319 }
00320 
00321 } // 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