26 #include <QTextStream>
27 #include <QGridLayout>
28 #include <QVBoxLayout>
29 #include <QSpacerItem>
30 #include <QHeaderView>
32 #include <QPushButton>
33 #include <QButtonGroup>
34 #include <QRadioButton>
40 #include <QApplication>
41 #include <QDesktopWidget>
43 #include <KGlobalSettings>
44 #include <KConfigGroup>
45 #include <KIntNumInput>
52 QList<KGrGameData *> & gameList,
53 const QString & pSystemDir,
const QString & pUserDir,
58 myGameList (gameList),
59 defaultGame (gameIndex),
60 defaultLevel (requestedLevel),
61 systemDir (pSystemDir),
74 selectedGame = defaultGame;
78 while (exec() == QDialog::Accepted) {
79 selectedGame = slGameIndex;
81 if (myGameList.at (selectedGame)->owner ==
SYSTEM) {
87 i18n (
"Sorry, you can only save or move "
88 "into one of your own games."));
93 i18n (
"Sorry, you can only delete a level "
94 "from one of your own games."));
99 i18n (
"Sorry, you can only edit the game "
100 "information on your own games."));
108 selectedLevel = number->value();
109 if ((selectedLevel > myGameList.at (selectedGame)->nLevels) &&
113 i18n (
"There is no level %1 in \"%2\", "
114 "so you cannot play or edit it.",
116 myGameList.at (selectedGame)->name));
122 return (selectedLevel > 0);
125 void KGrSLDialog::setupWidgets()
127 int margin = marginHint();
128 int spacing = spacingHint();
129 QWidget * dad =
new QWidget (
this);
131 setCaption (i18n (
"Select Game"));
132 setButtons (KDialog::Ok | KDialog::Cancel | KDialog::Help);
133 setDefaultButton (KDialog::Ok);
135 QVBoxLayout * mainLayout =
new QVBoxLayout (dad);
136 mainLayout->setSpacing (spacing);
137 mainLayout->setMargin (margin);
140 (i18n (
"<html><b>Please select a game:</b></html>"), dad);
141 mainLayout->addWidget (gameL, 5);
143 games =
new QTreeWidget (dad);
144 mainLayout->addWidget (games, 50);
145 games->setColumnCount (4);
146 games->setHeaderLabels (QStringList() <<
147 i18n (
"Name of Game") <<
151 games->setRootIsDecorated (
false);
153 QHBoxLayout * hboxLayout1 =
new QHBoxLayout();
154 hboxLayout1->setSpacing (6);
155 hboxLayout1->setMargin (0);
157 gameN =
new QLabel (
"", dad);
158 QFont f = gameN->font();
161 hboxLayout1->addWidget (gameN);
163 QSpacerItem * spacerItem1 =
new QSpacerItem
164 (21, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
165 hboxLayout1->addItem (spacerItem1);
167 gameD =
new QLabel (
"", dad);
168 hboxLayout1->addWidget (gameD);
169 mainLayout->addLayout (hboxLayout1, 5);
171 gameAbout =
new QTextEdit (dad);
172 gameAbout->setReadOnly (
true);
173 mainLayout->addWidget (gameAbout, 25);
176 separator->setFrameShape (QFrame::HLine);
177 mainLayout->addWidget (separator);
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);
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);
192 QGridLayout * grid =
new QGridLayout;
193 mainLayout->addLayout (grid);
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);
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);
213 levelNH =
new QPushButton (i18n (
"Edit Level Name && Hint"), dad);
214 mainLayout->addWidget (levelNH);
216 slName =
new QLabel (
"", dad);
217 grid->addWidget (slName, 2, 1, 1, 4);
219 grid->addWidget (thumbNail, 1, 6, 4, 5);
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);
228 int cell = slParent->width() / (
FIELDWIDTH + 4);
233 const QRect avail = QApplication::desktop()->availableGeometry(
this);
234 if ((avail.height() - slParent->height()) <= 120) {
235 dad->setFixedHeight (slParent->height() - 120);
239 number-> setTracking (
true);
240 number->setValue (defaultLevel);
242 slSetGames (defaultGame);
248 OKText = i18n (
"Start Game");
249 number->setValue (1);
250 number->setEnabled (
false);
251 display->setEnabled (
false);
257 OKText = i18n (
"Play Level");
260 OKText = i18n (
"Replay Level");
263 OKText = i18n (
"Show Solution");
266 OKText = i18n (
"Edit Level");
269 OKText = i18n (
"Save New");
272 OKText = i18n (
"Save Change");
275 OKText = i18n (
"Delete Level");
278 OKText = i18n (
"Move To...");
281 OKText = i18n (
"Edit Game Info");
282 number->setValue (1);
283 number->setEnabled (
false);
284 display->setEnabled (
false);
292 if (!OKText.isEmpty()) {
293 setButtonGuiItem (KDialog::Ok, KGuiItem (OKText));
297 slShowLevel (number->value());
299 if (display->isEnabled()) {
301 display->selectAll();
305 thumbNail->setFrameStyle (QFrame::Box | QFrame::Plain);
306 thumbNail->setLineWidth (1);
310 connect (games, SIGNAL (itemSelectionChanged()),
this, SLOT (slGame()));
312 connect (display, SIGNAL (valueChanged(QString)),
313 this, SLOT (slUpdate(QString)));
315 connect (number, SIGNAL(valueChanged(
int)),
this, SLOT(slShowLevel(
int)));
321 connect (levelNH, SIGNAL (clicked()),
325 levelNH->setEnabled (
false);
329 connect (games, SIGNAL(itemSelectionChanged()),
this, SLOT(slPaintLevel()));
330 connect (number, SIGNAL (sliderReleased()),
this, SLOT (slPaintLevel()));
332 connect (
this, SIGNAL (helpClicked()),
this, SLOT (slotHelp()));
339 void KGrSLDialog::slSetGames (
int cIndex)
342 int imax = myGameList.count();
349 QList<char> sortOrder1, sortOrder2;
350 sortOrder1 <<
'N' <<
'C' <<
'T';
351 sortOrder2 <<
'T' <<
'K';
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)) {
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")));
371 games->addTopLevelItem (thisGame);
373 if (slGameIndex < 0) {
378 games->setCurrentItem (thisGame);
385 if (slGameIndex < 0) {
393 games->header()->setResizeMode (0, QHeaderView::ResizeToContents);
394 games->header()->setResizeMode (1, QHeaderView::ResizeToContents);
395 games->header()->setResizeMode (2, QHeaderView::ResizeToContents);
402 void KGrSLDialog::slGame()
405 if (slGameIndex < 0) {
410 if (games->selectedItems().size() <= 0) {
415 (games->selectedItems().first()))->id();
418 if (myGameList.at (n)->nLevels > 0) {
419 number->setMaximum (myGameList.at (n)->nLevels);
420 display->setMaximum (myGameList.at (n)->nLevels);
423 number->setMaximum (1);
424 display->setMaximum (1);
427 KConfigGroup gameGroup (KGlobal::config(),
"KDEGame");
440 number->setValue (defaultLevel);
444 lev = gameGroup.readEntry (
"Level_" + myGameList.at (n)->prefix, 1);
445 number->setValue (lev);
451 if ((n == N) && (slAction !=
SL_CREATE)) {
453 number->setValue (defaultLevel);
457 number->setMaximum (myGameList.at (n)->nLevels + 1);
458 display->setMaximum (myGameList.at (n)->nLevels + 1);
459 number->setValue (number->maximum());
463 number->setValue (1);
467 slShowLevel (number->value());
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));
474 gameD->setText (i18np (
"1 level, uses Traditional rules.",
475 "%1 levels, uses Traditional rules.", levCnt));
476 gameN->setText (myGameList.at (n)->name);
478 if (myGameList.at (n)->about.isEmpty()) {
479 s = i18n (
"Sorry, there is no further information about this game.");
482 s = (i18n (myGameList.at (n)->about.constData()));
484 gameAbout->setText (s);
487 void KGrSLDialog::slShowLevel (
int i)
490 display->setValue (i);
493 void KGrSLDialog::slUpdate (
const QString & text)
498 int n = s.toInt (&ok);
500 number->setValue (n);
505 i18n (
"This level number is not valid. It can not be used."));
508 void KGrSLDialog::slPaintLevel()
511 if (slGameIndex < 0) {
515 QString dir = (myGameList.at (slGameIndex)->owner ==
USER) ?
517 thumbNail->
setLevelData (dir, myGameList.at (slGameIndex)->prefix,
518 number->value(), slName);
519 thumbNail->repaint();
522 void KGrSLDialog::slotHelp()
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\".");
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.");
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.");
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.");
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.");
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.");
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.");
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.");
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"
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.");
600 const int internalId)
603 mInternalId = internalId;
613 mInternalId = internalId;
636 int level, QLabel * sln)
645 sln->setText ((d.
name.size() > 0) ? i18n ((
const char *) d.
name) :
"");
665 QColor concreteColor = QColor (
"#585858");
668 QColor heroColor = QColor (
"#00ff00");
669 QColor enemyColor = QColor (
"#0080ff");
671 gold.setNamedColor (
"gold");
673 pen.setColor (backgroundColor);
676 if (levelLayout.isEmpty()) {
685 obj = levelLayout.at (j*FIELDWIDTH + i);
691 pen.setColor (brickColor); p.setPen (pen);
break;
693 pen.setColor (concreteColor); p.setPen (pen);
break;
695 pen.setColor (ladderColor); p.setPen (pen);
break;
697 pen.setColor (poleColor); p.setPen (pen);
break;
699 pen.setColor (heroColor); p.setPen (pen);
break;
701 pen.setColor (enemyColor); p.setPen (pen);
break;
704 pen.setColor (backgroundColor); p.setPen (pen);
break;
708 p.drawLine (i*n+fw, j*n+fw, i*n+(n-1)+fw, j*n+fw);
711 pen.setColor (backgroundColor);
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);
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);
729 pen.setColor (Qt::black);
731 p.drawRect (rect().left(), rect().top(), rect().right(), rect().bottom());
734 #include "kgrselector.moc"
The KGrGameIO class handles I/O for text-files containing KGoldrunner games and levels.
KGrThumbNail(QWidget *parent=0)
KGrGameListItem(const QStringList &data, const int internalId=-1)
QByteArray layout
Codes for the level layout (mandatory).
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.
static QColor ladderColor
KGrSLDialog(int action, int requestedLevel, int gameIndex, QList< KGrGameData * > &gameList, const QString &pSystemDir, const QString &pUserDir, QWidget *parent=0)
static void information(QWidget *parent, const QString &caption, const QString &text, const QString &dontShowAgain=QString())
KGrLevelData structure: contains attributes of a KGoldrunner level.
void setLevelData(const QString &dir, const QString &prefix, int level, QLabel *sln)
void paintEvent(QPaintEvent *event)
IOStatus
Return values from I/O operations.
static QColor backgroundColor
bool selectLevel(int &selectedGame, int &selectedLevel)
void setId(const int internalId)
QByteArray name
Level name (optional).