Implemented drawing the grid in player application
[getaclue.git] / player_mainwindow.py
index 58de825..00be083 100644 (file)
@@ -4,14 +4,76 @@
 
 # Main window class for GetAClue player
 
 
 # Main window class for GetAClue player
 
+import cPickle
 import pygtk
 pygtk.require20 ()
 import gtk
 import pygtk
 pygtk.require20 ()
 import gtk
+import pango
+import crosswordpuzzle
 
 class MainWindow:
        def gtk_main_quit (self, *args):
                gtk.main_quit ()
 
 
 class MainWindow:
        def gtk_main_quit (self, *args):
                gtk.main_quit ()
 
+       # drawing for puzzle grid
+       def on_puzzlegrid_expose_event (self, drawarea, event):
+               # if puzzle is loaded
+               if self.puzzle:
+                       # size the area
+                       drawarea.set_size_request (self.puzzle.cols*30+2, self.puzzle.rows*30+2)
+
+                       # set background color to white
+                       col = drawarea.window.get_colormap ().alloc_color (gtk.gdk.Color ("white"))
+                       drawarea.window.set_background (col)
+
+                       #numlayout = gtk.PrintContext().create_pango_layout ()
+                       #numlayout.set_font_description (pango.FontDescription ("Sans 8"))
+                       ctx = drawarea.window.cairo_create ()
+                       ctx.set_line_width (1.5)
+
+                       for row in range (self.puzzle.rows):
+                               for col in range (self.puzzle.cols):
+                                       # if the area is not occupied
+                                       if (self.puzzle.data[row][col].occupied_across is False and
+                                               self.puzzle.data[row][col].occupied_down is False):
+                                               ctx.rectangle (col*30, row*30, 30, 30)
+                                               ctx.fill ()
+                                       else:
+                                               ctx.rectangle (col*30, row*30, 30, 30)
+                                               ctx.stroke ()
+                                               # if numbered
+                                               if self.puzzle.data[row][col].numbered <> 0:
+                                                       ctx.select_font_face ("Sans 7")
+                                                       ctx.move_to (col*30+2, row*30+10)
+                                                       ctx.show_text (str(self.puzzle.data[row][col].numbered))
+
+                       return False
+
+       def load_clues (self):
+               # get the clues list store objects
+               across = self.ui.get_object ("clues_across")
+               down = self.ui.get_object ("clues_down")
+               across.clear ()
+               down.clear ()
+
+               # if puzzle is loaded
+               if self.puzzle:
+                       clues_across = self.puzzle.get_clues_across ()
+                       clues_down = self.puzzle.get_clues_down ()
+                       # insert the numbers and the clues for across
+                       for word, clue in clues_across:
+                               across.append ([str(self.puzzle.data[word[1]][word[2]].numbered),
+                                                       clue])
+                       # insert the numbers and the clues for down
+                       for word, clue in clues_down:
+                               down.append ([ str(self.puzzle.data[word[1]][word[2]].numbered),
+                                                       clue])
+
+       def open_file (self, file):
+               self.puzzle = cPickle.load (open (file, "rb"))
+               self.window.set_title ("GetAClue player - " + file)
+               self.load_clues ()
+
        def __init__ (self, file_to_play = None):
                # load the user interface
                self.ui = gtk.Builder ()
        def __init__ (self, file_to_play = None):
                # load the user interface
                self.ui = gtk.Builder ()
@@ -41,9 +103,12 @@ class MainWindow:
                # connect the signals
                self.ui.connect_signals (self)
 
                # connect the signals
                self.ui.connect_signals (self)
 
-               # set the window title
+               # set the puzzle to None
+               self.puzzle = None
+
+               # open the file if it is set
                if file_to_play:
                if file_to_play:
-                       self.window.set_title ("GetAClue player - " + file_to_play)
+                       self.open_file (file_to_play)
 
                gtk.main ()
 
 
                gtk.main ()