import gtkimport gtk.gladefrom cairo import *# TODO Petites constantes pourries (1)HEIGHT=9WIDTH=9class SudoGrid: """ Coordonnees des elements d'une grille evoluee (en fonction d'une grille standard) """ def __init__(self): self.perms = [[(x,y) for y in xrange(HEIGHT)] for x in xrange(WIDTH)]class SudoGridSwap(list): """ Liste de couples de coordonnees (permet de stocker des permutations d'elements d'une SudoGrid) """ def apply(sudogrid): for i in self: sudogrid([i[0][0],i[0][1]), sudogrid(i[1][0],i[1][1]) = sudogrid([i[1][0],i[1][1]), sudogrid(i[0][0],i[0][1])class SudoGridFace(gtk.DrawingArea): """ Widget GTK ecrit en Cairo pour afficher une SudoGrid """ def __init__(self): super(SudoGridFace,self).__init__() self.connect("expose_event",self.expose) def expose(self,widget,event): context=widget.window.cairo_create() context.rectangle(event.area.x, event.area.y, event.area.width, event.area.height) context.clip() self.draw(context) return False def draw(self,context): # TODO Petites constantes pourries (2) margin=0.02 line_width=0.005 rect = self.get_allocation() context.scale(rect.width, rect.height) height = float(HEIGHT) * (1 + 2.0*margin) width = float(WIDTH) * (1 + 2.0*margin) # Rectangle dans lequel on affiche la SudoGrid context.rectangle(margin, margin, 1.0-2*margin, 1.0-2*margin) context.set_line_width(5*line_width) context.set_source_rgb(0,0,0) context.stroke() context.rectangle(margin, margin, 1.0-2*margin, 1.0-2*margin) context.set_source_rgb(1,1,1) context.fill() """ Contours des cases """ context.set_line_width(line_width) context.set_source_rgb(0,0,0) for c in range(1,WIDTH): currentx = c/width + margin context.move_to(currentx, margin) context.line_to(currentx, 1.0-margin) for l in range(1,HEIGHT): currenty = l/height + margin context.move_to(margin, currenty) context.line_to(1.0-margin, currenty) context.select_font_face ("Sans", FONT_SLANT_NORMAL) context.set_font_size(0.5/HEIGHT) # Les identifiants des cases (hop hop ca va changer !) for c in range(0,WIDTH): for l in range(0,HEIGHT): text = str(l)+str(c) fxb,fyb,fw,fh,fxa,fya = context.text_extents(text) currentx = (c+0.5)/width + margin - (fw/2 + fxb) currenty = (l+0.5)/width + margin - (fh/2 + fyb) context.move_to(currentx,currenty) context.show_text(text) context.stroke()def main(): window = gtk.Window() window.set_default_size(900,300) table = gtk.Table(1,3) window.add(table) faces = list() for i in range(0,3): faces.insert(i,SudoGridFace()) table.attach(faces[i],i,i+1,0,1) window.connect("destroy",gtk.main_quit) window.show_all() gtk.main()if __name__ == "__main__": main()