From d519927a929070d2ab04ab917f3d22cf3569db6c Mon Sep 17 00:00:00 2001 From: valpo Date: Mon, 10 Aug 2015 16:07:43 +0200 Subject: [PATCH] enable remaining buttons in game view --- qt/buffer.cpp | 6 +++++ qt/buffer.h | 1 + qt/mainwindow.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++----- qt/mainwindow.h | 8 +++++- qt/mainwindow.ui | 4 +-- 5 files changed, 78 insertions(+), 9 deletions(-) diff --git a/qt/buffer.cpp b/qt/buffer.cpp index a32754d..006fb51 100644 --- a/qt/buffer.cpp +++ b/qt/buffer.cpp @@ -50,3 +50,9 @@ bool Buffer::empty() const return buffer.isEmpty(); } +void Buffer::clear() +{ + QMutexLocker locker(&lock); + buffer.clear(); +} + diff --git a/qt/buffer.h b/qt/buffer.h index 74aa72a..d15b3b6 100644 --- a/qt/buffer.h +++ b/qt/buffer.h @@ -23,6 +23,7 @@ class Buffer : public QObject void put(const Sudoku& s); Sudoku get(); bool empty() const; + void clear(); signals: void added(); // emitted if item added diff --git a/qt/mainwindow.cpp b/qt/mainwindow.cpp index c7a86d2..ce4f388 100644 --- a/qt/mainwindow.cpp +++ b/qt/mainwindow.cpp @@ -19,7 +19,7 @@ static unsigned SudokuIn[9][9] = { }; MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), sudoku(9) + QMainWindow(parent), sudoku(9), startGame(9) { setupUi(this); initSudoku(); @@ -105,16 +105,72 @@ void MainWindow::refreshView() void MainWindow::on_generateButton_clicked() { qDebug("generate sudoku"); + viewRefresh->stop(); + while (solver) { + bool res = solver->wait(); + if (res) { + solver->deleteLater(); + solver = nullptr; + } + } + buffer.clear(); + state = State::IDLE; sudoku.generate(); + startGame = sudoku; showSudoku(sudoku); qDebug("generate sudoku finished"); } void MainWindow::on_playButton_clicked() { - Solver *solv = new Solver(&buffer, sudoku, this); - connect(solv, SIGNAL(finished()), this, SLOT(solverFinished())); - solv->start(); - solverRunning = true; - viewRefresh->start(0); + if (state == State::SOLVER_PAUSING or state == State::IDLE) { + if (!solver) { + startGame = sudoku; + solver = new Solver(&buffer, sudoku, this); + connect(solver, SIGNAL(finished()), this, SLOT(solverFinished())); + solver->start(); + solverRunning = true; + } + viewRefresh->start(0); + playButton->setText("Stop"); + state = State::SOLVER_PLAYING; + } + else if (state == State::SOLVER_PLAYING) { + viewRefresh->stop(); + playButton->setText("Play"); + state = State::SOLVER_PAUSING; + } +} + +void MainWindow::on_resetButton_clicked() +{ + viewRefresh->stop(); + while (solver) { + bool res = solver->wait(); + if (res) { + solver->deleteLater(); + solver = nullptr; + } + } + buffer.clear(); + sudoku = startGame; + showSudoku(sudoku); + state = State::IDLE; + playButton->setText("Play"); +} + +void MainWindow::on_nextButton_clicked() +{ + + viewRefresh->stop(); + if (!solver) { + startGame = sudoku; + solver = new Solver(&buffer, sudoku, this); + connect(solver, SIGNAL(finished()), this, SLOT(solverFinished())); + solver->start(); + solverRunning = true; + } + state = State::SOLVER_PAUSING; + playButton->setText("Play"); + refreshView(); } diff --git a/qt/mainwindow.h b/qt/mainwindow.h index c1e1ad1..16397de 100644 --- a/qt/mainwindow.h +++ b/qt/mainwindow.h @@ -10,16 +10,20 @@ #include class SudokuLabel; +class Solver; class MainWindow : public QMainWindow, private Ui::MainWindow { Q_OBJECT - Sudoku sudoku; + enum class State { IDLE, SOLVER_PLAYING, SOLVER_PAUSING }; + State state = State::IDLE; + Sudoku sudoku, startGame; Buffer buffer; QTimer *viewRefresh; bool solverRunning; std::vector labelVec; // speed up access to labels QElapsedTimer *bencher = 0; + Solver *solver = 0; public: explicit MainWindow(QWidget *parent = 0); void showSudoku(const Sudoku& s); @@ -30,6 +34,8 @@ private slots: void refreshView(); // gets item from buffer and displays it void on_generateButton_clicked(); void on_playButton_clicked(); + void on_resetButton_clicked(); + void on_nextButton_clicked(); }; #endif // MAINWINDOW_H diff --git a/qt/mainwindow.ui b/qt/mainwindow.ui index e742f33..9046b6e 100644 --- a/qt/mainwindow.ui +++ b/qt/mainwindow.ui @@ -50,14 +50,14 @@ p, li { white-space: pre-wrap; } - + Next - + Reset