25 #include "ui_settings.h"
27 #include <kmahjonggconfigdialog.h>
28 #include <highscore/kscoredialog.h>
29 #include <kstandardgameaction.h>
32 #include <kactioncollection.h>
34 #include <kconfigdialog.h>
38 #include <klineedit.h>
41 #include <kmessagebox.h>
42 #include <kpushbutton.h>
43 #include <kseparator.h>
44 #include <kshortcutsdialog.h>
45 #include <kstandardaction.h>
46 #include <kstandarddirs.h>
47 #include <kstandardguiitem.h>
48 #include <kstatusbar.h>
49 #include <ktoggleaction.h>
59 class Settings :
public QWidget,
public Ui::Settings
76 m_board =
new Board(
this);
79 setCentralWidget(m_board);
89 void App::setupStatusBar()
91 m_gameTipLabel =
new QLabel(i18n(
"Select a tile"), statusBar());
92 statusBar()->addWidget(m_gameTipLabel, 1);
94 m_gameTimerLabel =
new QLabel(i18n(
"Time: 0:00:00"), statusBar());
95 statusBar()->addWidget(m_gameTimerLabel);
97 m_gameTilesLabel =
new QLabel(i18n(
"Removed: 0/0"), statusBar());
98 statusBar()->addWidget(m_gameTilesLabel);
100 m_gameCheatLabel =
new QLabel(i18n(
"Cheat mode"), statusBar());
101 statusBar()->addWidget(m_gameCheatLabel);
102 m_gameCheatLabel->
hide();
105 void App::setupActions()
108 KStandardGameAction::gameNew(
this, SIGNAL(
invokeNewGame()), actionCollection());
109 KStandardGameAction::restart(
this, SLOT(restartGame()), actionCollection());
110 KStandardGameAction::pause(
this, SLOT(togglePause()), actionCollection());
111 KStandardGameAction::highscores(
this, SLOT(showHighscores()), actionCollection());
112 KStandardGameAction::quit(
this, SLOT(close()), actionCollection());
115 KStandardGameAction::undo(
this, SLOT(undo()), actionCollection());
116 KStandardGameAction::redo(
this, SLOT(redo()), actionCollection());
117 KStandardGameAction::hint(
this, SLOT(hint()), actionCollection());
119 KToggleAction *soundAction =
new KToggleAction(KIcon(
QLatin1String(
"speaker")), i18n(
"Play Sounds"),
this);
121 actionCollection()->addAction(
QLatin1String(
"sounds"), soundAction);
122 connect(soundAction, SIGNAL(triggered(
bool)), m_board, SLOT(setSoundsEnabled(
bool)));
125 KStandardAction::preferences(
this, SLOT(showSettingsDialog()), actionCollection());
127 connect(m_board, SIGNAL(cheatStatusChanged()),
this, SLOT(updateCheatDisplay()));
128 connect(m_board, SIGNAL(changed()),
this, SLOT(updateItems()));
129 connect(m_board, SIGNAL(tilesDoNotMatch()),
this, SLOT(notifyTilesDoNotMatch()));
130 connect(m_board, SIGNAL(invalidMove()),
this, SLOT(notifyInvalidMove()));
131 connect(m_board, SIGNAL(selectATile()),
this, SLOT(notifySelectATile()));
132 connect(m_board, SIGNAL(selectAMatchingTile()),
this, SLOT(notifySelectAMatchingTile()));
133 connect(m_board, SIGNAL(selectAMove()),
this, SLOT(notifySelectAMove()));
136 connect(timer, SIGNAL(timeout()),
this, SLOT(updateTimeDisplay()));
139 connect(m_board, SIGNAL(changed()),
this, SLOT(updateTileDisplay()));
140 connect(m_board, SIGNAL(endOfGame()),
this, SLOT(slotEndOfGame()));
142 connect(
this, SIGNAL(
invokeNewGame()), m_board, SLOT(newGame()));
143 connect(m_board, SIGNAL(newGameStarted()),
this, SLOT(newGame()));
148 setCheatModeEnabled(
false);
149 setPauseEnabled(
false);
153 void App::restartGame()
161 setCheatModeEnabled(
false);
168 void App::togglePause()
173 void App::setPauseEnabled(
bool enabled)
185 setCheatModeEnabled(
true);
206 m_board->makeHintMove();
209 setCheatModeEnabled(
true);
214 void App::updateItems()
217 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Undo))->setEnabled(
false);
218 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Redo))->setEnabled(
false);
219 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Pause))->setEnabled(
false);
220 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Hint))->setEnabled(
false);
222 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Undo))->setEnabled(
false);
223 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Redo))->setEnabled(
false);
224 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Restart))->setEnabled(
false);
225 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Pause))->setChecked(
true);
226 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Hint))->setEnabled(
false);
227 }
else if (m_board->
isStuck()) {
228 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Pause))->setEnabled(
false);
229 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Hint))->setEnabled(
false);
231 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Undo))->setEnabled(m_board->
canUndo());
232 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Redo))->setEnabled(m_board->
canRedo());
233 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Restart))->setEnabled(m_board->
canUndo());
234 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Pause))->setEnabled(
true);
235 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Pause))->setChecked(
false);
236 actionCollection()->action(KStandardGameAction::name(KStandardGameAction::Hint))->setEnabled(
true);
240 void App::slotEndOfGame()
246 QString timeString = i18nc(
"time string: hh:mm:ss",
"%1:%2:%3",
250 KScoreDialog::FieldInfo scoreInfo;
252 scoreInfo[KScoreDialog::Time] = timeString;
254 KScoreDialog scoreDialog(KScoreDialog::Name | KScoreDialog::Time | KScoreDialog::Score,
this);
255 scoreDialog.addField(KScoreDialog::Custom1, i18n(
"Gravity"),
"gravity");
258 scoreInfo[KScoreDialog::Custom1] = i18n(
"Yes");
260 scoreInfo[KScoreDialog::Custom1] = i18n(
"No");
265 QString message = i18n(
"\nYou could have been in the highscores\nif you did not use Undo or Hint.\nTry without them next time.");
266 KMessageBox::information(
this, message, i18n(
"End of Game"));
268 if (scoreDialog.addScore(scoreInfo) > 0) {
269 QString message = i18n(
"Congratulations!\nYou made it into the hall of fame.");
270 scoreDialog.setComment(message);
273 QString message = i18nc(
"%1 - time string like hh:mm:ss",
"You made it in %1", timeString);
274 KMessageBox::information(
this, message, i18n(
"End of Game"));
281 void App::updateTimeDisplay()
288 QString message = i18n(
"Your time: %1:%2:%3 %4",
289 QString().sprintf(
"%02d", currentTime / 3600),
290 QString().sprintf(
"%02d", (currentTime / 60) % 60),
291 QString().sprintf(
"%02d", currentTime % 60),
294 m_gameTimerLabel->
setText(message);
299 void App::updateTileDisplay()
301 int numberOfTiles = (m_board->
xTiles() * m_board->
yTiles());
302 QString message = i18n(
"Removed: %1/%2 ",
306 m_gameTilesLabel->
setText(message);
309 void App::updateCheatDisplay()
314 int App::score(
int x,
int y,
int seconds,
bool gravity)
const
316 double ntiles = x * y;
317 double tilespersec = ntiles /
static_cast<double>(seconds);
319 double sizebonus = std::sqrt(ntiles / static_cast<double>(14.0 * 6.0));
320 double points = tilespersec / 0.14 * 100.0;
323 return static_cast<int>(2.0 * points * sizebonus);
325 return static_cast<int>(points * sizebonus);
329 void App::notifySelectATile()
331 m_gameTipLabel->
setText(i18n(
"Select a tile"));
334 void App::notifySelectAMatchingTile()
336 m_gameTipLabel->
setText(i18n(
"Select a matching tile"));
339 void App::notifySelectAMove()
341 m_gameTipLabel->
setText(i18n(
"Select the move you want by clicking on the blue line"));
344 void App::notifyTilesDoNotMatch()
346 m_gameTipLabel->
setText(i18n(
"This tile did not match the one you selected"));
349 void App::notifyInvalidMove()
351 m_gameTipLabel->
setText(i18n(
"You cannot make this move"));
354 void App::setCheatModeEnabled(
bool enabled)
360 void App::showHighscores()
362 KScoreDialog scoreDialog(KScoreDialog::Name | KScoreDialog::Time,
this);
363 scoreDialog.addField(KScoreDialog::Custom1, i18n(
"Gravity"),
"gravity");
367 void App::keyBindings()
369 KShortcutsDialog::configure(actionCollection(), KShortcutsEditor::LetterShortcutsAllowed,
this);
372 void App::showSettingsDialog()
374 if (KConfigDialog::showDialog(
"settings")) {
379 KMahjonggConfigDialog *dialog =
new KMahjonggConfigDialog(
this,
"settings",
Prefs::self());
380 dialog->addPage(
new Settings(0), i18n(
"General"),
"games-config-options");
381 dialog->addTilesetPage();
382 dialog->addBackgroundPage();
383 dialog->setHelp(
QString(),
"kshisen");
384 connect(dialog, SIGNAL(settingsChanged(
QString)), m_board, SLOT(loadSettings()));
bool canUndo() const
Returns if undo step is available.
void setGameStuckEnabled(bool enabled)
Sets whether there are no matching tiles left.
bool hasCheated() const
Returns whether player is in cheat mode.
bool isOver() const
Returns whether the game is over.
int tilesLeft() const
Returns the number of tiles left on the board.
bool isPaused() const
Returns whether the game is in pause mode.
static bool sounds()
Get Sounds.
Class holding the game board and its functions.
bool isStuck() const
Returns whether there are still matching tiles left.
void setObjectName(const QString &name)
void setText(const QString &)
void invokeNewGame()
Invokes the creation of a new game.
int currentTime() const
Returns the current game time in seconds.
QString & sprintf(const char *cformat,...)
static int size()
Get Size.
void resetRedo()
Resets the redo history.
void setPauseEnabled(bool enabled)
Controls the pause mode.
bool canRedo() const
Returns if redo step is available.
void redo()
Redoes one step.
void resetTimer()
Resets the game timer.
void setGameOverEnabled(bool enabled)
Sets whether the game is over.
void undo()
Undoes one step.
void setCheatModeEnabled(bool enabled)
Sets whether the game is in cheat mode.