diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/solverwindow.cc | 100 | ||||
-rw-r--r-- | src/solverwindow.h | 12 | ||||
-rw-r--r-- | src/sudokuwidget.cc | 37 | ||||
-rw-r--r-- | src/sudokuwidget.h | 12 |
4 files changed, 157 insertions, 4 deletions
diff --git a/src/solverwindow.cc b/src/solverwindow.cc index 9a04e46..f900986 100644 --- a/src/solverwindow.cc +++ b/src/solverwindow.cc @@ -1,12 +1,16 @@ #include "solverwindow.h" #include "sudokuwidget.h" +#include "sudoku.h" +#include <QtGui> +#include <QDir> #include <QHBoxLayout> #include <QVBoxLayout> - #include <QPushButton> +const QString HOMEDIR(QDir::homePath() + "/.sudoku"); + SolverWindow::SolverWindow() { QHBoxLayout *windowlayout = new QHBoxLayout(); @@ -18,14 +22,17 @@ SolverWindow::SolverWindow() // add load button QPushButton *loadbutton = new QPushButton(tr("Load")); sidebarlayout->addWidget(loadbutton); + connect(loadbutton, SIGNAL(clicked()), this, SLOT(load())); // add save button QPushButton *savebutton = new QPushButton(tr("Save")); sidebarlayout->addWidget(savebutton); + connect(savebutton, SIGNAL(clicked()), this, SLOT(save())); // add step button QPushButton *stepbutton = new QPushButton(tr("Step")); sidebarlayout->addWidget(stepbutton); + connect(stepbutton, SIGNAL(clicked()), this, SLOT(step())); // add stretch sidebarlayout->addStretch(1); @@ -33,15 +40,102 @@ SolverWindow::SolverWindow() // add clear button QPushButton *clearbutton = new QPushButton(tr("Clear")); sidebarlayout->addWidget(clearbutton); + connect(clearbutton, SIGNAL(clicked()), this, SLOT(clear())); // add sidebar layout windowlayout->addLayout(sidebarlayout); // sudoku widget - SudokuWidget *sudokuwidget = new SudokuWidget(); - windowlayout->addWidget(sudokuwidget); + solverwindow_sudokuwidget = new SudokuWidget(); + windowlayout->addWidget(solverwindow_sudokuwidget); // set window layout setLayout(windowlayout); + + // create home directory + QDir directory; + if (!directory.exists(HOMEDIR)) { + directory.mkdir(HOMEDIR); + } +} + +void SolverWindow::load() +{ + QString filename = QFileDialog::getOpenFileName(this, tr("Open..."), HOMEDIR, "Sudoku (*.sudoku)"); + if (filename.isEmpty()) { + return; + } + + QFile file(filename); + if (!file.open(QFile::ReadOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Open file"), + tr("Could not open file %1:\n%2.") + .arg(filename) + .arg(file.errorString())); + return; + } + + QApplication::setOverrideCursor(Qt::WaitCursor); + + QTextStream textstream(&file); + + Sudoku sudoku; + for (int row = 0; row < 9; row++) { + for (int column = 0; column < 9 ; column++) { + int i; + textstream >> i; + sudoku.value(row, column) = i; + } + } + solverwindow_sudokuwidget->set_values(sudoku); + + file.close(); + + QApplication::restoreOverrideCursor(); } + +void SolverWindow::save() +{ + QString filename = QFileDialog::getSaveFileName(this, tr("Save as..."), HOMEDIR, "Sudoku (*.sudoku)"); + if (filename.isEmpty()) { + return; + } + + QFile file(filename); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + QMessageBox::warning(this, tr("Save file"), + tr("Cannot write file %1:\n%2.") + .arg(filename) + .arg(file.errorString())); + return; + } + + QApplication::setOverrideCursor(Qt::WaitCursor); + + QTextStream textstream(&file); + + Sudoku sudoku; + solverwindow_sudokuwidget->get_values(sudoku); + for (int row = 0; row < 9; row++) { + for (int column = 0; column < 9 ; column++) { + textstream << sudoku.value(row, column) << '\n'; + } + } + + file.close(); + + QApplication::restoreOverrideCursor(); + + return; +} + +void SolverWindow::clear() +{ + Sudoku sudoku; + solverwindow_sudokuwidget->set_values(sudoku); +} + +void SolverWindow::step() +{ +}
\ No newline at end of file diff --git a/src/solverwindow.h b/src/solverwindow.h index 4743332..886e721 100644 --- a/src/solverwindow.h +++ b/src/solverwindow.h @@ -4,12 +4,24 @@ #include <QWidget> +class SudokuWidget; + class SolverWindow : public QWidget { Q_OBJECT public: SolverWindow(); + +public slots: + + void load(); + void save(); + void step(); + void clear(); + +private: + SudokuWidget *solverwindow_sudokuwidget; }; #endif // __INCLUDED_SUDOKUSOLVER_SOLVERWINDOW__
\ No newline at end of file diff --git a/src/sudokuwidget.cc b/src/sudokuwidget.cc index ef60e67..742798f 100644 --- a/src/sudokuwidget.cc +++ b/src/sudokuwidget.cc @@ -3,6 +3,7 @@ #include <QLineEdit> #include <QGridLayout> +#include <QString> SudokuWidget::SudokuWidget() { @@ -10,11 +11,45 @@ SudokuWidget::SudokuWidget() for (int row = 0; row < 9; row++) { for (int column = 0; column < 9 ; column++) { + sudokuwidget_value[row][column] = new QLineEdit(); + sudokuwidget_value[row][column]->setFrame(false); + sudokuwidget_value[row][column]->setAlignment(Qt::AlignCenter); + gridlayout->addWidget(sudokuwidget_value[row][column], row, column); } - } setLayout(gridlayout); +} + +void SudokuWidget::set_values(const Sudoku & values) +{ + for (int row = 0; row < 9; row++) { + for (int column = 0; column < 9 ; column++) { + int i = values.value(row, column); + if (i > 0) { + QString str = QString::number(i); + sudokuwidget_value[row][column]->setText(str); + } else { + sudokuwidget_value[row][column]->clear(); + } + } + } +} + +void SudokuWidget::get_values(Sudoku & values) +{ + for (int row = 0; row < 9; row++) { + for (int column = 0; column < 9 ; column++) { + bool ok; + QString str(sudokuwidget_value[row][column]->text()); + int i = str.toInt(&ok); + if (ok && (i > 0) && (i <= 9)) { + values.value(row, column) = i; + } else { + values.value(row, column) = 0; + } + } + } }
\ No newline at end of file diff --git a/src/sudokuwidget.h b/src/sudokuwidget.h index 59e4f3c..06d1b6f 100644 --- a/src/sudokuwidget.h +++ b/src/sudokuwidget.h @@ -5,6 +5,8 @@ #include <QtGui> #include <QWidget> +#include "sudoku.h" + class QLineEdit; class SudokuWidget : public QWidget @@ -13,6 +15,16 @@ class SudokuWidget : public QWidget public: SudokuWidget(); + + /** + * @brief set widgets value from sudoku values + * */ + void set_values(const Sudoku & values); + + /** + * @brief set sudoku values from widget values + * */ + void get_values(Sudoku & values); private: QLineEdit * sudokuwidget_value[9][9]; |