X-Git-Url: https://harishankar.org/repos/?p=getaclue.git;a=blobdiff_plain;f=player_mainwindow.py;h=3ea54205e69e3f2abb2598161185791b3646eeee;hp=58de825f56548c6af95a49f3edda2d231cf3adae;hb=e17ecb8ed14ad55b0ac2d1a45ae78b8452c8db18;hpb=8178235fb21779eed3615e502f2c4fdebd42150e diff --git a/player_mainwindow.py b/player_mainwindow.py index 58de825..3ea5420 100644 --- a/player_mainwindow.py +++ b/player_mainwindow.py @@ -4,14 +4,205 @@ # Main window class for GetAClue player +import cPickle import pygtk pygtk.require20 () import gtk +import pango +import crosswordpuzzle class MainWindow: def gtk_main_quit (self, *args): + if self.puzzle: + dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, + gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, + "Puzzle is open. Are you sure you wish to quit?") + if dlg.run () == gtk.RESPONSE_NO: + dlg.destroy () + return + dlg.destroy () + gtk.main_quit () + # callback for menu item quit activated event + def on_quit_activate (self, menuitem): + self.gtk_main_quit () + + # callback for puzzle grid mouse button release event + def on_puzzlegrid_button_press_event (self, drawarea, event): + self.window.set_focus (drawarea) + return True + + # moving the current selection in grid by one up or down + def move_selection_updown (self, step): + # increase or reduce the row by step until an occupied grid is found + # black block + last_occupied_row = self.selected_row + while True: + self.selected_row += step + if self.selected_row < 0 or self.selected_row >= self.puzzle.rows: + self.selected_row = last_occupied_row + break + if (self.puzzle.data[self.selected_row][self.selected_col].occupied_across is True + or self.puzzle.data[self.selected_row][self.selected_col].occupied_down is True): + break + + # moving the current selection in grid by one across either way + def move_selection_across (self, step): + # increase or reduce the row by step until an occupied grid is found + # black block + last_occupied_col = self.selected_col + while True: + self.selected_col += step + if self.selected_col < 0 or self.selected_col >= self.puzzle.cols: + self.selected_col = last_occupied_col + break + if (self.puzzle.data[self.selected_row][self.selected_col].occupied_across is True + or self.puzzle.data[self.selected_row][self.selected_col].occupied_down is True): + break + + # callback for puzzle grid key release event + def on_puzzlegrid_key_press_event (self, drawarea, event): + key = gtk.gdk.keyval_name (event.keyval) + + if event.state == gtk.gdk.SHIFT_MASK and key == "Up": + # reduce the row by 1 until you find an occupied grid and not a + # black block + self.move_selection_updown (-1) + drawarea.queue_draw () + elif event.state == gtk.gdk.SHIFT_MASK and key == "Down": + # increase the row by 1 until you find an occupied grid and not a + # black block + self.move_selection_updown (1) + drawarea.queue_draw () + elif event.state == gtk.gdk.SHIFT_MASK and key == "Right": + # increase the column by 1 until you find an occupied grid and not + # a black block + self.move_selection_across (1) + drawarea.queue_draw () + elif event.state == gtk.gdk.SHIFT_MASK and key == "Left": + # decrease the column by 1 until you find an occupied grid and not + # a black block + self.move_selection_across (-1) + drawarea.queue_draw () + return False + + # puzzle grid focus in event + def on_puzzlegrid_focus_out_event (self, drawarea, event): + col = drawarea.window.get_colormap ().alloc_color (gtk.gdk.Color ("gray")) + drawarea.window.set_background (col) + + return False + + # puzzle grid focus out event + def on_puzzlegrid_focus_in_event (self, drawarea, event): + col = drawarea.window.get_colormap ().alloc_color (gtk.gdk.Color ("white")) + drawarea.window.set_background (col) + return False + + # callback for drawing the 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) + + #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) + + # run through the grid + for row in range (self.puzzle.rows): + for col in range (self.puzzle.cols): + # (re)set foreground color + ctx.set_source_rgb (0, 0, 0) + # 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: + # if selected row/column + if row == self.selected_row and col == self.selected_col: + ctx.set_source_rgb (1, 1, 0.6) + ctx.rectangle (col*30,row*30, 30, 30) + ctx.fill () + else: + ctx.set_source_rgb (1, 1, 1) + ctx.rectangle (col*30, row*30, 30, 30) + ctx.fill () + ctx.set_source_rgb (0, 0, 0) + ctx.rectangle (col*30, row*30, 30, 30) + ctx.stroke () + + # if numbered + if self.puzzle.data[row][col].numbered <> 0: + ctx.set_source_rgb (0, 0, 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): + # try to open the file + try: + # load the puzzle + self.puzzle = cPickle.load (open (file, "rb")) + # assert that it is unfrozen otherwise raise frozen grid exception + self.puzzle.assert_frozen_grid () + + # set selected initial row and column to 0 + self.selected_row = 0 + self.selected_col = 0 + self.window.set_title ("GetAClue player - " + file) + # load the clues + self.load_clues () + # handle unpickling, and file errors + except (cPickle.UnpicklingError, IOError, OSError): + dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, + gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, + "Invalid file. Cannot be loaded") + dlg.run () + dlg.destroy () + # if the puzzle has no words, then it cannot be played obviously + except crosswordpuzzle.NoWordsException: + self.puzzle = None + dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, + gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, + "Word grid has no words. Cannot play") + dlg.run () + dlg.destroy () + # if the puzzle is not frozen then it cannot be played + except crosswordpuzzle.FrozenGridException: + self.puzzle = None + dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, + gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, + "Word grid is not finalized/frozen. Cannot play") + dlg.run () + dlg.destroy () + def __init__ (self, file_to_play = None): # load the user interface self.ui = gtk.Builder () @@ -41,9 +232,12 @@ class MainWindow: # 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: - self.window.set_title ("GetAClue player - " + file_to_play) + self.open_file (file_to_play) gtk.main ()