X-Git-Url: https://harishankar.org/repos/?p=getaclue.git;a=blobdiff_plain;f=player_mainwindow.py;h=24fabf6033992d2172ec260aab1a522d8f3d0899;hp=89459ae4114f35cb7afa20c235d9269cc3ec7bbd;hb=f75d1272342c253939b762073bd87a9432efc11e;hpb=1f9de52c4798a22293aaf2c32611090a099d0ce4 diff --git a/player_mainwindow.py b/player_mainwindow.py index 89459ae..24fabf6 100644 --- a/player_mainwindow.py +++ b/player_mainwindow.py @@ -4,6 +4,8 @@ # Main window class for GetAClue player +import sys +import os.path import cPickle import pygtk pygtk.require20 () @@ -16,22 +18,137 @@ class MainWindow: # typing mode constants ACROSS = 1 DOWN = 2 + # license text to be displayed in the about dialog + LICENSE_TEXT = """GetAClue is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +GetAClue is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GetAClue. If not, see .""" + + # 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, + gtk.FILE_CHOOSER_ACTION_OPEN, + (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) - def gtk_main_quit (self, *args): + 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 () + + # 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, - "Puzzle is open. Are you sure you wish to quit?") - if dlg.run () <> gtk.RESPONSE_YES: - dlg.destroy () - return False + "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 () - self.gtk_main_quit () + # callback for menu item hide solution activated event + def on_hidesolution_activate (self, menuitem): + if self.puzzle: + # hide the solution + self.puzzle.reveal_solution (False) + puzgrid = self.ui.get_object ("puzzlegrid") + # redraw the grid + puzgrid.queue_draw () + + # callback for menu item reveal solution activated event + def on_revealsolution_activate (self, menuitem): + if self.puzzle: + # confirm first + dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, + gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, + "This will reveal all words in the puzzle! Are you sure?") + if dlg.run () == gtk.RESPONSE_YES: + # reveal the solution + self.puzzle.reveal_solution () + # redraw the grid + puzgrid = self.ui.get_object ("puzzlegrid") + puzgrid.queue_draw () + dlg.destroy () # callback for menu item reveal word activated event def on_revealword_activate (self, menuitem): @@ -59,6 +176,10 @@ class MainWindow: puzgrid.queue_draw () dlg.destroy () + # callback for menu help about activated event + def on_about_activate (self, menu_item): + # display the about dialog + self.about () # function to set the selected row/col based on the number clicked # on the clues list and also set the typing mode @@ -353,6 +474,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: @@ -394,10 +527,28 @@ class MainWindow: dlg.run () dlg.destroy () + # about dialog + def about (self): + dlg = gtk.AboutDialog () + dlg.set_name ("GetAClue Player") + dlg.set_copyright ("Copyright 2010 V.Harishankar") + dlg.set_website ("http://harishankar.org/software") + dlg.set_authors (("Harishankar",)) + dlg.set_logo (self.window.get_icon()) + dlg.set_license (self.LICENSE_TEXT) + dlg.set_comments ("Create and play Crossword puzzles") + dlg.run () + dlg.destroy () + def __init__ (self, file_to_play = None): # load the user interface self.ui = gtk.Builder () - self.ui.add_from_file ("playerwindow.glade") + + # Path for the interface file - change this if you are distributing + # the application and put the icon, interface file in a different + # location!! + gladepath = os.path.join (sys.path[0], "playerwindow.glade") + self.ui.add_from_file (gladepath) # window object self.window = self.ui.get_object ("mainwindow")