X-Git-Url: https://harishankar.org/repos/?p=getaclue.git;a=blobdiff_plain;f=player_mainwindow.py;h=8234893d0a6923de42a5e4cf55bb6ba377286cbf;hp=b384b542cd1aa51a01c4f39fc1b3bc9cc49b12bf;hb=8e96d1e907d35e47615ff1fe610bca7c88f392cd;hpb=d7084b7094c99003960b33d4bd4fb655248c6ab3 diff --git a/player_mainwindow.py b/player_mainwindow.py index b384b54..8234893 100644 --- a/player_mainwindow.py +++ b/player_mainwindow.py @@ -17,11 +17,8 @@ class MainWindow: ACROSS = 1 DOWN = 2 - def gtk_main_quit (self, *args): - gtk.main_quit () - - # callback for menu item quit activated event - def on_quit_activate (self, menuitem): + # quit verification + def verify_quit (self): if self.puzzle: dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, @@ -30,8 +27,89 @@ class MainWindow: dlg.destroy () return False dlg.destroy () + return True + + # callback for menu item open activated event + def on_open_activate (self, menuitem): + dlg = gtk.FileChooserDialog ("Open a GetAClue puzzle", self.window, + gtk.FILE_CHOOSER_ACTION_OPEN, + (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) + + if dlg.run () == gtk.RESPONSE_OK: + puzzlefile = dlg.get_filename () + self.open_file (puzzlefile) + + dlg.destroy () + + # callback for menu item save as activated event + def on_save_as_activate (self, menuitem): + if self.puzzle: + dlg = gtk.FileChooserDialog ("Save GetAClue puzzle as", self.window, + gtk.FILE_CHOOSER_ACTION_SAVE, + (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, + gtk.RESPONSE_OK)) + if dlg.run () == gtk.RESPONSE_OK: + puzzlefile = dlg.get_filename () + self.save_file (puzzlefile) + + dlg.destroy () + + # callback for main window destroy + def on_mainwindow_destroy (self, args): + gtk.main_quit () + - self.gtk_main_quit () + # callback for window closing dialog + def on_mainwindow_delete_event (self, window, event): + # verify whether really to quit or not if a puzzle is open + v = self.verify_quit () + # return False for deleting and True for not deleting + return not v + + # callback for menu item quit activated event + def on_quit_activate (self, menuitem): + # verify whether really to quit or not if a puzzle is open + v = self.verify_quit () + # if verified, then quit + if v is True: + self.window.destroy () + + # callback for menu item clear grid activated event + def on_cleargrid_activate (self, menuitem): + if self.puzzle: + dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, + gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, + "Are you sure you wish to clear your entries?") + if dlg.run () == gtk.RESPONSE_YES: + # clear the guesses + self.puzzle.clear_guesses () + # redraw the grid + puzgrid = self.ui.get_object ("puzzlegrid") + puzgrid.queue_draw () + dlg.destroy() + + # callback for menu item verify board activated event + def on_verify_activate (self, menuitem): + if self.puzzle: + try: + ans = self.puzzle.is_solution_correct () + # if the solution is correct + if ans: + dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, + gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, + "Success! Your entries are correct.") + dlg.run () + else: + dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, + gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, + "Your solution has some errors. Fix them and try again") + dlg.run () + except crosswordpuzzle.IncompleteSolutionException: + dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, + gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, + "You've not completed the board yet. Cannot verify") + dlg.run () + dlg.destroy () # callback for menu item hide solution activated event def on_hidesolution_activate (self, menuitem): @@ -377,6 +455,18 @@ class MainWindow: down.append ([ str(self.puzzle.data[word[1]][word[2]].numbered), clue]) + def save_file (self, file): + # try to save the file + try: + cPickle.dump (self.puzzle, open (file, "wb"), cPickle.HIGHEST_PROTOCOL) + except (IOError, OSError, cPickle.PicklingError): + dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, + gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, + "Error in saving puzzle") + dlg.run () + dlg.destroy () + + # open a file def open_file (self, file): # try to open the file try: