20#include "kexthighscore_item.h"
24#include <kdialogbase.h>
27#include "khighscore.h"
28#include "kexthighscore_internal.h"
29#include "kexthighscore_gui.h"
32namespace KExtHighscore
36Item::Item(
const TQVariant &def,
const TQString &label,
int alignment)
37 : _default(def), _label(label), _alignment(alignment),
38 _format(NoFormat), _special(NoSpecial)
51 bool buint = ( _default.type()==TQVariant::UInt );
52 bool bdouble = ( _default.type()==TQVariant::Double );
53 bool bnum = ( buint || bdouble || _default.type()==TQVariant::Int );
64 Q_ASSERT( _default.type()==TQVariant::DateTime );
75 bool buint = ( _default.type()==TQVariant::UInt );
76 bool bnum = ( buint || _default.type()==TQVariant::Double
77 || _default.type()==TQVariant::Int );
83 case NegativeNotDefined:
84 Q_ASSERT(bnum && !buint);
86 case DefaultNotDefined:
89 Q_ASSERT( _default.type()==TQVariant::String );
98TQString Item::timeFormat(uint n)
100 Q_ASSERT( n<=3600 && n!=0 );
102 return TQString::number(n / 60).rightJustify(2,
'0') +
':'
103 + TQString::number(n % 60).rightJustify(2,
'0');
110 if ( value.toUInt()==0 )
return "--";
112 case NegativeNotDefined:
113 if ( value.toInt()<0 )
return "--";
115 case DefaultNotDefined:
116 if ( value==_default )
return "--";
119 if ( value.toString()==ItemContainer::ANONYMOUS )
120 return i18n(ItemContainer::ANONYMOUS_LABEL);
128 return TQString::number(value.toDouble(),
'f', 1);
130 return TQString::number(value.toDouble(),
'f', 1) +
"%";
132 return timeFormat(value.toUInt());
134 if ( value.toDateTime().isNull() )
return "--";
135 return TDEGlobal::locale()->formatDateTime(value.toDateTime());
140 return value.toString();
144Score::Score(ScoreType type)
147 const ItemArray &items = internal->scoreInfos();
148 for (uint i=0; i<items.size(); i++)
149 _data[items[i]->name()] = items[i]->item()->defaultValue();
157 Q_ASSERT( _data.contains(name) );
163 Q_ASSERT( _data.contains(name) );
164 Q_ASSERT( _data[name].
type()==value.type() );
176 return internal->manager.isStrictlyLess(*
this,
score);
179TQDataStream &operator <<(TQDataStream &s,
const Score &score)
181 s << (TQ_UINT8)score.
type();
186TQDataStream &operator >>(TQDataStream &s, Score &score)
190 score._type = (ScoreType)type;
196MultiplayerScores::MultiplayerScores()
199MultiplayerScores::~MultiplayerScores()
205 for (uint i=0; i<_scores.size(); i++) {
207 TQVariant name = _scores[i].data(
"name");
209 _scores[i].setData(
"name", name);
210 _scores[i]._data[
"mean score"] = double(0);
211 _scores[i]._data[
"nb won games"] = uint(0);
224 _scores[i].setData(
"name", name);
229 TQVariant name = _scores[i].data(
"name");
230 double mean = _scores[i].data(
"mean score").toDouble();
231 uint won = _scores[i].data(
"nb won games").toUInt();
233 _scores[i].setData(
"name", name);
235 mean += (double(score.
score()) - mean) / _nbGames[i];
236 _scores[i]._data[
"mean score"] = mean;
237 if ( score.
type()==Won ) won++;
238 _scores[i]._data[
"nb won games"] = won;
244 if ( _nbGames.size()<2 ) kdWarning(11002) <<
"less than 2 players" << endl;
247 uint nb = _nbGames[0];
248 for (uint i=1; i<_nbGames.size(); i++)
249 if ( _nbGames[i]!=nb ) ok =
false;
251 kdWarning(11002) <<
"players have not same number of games" << endl;
255 TQValueVector<Score> ordered;
256 for (uint i=0; i<_scores.size(); i++) {
257 uint won = _scores[i].data(
"nb won games").toUInt();
258 double mean = _scores[i].data(
"mean score").toDouble();
259 TQValueVector<Score>::iterator it;
260 for(it = ordered.begin(); it!=ordered.end(); ++it) {
261 uint cwon = (*it).data(
"nb won games").toUInt();
262 double cmean = (*it).data(
"mean score").toDouble();
263 if ( won<cwon || (won==cwon && mean<cmean) ) {
264 ordered.insert(it, _scores[i]);
268 if ( it==ordered.end() ) ordered.push_back(_scores[i]);
272 KDialogBase dialog(KDialogBase::Plain, i18n(
"Multiplayers Scores"),
273 KDialogBase::Close, KDialogBase::Close,
274 parent,
"show_multiplayers_score",
true,
true);
275 TQHBoxLayout *hbox =
new TQHBoxLayout(dialog.plainPage(),
276 KDialog::marginHint(), KDialog::spacingHint());
278 TQVBox *vbox =
new TQVBox(dialog.plainPage());
279 hbox->addWidget(vbox);
280 if ( _nbGames[0]==0 ) (void)
new TQLabel(i18n(
"No game played."), vbox);
282 (void)
new TQLabel(i18n(
"Scores for last game:"), vbox);
283 (void)
new LastMultipleScoresList(ordered, vbox);
286 if ( _nbGames[0]>1 ) {
287 vbox =
new TQVBox(dialog.plainPage());
288 hbox->addWidget(vbox);
289 (void)
new TQLabel(i18n(
"Scores for the last %1 games:")
290 .arg(_nbGames[0]), vbox);
291 (void)
new TotalMultipleScoresList(ordered, vbox);
294 dialog.enableButtonSeparator(
false);
305TQDataStream &operator >>(TQDataStream &s, MultiplayerScores &score)
Manage a bunch of Item which are saved under the same group in KHighscores config file.
virtual TQString pretty(uint i, const TQVariant &value) const
Format
Possible display format.
virtual TQVariant read(uint i, const TQVariant &value) const
Special
Possible special value for display format.
Item(const TQVariant &def=TQVariant::Invalid, const TQString &label=TQString(), int alignment=TQt::AlignRight)
Constructor.
void setPrettyFormat(Format format)
Set the display format.
void setPrettySpecial(Special special)
Set the special value for display.
This class is used to store and show scores for multiplayer games.
void clear()
Clear all scores.
void setPlayerCount(uint nb)
Set the number of players and clear the scores.
void setName(uint player, const TQString &name)
Set the name of player.
void addScore(uint player, const Score &score)
Add the score of player.
void show(TQWidget *parent)
Show scores.
This class contains data for a score.
bool operator<(const Score &score)
Comparison operator.
const TQVariant & data(const TQString &name) const
void setData(const TQString &name, const TQVariant &value)
Set the data associated with the named Item.