X-Git-Url: https://harishankar.org/repos/?p=getaclue.git;a=blobdiff_plain;f=player_mainwindow.py;fp=player_mainwindow.py;h=3ea54205e69e3f2abb2598161185791b3646eeee;hp=452410627514265538c31ae052adb98653fd4018;hb=e17ecb8ed14ad55b0ac2d1a45ae78b8452c8db18;hpb=30c57b62f1742d48bce4823ba13d60dd10176f93 diff --git a/player_mainwindow.py b/player_mainwindow.py index 4524106..3ea5420 100644 --- a/player_mainwindow.py +++ b/player_mainwindow.py @@ -13,10 +13,23 @@ 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_release_event (self, drawarea, event): + def on_puzzlegrid_button_press_event (self, drawarea, event): self.window.set_focus (drawarea) return True @@ -153,11 +166,42 @@ class MainWindow: clue]) def open_file (self, file): - self.puzzle = cPickle.load (open (file, "rb")) - self.selected_row = 0 - self.selected_col = 0 - self.window.set_title ("GetAClue player - " + file) - self.load_clues () + # 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