diff --git a/src/gui/ksudoku.h b/src/gui/ksudoku.h --- a/src/gui/ksudoku.h +++ b/src/gui/ksudoku.h @@ -86,9 +86,9 @@ * Default Destructor */ virtual ~KSudoku(); - + void loadGame(const QUrl& url); - + public: void updateShapesList(); void createCustomShape(); @@ -102,20 +102,21 @@ public slots: void onCompleted(bool isCorrect, const QTime& required, bool withHelp = false); - + void showWelcomeScreen(); void startGame(const ::ksudoku::Game& game); void endCurrentGame(); bool queryClose() override; - + private slots: void difficultyChanged (KGameDifficulty::standardLevel difficulty); void difficultyChanged (int difficulty); // IDW test. void symmetryChanged (int symmetry); void gameNew(); + void gameRestart(); void gameOpen(); void gameSave(); void gameSaveAs(); @@ -153,16 +154,16 @@ private: QWidget* wrapper; - + QAction* m_gameSave; QAction* m_gameSaveAs; ksudoku::GameVariantCollection* m_gameVariants; ksudoku::WelcomeScreen* m_welcomeScreen; - + QWidget* m_gameWidget; ksudoku::ValueListWidget* m_valueListWidget; - + ksudoku::KsView* m_gameUI; ksudoku::GameActions* m_gameActions; diff --git a/src/gui/ksudoku.cpp b/src/gui/ksudoku.cpp --- a/src/gui/ksudoku.cpp +++ b/src/gui/ksudoku.cpp @@ -114,9 +114,9 @@ // // m = view->status(); // // if(currentView()) // // m = currentView()->status(); -// +// // // TODO fix this: add new status bar generation code -// +// // statusBar()->showMessage(m); // } @@ -236,8 +236,8 @@ void KSudoku::startGame(const Game& game) { m_welcomeScreen->hide(); endCurrentGame(); - - + + KsView* view = new KsView(game, m_gameActions, this); view->setValueListWidget(m_valueListWidget); @@ -352,10 +352,10 @@ void KSudoku::endCurrentGame() { m_valueListWidget->hide(); - + delete m_gameUI; m_gameUI = 0; - + adaptActions2View(); } @@ -457,6 +457,7 @@ setAcceptDrops(true); KStandardGameAction::gameNew(this, SLOT(gameNew()), actionCollection()); + KStandardGameAction::restart(this, SLOT(gameRestart()), actionCollection()); KStandardGameAction::load(this, SLOT(gameOpen()), actionCollection()); m_gameSave = KStandardGameAction::save(this, SLOT(gameSave()), actionCollection()); m_gameSaveAs = KStandardGameAction::saveAs(this, SLOT(gameSaveAs()), actionCollection()); @@ -545,6 +546,9 @@ m_gameSave->setEnabled(game.isValid()); m_gameSaveAs->setEnabled(game.isValid()); + action("game_new")->setEnabled(game.isValid()); + action("game_restart")->setEnabled(game.isValid()); + action("game_print")->setEnabled(game.isValid()); if(game.isValid()) { action("move_undo")->setEnabled(game.canUndo()); action("move_redo")->setEnabled(game.canRedo()); @@ -647,15 +651,35 @@ if(!m_gameUI->game().wasFinished()) { if(KMessageBox::questionYesNo(this, i18n("Do you really want to end this game in order to start a new one?"), - i18nc("window title", "Restart Game"), - KGuiItem(i18nc("button label", "Restart Game")), + i18nc("window title", "New Game"), + KGuiItem(i18nc("button label", "New Game")), KStandardGuiItem::cancel() ) != KMessageBox::Yes) return; } showWelcomeScreen(); } +void KSudoku::gameRestart() +{ + if (!currentView()) return; + + auto game = currentGame(); + + // only show question when the current game hasn't been finished until now + if (!game.wasFinished()) { + if (KMessageBox::questionYesNo(this, + i18n("Do you really want to restart this game?"), + i18nc("window title", "Restart Game"), + KGuiItem(i18nc("button label", "Restart Game")), + KStandardGuiItem::cancel() ) != KMessageBox::Yes) { + return; + } + } + + game.restart(); +} + void KSudoku::gameOpen() { // this slot is called whenever the Game->Open menu is selected, diff --git a/src/gui/ksudokugame.h b/src/gui/ksudokugame.h --- a/src/gui/ksudokugame.h +++ b/src/gui/ksudokugame.h @@ -114,6 +114,11 @@ Game& operator=(const Game& game); public: + /** + * Restarts the game and marks it as unfinished + */ + void restart(); + int order() const; int value(int index) const; diff --git a/src/gui/ksudokugame.cpp b/src/gui/ksudokugame.cpp --- a/src/gui/ksudokugame.cpp +++ b/src/gui/ksudokugame.cpp @@ -198,6 +198,15 @@ // IDW test. Eliminated optimized[] arrays and xxxConnection() functions. } +void Game::restart() { + while (canUndo()) { + interface()->undo(); + } + m_private->history.clear(); // otherwise we could do redo + m_private->wasFinished = false; + m_private->emitModified(true); // e.g. to update undo/redo action state +} + int Game::order() const { if(!m_private) return 0; return m_private->puzzle->order(); diff --git a/src/gui/ksudokuui.rc b/src/gui/ksudokuui.rc --- a/src/gui/ksudokuui.rc +++ b/src/gui/ksudokuui.rc @@ -1,14 +1,15 @@ &Game + @@ -44,6 +45,7 @@ Main Toolbar +