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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
| #! /usr/bin/env python2.7
# -*- coding: utf-8 -*-
# Source : http://www.developpez.net/forums/d1281788/autres-langages/python-zope/gui/pyside-pyqt/coquille-vide-sudoku/#post7181230
from __future__ import division
import sys
from copy import deepcopy
from PySide import QtCore, QtGui
def cellColor(painter, option, color):
"""
???
"""
r = option.rect
x, y, w, h = r.x() + 1, r.y() + 1, r.width()-2, r.height()-2
if isinstance(color, (str, unicode)):
color = QtGui.QColor(color)
elif isinstance(color, (list, tuple)):
if len(color) == 3:
r, g, b = color
alpha = 255
else:
r, g, b, alpha = color
color = QtGui.QColor(r, g, b, alpha)
painter.fillRect(x, y, w, h, color)
def cellBorder(painter, option, where):
"""
???
"""
r = option.rect
x, y, w, h = r.x(), r.y(), r.width(), r.height()
if where == 'up':
x1, y1, x2, y2 = x, y, x + w, y
elif where == 'bottom':
x1, y1, x2, y2 = x + w, y + h, x, y + h
elif where == 'right':
x1, y1, x2, y2 = x + w, y, x + w, y + h
elif where == 'left':
x1, y1, x2, y2 = x, y + h, x, y
else:
return None
pen = QtGui.QPen()
pen.setWidth(4)
painter.setPen(pen)
painter.drawLine(x1, y1, x2, y2)
class SudokuDelegate(QtGui.QItemDelegate):
def __init__(self, parent=None):
super(SudokuDelegate, self).__init__(parent)
def initGrid(self, grid):
self.grid_0 = grid
def paint(self, painter, option, index):
"""
???
"""
row, col = index.row(), index.column()
if row == 0 or row == 3 or row == 6:
if col in[0,3,6]:
cellBorder(painter, option, 'left')
cellBorder(painter, option, 'up')
elif col == 8:
cellBorder(painter, option, 'right')
cellBorder(painter, option, 'up')
else:
cellBorder(painter, option, 'up')
elif row in [1,2,4,5,7]:
if col in [0,3,6]:
cellBorder(painter, option, 'left')
elif col == 8:
cellBorder(painter, option, 'right')
elif row == 8:
if col in[0,3,6]:
cellBorder(painter, option, 'left')
cellBorder(painter, option, 'bottom')
elif col == 8:
cellBorder(painter, option, 'right')
cellBorder(painter, option, 'bottom')
else:
cellBorder(painter, option, 'bottom')
# Background color for initial known cells
if self.grid_0[row][col]!=0:
cellColor(painter, option, [200,200,200])
QtGui.QItemDelegate.paint(self, painter, option, index)
class MainWindow(QtGui.QWidget):
def __init__(
self,
parent = None
):
super(MainWindow, self).__init__(parent)
# General grid
self.table = QtGui.QTableWidget(self)
self.nbrow, self.nbcol = 9, 9
self.table.setRowCount(self.nbrow)
self.table.setColumnCount(self.nbcol)
# Each cell has dimension 50 pixels x 50 pixels
for row in range(0, self.nbrow):
self.table.setRowHeight(row, 50)
for col in range(0, self.nbcol):
self.table.setColumnWidth(col, 50)
# Each cell contains one single QTableWidgetItem
for row in range(0, self.nbrow):
for col in range(0, self.nbcol):
item = QtGui.QTableWidgetItem()
item.setTextAlignment(
QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter
)
self.table.setItem(row, col, item)
# Header formatting
font = QtGui.QFont()
font.setFamily(u"DejaVu Sans")
font.setPointSize(12)
self.table.horizontalHeader().setFont(font)
self.table.verticalHeader().setFont(font)
# Font used
font = QtGui.QFont()
font.setFamily(u"DejaVu Sans")
font.setPointSize(20)
self.table.setFont(font)
# Global Size
self.resize(60*9, 60*9 + 20)
# Layout of the table
layout = QtGui.QGridLayout()
layout.addWidget(self.table, 0, 0)
self.setLayout(layout)
# Initial grid (to test)
self.grid_0 = [
[0,0,0, 0,2,0, 9,0,1],
[0,0,0, 0,0,0, 0,0,3],
[0,8,0, 3,0,0, 4,5,0],
#
[0,4,7, 0,0,5, 0,8,0],
[0,0,0, 0,0,0, 0,0,0],
[0,2,0, 9,0,0, 7,4,0],
#
[0,9,5, 0,0,2, 0,3,0],
[6,0,0, 0,0,8, 0,0,0],
[7,0,4, 0,6,0, 0,0,0]
]
# Use of the delegate so to use thicker lines.
self.delegate = SudokuDelegate(self.table)
self.table.setItemDelegate(self.delegate)
self.delegate.initGrid(self.grid_0)
# Initialization of the actual grid
self.grid = deepcopy(self.grid_0)
# Display the grid.
self.update(self.grid)
# ??
self.cellToFill = []
for row in range(9):
for col in range(9):
if self.grid_0[row][col] == 0:
self.cellToFill.append((row, col))
# Set the focus in the first cell
self.table.setFocus()
self.table.setCurrentCell(0, 0)
self.table.keyPressEvent = self.keyPressEvent
def keyPressEvent(self, event):
row, col = self.table.currentRow(), self.table.currentColumn()
# One digit
if ord('1') <= event.key() <= ord('9'):
if (row, col) in self.cellToFill:
font = QtGui.QFont()
font.setFamily(u"DejaVu Sans")
font.setPointSize(20)
self.table.item(row, col).setFont(font)
color = QtGui.QColor(0, 0, 0)
self.table.item(row, col).setForeground(color)
self.table.item(row, col).setText(chr(event.key()))
# Arrow moving
elif event.key() == QtCore.Qt.Key_Up:
self.table.setCurrentCell((row - 1) % 9, col)
elif event.key() == QtCore.Qt.Key_Down:
self.table.setCurrentCell((row + 1) % 9, col)
elif event.key() == QtCore.Qt.Key_Left:
self.table.setCurrentCell(row, (col - 1) % 9)
elif event.key() == QtCore.Qt.Key_Right:
self.table.setCurrentCell(row, (col + 1) % 9)
def update(self, g):
for row in range(0, len(g[0])):
for col in range(0, len(g)):
if g[row][col] == 0:
font = QtGui.QFont()
font.setFamily(u"DejaVu Sans")
font.setPointSize(12)
self.table.item(row, col).setFont(font)
color = QtGui.QColor(0, 0, 255, 255) # bleu
self.table.item(row, col).setForeground(color)
self.table.item(row, col).setText(
u"1 2 3\n4 5 6\n7 8 9"
)
self.table.item(
row, col
).setFlags(
QtCore.Qt.ItemIsEnabled
| QtCore.Qt.ItemIsSelectable
# | QtCore.Qt.ItemIsEditable
)
else:
self.table.item(row, col).setText(unicode(g[row][col]))
self.table.item(
row, col
).setFlags(
QtCore.Qt.ItemIsEnabled
| QtCore.Qt.ItemIsSelectable
)
# Just for testing !
color = QtGui.QColor(160, 255, 160, 255) # Light green
self.table.item(2, 4).setBackground(color)
color = QtGui.QColor(255, 160, 160, 255) # Light red
self.table.item(6, 3).setBackground(color)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
fen = MainWindow()
fen.show()
sys.exit(app.exec_()) |
Partager