summaryrefslogtreecommitdiff
path: root/src/sudokuwidget.cc
blob: 742798fa2040537ba30453153d73a5636733ebb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include "sudokuwidget.h"

#include <QLineEdit>
#include <QGridLayout>
#include <QString>

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]->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;
			}
		}
	}	
}