• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

libkdegames/highscore

  • sources
  • kde-4.14
  • kdegames
  • libkdegames
  • highscore
kexthighscore_item.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2001-2003 Nicolas Hadacek (hadacek@kde.org)
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kexthighscore_item.h"
21 
22 #include <QLayout>
23 //Added by qt3to4:
24 #include <QLabel>
25 #include <QHBoxLayout>
26 #include <kglobal.h>
27 #include <kdialog.h>
28 #include <kdebug.h>
29 #include <kvbox.h>
30 #include <kpagedialog.h>
31 #include "khighscore.h"
32 #include "kexthighscore_internal.h"
33 #include "kexthighscore_gui.h"
34 
35 
36 namespace KExtHighscore
37 {
38 
39 //-----------------------------------------------------------------------------
40 Item::Item(const QVariant &def, const QString &label, Qt::AlignmentFlag alignment)
41  : _default(def), _label(label), _alignment(alignment),
42  _format(NoFormat), _special(NoSpecial)
43 {}
44 
45 Item::~Item()
46 {}
47 
48 QVariant Item::read(uint, const QVariant &value) const
49 {
50  return value;
51 }
52 
53 void Item::setPrettyFormat(Format format)
54 {
55  bool buint = ( _default.type()==QVariant::UInt );
56  bool bdouble = ( _default.type()==QVariant::Double );
57  bool bnum = ( buint || bdouble || _default.type()==QVariant::Int );
58 
59  switch (format) {
60  case OneDecimal:
61  case Percentage:
62  Q_ASSERT(bdouble);
63  break;
64  case MinuteTime:
65  Q_ASSERT(bnum);
66  break;
67  case DateTime:
68  Q_ASSERT( _default.type()==QVariant::DateTime );
69  break;
70  case NoFormat:
71  break;
72  }
73 
74  _format = format;
75 }
76 
77 void Item::setPrettySpecial(Special special)
78 {
79  bool buint = ( _default.type()==QVariant::UInt );
80  bool bnum = ( buint || _default.type()==QVariant::Double
81  || _default.type()==QVariant::Int );
82 
83  switch (special) {
84  case ZeroNotDefined:
85  Q_ASSERT(bnum);
86  break;
87  case NegativeNotDefined:
88  Q_ASSERT(bnum && !buint);
89  break;
90  case DefaultNotDefined:
91  break;
92  case Anonymous:
93  Q_ASSERT( _default.type()==QVariant::String );
94  break;
95  case NoSpecial:
96  break;
97  }
98 
99  _special = special;
100 }
101 
102 QString Item::timeFormat(uint n)
103 {
104  Q_ASSERT( n<=3600 && n!=0 );
105  n = 3600 - n;
106  return QString::number(n / 60).rightJustified(2, QLatin1Char( '0' )) + QLatin1Char( ':' )
107  + QString::number(n % 60).rightJustified(2, QLatin1Char( '0' ));
108 }
109 
110 QString Item::pretty(uint, const QVariant &value) const
111 {
112  switch (_special) {
113  case ZeroNotDefined:
114  if ( value.toUInt()==0 ) return QLatin1String( "--" );
115  break;
116  case NegativeNotDefined:
117  if ( value.toInt()<0 ) return QLatin1String( "--" );
118  break;
119  case DefaultNotDefined:
120  if ( value==_default ) return QLatin1String( "--" );
121  break;
122  case Anonymous:
123  if ( value.toString()==QLatin1String( ItemContainer::ANONYMOUS ) )
124  return i18n(ItemContainer::ANONYMOUS_LABEL);
125  break;
126  case NoFormat:
127  break;
128  }
129 
130  switch (_format) {
131  case OneDecimal:
132  return QString::number(value.toDouble(), 'f', 1);
133  case Percentage:
134  return QString::number(value.toDouble(), 'f', 1) + QLatin1Char( '%' );
135  case MinuteTime:
136  return timeFormat(value.toUInt());
137  case DateTime:
138  if ( value.toDateTime().isNull() ) return QLatin1String( "--" );
139  return KGlobal::locale()->formatDateTime(value.toDateTime());
140  case NoSpecial:
141  break;
142  }
143 
144  return value.toString();
145 }
146 
147 //-----------------------------------------------------------------------------
148 Score::Score(ScoreType type)
149  : _type(type)
150 {
151  const ItemArray &items = internal->scoreInfos();
152  for (int i=0; i<items.size(); i++)
153  _data[items[i]->name()] = items[i]->item()->defaultValue();
154 }
155 
156 Score::~Score()
157 {}
158 
159 QVariant Score::data(const QString &name) const
160 {
161  Q_ASSERT( _data.contains(name) );
162  return _data[name];
163 }
164 
165 void Score::setData(const QString &name, const QVariant &value)
166 {
167  Q_ASSERT( _data.contains(name) );
168  Q_ASSERT( _data[name].type()==value.type() );
169  _data[name] = value;
170 }
171 
172 bool Score::isTheWorst() const
173 {
174  Score s;
175  return score()==s.score();
176 }
177 
178 bool Score::operator <(const Score &score)
179 {
180  return internal->manager.isStrictlyLess(*this, score);
181 }
182 
183 QDataStream &operator <<(QDataStream &s, const Score &score)
184 {
185  s << (quint8)score.type();
186  s << score._data;
187  return s;
188 }
189 
190 QDataStream &operator >>(QDataStream &s, Score &score)
191 {
192  quint8 type;
193  s >> type;
194  score._type = (ScoreType)type;
195  s >> score._data;
196  return s;
197 }
198 
199 //-----------------------------------------------------------------------------
200 MultiplayerScores::MultiplayerScores()
201 {}
202 
203 MultiplayerScores::~MultiplayerScores()
204 {}
205 
206 void MultiplayerScores::clear()
207 {
208  Score score;
209  for (int i=0; i<_scores.size(); i++) {
210  _nbGames[i] = 0;
211  QVariant name = _scores[i].data(QLatin1String( "name" ));
212  _scores[i] = score;
213  _scores[i].setData(QLatin1String( "name" ), name);
214  _scores[i]._data[QLatin1String( "mean score" )] = double(0);
215  _scores[i]._data[QLatin1String( "nb won games" )] = uint(0);
216  }
217 }
218 
219 void MultiplayerScores::setPlayerCount(uint nb)
220 {
221  _nbGames.resize(nb);
222  _scores.resize(nb);
223  clear();
224 }
225 
226 void MultiplayerScores::setName(uint i, const QString &name)
227 {
228  _scores[i].setData(QLatin1String( "name" ), name);
229 }
230 
231 void MultiplayerScores::addScore(uint i, const Score &score)
232 {
233  QVariant name = _scores[i].data(QLatin1String( "name" ));
234  double mean = _scores[i].data(QLatin1String( "mean score" )).toDouble();
235  uint won = _scores[i].data(QLatin1String( "nb won games" )).toUInt();
236  _scores[i] = score;
237  _scores[i].setData(QLatin1String( "name" ), name);
238  _nbGames[i]++;
239  mean += (double(score.score()) - mean) / _nbGames[i];
240  _scores[i]._data[QLatin1String( "mean score" )] = mean;
241  if ( score.type()==Won ) won++;
242  _scores[i]._data[QLatin1String( "nb won games" )] = won;
243 }
244 
245 void MultiplayerScores::show(QWidget *parent)
246 {
247  // check consistency
248  if ( _nbGames.size()<2 ) kWarning(11002) << "less than 2 players";
249  else {
250  bool ok = true;
251  uint nb = _nbGames[0];
252  for (int i=1; i<_nbGames.size(); i++)
253  if ( _nbGames[i]!=nb ) ok = false;
254  if (!ok)
255  kWarning(11002) << "players have not same number of games";
256  }
257 
258  // order the players according to the number of won games
259  QVector<Score> ordered;
260  for (int i=0; i<_scores.size(); i++) {
261  uint won = _scores[i].data(QLatin1String( "nb won games" )).toUInt();
262  double mean = _scores[i].data(QLatin1String( "mean score" )).toDouble();
263  QVector<Score>::iterator it;
264  for(it = ordered.begin(); it!=ordered.end(); ++it) {
265  uint cwon = (*it).data(QLatin1String( "nb won games" )).toUInt();
266  double cmean = (*it).data(QLatin1String( "mean score" )).toDouble();
267  if ( won<cwon || (won==cwon && mean<cmean) ) {
268  ordered.insert(it, _scores[i]);
269  break;
270  }
271  }
272  if ( it==ordered.end() ) ordered.push_back(_scores[i]);
273  }
274 
275  // show the scores
276  KPageDialog dialog(parent);
277  dialog.setCaption(i18n("Multiplayers Scores"));
278  dialog.setButtons(KDialog::Close);
279  dialog.setModal(true);
280  dialog.setFaceType(KPageDialog::Plain);
281  KPageWidgetItem *page = new KPageWidgetItem( new QLabel(QLatin1String( "" )), QLatin1String( "" ) );
282  QHBoxLayout *hbox = new QHBoxLayout(page->widget());
283  hbox->setMargin(KDialog::marginHint());
284  hbox->setSpacing(KDialog::spacingHint());
285 
286  KVBox *vbox = new KVBox(page->widget());
287  hbox->addWidget(vbox);
288  if ( _nbGames[0]==0 ) (void)new QLabel(i18n("No game played."), vbox);
289  else {
290  (void)new QLabel(i18n("Scores for last game:"), vbox);
291  (void)new LastMultipleScoresList(ordered, vbox);
292  }
293 
294  if ( _nbGames[0]>1 ) {
295  vbox = new KVBox(page->widget());
296  hbox->addWidget(vbox);
297  (void)new QLabel(i18n("Scores for the last %1 games:",
298  _nbGames[0]), vbox);
299  (void)new TotalMultipleScoresList(ordered, vbox);
300  }
301 
302  //dialog.showButtonSeparator(false);
303  dialog.addPage(page);
304  dialog.exec();
305 }
306 
307 QDataStream &operator <<(QDataStream &s, const MultiplayerScores &score)
308 {
309  s << score._scores;
310  s << score._nbGames;
311  return s;
312 }
313 
314 QDataStream &operator >>(QDataStream &s, MultiplayerScores &score)
315 {
316 
317 
318  s >> score._scores;
319  s >> score._nbGames;
320  return s;
321 }
322 
323 } // namespace
QWidget
KPageDialog
KExtHighscore::MultiplayerScores
This class is used to store and show scores for multiplayer games.
Definition: kexthighscore_item.h:271
KExtHighscore::MultiplayerScores::show
void show(QWidget *parent)
Show scores.
Definition: kexthighscore_item.cpp:245
QMap::contains
bool contains(const Key &key) const
QVector::begin
iterator begin()
KExtHighscore::MultiplayerScores::setName
void setName(uint player, const QString &name)
Set the name of player.
Definition: kexthighscore_item.cpp:226
KExtHighscore::Score::score
uint score() const
Definition: kexthighscore_item.h:212
KExtHighscore::Item::Anonymous
Definition: kexthighscore_item.h:72
KExtHighscore::TotalMultipleScoresList
Definition: kexthighscore_gui.h:142
QDataStream
KExtHighscore::MultiplayerScores::setPlayerCount
void setPlayerCount(uint nb)
Set the number of players and clear the scores.
Definition: kexthighscore_item.cpp:219
QVariant::toDateTime
QDateTime toDateTime() const
KExtHighscore::MultiplayerScores::~MultiplayerScores
~MultiplayerScores()
Definition: kexthighscore_item.cpp:203
KExtHighscore::Item::Format
Format
Possible display format.
Definition: kexthighscore_item.h:54
QVector::insert
void insert(int i, const T &value)
KExtHighscore::Item::NegativeNotDefined
Definition: kexthighscore_item.h:71
QHBoxLayout
KExtHighscore::Score::setData
void setData(const QString &name, const QVariant &value)
Set the data associated with the named Item.
Definition: kexthighscore_item.cpp:165
KExtHighscore::Item::NoFormat
Definition: kexthighscore_item.h:54
KExtHighscore::ItemContainer::ANONYMOUS_LABEL
static const char ANONYMOUS_LABEL[]
Definition: kexthighscore_internal.h:109
KExtHighscore::Score::type
ScoreType type() const
Definition: kexthighscore_item.h:188
KExtHighscore::Item::DefaultNotDefined
Definition: kexthighscore_item.h:72
KExtHighscore::Item::read
virtual QVariant read(uint i, const QVariant &value) const
Definition: kexthighscore_item.cpp:48
KExtHighscore::Item::~Item
virtual ~Item()
Definition: kexthighscore_item.cpp:45
KExtHighscore::Won
Definition: kexthighscore_item.h:168
KExtHighscore::MultiplayerScores::addScore
void addScore(uint player, const Score &score)
Add the score of player.
Definition: kexthighscore_item.cpp:231
kexthighscore_gui.h
QVector::data
T * data()
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QString::number
QString number(int n, int base)
QVariant::toUInt
uint toUInt(bool *ok) const
QVector::resize
void resize(int size)
QVariant::toInt
int toInt(bool *ok) const
QString::rightJustified
QString rightJustified(int width, QChar fill, bool truncate) const
khighscore.h
KExtHighscore::Item::Percentage
Definition: kexthighscore_item.h:54
KExtHighscore::Item::Special
Special
Possible special value for display format.
Definition: kexthighscore_item.h:71
KExtHighscore::Item::MinuteTime
Definition: kexthighscore_item.h:54
KExtHighscore::Score::operator<
bool operator<(const Score &score)
Comparison operator.
Definition: kexthighscore_item.cpp:178
KExtHighscore::Score::~Score
~Score()
Definition: kexthighscore_item.cpp:156
KExtHighscore::ScoreType
ScoreType
Possible score type.
Definition: kexthighscore_item.h:168
kexthighscore_internal.h
KExtHighscore::ItemContainer::ANONYMOUS
static const char ANONYMOUS[]
Definition: kexthighscore_internal.h:108
QString
KExtHighscore::MultiplayerScores::MultiplayerScores
MultiplayerScores()
Definition: kexthighscore_item.cpp:200
KExtHighscore::Item::ZeroNotDefined
Definition: kexthighscore_item.h:71
QLayout::setMargin
void setMargin(int margin)
KExtHighscore::Item::setPrettyFormat
void setPrettyFormat(Format format)
Set the display format.
Definition: kexthighscore_item.cpp:53
KExtHighscore::Item::DateTime
Definition: kexthighscore_item.h:55
QLatin1Char
KExtHighscore::Item::Item
Item(const QVariant &def=QVariant::Invalid, const QString &label=QString(), Qt::AlignmentFlag alignment=Qt::AlignRight)
Constructor.
Definition: kexthighscore_item.cpp:40
KExtHighscore::operator<<
QDataStream & operator<<(QDataStream &s, const Score &score)
Definition: kexthighscore_item.cpp:183
KExtHighscore::MultiplayerScores::clear
void clear()
Clear all scores.
Definition: kexthighscore_item.cpp:206
QDateTime::isNull
bool isNull() const
KExtHighscore::LastMultipleScoresList
Definition: kexthighscore_gui.h:128
QVector
KExtHighscore::operator>>
QDataStream & operator>>(QDataStream &s, Score &score)
Definition: kexthighscore_item.cpp:190
QLatin1String
kexthighscore_item.h
KExtHighscore::Score
This class contains data for a score.
Definition: kexthighscore_item.h:178
KExtHighscore::Item::pretty
virtual QString pretty(uint i, const QVariant &value) const
Definition: kexthighscore_item.cpp:110
QVector::push_back
void push_back(const T &value)
QVariant::toDouble
double toDouble(bool *ok) const
KExtHighscore::Item::OneDecimal
Definition: kexthighscore_item.h:54
QVariant::type
Type type() const
KExtHighscore::Item::setPrettySpecial
void setPrettySpecial(Special special)
Set the special value for display.
Definition: kexthighscore_item.cpp:77
KExtHighscore::ItemArray::item
const ItemContainer * item(const QString &name) const
Definition: kexthighscore_internal.cpp:125
QLabel
QVector::size
int size() const
QVariant::toString
QString toString() const
QVector::end
iterator end()
KExtHighscore::Score::Score
Score(ScoreType type=Won)
Definition: kexthighscore_item.cpp:148
KExtHighscore::Score::data
QVariant data(const QString &name) const
Definition: kexthighscore_item.cpp:159
QBoxLayout::setSpacing
void setSpacing(int spacing)
KExtHighscore::ItemArray
Manage a bunch of Item which are saved under the same group in KHighscores config file...
Definition: kexthighscore_internal.h:132
KExtHighscore::Item::NoSpecial
Definition: kexthighscore_item.h:71
QVariant
KExtHighscore::Score::isTheWorst
bool isTheWorst() const
Definition: kexthighscore_item.cpp:172
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:46 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdegames/highscore

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

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal