libkdegames Library API Documentation

kexthighscore_item.cpp

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