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

libtdegames

  • libtdegames
  • highscore
kexthighscore_gui.cpp
1 /*
2  This file is part of the TDE 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_gui.h"
21 #include "kexthighscore_gui.moc"
22 
23 #include <tqlayout.h>
24 #include <tqtextstream.h>
25 #include <tqheader.h>
26 #include <tqgrid.h>
27 #include <tqvgroupbox.h>
28 
29 #include <tdeapplication.h>
30 #include <tdemessagebox.h>
31 #include <kurllabel.h>
32 #include <kopenwith.h>
33 #include <krun.h>
34 #include <tdefiledialog.h>
35 #include <tdetempfile.h>
36 #include <tdeio/netaccess.h>
37 #include <kiconloader.h>
38 
39 #include "kexthighscore_internal.h"
40 #include "kexthighscore.h"
41 #include "kexthighscore_tab.h"
42 
43 
44 namespace KExtHighscore
45 {
46 
47 //-----------------------------------------------------------------------------
48 ShowItem::ShowItem(TQListView *list, bool highlight)
49  : TDEListViewItem(list), _highlight(highlight)
50 {}
51 
52 void ShowItem::paintCell(TQPainter *p, const TQColorGroup &cg,
53  int column, int width, int align)
54 {
55  TQColorGroup cgrp(cg);
56  if (_highlight) cgrp.setColor(TQColorGroup::Text, red);
57  TDEListViewItem::paintCell(p, cgrp, column, width, align);
58 }
59 
60 //-----------------------------------------------------------------------------
61 ScoresList::ScoresList(TQWidget *parent)
62  : TDEListView(parent)
63 {
64  setSelectionMode(TQListView::NoSelection);
65  setItemMargin(3);
66  setAllColumnsShowFocus(true);
67  setSorting(-1);
68  header()->setClickEnabled(false);
69  header()->setMovingEnabled(false);
70 }
71 
72 void ScoresList::addHeader(const ItemArray &items)
73 {
74  addLineItem(items, 0, 0);
75 }
76 
77 TQListViewItem *ScoresList::addLine(const ItemArray &items,
78  uint index, bool highlight)
79 {
80  TQListViewItem *item = new ShowItem(this, highlight);
81  addLineItem(items, index, item);
82  return item;
83 }
84 
85 void ScoresList::addLineItem(const ItemArray &items,
86  uint index, TQListViewItem *line)
87 {
88  uint k = 0;
89  for (uint i=0; i<items.size(); i++) {
90  const ItemContainer &container = *items[i];
91  if ( !container.item()->isVisible() ) continue;
92  if (line) line->setText(k, itemText(container, index));
93  else {
94  addColumn( container.item()->label() );
95  setColumnAlignment(k, container.item()->alignment());
96  }
97  k++;
98  }
99 }
100 
101 //-----------------------------------------------------------------------------
102 HighscoresList::HighscoresList(TQWidget *parent)
103  : ScoresList(parent)
104 {}
105 
106 TQString HighscoresList::itemText(const ItemContainer &item, uint row) const
107 {
108  return item.pretty(row);
109 }
110 
111 void HighscoresList::load(const ItemArray &items, int highlight)
112 {
113  clear();
114  TQListViewItem *line = 0;
115  for (int j=items.nbEntries()-1; j>=0; j--) {
116  TQListViewItem *item = addLine(items, j, j==highlight);
117  if ( j==highlight ) line = item;
118  }
119  if (line) ensureItemVisible(line);
120 }
121 
122 //-----------------------------------------------------------------------------
123 HighscoresWidget::HighscoresWidget(TQWidget *parent)
124  : TQWidget(parent, "show_highscores_widget"),
125  _statsTab(0), _histoTab(0)
126 {
127  const ScoreInfos &s = internal->scoreInfos();
128  const PlayerInfos &p = internal->playerInfos();
129 
130  TQVBoxLayout *vbox = new TQVBoxLayout(this, KDialogBase::spacingHint());
131 
132  _tw = new TQTabWidget(this);
133  connect(_tw, TQ_SIGNAL(currentChanged(TQWidget *)), TQ_SLOT(tabChanged()));
134  vbox->addWidget(_tw);
135 
136  // scores tab
137  _scoresList = new HighscoresList(_tw);
138  _scoresList->addHeader(s);
139  _tw->addTab(_scoresList, i18n("Best &Scores"));
140 
141  // players tab
142  _playersList = new HighscoresList(_tw);
143  _playersList->addHeader(p);
144  _tw->addTab(_playersList, i18n("&Players"));
145 
146  // statistics tab
147  if ( internal->showStatistics ) {
148  _statsTab = new StatisticsTab(_tw);
149  _tw->addTab(_statsTab, i18n("Statistics"));
150  }
151 
152  // histogram tab
153  if ( p.histogram().size()!=0 ) {
154  _histoTab = new HistogramTab(_tw);
155  _tw->addTab(_histoTab, i18n("Histogram"));
156  }
157 }
158 
159 void HighscoresWidget::changeTab(int i)
160 {
161  if ( i!=_tw->currentPageIndex() )
162  _tw->setCurrentPage(i);
163 }
164 
165 void HighscoresWidget::load(int rank)
166 {
167  _scoresList->load(internal->scoreInfos(), rank);
168  _playersList->load(internal->playerInfos(), internal->playerInfos().id());
169  if (_statsTab) _statsTab->load();
170  if (_histoTab) _histoTab->load();
171 }
172 
173 //-----------------------------------------------------------------------------
174 HighscoresDialog::HighscoresDialog(int rank, TQWidget *parent)
175  : KDialogBase(internal->nbGameTypes()>1 ? TreeList : Plain,
176  i18n("Highscores"), Close|User1|User2, Close,
177  parent, "show_highscores", true, true,
178  KGuiItem(i18n("Configure..."), "configure"),
179  KGuiItem(i18n("Export..."))), _rank(rank), _tab(0)
180 {
181  _widgets.resize(internal->nbGameTypes(), 0);
182 
183  if ( internal->nbGameTypes()>1 ) {
184  for (uint i=0; i<internal->nbGameTypes(); i++) {
185  TQString title = internal->manager.gameTypeLabel(i, Manager::I18N);
186  TQString icon = internal->manager.gameTypeLabel(i, Manager::Icon);
187  TQWidget *w = addVBoxPage(title, TQString(),
188  BarIcon(icon, TDEIcon::SizeLarge));
189  if ( i==internal->gameType() ) createPage(w);
190  }
191 
192  connect(this, TQ_SIGNAL(aboutToShowPage(TQWidget *)),
193  TQ_SLOT(createPage(TQWidget *)));
194  showPage(internal->gameType());
195  } else {
196  TQVBoxLayout *vbox = new TQVBoxLayout(plainPage());
197  createPage(plainPage());
198  vbox->addWidget(_widgets[0]);
199  setMainWidget(_widgets[0]);
200  }
201 }
202 
203 void HighscoresDialog::createPage(TQWidget *page)
204 {
205  internal->hsConfig().readCurrentConfig();
206  _current = page;
207  bool several = ( internal->nbGameTypes()>1 );
208  int i = (several ? pageIndex(page) : 0);
209  if ( _widgets[i]==0 ) {
210  _widgets[i] = new HighscoresWidget(page);
211  connect(_widgets[i], TQ_SIGNAL(tabChanged(int)), TQ_SLOT(tabChanged(int)));
212  }
213  uint type = internal->gameType();
214  if (several) internal->setGameType(i);
215  _widgets[i]->load(uint(i)==type ? _rank : -1);
216  if (several) setGameType(type);
217  _widgets[i]->changeTab(_tab);
218 }
219 
220 void HighscoresDialog::slotUser1()
221 {
222  if ( KExtHighscore::configure(this) )
223  createPage(_current);
224 }
225 
226 void HighscoresDialog::slotUser2()
227 {
228  KURL url = KFileDialog::getSaveURL(TQString(), TQString(), this);
229  if ( url.isEmpty() ) return;
230  if ( TDEIO::NetAccess::exists(url, true, this) ) {
231  KGuiItem gi = KStdGuiItem::save();
232  gi.setText(i18n("Overwrite"));
233  int res = KMessageBox::warningContinueCancel(this,
234  i18n("The file already exists. Overwrite?"),
235  i18n("Export"), gi);
236  if ( res==KMessageBox::Cancel ) return;
237  }
238  KTempFile tmp;
239  internal->exportHighscores(*tmp.textStream());
240  tmp.close();
241  TDEIO::NetAccess::upload(tmp.name(), url, this);
242  tmp.unlink();
243 }
244 
245 //-----------------------------------------------------------------------------
246 LastMultipleScoresList::LastMultipleScoresList(
247  const TQValueVector<Score> &scores, TQWidget *parent)
248  : ScoresList(parent), _scores(scores)
249 {
250  const ScoreInfos &s = internal->scoreInfos();
251  addHeader(s);
252  for (uint i=0; i<scores.size(); i++) addLine(s, i, false);
253 }
254 
255 void LastMultipleScoresList::addLineItem(const ItemArray &si,
256  uint index, TQListViewItem *line)
257 {
258  uint k = 1; // skip "id"
259  for (uint i=0; i<si.size()-2; i++) {
260  if ( i==3 ) k = 5; // skip "date"
261  const ItemContainer *container = si[k];
262  k++;
263  if (line) line->setText(i, itemText(*container, index));
264  else {
265  addColumn( container->item()->label() );
266  setColumnAlignment(i, container->item()->alignment());
267  }
268  }
269 }
270 
271 TQString LastMultipleScoresList::itemText(const ItemContainer &item,
272  uint row) const
273 {
274  TQString name = item.name();
275  if ( name=="rank" )
276  return (_scores[row].type()==Won ? i18n("Winner") : TQString());
277  TQVariant v = _scores[row].data(name);
278  if ( name=="name" ) return v.toString();
279  return item.item()->pretty(row, v);
280 }
281 
282 //-----------------------------------------------------------------------------
283 TotalMultipleScoresList::TotalMultipleScoresList(
284  const TQValueVector<Score> &scores, TQWidget *parent)
285  : ScoresList(parent), _scores(scores)
286 {
287  const ScoreInfos &s = internal->scoreInfos();
288  addHeader(s);
289  for (uint i=0; i<scores.size(); i++) addLine(s, i, false);
290 }
291 
292 void TotalMultipleScoresList::addLineItem(const ItemArray &si,
293  uint index, TQListViewItem *line)
294 {
295  const PlayerInfos &pi = internal->playerInfos();
296  uint k = 1; // skip "id"
297  for (uint i=0; i<4; i++) { // skip additional fields
298  const ItemContainer *container;
299  if ( i==2 ) container = pi.item("nb games");
300  else if ( i==3 ) container = pi.item("mean score");
301  else {
302  container = si[k];
303  k++;
304  }
305  if (line) line->setText(i, itemText(*container, index));
306  else {
307  TQString label =
308  (i==2 ? i18n("Won Games") : container->item()->label());
309  addColumn(label);
310  setColumnAlignment(i, container->item()->alignment());
311  }
312  }
313 }
314 
315 TQString TotalMultipleScoresList::itemText(const ItemContainer &item,
316  uint row) const
317 {
318  TQString name = item.name();
319  if ( name=="rank" ) return TQString::number(_scores.size()-row);
320  if ( name=="nb games" )
321  return TQString::number( _scores[row].data("nb won games").toUInt() );
322  TQVariant v = _scores[row].data(name);
323  if ( name=="name" ) return v.toString();
324  return item.item()->pretty(row, v);
325 }
326 
327 
328 //-----------------------------------------------------------------------------
329 ConfigDialog::ConfigDialog(TQWidget *parent)
330  : KDialogBase(Swallow, i18n("Configure Highscores"),
331  Ok|Apply|Cancel, Cancel,
332  parent, "configure_highscores", true, true),
333  _saved(false)
334 {
335  TQWidget *page = new TQWidget(this);
336  setMainWidget(page);
337 
338  TQGridLayout *pageTop =
339  new TQGridLayout(page, 2, 2, spacingHint(), spacingHint());
340 
341  TQLabel *label = new TQLabel(i18n("Nickname:"), page);
342  pageTop->addWidget(label, 0, 0);
343  _nickname = new TQLineEdit(page);
344  connect(_nickname, TQ_SIGNAL(textChanged(const TQString &)),
345  TQ_SLOT(modifiedSlot()));
346  connect(_nickname, TQ_SIGNAL(textChanged(const TQString &)),
347  TQ_SLOT(nickNameChanged(const TQString &)));
348 
349  _nickname->setMaxLength(16);
350  pageTop->addWidget(_nickname, 0, 1);
351 
352  label = new TQLabel(i18n("Comment:"), page);
353  pageTop->addWidget(label, 1, 0);
354  _comment = new TQLineEdit(page);
355  connect(_comment, TQ_SIGNAL(textChanged(const TQString &)),
356  TQ_SLOT(modifiedSlot()));
357  _comment->setMaxLength(50);
358  pageTop->addWidget(_comment, 1, 1);
359 
360  load();
361  enableButtonOK( !_nickname->text().isEmpty() );
362  enableButtonApply(false);
363 }
364 
365 void ConfigDialog::nickNameChanged(const TQString &text)
366 {
367  enableButtonOK( !text.isEmpty() );
368 }
369 
370 
371 void ConfigDialog::modifiedSlot()
372 {
373  enableButtonApply(true && !_nickname->text().isEmpty() );
374 }
375 
376 void ConfigDialog::accept()
377 {
378  if ( save() ) {
379  KDialogBase::accept();
380  tdeApp->config()->sync(); // safer
381  }
382 }
383 
384 void ConfigDialog::load()
385 {
386  internal->hsConfig().readCurrentConfig();
387  const PlayerInfos &infos = internal->playerInfos();
388  _nickname->setText(infos.isAnonymous() ? TQString() : infos.name());
389  _comment->setText(infos.comment());
390 }
391 
392 bool ConfigDialog::save()
393 {
394  // do not bother the user with "nickname empty" if he has not
395  // messed with nickname settings ...
396  TQString newName = _nickname->text();
397  if ( newName.isEmpty() && !internal->playerInfos().isAnonymous() )
398  return true;
399 
400  if ( newName.isEmpty() ) {
401  KMessageBox::sorry(this, i18n("Please choose a non empty nickname."));
402  return false;
403  }
404  if ( internal->playerInfos().isNameUsed(newName) ) {
405  KMessageBox::sorry(this, i18n("Nickname already in use. Please "
406  "choose another one"));
407  return false;
408  }
409 
410  int res = internal->modifySettings(newName, _comment->text(), this);
411  if (res) {
412  load(); // needed to update view when "apply" is clicked
413  enableButtonApply(false);
414  }
415  _saved = true;
416  return res;
417 }
418 
419 //-----------------------------------------------------------------------------
420 AskNameDialog::AskNameDialog(TQWidget *parent)
421  : KDialogBase(Plain, i18n("Enter Your Nickname"), Ok | Cancel, Ok,
422  parent, "ask_name_dialog")
423 {
424  internal->hsConfig().readCurrentConfig();
425 
426  TQVBoxLayout *top =
427  new TQVBoxLayout(plainPage(), marginHint(), spacingHint());
428  TQLabel *label =
429  new TQLabel(i18n("Congratulations, you have won!"), plainPage());
430  top->addWidget(label);
431 
432  TQHBoxLayout *hbox = new TQHBoxLayout(top);
433  label = new TQLabel(i18n("Enter your nickname:"), plainPage());
434  hbox->addWidget(label);
435  _edit = new TQLineEdit(plainPage());
436  _edit->setFocus();
437  connect(_edit, TQ_SIGNAL(textChanged(const TQString &)), TQ_SLOT(nameChanged()));
438  hbox->addWidget(_edit);
439 
440  top->addSpacing(spacingHint());
441  _checkbox = new TQCheckBox(i18n("Do not ask again."), plainPage());
442  top->addWidget(_checkbox);
443 
444  nameChanged();
445 }
446 
447 void AskNameDialog::nameChanged()
448 {
449  enableButtonOK( !name().isEmpty()
450  && !internal->playerInfos().isNameUsed(name()) );
451 }
452 
453 } // 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.