kexthighscore_tab.cpp
00001 /* 00002 This file is part of the KDE games library 00003 Copyright (C) 2002 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., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kexthighscore_tab.h" 00021 #include "kexthighscore_tab.moc" 00022 00023 #include <tqlayout.h> 00024 #include <tqlabel.h> 00025 #include <tqvgroupbox.h> 00026 #include <tqgrid.h> 00027 #include <tqheader.h> 00028 00029 #include <kdialogbase.h> 00030 #include <klistview.h> 00031 #include <kdebug.h> 00032 #include <kglobal.h> 00033 00034 #include "kexthighscore.h" 00035 #include "kexthighscore_internal.h" 00036 00037 00038 namespace KExtHighscore 00039 { 00040 00041 //----------------------------------------------------------------------------- 00042 PlayersCombo::PlayersCombo(TQWidget *parent, const char *name) 00043 : TQComboBox(parent, name) 00044 { 00045 const PlayerInfos &p = internal->playerInfos(); 00046 for (uint i = 0; i<p.nbEntries(); i++) 00047 insertItem(p.prettyName(i)); 00048 insertItem(TQString("<") + i18n("all") + '>'); 00049 connect(this, TQT_SIGNAL(activated(int)), TQT_SLOT(activatedSlot(int))); 00050 } 00051 00052 void PlayersCombo::activatedSlot(int i) 00053 { 00054 const PlayerInfos &p = internal->playerInfos(); 00055 if ( i==(int)p.nbEntries() ) emit allSelected(); 00056 else if ( i==(int)p.nbEntries()+1 ) emit noneSelected(); 00057 else emit playerSelected(i); 00058 } 00059 00060 void PlayersCombo::load() 00061 { 00062 const PlayerInfos &p = internal->playerInfos(); 00063 for (uint i = 0; i<p.nbEntries(); i++) 00064 changeItem(p.prettyName(i), i); 00065 } 00066 00067 //----------------------------------------------------------------------------- 00068 AdditionalTab::AdditionalTab(TQWidget *parent, const char *name) 00069 : TQWidget(parent, name) 00070 { 00071 TQVBoxLayout *top = new TQVBoxLayout(this, KDialogBase::marginHint(), 00072 KDialogBase::spacingHint()); 00073 00074 TQHBoxLayout *hbox = new TQHBoxLayout(top); 00075 TQLabel *label = new TQLabel(i18n("Select player:"), this); 00076 hbox->addWidget(label); 00077 _combo = new PlayersCombo(this); 00078 connect(_combo, TQT_SIGNAL(playerSelected(uint)), 00079 TQT_SLOT(playerSelected(uint))); 00080 connect(_combo, TQT_SIGNAL(allSelected()), TQT_SLOT(allSelected())); 00081 hbox->addWidget(_combo); 00082 hbox->addStretch(1); 00083 } 00084 00085 void AdditionalTab::init() 00086 { 00087 uint id = internal->playerInfos().id(); 00088 _combo->setCurrentItem(id); 00089 playerSelected(id); 00090 } 00091 00092 void AdditionalTab::allSelected() 00093 { 00094 display(internal->playerInfos().nbEntries()); 00095 } 00096 00097 TQString AdditionalTab::percent(uint n, uint total, bool withBraces) 00098 { 00099 if ( n==0 || total==0 ) return TQString(); 00100 TQString s = TQString("%1%").arg(100.0 * n / total, 0, 'f', 1); 00101 return (withBraces ? TQString("(") + s + ")" : s); 00102 } 00103 00104 void AdditionalTab::load() 00105 { 00106 _combo->load(); 00107 } 00108 00109 00110 //----------------------------------------------------------------------------- 00111 const char *StatisticsTab::COUNT_LABELS[Nb_Counts] = { 00112 I18N_NOOP("Total:"), I18N_NOOP("Won:"), I18N_NOOP("Lost:"), 00113 I18N_NOOP("Draw:") 00114 }; 00115 const char *StatisticsTab::TREND_LABELS[Nb_Trends] = { 00116 I18N_NOOP("Current:"), I18N_NOOP("Max won:"), I18N_NOOP("Max lost:") 00117 }; 00118 00119 StatisticsTab::StatisticsTab(TQWidget *parent) 00120 : AdditionalTab(parent, "statistics_tab") 00121 { 00122 // construct GUI 00123 TQVBoxLayout *top = static_cast<TQVBoxLayout *>(layout()); 00124 00125 TQHBoxLayout *hbox = new TQHBoxLayout(top); 00126 TQVBoxLayout *vbox = new TQVBoxLayout(hbox); 00127 TQVGroupBox *group = new TQVGroupBox(i18n("Game Counts"), this); 00128 vbox->addWidget(group); 00129 TQGrid *grid = new TQGrid(3, group); 00130 grid->setSpacing(KDialogBase::spacingHint()); 00131 for (uint k=0; k<Nb_Counts; k++) { 00132 if ( Count(k)==Draw && !internal->showDrawGames ) continue; 00133 (void)new TQLabel(i18n(COUNT_LABELS[k]), grid); 00134 _nbs[k] = new TQLabel(grid); 00135 _percents[k] = new TQLabel(grid); 00136 } 00137 00138 group = new TQVGroupBox(i18n("Trends"), this); 00139 vbox->addWidget(group); 00140 grid = new TQGrid(2, group); 00141 grid->setSpacing(KDialogBase::spacingHint()); 00142 for (uint k=0; k<Nb_Trends; k++) { 00143 (void)new TQLabel(i18n(TREND_LABELS[k]), grid); 00144 _trends[k] = new TQLabel(grid); 00145 } 00146 00147 hbox->addStretch(1); 00148 top->addStretch(1); 00149 } 00150 00151 void StatisticsTab::load() 00152 { 00153 AdditionalTab::load(); 00154 const PlayerInfos &pi = internal->playerInfos(); 00155 uint nb = pi.nbEntries(); 00156 _data.resize(nb+1); 00157 for (uint i=0; i<_data.size()-1; i++) { 00158 _data[i].count[Total] = pi.item("nb games")->read(i).toUInt(); 00159 _data[i].count[Lost] = pi.item("nb lost games")->read(i).toUInt() 00160 + pi.item("nb black marks")->read(i).toUInt(); // legacy 00161 _data[i].count[Draw] = pi.item("nb draw games")->read(i).toUInt(); 00162 _data[i].count[Won] = _data[i].count[Total] - _data[i].count[Lost] 00163 - _data[i].count[Draw]; 00164 _data[i].trend[CurrentTrend] = 00165 pi.item("current trend")->read(i).toInt(); 00166 _data[i].trend[WonTrend] = pi.item("max won trend")->read(i).toUInt(); 00167 _data[i].trend[LostTrend] = 00168 -(int)pi.item("max lost trend")->read(i).toUInt(); 00169 } 00170 00171 for (uint k=0; k<Nb_Counts; k++) _data[nb].count[k] = 0; 00172 for (uint k=0; k<Nb_Trends; k++) _data[nb].trend[k] = 0; 00173 for (uint i=0; i<_data.size()-1; i++) { 00174 for (uint k=0; k<Nb_Counts; k++) 00175 _data[nb].count[k] += _data[i].count[k]; 00176 for (uint k=0; k<Nb_Trends; k++) 00177 _data[nb].trend[k] += _data[i].trend[k]; 00178 } 00179 for (uint k=0; k<Nb_Trends; k++) 00180 _data[nb].trend[k] /= (_data.size()-1); 00181 00182 init(); 00183 } 00184 00185 TQString StatisticsTab::percent(const Data &d, Count count) const 00186 { 00187 if ( count==Total ) return TQString(); 00188 return AdditionalTab::percent(d.count[count], d.count[Total], true); 00189 } 00190 00191 void StatisticsTab::display(uint i) 00192 { 00193 const Data &d = _data[i]; 00194 for (uint k=0; k<Nb_Counts; k++) { 00195 if ( Count(k) && !internal->showDrawGames ) continue; 00196 _nbs[k]->setText(TQString::number(d.count[k])); 00197 _percents[k]->setText(percent(d, Count(k))); 00198 } 00199 for (uint k=0; k<Nb_Trends; k++) { 00200 TQString s; 00201 if ( d.trend[k]>0 ) s = '+'; 00202 int prec = (i==internal->playerInfos().nbEntries() ? 1 : 0); 00203 _trends[k]->setText(s + TQString::number(d.trend[k], 'f', prec)); 00204 } 00205 } 00206 00207 //----------------------------------------------------------------------------- 00208 HistogramTab::HistogramTab(TQWidget *parent) 00209 : AdditionalTab(parent, "histogram_tab") 00210 { 00211 // construct GUI 00212 TQVBoxLayout *top = static_cast<TQVBoxLayout *>(layout()); 00213 00214 _list = new KListView(this); 00215 _list->setSelectionMode(TQListView::NoSelection); 00216 _list->setItemMargin(3); 00217 _list->setAllColumnsShowFocus(true); 00218 _list->setSorting(-1); 00219 _list->header()->setClickEnabled(false); 00220 _list->header()->setMovingEnabled(false); 00221 top->addWidget(_list); 00222 00223 _list->addColumn(i18n("From")); 00224 _list->addColumn(i18n("To")); 00225 _list->addColumn(i18n("Count")); 00226 _list->addColumn(i18n("Percent")); 00227 for (uint i=0; i<4; i++) _list->setColumnAlignment(i, AlignRight); 00228 _list->addColumn(TQString()); 00229 00230 const Item *sitem = internal->scoreInfos().item("score")->item(); 00231 const PlayerInfos &pi = internal->playerInfos(); 00232 const TQMemArray<uint> &sh = pi.histogram(); 00233 for (uint k=1; k<pi.histoSize(); k++) { 00234 TQString s1 = sitem->pretty(0, sh[k-1]); 00235 TQString s2; 00236 if ( k==sh.size() ) s2 = "..."; 00237 else if ( sh[k]!=sh[k-1]+1 ) s2 = sitem->pretty(0, sh[k]); 00238 (void)new KListViewItem(_list, s1, s2); 00239 } 00240 } 00241 00242 void HistogramTab::load() 00243 { 00244 AdditionalTab::load(); 00245 const PlayerInfos &pi = internal->playerInfos(); 00246 uint n = pi.nbEntries(); 00247 uint s = pi.histoSize() - 1; 00248 _counts.resize((n+1) * s); 00249 _data.fill(0, n+1); 00250 for (uint k=0; k<s; k++) { 00251 _counts[n*s + k] = 0; 00252 for (uint i=0; i<n; i++) { 00253 uint nb = pi.item(pi.histoName(k+1))->read(i).toUInt(); 00254 _counts[i*s + k] = nb; 00255 _counts[n*s + k] += nb; 00256 _data[i] += nb; 00257 _data[n] += nb; 00258 } 00259 } 00260 00261 init(); 00262 } 00263 00264 void HistogramTab::display(uint i) 00265 { 00266 const PlayerInfos &pi = internal->playerInfos(); 00267 TQListViewItem *item = _list->firstChild(); 00268 uint s = pi.histoSize() - 1; 00269 for (int k=s-1; k>=0; k--) { 00270 uint nb = _counts[i*s + k]; 00271 item->setText(2, TQString::number(nb)); 00272 item->setText(3, percent(nb, _data[i])); 00273 uint width = (_data[i]==0 ? 0 : tqRound(150.0 * nb / _data[i])); 00274 TQPixmap pixmap(width, 10); 00275 pixmap.fill(blue); 00276 item->setPixmap(4, pixmap); 00277 item = item->nextSibling(); 00278 } 00279 } 00280 00281 } // namespace