From: Harishankar Date: Wed, 8 Dec 2010 09:01:01 +0000 (+0530) Subject: Implemented saving open puzzle in player X-Git-Url: https://harishankar.org/repos/?p=getaclue.git;a=commitdiff_plain;h=8e96d1e907d35e47615ff1fe610bca7c88f392cd Implemented saving open puzzle in player Implemented saving the puzzle for future restore. --- diff --git a/player_mainwindow.py b/player_mainwindow.py index 7aa3af7..8234893 100644 --- a/player_mainwindow.py +++ b/player_mainwindow.py @@ -17,6 +17,18 @@ class MainWindow: ACROSS = 1 DOWN = 2 + # quit verification + def verify_quit (self): + 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_YES: + 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, @@ -29,18 +41,18 @@ class MainWindow: dlg.destroy () - # quit verification - def verify_quit (self): + # callback for menu item save as activated event + def on_save_as_activate (self, menuitem): 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_YES: - dlg.destroy () - return False - dlg.destroy () - return True + 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): @@ -443,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: diff --git a/playerwindow.glade b/playerwindow.glade index 83639ba..32dc750 100644 --- a/playerwindow.glade +++ b/playerwindow.glade @@ -61,6 +61,7 @@ True Save _As... True +