20 #include "kexthighscore_tab.h"
21 #include "kexthighscore_tab.moc"
25 #include <tqvgroupbox.h>
29 #include <kdialogbase.h>
30 #include <tdelistview.h>
32 #include <tdeglobal.h>
34 #include "kexthighscore.h"
35 #include "kexthighscore_internal.h"
38 namespace KExtHighscore
42 PlayersCombo::PlayersCombo(TQWidget *parent,
const char *name)
43 : TQComboBox(parent, name)
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)));
52 void PlayersCombo::activatedSlot(
int i)
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);
60 void PlayersCombo::load()
62 const PlayerInfos &p =
internal->playerInfos();
63 for (uint i = 0; i<p.nbEntries(); i++)
64 changeItem(p.prettyName(i), i);
68 AdditionalTab::AdditionalTab(TQWidget *parent,
const char *name)
69 : TQWidget(parent, name)
71 TQVBoxLayout *top =
new TQVBoxLayout(
this, KDialogBase::marginHint(),
72 KDialogBase::spacingHint());
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);
85 void AdditionalTab::init()
87 uint
id =
internal->playerInfos().id();
88 _combo->setCurrentItem(
id);
92 void AdditionalTab::allSelected()
94 display(internal->playerInfos().nbEntries());
97 TQString AdditionalTab::percent(uint n, uint total,
bool withBraces)
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);
104 void AdditionalTab::load()
111 const char *StatisticsTab::COUNT_LABELS[Nb_Counts] = {
112 I18N_NOOP(
"Total:"), I18N_NOOP(
"Won:"), I18N_NOOP(
"Lost:"),
115 const char *StatisticsTab::TREND_LABELS[Nb_Trends] = {
116 I18N_NOOP(
"Current:"), I18N_NOOP(
"Max won:"), I18N_NOOP(
"Max lost:")
119 StatisticsTab::StatisticsTab(TQWidget *parent)
120 : AdditionalTab(parent,
"statistics_tab")
123 TQVBoxLayout *top =
static_cast<TQVBoxLayout *
>(layout());
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);
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);
151 void StatisticsTab::load()
153 AdditionalTab::load();
154 const PlayerInfos &pi =
internal->playerInfos();
155 uint nb = pi.nbEntries();
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();
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();
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];
179 for (uint k=0; k<Nb_Trends; k++)
180 _data[nb].trend[k] /= (_data.size()-1);
185 TQString StatisticsTab::percent(
const Data &d, Count count)
const
187 if ( count==Total )
return TQString();
188 return AdditionalTab::percent(d.count[count], d.count[Total],
true);
191 void StatisticsTab::display(uint i)
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)));
199 for (uint k=0; k<Nb_Trends; k++) {
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));
208 HistogramTab::HistogramTab(TQWidget *parent)
209 : AdditionalTab(parent,
"histogram_tab")
212 TQVBoxLayout *top =
static_cast<TQVBoxLayout *
>(layout());
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);
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());
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]);
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);
242 void HistogramTab::load()
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);
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;
264 void HistogramTab::display(uint i)
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);
276 item->setPixmap(4, pixmap);
277 item = item->nextSibling();