summaryrefslogtreecommitdiff
path: root/src/sudokuwidget.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/sudokuwidget.cc')
-rw-r--r--src/sudokuwidget.cc98
1 files changed, 90 insertions, 8 deletions
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);
+ }
+}