• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdegames
 

libtdegames

  • libtdegames
  • highscore
kexthighscore_tab.cpp
1 /*
2  This file is part of the TDE games library
3  Copyright (C) 2002 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_tab.h"
21 #include "kexthighscore_tab.moc"
22 
23 #include <tqlayout.h>
24 #include <tqlabel.h>
25 #include <tqvgroupbox.h>
26 #include <tqgrid.h>
27 #include <tqheader.h>
28 
29 #include <kdialogbase.h>
30 #include <tdelistview.h>
31 #include <kdebug.h>
32 #include <tdeglobal.h>
33 
34 #include "kexthighscore.h"
35 #include "kexthighscore_internal.h"
36 
37 
38 namespace KExtHighscore
39 {
40 
41 //-----------------------------------------------------------------------------
42 PlayersCombo::PlayersCombo(TQWidget *parent, const char *name)
43  : TQComboBox(parent, name)
44 {
45  const PlayerInfos &p = internal->playerInfos();
46  for (uint i = 0; i<p.nbEntries(); i++)
47  insertItem(p.prettyName(i));
48  insertItem(TQString("<") + i18n("all") + '>');
49  connect(this, TQ_SIGNAL(activated(int)), TQ_SLOT(activatedSlot(int)));
50 }
51 
52 void PlayersCombo::activatedSlot(int i)
53 {
54  const PlayerInfos &p = internal->playerInfos();
55  if ( i==(int)p.nbEntries() ) emit allSelected();
56  else if ( i==(int)p.nbEntries()+1 ) emit noneSelected();
57  else emit playerSelected(i);
58 }
59 
60 void PlayersCombo::load()
61 {
62  const PlayerInfos &p = internal->playerInfos();
63  for (uint i = 0; i<p.nbEntries(); i++)
64  changeItem(p.prettyName(i), i);
65 }
66 
67 //-----------------------------------------------------------------------------
68 AdditionalTab::AdditionalTab(TQWidget *parent, const char *name)
69  : TQWidget(parent, name)
70 {
71  TQVBoxLayout *top = new TQVBoxLayout(this, KDialogBase::marginHint(),
72  KDialogBase::spacingHint());
73 
74  TQHBoxLayout *hbox = new TQHBoxLayout(top);
75  TQLabel *label = new TQLabel(i18n("Select player:"), this);
76  hbox->addWidget(label);
77  _combo = new PlayersCombo(this);
78  connect(_combo, TQ_SIGNAL(playerSelected(uint)),
79  TQ_SLOT(playerSelected(uint)));
80  connect(_combo, TQ_SIGNAL(allSelected()), TQ_SLOT(allSelected()));
81  hbox->addWidget(_combo);
82  hbox->addStretch(1);
83 }
84 
85 void AdditionalTab::init()
86 {
87  uint id = internal->playerInfos().id();
88  _combo->setCurrentItem(id);
89  playerSelected(id);
90 }
91 
92 void AdditionalTab::allSelected()
93 {
94  display(internal->playerInfos().nbEntries());
95 }
96 
97 TQString AdditionalTab::percent(uint n, uint total, bool withBraces)
98 {
99  if ( n==0 || total==0 ) return TQString();
100  TQString s = TQString("%1%").arg(100.0 * n / total, 0, 'f', 1);
101  return (withBraces ? TQString("(") + s + ")" : s);
102 }
103 
104 void AdditionalTab::load()
105 {
106  _combo->load();
107 }
108 
109 
110 //-----------------------------------------------------------------------------
111 const char *StatisticsTab::COUNT_LABELS[Nb_Counts] = {
112  I18N_NOOP("Total:"), I18N_NOOP("Won:"), I18N_NOOP("Lost:"),
113  I18N_NOOP("Draw:")
114 };
115 const char *StatisticsTab::TREND_LABELS[Nb_Trends] = {
116  I18N_NOOP("Current:"), I18N_NOOP("Max won:"), I18N_NOOP("Max lost:")
117 };
118 
119 StatisticsTab::StatisticsTab(TQWidget *parent)
120  : AdditionalTab(parent, "statistics_tab")
121 {
122  // construct GUI
123  TQVBoxLayout *top = static_cast<TQVBoxLayout *>(layout());
124 
125  TQHBoxLayout *hbox = new TQHBoxLayout(top);
126  TQVBoxLayout *vbox = new TQVBoxLayout(hbox);
127  TQVGroupBox *group = new TQVGroupBox(i18n("Game Counts"), this);
128  vbox->addWidget(group);
129  TQGrid *grid = new TQGrid(3, group);
130  grid->setSpacing(KDialogBase::spacingHint());
131  for (uint k=0; k<Nb_Counts; k++) {
132  if ( Count(k)==Draw && !internal->showDrawGames ) continue;
133  (void)new TQLabel(i18n(COUNT_LABELS[k]), grid);
134  _nbs[k] = new TQLabel(grid);
135  _percents[k] = new TQLabel(grid);
136  }
137 
138  group = new TQVGroupBox(i18n("Trends"), this);
139  vbox->addWidget(group);
140  grid = new TQGrid(2, group);
141  grid->setSpacing(KDialogBase::spacingHint());
142  for (uint k=0; k<Nb_Trends; k++) {
143  (void)new TQLabel(i18n(TREND_LABELS[k]), grid);
144  _trends[k] = new TQLabel(grid);
145  }
146 
147  hbox->addStretch(1);
148  top->addStretch(1);
149 }
150 
151 void StatisticsTab::load()
152 {
153  AdditionalTab::load();
154  const PlayerInfos &pi = internal->playerInfos();
155  uint nb = pi.nbEntries();
156  _data.resize(nb+1);
157  for (uint i=0; i<_data.size()-1; i++) {
158  _data[i].count[Total] = pi.item("nb games")->read(i).toUInt();
159  _data[i].count[Lost] = pi.item("nb lost games")->read(i).toUInt()
160  + pi.item("nb black marks")->read(i).toUInt(); // legacy
161  _data[i].count[Draw] = pi.item("nb draw games")->read(i).toUInt();
162  _data[i].count[Won] = _data[i].count[Total] - _data[i].count[Lost]
163  - _data[i].count[Draw];
164  _data[i].trend[CurrentTrend] =
165  pi.item("current trend")->read(i).toInt();
166  _data[i].trend[WonTrend] = pi.item("max won trend")->read(i).toUInt();
167  _data[i].trend[LostTrend] =
168  -(int)pi.item("max lost trend")->read(i).toUInt();
169  }
170 
171  for (uint k=0; k<Nb_Counts; k++) _data[nb].count[k] = 0;
172  for (uint k=0; k<Nb_Trends; k++) _data[nb].trend[k] = 0;
173  for (uint i=0; i<_data.size()-1; i++) {
174  for (uint k=0; k<Nb_Counts; k++)
175  _data[nb].count[k] += _data[i].count[k];
176  for (uint k=0; k<Nb_Trends; k++)
177  _data[nb].trend[k] += _data[i].trend[k];
178  }
179  for (uint k=0; k<Nb_Trends; k++)
180  _data[nb].trend[k] /= (_data.size()-1);
181 
182  init();
183 }
184 
185 TQString StatisticsTab::percent(const Data &d, Count count) const
186 {
187  if ( count==Total ) return TQString();
188  return AdditionalTab::percent(d.count[count], d.count[Total], true);
189 }
190 
191 void StatisticsTab::display(uint i)
192 {
193  const Data &d = _data[i];
194  for (uint k=0; k<Nb_Counts; k++) {
195  if ( Count(k) && !internal->showDrawGames ) continue;
196  _nbs[k]->setText(TQString::number(d.count[k]));
197  _percents[k]->setText(percent(d, Count(k)));
198  }
199  for (uint k=0; k<Nb_Trends; k++) {
200  TQString s;
201  if ( d.trend[k]>0 ) s = '+';
202  int prec = (i==internal->playerInfos().nbEntries() ? 1 : 0);
203  _trends[k]->setText(s + TQString::number(d.trend[k], 'f', prec));
204  }
205 }
206 
207 //-----------------------------------------------------------------------------
208 HistogramTab::HistogramTab(TQWidget *parent)
209  : AdditionalTab(parent, "histogram_tab")
210 {
211  // construct GUI
212  TQVBoxLayout *top = static_cast<TQVBoxLayout *>(layout());
213 
214  _list = new TDEListView(this);
215  _list->setSelectionMode(TQListView::NoSelection);
216  _list->setItemMargin(3);
217  _list->setAllColumnsShowFocus(true);
218  _list->setSorting(-1);
219  _list->header()->setClickEnabled(false);
220  _list->header()->setMovingEnabled(false);
221  top->addWidget(_list);
222 
223  _list->addColumn(i18n("From"));
224  _list->addColumn(i18n("To"));
225  _list->addColumn(i18n("Count"));
226  _list->addColumn(i18n("Percent"));
227  for (uint i=0; i<4; i++) _list->setColumnAlignment(i, AlignRight);
228  _list->addColumn(TQString());
229 
230  const Item *sitem = internal->scoreInfos().item("score")->item();
231  const PlayerInfos &pi = internal->playerInfos();
232  const TQMemArray<uint> &sh = pi.histogram();
233  for (uint k=1; k<pi.histoSize(); k++) {
234  TQString s1 = sitem->pretty(0, sh[k-1]);
235  TQString s2;
236  if ( k==sh.size() ) s2 = "...";
237  else if ( sh[k]!=sh[k-1]+1 ) s2 = sitem->pretty(0, sh[k]);
238  (void)new TDEListViewItem(_list, s1, s2);
239  }
240 }
241 
242 void HistogramTab::load()
243 {
244  AdditionalTab::load();
245  const PlayerInfos &pi = internal->playerInfos();
246  uint n = pi.nbEntries();
247  uint s = pi.histoSize() - 1;
248  _counts.resize((n+1) * s);
249  _data.fill(0, n+1);
250  for (uint k=0; k<s; k++) {
251  _counts[n*s + k] = 0;
252  for (uint i=0; i<n; i++) {
253  uint nb = pi.item(pi.histoName(k+1))->read(i).toUInt();
254  _counts[i*s + k] = nb;
255  _counts[n*s + k] += nb;
256  _data[i] += nb;
257  _data[n] += nb;
258  }
259  }
260 
261  init();
262 }
263 
264 void HistogramTab::display(uint i)
265 {
266  const PlayerInfos &pi = internal->playerInfos();
267  TQListViewItem *item = _list->firstChild();
268  uint s = pi.histoSize() - 1;
269  for (int k=s-1; k>=0; k--) {
270  uint nb = _counts[i*s + k];
271  item->setText(2, TQString::number(nb));
272  item->setText(3, percent(nb, _data[i]));
273  uint width = (_data[i]==0 ? 0 : tqRound(150.0 * nb / _data[i]));
274  TQPixmap pixmap(width, 10);
275  pixmap.fill(blue);
276  item->setPixmap(4, pixmap);
277  item = item->nextSibling();
278  }
279 }
280 
281 } // namespace

libtdegames

Skip menu "libtdegames"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libtdegames

Skip menu "libtdegames"
  • libtdegames
Generated for libtdegames by doxygen 1.9.1
This website is maintained by Timothy Pearson.