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

kgoldrunner

  • sources
  • kde-4.14
  • kdegames
  • kgoldrunner
  • src
kgrselector.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright 2003 Marco Krüger <grisuji@gmx.de> *
3  * Copyright 2003 Ian Wadham <iandw.au@gmail.com> *
4  * Copyright 2009 Ian Wadham <iandw.au@gmail.com> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18  ****************************************************************************/
19 
20 #include "kgrglobals.h"
21 
22 #include "kgrselector.h"
23 
24 #include "kgrgameio.h"
25 
26 #include <QTextStream>
27 #include <QGridLayout>
28 #include <QVBoxLayout>
29 #include <QSpacerItem>
30 #include <QHeaderView>
31 #include <QScrollBar>
32 #include <QPushButton>
33 #include <QButtonGroup>
34 #include <QRadioButton>
35 #include <QLabel>
36 #include <QTextEdit>
37 
38 #include <QPainter>
39 
40 #include <QApplication>
41 #include <QDesktopWidget>
42 
43 #include <KGlobalSettings>
44 #include <KConfigGroup>
45 #include <KIntNumInput>
46 
47 /******************************************************************************/
48 /***************** DIALOG BOX TO SELECT A GAME AND LEVEL *****************/
49 /******************************************************************************/
50 
51 KGrSLDialog::KGrSLDialog (int action, int requestedLevel, int gameIndex,
52  QList<KGrGameData *> & gameList,
53  const QString & pSystemDir, const QString & pUserDir,
54  QWidget * parent)
55  :
56  KDialog (parent),
57  slAction (action),
58  myGameList (gameList),
59  defaultGame (gameIndex),
60  defaultLevel (requestedLevel),
61  systemDir (pSystemDir),
62  userDir (pUserDir),
63  slParent (parent)
64 {
65  setupWidgets();
66 }
67 
68 KGrSLDialog::~KGrSLDialog()
69 {
70 }
71 
72 bool KGrSLDialog::selectLevel (int & selectedGame, int & selectedLevel)
73 {
74  selectedGame = defaultGame;
75  selectedLevel = 0; // 0 = no selection (Cancel) or invalid.
76 
77  // Create and run a modal dialog box to select a game and level.
78  while (exec() == QDialog::Accepted) {
79  selectedGame = slGameIndex;
80  selectedLevel = 0; // In case the selection is invalid.
81  if (myGameList.at (selectedGame)->owner == SYSTEM) {
82  switch (slAction) {
83  case SL_CREATE: // Can save only in a USER collection.
84  case SL_SAVE:
85  case SL_MOVE:
86  KGrMessage::information (slParent, i18n ("Select Level"),
87  i18n ("Sorry, you can only save or move "
88  "into one of your own games."));
89  continue; // Re-run the dialog box.
90  break;
91  case SL_DELETE: // Can delete only in a USER collection.
92  KGrMessage::information (slParent, i18n ("Select Level"),
93  i18n ("Sorry, you can only delete a level "
94  "from one of your own games."));
95  continue; // Re-run the dialog box.
96  break;
97  case SL_UPD_GAME: // Can edit info only in a USER collection.
98  KGrMessage::information (slParent, i18n ("Edit Game Info"),
99  i18n ("Sorry, you can only edit the game "
100  "information on your own games."));
101  continue; // Re-run the dialog box.
102  break;
103  default:
104  break;
105  }
106  }
107 
108  selectedLevel = number->value();
109  if ((selectedLevel > myGameList.at (selectedGame)->nLevels) &&
110  (slAction != SL_CREATE) && (slAction != SL_SAVE) &&
111  (slAction != SL_MOVE) && (slAction != SL_UPD_GAME)) {
112  KGrMessage::information (slParent, i18n ("Select Level"),
113  i18n ("There is no level %1 in \"%2\", "
114  "so you cannot play or edit it.",
115  selectedLevel,
116  myGameList.at (selectedGame)->name));
117  selectedLevel = 0; // Set an invalid selection.
118  continue; // Re-run the dialog box.
119  }
120  break; // Accepted and valid.
121  }
122  return (selectedLevel > 0); // 0 = cancelled or invalid.
123 }
124 
125 void KGrSLDialog::setupWidgets()
126 {
127  int margin = marginHint();
128  int spacing = spacingHint();
129  QWidget * dad = new QWidget (this);
130  setMainWidget (dad);
131  setCaption (i18n ("Select Game"));
132  setButtons (KDialog::Ok | KDialog::Cancel | KDialog::Help);
133  setDefaultButton (KDialog::Ok);
134 
135  QVBoxLayout * mainLayout = new QVBoxLayout (dad);
136  mainLayout->setSpacing (spacing);
137  mainLayout->setMargin (margin);
138 
139  gameL = new QLabel
140  (i18n ("<html><b>Please select a game:</b></html>"), dad);
141  mainLayout->addWidget (gameL, 5);
142 
143  games = new QTreeWidget (dad);
144  mainLayout->addWidget (games, 50);
145  games->setColumnCount (4);
146  games->setHeaderLabels (QStringList() <<
147  i18n ("Name of Game") <<
148  i18n ("Rules") <<
149  i18n ("Levels") <<
150  i18n ("Skill"));
151  games->setRootIsDecorated (false);
152 
153  QHBoxLayout * hboxLayout1 = new QHBoxLayout();
154  hboxLayout1->setSpacing (6);
155  hboxLayout1->setMargin (0);
156 
157  gameN = new QLabel ("", dad); // Name of selected game.
158  QFont f = gameN->font();
159  f.setBold (true);
160  gameN->setFont (f);
161  hboxLayout1->addWidget (gameN);
162 
163  QSpacerItem * spacerItem1 = new QSpacerItem
164  (21, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
165  hboxLayout1->addItem (spacerItem1);
166 
167  gameD = new QLabel ("", dad); // Description of game.
168  hboxLayout1->addWidget (gameD);
169  mainLayout->addLayout (hboxLayout1, 5);
170 
171  gameAbout = new QTextEdit (dad);
172  gameAbout->setReadOnly (true);
173  mainLayout->addWidget (gameAbout, 25);
174 
175  QFrame * separator = new QFrame (dad);
176  separator->setFrameShape (QFrame::HLine);
177  mainLayout->addWidget (separator);
178 
179  if ((slAction == SL_START) || (slAction == SL_UPD_GAME)) {
180  dad-> setWindowTitle (i18n ("Select Game"));
181  QLabel * startMsg = new QLabel
182  ("<b>" + i18n ("Level 1 of the selected game is:") + "</b>", dad);
183  mainLayout->addWidget (startMsg, 5);
184  }
185  else {
186  dad-> setWindowTitle (i18n ("Select Game/Level"));
187  QLabel * selectLev = new QLabel
188  ("<b>" + i18n ("Please select a level:") + "</b>", dad);
189  mainLayout->addWidget (selectLev, 5);
190  }
191 
192  QGridLayout * grid = new QGridLayout;
193  mainLayout->addLayout (grid);
194 
195  number = new QScrollBar (Qt::Vertical, dad);
196  number->setRange (1, 150);
197  number->setSingleStep (1);
198  number->setPageStep (10);
199  number->setValue (1);
200  grid->addWidget (number, 1, 5, 4, 1);
201 
202  QWidget * numberPair = new QWidget (dad);
203  QHBoxLayout *hboxLayout2 = new QHBoxLayout (numberPair);
204  hboxLayout2->setMargin (0);
205  numberPair->setLayout (hboxLayout2);
206  grid->addWidget (numberPair, 1, 1, 1, 3);
207  numberL = new QLabel (i18n ("Level number:"), numberPair);
208  display = new KIntSpinBox (numberPair);
209  display->setRange (1, 150);
210  hboxLayout2->addWidget (numberL);
211  hboxLayout2->addWidget (display);
212 
213  levelNH = new QPushButton (i18n ("Edit Level Name && Hint"), dad);
214  mainLayout->addWidget (levelNH);
215 
216  slName = new QLabel ("", dad);
217  grid->addWidget (slName, 2, 1, 1, 4);
218  thumbNail = new KGrThumbNail (dad);
219  grid->addWidget (thumbNail, 1, 6, 4, 5);
220 
221  // Set thumbnail cell size to about 1/5 of game cell size.
222  int cellSize = slParent->width() / (5 * (FIELDWIDTH + 4));
223  cellSize = (cellSize < 4) ? 4 : cellSize;
224  thumbNail-> setFixedWidth ((FIELDWIDTH * cellSize) + 2);
225  thumbNail-> setFixedHeight ((FIELDHEIGHT * cellSize) + 2);
226 
227  // Base the geometry of the dialog box on the playing area.
228  int cell = slParent->width() / (FIELDWIDTH + 4);
229  dad-> setMinimumSize ((FIELDWIDTH*cell/2), (FIELDHEIGHT-3)*cell);
230 
231  // Avoid spilling into the Taskbar or Apple Dock area if they get too close.
232  // Otherwise allow the dialog to choose its size and then be resizeable.
233  const QRect avail = QApplication::desktop()->availableGeometry(this);
234  if ((avail.height() - slParent->height()) <= 120) {
235  dad->setFixedHeight (slParent->height() - 120); // Keep 120 for buttons.
236  }
237 
238  // Set the default for the level-number in the scrollbar.
239  number-> setTracking (true);
240  number->setValue (defaultLevel);
241 
242  slSetGames (defaultGame);
243 
244  // Vary the dialog according to the action.
245  QString OKText = "";
246  switch (slAction) {
247  case SL_START: // Must start at level 1, but can choose a game.
248  OKText = i18n ("Start Game");
249  number->setValue (1);
250  number->setEnabled (false);
251  display->setEnabled (false);
252  number->hide();
253  numberL->hide();
254  display->hide();
255  break;
256  case SL_ANY: // Can start playing at any level in any game.
257  OKText = i18n ("Play Level");
258  break;
259  case SL_REPLAY: // Can ask to see a replay of any level in any game.
260  OKText = i18n ("Replay Level");
261  break;
262  case SL_SOLVE: // Can ask to see a solution of any level in any game.
263  OKText = i18n ("Show Solution");
264  break;
265  case SL_UPDATE: // Can use any level in any game as edit input.
266  OKText = i18n ("Edit Level");
267  break;
268  case SL_CREATE: // Can save a new level only in a USER game.
269  OKText = i18n ("Save New");
270  break;
271  case SL_SAVE: // Can save an edited level only in a USER game.
272  OKText = i18n ("Save Change");
273  break;
274  case SL_DELETE: // Can delete a level only in a USER game.
275  OKText = i18n ("Delete Level");
276  break;
277  case SL_MOVE: // Can move a level only into a USER game.
278  OKText = i18n ("Move To...");
279  break;
280  case SL_UPD_GAME: // Can only edit USER game details.
281  OKText = i18n ("Edit Game Info");
282  number->setValue (1);
283  number->setEnabled (false);
284  display->setEnabled (false);
285  number->hide();
286  numberL->hide();
287  display->hide();
288  break;
289 
290  default: break; // Keep the default settings.
291  }
292  if (!OKText.isEmpty()) {
293  setButtonGuiItem (KDialog::Ok, KGuiItem (OKText));
294  }
295 
296  // Set value in the line-edit box.
297  slShowLevel (number->value());
298 
299  if (display->isEnabled()) {
300  display->setFocus(); // Set the keyboard input on.
301  display->selectAll();
302  }
303 
304  // Paint a thumbnail sketch of the level.
305  thumbNail->setFrameStyle (QFrame::Box | QFrame::Plain);
306  thumbNail->setLineWidth (1);
307  slPaintLevel();
308  thumbNail->show();
309 
310  connect (games, SIGNAL (itemSelectionChanged()), this, SLOT (slGame()));
311 
312  connect (display, SIGNAL (valueChanged(QString)),
313  this, SLOT (slUpdate(QString)));
314 
315  connect (number, SIGNAL(valueChanged(int)), this, SLOT(slShowLevel(int)));
316 
317  // Only enable name and hint dialog here if saving a new or edited level.
318  // At other times the name and hint have not been loaded or initialised yet.
319  if ((slAction == SL_CREATE) || (slAction == SL_SAVE)) {
320  // Signal editNameAndHint() relays the click to a KGrEditor connection.
321  connect (levelNH, SIGNAL (clicked()),
322  this, SIGNAL (editNameAndHint()));
323  }
324  else {
325  levelNH->setEnabled (false);
326  levelNH->hide();
327  }
328 
329  connect (games, SIGNAL(itemSelectionChanged()), this, SLOT(slPaintLevel()));
330  connect (number, SIGNAL (sliderReleased()), this, SLOT (slPaintLevel()));
331 
332  connect (this, SIGNAL (helpClicked()), this, SLOT (slotHelp()));
333 }
334 
335 /******************************************************************************/
336 /***************** LOAD THE LIST OF GAMES (COLLECTIONS) *****************/
337 /******************************************************************************/
338 
339 void KGrSLDialog::slSetGames (int cIndex)
340 {
341  int i;
342  int imax = myGameList.count();
343 
344  // Set values in the table that holds details of available games.
345  // The table is displayed in order of skill then the kind of rules.
346  games->clear();
347  slGameIndex = -1;
348 
349  QList<char> sortOrder1, sortOrder2; // Crude, but effective.
350  sortOrder1 << 'N' << 'C' << 'T';
351  sortOrder2 << 'T' << 'K';
352 
353  foreach (char sortItem1, sortOrder1) {
354  foreach (char sortItem2, sortOrder2) {
355  for (i = 0; i < imax; i++) {
356  if ((myGameList.at (i)->skill == sortItem1) &&
357  (myGameList.at (i)->rules == sortItem2)) {
358  QStringList data;
359  data
360  << myGameList.at (i)->name
361  << ((myGameList.at (i)->rules == 'K') ?
362  i18nc ("Rules", "KGoldrunner") :
363  i18nc ("Rules", "Traditional"))
364  << QString().setNum (myGameList.at (i)->nLevels)
365  << ((myGameList.at (i)->skill == 'T') ?
366  i18nc ("Skill Level", "Tutorial") :
367  ((myGameList.at (i)->skill == 'N') ?
368  i18nc ("Skill Level", "Normal") :
369  i18nc ("Skill Level", "Championship")));
370  KGrGameListItem * thisGame = new KGrGameListItem (data, i);
371  games->addTopLevelItem (thisGame);
372 
373  if (slGameIndex < 0) {
374  slGameIndex = i; // There is at least one game.
375  }
376  if (i == cIndex) {
377  // Mark the currently selected game (default 0).
378  games->setCurrentItem (thisGame);
379  }
380  }
381  } // End "for" loop.
382  }
383  }
384 
385  if (slGameIndex < 0) {
386  return; // The game-list is empty (unlikely).
387  }
388 
389  // Fetch and display information on the selected game.
390  slGame();
391 
392  // Make the column for the game's name a bit wider.
393  games->header()->setResizeMode (0, QHeaderView::ResizeToContents);
394  games->header()->setResizeMode (1, QHeaderView::ResizeToContents);
395  games->header()->setResizeMode (2, QHeaderView::ResizeToContents);
396 }
397 
398 /******************************************************************************/
399 /***************** SLOTS USED BY LEVEL SELECTION DIALOG *****************/
400 /******************************************************************************/
401 
402 void KGrSLDialog::slGame()
403 {
404 
405  if (slGameIndex < 0) {
406  // Ignore the "highlighted" signal caused by inserting in an empty box.
407  return;
408  }
409 
410  if (games->selectedItems().size() <= 0) {
411  return;
412  }
413 
414  slGameIndex = (dynamic_cast<KGrGameListItem *>
415  (games->selectedItems().first()))->id();
416  int n = slGameIndex; // Game selected.
417  int N = defaultGame; // Current game.
418  if (myGameList.at (n)->nLevels > 0) {
419  number->setMaximum (myGameList.at (n)->nLevels);
420  display->setMaximum (myGameList.at (n)->nLevels);
421  }
422  else {
423  number->setMaximum (1); // Avoid range errors.
424  display->setMaximum (1);
425  }
426 
427  KConfigGroup gameGroup (KGlobal::config(), "KDEGame");
428  int lev = 1;
429 
430  // Set a default level number for the selected game.
431  switch (slAction) {
432  case SL_ANY:
433  case SL_REPLAY:
434  case SL_SOLVE:
435  case SL_UPDATE:
436  case SL_DELETE:
437  case SL_UPD_GAME:
438  // If selecting the current game, use the current level number.
439  if (n == N) {
440  number->setValue (defaultLevel);
441  }
442  // Else use the last level played in the selected game (from KConfig).
443  else {
444  lev = gameGroup.readEntry ("Level_" + myGameList.at (n)->prefix, 1);
445  number->setValue (lev); // Else use level 1.
446  }
447  break;
448  case SL_CREATE:
449  case SL_SAVE:
450  case SL_MOVE:
451  if ((n == N) && (slAction != SL_CREATE)) {
452  // Saving/moving level in current game: use current number.
453  number->setValue (defaultLevel);
454  }
455  else {
456  // Saving new/edited level or relocating a level: use "nLevels + 1".
457  number->setMaximum (myGameList.at (n)->nLevels + 1);
458  display->setMaximum (myGameList.at (n)->nLevels + 1);
459  number->setValue (number->maximum());
460  }
461  break;
462  default:
463  number->setValue (1); // Default is level 1.
464  break;
465  }
466 
467  slShowLevel (number->value());
468 
469  int levCnt = myGameList.at (n)->nLevels;
470  if (myGameList.at (n)->rules == 'K')
471  gameD->setText (i18np ("1 level, uses KGoldrunner rules.",
472  "%1 levels, uses KGoldrunner rules.", levCnt));
473  else
474  gameD->setText (i18np ("1 level, uses Traditional rules.",
475  "%1 levels, uses Traditional rules.", levCnt));
476  gameN->setText (myGameList.at (n)->name);
477  QString s;
478  if (myGameList.at (n)->about.isEmpty()) {
479  s = i18n ("Sorry, there is no further information about this game.");
480  }
481  else {
482  s = (i18n (myGameList.at (n)->about.constData()));
483  }
484  gameAbout->setText (s);
485 }
486 
487 void KGrSLDialog::slShowLevel (int i)
488 {
489  // Display the level number as the slider is moved.
490  display->setValue (i);
491 }
492 
493 void KGrSLDialog::slUpdate (const QString & text)
494 {
495  // Move the slider when a valid level number is entered.
496  QString s = text;
497  bool ok = false;
498  int n = s.toInt (&ok);
499  if (ok) {
500  number->setValue (n);
501  slPaintLevel();
502  }
503  else
504  KGrMessage::information (this, i18n ("Select Level"),
505  i18n ("This level number is not valid. It can not be used."));
506 }
507 
508 void KGrSLDialog::slPaintLevel()
509 {
510  // Repaint the thumbnail sketch of the level whenever the level changes.
511  if (slGameIndex < 0) {
512  return; // Owner has no games.
513  }
514  // Fetch level-data and save layout, name and label in the thumbnail.
515  QString dir = (myGameList.at (slGameIndex)->owner == USER) ?
516  userDir : systemDir;
517  thumbNail->setLevelData (dir, myGameList.at (slGameIndex)->prefix,
518  number->value(), slName);
519  thumbNail->repaint(); // Will call "paintEvent (e)".
520 }
521 
522 void KGrSLDialog::slotHelp()
523 {
524  // Help for "Select Game and Level" dialog box.
525  QString s =
526  i18n ("The main button at the bottom echoes the "
527  "menu action you selected. Click it after choosing "
528  "a game and level - or use \"Cancel\".");
529 
530  if (slAction == SL_START) {
531  s += i18n ("\n\nIf this is your first time in KGoldrunner, select the "
532  "tutorial game, which gives you hints as you go.\n\n"
533  "Otherwise, just click on the name of a game in the table, "
534  "then, to start at level 001, click on the main button at the "
535  "bottom. Play begins when you move the mouse or press a key.");
536  }
537  else {
538  switch (slAction) {
539  case SL_UPDATE:
540  s += i18n ("\n\nYou can select System levels for editing (or "
541  "copying), but you must save the result in a game you have "
542  "created. Use the left mouse-button as a paintbrush and the "
543  "editor toolbar buttons as a palette. Use the 'Erase' button "
544  "or the right mouse-button to erase. You can drag the mouse "
545  "with a button held down and paint or erase multiple squares.");
546  break;
547  case SL_CREATE:
548  s += i18n("\n\nYou can add a name and hint to your new level here, "
549  "but you must save the level you have created into one of "
550  "your own games. By default your new level will go at the "
551  "end of your game, but you can also select a level number and "
552  "save into the middle of your game.");
553  break;
554  case SL_SAVE:
555  s += i18n("\n\nYou can create or edit a name and hint here, before "
556  "saving. If you change the game or level, you can do a copy "
557  "or \"Save As\", but you must always save into one of your "
558  "own games. If you save a level into the middle of a series, "
559  "the other levels are automatically re-numbered.");
560  break;
561  case SL_DELETE:
562  s += i18n ("\n\nYou can only delete levels from one of your own "
563  "games. If you delete a level from the middle of a series, "
564  "the other levels are automatically re-numbered.");
565  break;
566  case SL_MOVE:
567  s += i18n ("\n\nTo move (re-number) a level, you must first select "
568  "it by using \"Edit Any Level...\", then you can use "
569  "\"Move Level...\" to move it to a new number or even a "
570  "different game. Other levels are automatically re-numbered as "
571  "required. You can only move levels within your own games.");
572  break;
573  case SL_UPD_GAME:
574  s += i18n ("\n\nWhen editing game info you only need to choose a "
575  "game, then you can go to a dialog where you edit the "
576  "details of the game.");
577  break;
578  default:
579  break;
580  }
581  s += i18n ("\n\nClick on the table to choose a game. "
582  "In the table and below it you can see more information about the "
583  "selected game, including how many levels there are, how difficult "
584  "the game is and what "
585  "rules the enemies follow (see the KGoldrunner Handbook).\n\n"
586  "You select "
587  "a level number by typing it or using the spin box or scroll bar. "
588  "As you vary the game or level, the thumbnail area shows a "
589  "preview of your choice.");
590  }
591 
592  KGrMessage::information (slParent, i18n ("Help: Select Game & Level"), s);
593 }
594 
595 /*******************************************************************************
596 ************************* ITEM FOR THE LIST OF GAMES *************************
597 *******************************************************************************/
598 
599 KGrGameListItem::KGrGameListItem (const QStringList & data,
600  const int internalId)
601  : QTreeWidgetItem (data)
602 {
603  mInternalId = internalId;
604 }
605 
606 int KGrGameListItem::id() const
607 {
608  return mInternalId;
609 }
610 
611 void KGrGameListItem::setId (const int internalId)
612 {
613  mInternalId = internalId;
614 }
615 
616 /******************************************************************************/
617 /********************** CLASS TO DISPLAY THUMBNAIL ***********************/
618 /******************************************************************************/
619 
620 KGrThumbNail::KGrThumbNail (QWidget * parent)
621  :
622  QFrame (parent),
623  io (new KGrGameIO (parent))
624 {
625  // Let the parent do all the work. We need a class here so that
626  // QFrame::paintEvent (QPaintEvent *) can be re-implemented and
627  // the thumbnail can be automatically re-painted when required.
628 }
629 
630 KGrThumbNail::~KGrThumbNail()
631 {
632  delete io;
633 }
634 
635 void KGrThumbNail::setLevelData (const QString & dir, const QString& prefix,
636  int level, QLabel * sln)
637 {
638  KGrLevelData d;
639  QString filePath;
640 
641  IOStatus stat = io->fetchLevelData (dir, prefix, level, d, filePath);
642  if (stat == OK) {
643  // Keep a safe copy of the layout. Translate and display the name.
644  levelLayout = d.layout;
645  sln->setText ((d.name.size() > 0) ? i18n ((const char *) d.name) : "");
646  }
647  else {
648  // Level-data inaccessible or not found.
649  levelLayout = "";
650  sln->setText ("");
651  }
652 }
653 
654 void KGrThumbNail::paintEvent (QPaintEvent * /* event (unused) */)
655 {
656  QPainter p (this);
657  QFile openFile;
658  QPen pen = p.pen();
659  char obj = FREE;
660  int fw = 1; // Set frame width.
661  int n = width() / FIELDWIDTH; // Set thumbnail cell-size.
662 
663  QColor backgroundColor = QColor ("#000038"); // Midnight blue.
664  QColor brickColor = QColor ("#9c0f0f"); // Oxygen's brick-red.
665  QColor concreteColor = QColor ("#585858"); // Dark grey.
666  QColor ladderColor = QColor ("#a0a0a0"); // Steely grey.
667  QColor poleColor = QColor ("#a0a0a0"); // Steely grey.
668  QColor heroColor = QColor ("#00ff00"); // Green.
669  QColor enemyColor = QColor ("#0080ff"); // Bright blue.
670  QColor gold;
671  gold.setNamedColor ("gold"); // Gold.
672 
673  pen.setColor (backgroundColor);
674  p.setPen (pen);
675 
676  if (levelLayout.isEmpty()) {
677  // There is no file, so fill the thumbnail with "FREE" cells.
678  p.drawRect (QRect (fw, fw, FIELDWIDTH*n, FIELDHEIGHT*n));
679  return;
680  }
681 
682  for (int j = 0; j < FIELDHEIGHT; j++)
683  for (int i = 0; i < FIELDWIDTH; i++) {
684 
685  obj = levelLayout.at (j*FIELDWIDTH + i);
686 
687  // Set the colour of each object.
688  switch (obj) {
689  case BRICK:
690  case FBRICK:
691  pen.setColor (brickColor); p.setPen (pen); break;
692  case CONCRETE:
693  pen.setColor (concreteColor); p.setPen (pen); break;
694  case LADDER:
695  pen.setColor (ladderColor); p.setPen (pen); break;
696  case BAR:
697  pen.setColor (poleColor); p.setPen (pen); break;
698  case HERO:
699  pen.setColor (heroColor); p.setPen (pen); break;
700  case ENEMY:
701  pen.setColor (enemyColor); p.setPen (pen); break;
702  default:
703  // Set the background colour for FREE, HLADDER and NUGGET.
704  pen.setColor (backgroundColor); p.setPen (pen); break;
705  }
706 
707  // Draw n x n pixels as n lines of length n.
708  p.drawLine (i*n+fw, j*n+fw, i*n+(n-1)+fw, j*n+fw);
709  if (obj == BAR) {
710  // For a pole, only the top line is drawn in white.
711  pen.setColor (backgroundColor);
712  p.setPen (pen);
713  }
714  for (int k = 1; k < n; k++) {
715  p.drawLine (i*n+fw, j*n+k+fw, i*n+(n-1)+fw, j*n+k+fw);
716  }
717 
718  // For a nugget, add just a vertical touch of yellow (2-3 pixels).
719  if (obj == NUGGET) {
720  int k = (n/2)+fw;
721  pen.setColor (gold); // Gold.
722  p.setPen (pen);
723  p.drawLine (i*n+k, j*n+k, i*n+k, j*n+(n-1)+fw);
724  p.drawLine (i*n+k+1, j*n+k, i*n+k+1, j*n+(n-1)+fw);
725  }
726  }
727 
728  // Finally, draw a small black border around the outside of the thumbnail.
729  pen.setColor (Qt::black);
730  p.setPen (pen);
731  p.drawRect (rect().left(), rect().top(), rect().right(), rect().bottom());
732 }
733 
734 #include "kgrselector.moc"
QSpacerItem
QTreeWidget::addTopLevelItem
void addTopLevelItem(QTreeWidgetItem *item)
SL_SAVE
Definition: kgrglobals.h:60
QWidget
BAR
const char BAR
Definition: kgrglobals.h:39
KGrSLDialog::~KGrSLDialog
~KGrSLDialog()
Definition: kgrselector.cpp:68
KGrGameIO
The KGrGameIO class handles I/O for text-files containing KGoldrunner games and levels.
Definition: kgrgameio.h:58
SL_REPLAY
Definition: kgrglobals.h:62
KGrThumbNail::KGrThumbNail
KGrThumbNail(QWidget *parent=0)
Definition: kgrselector.cpp:620
QGridLayout::addWidget
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
QByteArray::at
char at(int i) const
QAbstractSlider::setRange
void setRange(int min, int max)
QFrame::setFrameShape
void setFrameShape(Shape)
SL_UPD_GAME
Definition: kgrglobals.h:61
SL_START
Definition: kgrglobals.h:60
QFont
HERO
const char HERO
Definition: kgrglobals.h:31
KGrThumbNail::brickColor
static QColor brickColor
Definition: kgrselector.h:127
QList::at
const T & at(int i) const
QByteArray::isEmpty
bool isEmpty() const
QHBoxLayout
QScrollBar
KGrGameListItem::KGrGameListItem
KGrGameListItem(const QStringList &data, const int internalId=-1)
Definition: kgrselector.cpp:599
QRect::height
int height() const
QGridLayout
SL_ANY
Definition: kgrglobals.h:60
QAbstractSlider::setPageStep
void setPageStep(int)
KDialog
FIELDHEIGHT
const int FIELDHEIGHT
Definition: kgrglobals.h:49
KGrLevelData::layout
QByteArray layout
Codes for the level layout (mandatory).
Definition: kgrglobals.h:109
QFrame::setFrameStyle
void setFrameStyle(int style)
KGrGameListItem::id
int id() const
Definition: kgrselector.cpp:606
QPainter::drawLine
void drawLine(const QLineF &line)
QFrame::setLineWidth
void setLineWidth(int)
KGrGameIO::fetchLevelData
IOStatus fetchLevelData(const QString &dir, const QString &prefix, const int level, KGrLevelData &d, QString &filePath)
Find and read data for a level of a game, into a KGrLevelData structure.
Definition: kgrgameio.cpp:182
QColor::setNamedColor
void setNamedColor(const QString &name)
SYSTEM
Definition: kgrglobals.h:26
QFile
QTreeWidget::clear
void clear()
QWidget::width
width
QFont::setBold
void setBold(bool enable)
QPainter::drawRect
void drawRect(const QRectF &rectangle)
QTreeWidget
QRect
QWidget::setEnabled
void setEnabled(bool)
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
QList::count
int count(const T &value) const
NUGGET
const char NUGGET
Definition: kgrglobals.h:37
QWidget::setLayout
void setLayout(QLayout *layout)
QPainter::setPen
void setPen(const QColor &color)
QString::toInt
int toInt(bool *ok, int base) const
QBoxLayout::addItem
virtual void addItem(QLayoutItem *item)
QPainter
FIELDWIDTH
const int FIELDWIDTH
Definition: kgrglobals.h:48
QString::isEmpty
bool isEmpty() const
KGrThumbNail::ladderColor
static QColor ladderColor
Definition: kgrselector.h:128
KGrSLDialog::KGrSLDialog
KGrSLDialog(int action, int requestedLevel, int gameIndex, QList< KGrGameData * > &gameList, const QString &pSystemDir, const QString &pUserDir, QWidget *parent=0)
Definition: kgrselector.cpp:51
KGrMessage::information
static void information(QWidget *parent, const QString &caption, const QString &text, const QString &dontShowAgain=QString())
Definition: kgrdialog.cpp:306
KGrLevelData
KGrLevelData structure: contains attributes of a KGoldrunner level.
Definition: kgrglobals.h:103
QVBoxLayout
KGrThumbNail::setLevelData
void setLevelData(const QString &dir, const QString &prefix, int level, QLabel *sln)
Definition: kgrselector.cpp:635
QLabel::setText
void setText(const QString &)
QString
QList< KGrGameData * >
QWidget::hide
void hide()
QColor
LADDER
const char LADDER
Definition: kgrglobals.h:36
QPen::setColor
void setColor(const QColor &color)
QLayout::setMargin
void setMargin(int margin)
QTreeWidget::setColumnCount
void setColumnCount(int columns)
QStringList
QWidget::rect
QRect rect() const
KGrThumbNail::paintEvent
void paintEvent(QPaintEvent *event)
Definition: kgrselector.cpp:654
QWidget::font
font
QAbstractSlider::value
value
ENEMY
const char ENEMY
Definition: kgrglobals.h:29
QFrame
KGrThumbNail::~KGrThumbNail
~KGrThumbNail()
Definition: kgrselector.cpp:630
QWidget::repaint
void repaint()
BRICK
const char BRICK
Definition: kgrglobals.h:33
QTreeWidget::selectedItems
QList< QTreeWidgetItem * > selectedItems() const
QTreeWidget::setCurrentItem
void setCurrentItem(QTreeWidgetItem *item)
QTreeWidget::setHeaderLabels
void setHeaderLabels(const QStringList &labels)
IOStatus
IOStatus
Return values from I/O operations.
Definition: kgrgameio.h:29
KGrThumbNail::backgroundColor
static QColor backgroundColor
Definition: kgrselector.h:126
KGrThumbNail
Definition: kgrselector.h:117
FBRICK
const char FBRICK
Definition: kgrglobals.h:34
QTreeWidgetItem
QAbstractSlider::setSingleStep
void setSingleStep(int)
QApplication::desktop
QDesktopWidget * desktop()
kgrglobals.h
QWidget::setFixedHeight
void setFixedHeight(int h)
QString::setNum
QString & setNum(short n, int base)
QHeaderView::setResizeMode
void setResizeMode(ResizeMode mode)
kgrselector.h
KGrGameListItem
Definition: kgrselector.h:103
KGrSLDialog::selectLevel
bool selectLevel(int &selectedGame, int &selectedLevel)
Definition: kgrselector.cpp:72
USER
Definition: kgrglobals.h:26
QPen
QPushButton
QDesktopWidget::availableGeometry
const QRect availableGeometry(int screen) const
QTreeView::header
QHeaderView * header() const
QWidget::show
void show()
KGrSLDialog::editNameAndHint
void editNameAndHint()
QTextEdit::setReadOnly
void setReadOnly(bool ro)
OK
Definition: kgrgameio.h:29
QPaintEvent
KGrThumbNail::poleColor
static QColor poleColor
Definition: kgrselector.h:129
QTreeView::setRootIsDecorated
void setRootIsDecorated(bool show)
QByteArray::size
int size() const
SL_MOVE
Definition: kgrglobals.h:61
QAbstractSlider::setMaximum
void setMaximum(int)
QLabel
QTextEdit
kgrgameio.h
SL_DELETE
Definition: kgrglobals.h:61
QTextEdit::setText
void setText(const QString &text)
QPainter::pen
const QPen & pen() const
KGrGameListItem::setId
void setId(const int internalId)
Definition: kgrselector.cpp:611
CONCRETE
const char CONCRETE
Definition: kgrglobals.h:32
QBoxLayout::setSpacing
void setSpacing(int spacing)
QWidget::height
height
SL_UPDATE
Definition: kgrglobals.h:60
FREE
const char FREE
Definition: kgrglobals.h:28
SL_SOLVE
Definition: kgrglobals.h:62
SL_CREATE
Definition: kgrglobals.h:60
QBoxLayout::addLayout
void addLayout(QLayout *layout, int stretch)
KGrLevelData::name
QByteArray name
Level name (optional).
Definition: kgrglobals.h:110
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:24 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kgoldrunner

Skip menu "kgoldrunner"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

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