From 7c418d2130e233ce80a450c70e07951a5b7887a4 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 5 Aug 2012 13:49:37 +0000 Subject: Manage cell widget geometry manually and removed the grid layout, created the visual appereance of an actual sudoku puzzle. --- src/solverwindow.cc | 2 +- src/sudokuwidget.cc | 98 ++++++++++++++++++++++++++++++++++++++++++++++++----- src/sudokuwidget.h | 16 ++++++++- 3 files changed, 106 insertions(+), 10 deletions(-) diff --git a/src/solverwindow.cc b/src/solverwindow.cc index 7f88b5f..d799c56 100644 --- a/src/solverwindow.cc +++ b/src/solverwindow.cc @@ -53,7 +53,7 @@ SolverWindow::SolverWindow() // sudoku widget solverwindow_sudokuwidget = new SudokuWidget(); - windowlayout->addWidget(solverwindow_sudokuwidget); + windowlayout->addWidget(solverwindow_sudokuwidget, 1); // set window layout setLayout(windowlayout); diff --git a/src/sudokuwidget.cc b/src/sudokuwidget.cc index 7d756ae..afa0972 100644 --- a/src/sudokuwidget.cc +++ b/src/sudokuwidget.cc @@ -7,20 +7,14 @@ SudokuWidget::SudokuWidget() { - QGridLayout *gridlayout = new QGridLayout(); - for (int row = 0; row < 9; row++) { for (int column = 0; column < 9 ; column++) { - sudokuwidget_value[row][column] = new QLineEdit(); + sudokuwidget_value[row][column] = new QLineEdit(this); sudokuwidget_value[row][column]->setFrame(false); sudokuwidget_value[row][column]->setAlignment(Qt::AlignCenter); - - gridlayout->addWidget(sudokuwidget_value[row][column], row, column); } } - - setLayout(gridlayout); } QSize SudokuWidget::sizeHint () const @@ -57,4 +51,92 @@ void SudokuWidget::get_values(Sudoku & values) } } } -} \ No newline at end of file +} + +void SudokuWidget::resizeEvent(QResizeEvent *event) +{ + int sgx = width() / 9; + int sgy = height() / 9; + + // offset + if (sgx > sgy) { + sgx = sgy; + } else { + sgy = sgx; + } + int offset_x = (width() - 9 * sgx) / 2; + int offset_y = (height() - 9 * sgy) / 2; + + QFont font("default", 16); + font.setPixelSize(sgx / 2); + + + for (int row = 0; row < 9; row++) { + for (int column = 0; column < 9 ; column++) { + sudokuwidget_value[row][column]->setFont(font); + sudokuwidget_value[row][column]->setGeometry(offset_x + column * sgx + 2, offset_y + row * sgy + 2 , sgx - 4, sgy - 4); + + } + } +} + +void SudokuWidget::paintEvent(QPaintEvent *event) +{ + if ((width() < 16) || (height() < 16)) + return; + + // main line color + const QColor mainlinecolor(0, 0, 0); + // subgrid line color + const QColor sublinecolor(64, 64, 64); + + // subgrid size + int sgx = width() / 9; + int sgy = height() / 9; + + // offset + if (sgx > sgy) { + sgx = sgy; + } else { + sgy = sgx; + } + int offset_x = (width() - 9 * sgx) / 2; + int offset_y = (height() - 9 * sgy) / 2; + + // draw background + QPainter painter(this); + QPen pen(palette().color(QPalette::Base), 1, Qt::SolidLine); + painter.setPen(pen); + + painter.fillRect(offset_x, offset_y, sgx * 9 , sgy * 9, palette().color(QPalette::Base)); + + // draw subgrid + pen.setWidth(1); + pen.setColor(palette().color(QPalette::Text)); + painter.setPen(pen); + + for (int i = 0; i < 9; i++) { + if ( (i % 3) > 0 ) { + painter.drawLine(offset_x, offset_y + i * sgy, offset_x + sgx * 9, offset_y + i * sgy); + painter.drawLine(offset_x + i * sgx, offset_y, offset_x +i * sgx, offset_y + sgy * 9); + } + } + + // draw main grid lines + int sx = sgx * 3; + int sy = sgy * 3; + + for (int i = 0; i <= 3; i++) { + if ( (i % 3) == 0) { + pen.setColor(palette().color(QPalette::WindowText)); + pen.setWidth(1); + painter.setPen(pen); + } else { + pen.setColor(palette().color(QPalette::WindowText)); + pen.setWidth(2); + painter.setPen(pen); + } + painter.drawLine(offset_x, offset_y + i * sy, offset_x + sx * 3, offset_y + i * sy); + painter.drawLine(offset_x + i * sx, offset_y, offset_x + i * sx, offset_y + sy * 3); + } +} diff --git a/src/sudokuwidget.h b/src/sudokuwidget.h index c96df7d..ce1cffd 100644 --- a/src/sudokuwidget.h +++ b/src/sudokuwidget.h @@ -26,8 +26,22 @@ public: * */ void get_values(Sudoku & values); + /** + * @brief return the default size hint for this widget + * */ virtual QSize sizeHint() const; - + +protected: + /** + * @brief handle paint events + * */ + virtual void paintEvent(QPaintEvent *event); + + /** + * @brief handle resize events + * */ + virtual void resizeEvent(QResizeEvent *event); + private: QLineEdit * sudokuwidget_value[9][9]; -- cgit v1.2.3