Implemented opening a puzzle
[getaclue.git] / player_mainwindow.py
index 3d528cc..7aa3af7 100644 (file)
@@ -17,11 +17,20 @@ class MainWindow:
        ACROSS = 1
        DOWN = 2
 
-       def gtk_main_quit (self, *args):
-               gtk.main_quit ()
+       # 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))
 
-       # callback for menu item quit activated event
-       def on_quit_activate (self, menuitem):
+               if dlg.run () == gtk.RESPONSE_OK:
+                       puzzlefile = dlg.get_filename ()
+                       self.open_file (puzzlefile)
+
+               dlg.destroy ()
+
+       # 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 +39,116 @@ class MainWindow:
                                dlg.destroy ()
                                return False
                        dlg.destroy ()
+               return True
+
+
+       # 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,
+                                       "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):
+               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):
+               if self.puzzle:
+                       # reveal across/down word if any the position
+                       if self.puzzle.data[self.selected_row][self.selected_col].occupied_across is True:
+                               dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL,
+                                               gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO,
+                                               "Are you sure you wish to reveal across word at current cell?")
+                               # confirm that the user wants to reveal
+                               if dlg.run () == gtk.RESPONSE_YES:
+                                       self.puzzle.reveal_word_across (self.selected_row, self.selected_col)
+                                       # redraw the grid to reveal the word
+                                       puzgrid = self.ui.get_object ("puzzlegrid")
+                                       puzgrid.queue_draw ()
+                               dlg.destroy ()
+                       if self.puzzle.data[self.selected_row][self.selected_col].occupied_down is True:
+                               dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL,
+                                               gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO,
+                                               "Are you sure wish to reveal down word at current cell?")
+                               if dlg.run () == gtk.RESPONSE_YES:
+                                       self.puzzle.reveal_word_down (self.selected_row, self.selected_col)
+                                       # redraw the grid to reveal the word
+                                       puzgrid = self.ui.get_object ("puzzlegrid")
+                                       puzgrid.queue_draw ()
+                               dlg.destroy ()
 
-               self.gtk_main_quit ()
 
        # function to set the selected row/col based on the number clicked
        # on the clues list and also set the typing mode
@@ -112,23 +229,25 @@ class MainWindow:
        # selection across or down as the case may be
        def set_guess (self, guess_char):
                if self.puzzle:
-                       self.puzzle.data[self.selected_row][self.selected_col].guess = guess_char.upper ()
-                       # across mode typing
-                       if self.typing_mode == self.ACROSS:
-                               # move by one character across but only if there is no block
-                               # in between
-                               old_col = self.selected_col
-                               self.move_selection_across (1)
-                               if abs (self.selected_col - old_col) > 1:
-                                       self.selected_col = old_col
-                       # down mode typing
-                       else:
-                               # move by one character down but only if there is no block
-                               # in between
-                               old_row = self.selected_row
-                               self.move_selection_updown (1)
-                               if abs (self.selected_row - old_row) > 1:
-                                       self.selected_row = old_row
+                       # set a guess only if not revealed
+                       if self.puzzle.data[self.selected_row][self.selected_col].revealed is False:
+                               self.puzzle.data[self.selected_row][self.selected_col].guess = guess_char.upper ()
+                               # across mode typing
+                               if self.typing_mode == self.ACROSS:
+                                       # move by one character across but only if there is no block
+                                       # in between
+                                       old_col = self.selected_col
+                                       self.move_selection_across (1)
+                                       if abs (self.selected_col - old_col) > 1:
+                                               self.selected_col = old_col
+                               # down mode typing
+                               else:
+                                       # move by one character down but only if there is no block
+                                       # in between
+                                       old_row = self.selected_row
+                                       self.move_selection_updown (1)
+                                       if abs (self.selected_row - old_row) > 1:
+                                               self.selected_row = old_row
 
        # delete the guessed char in the previous row/col depending on the input mode
        # If input mode is ACROSS then delete guessed char at previous column else
@@ -214,7 +333,7 @@ class MainWindow:
                        # if it is backspace key then delete character at previous row/col
                        # depending on the input mode. If across editing mode, then delete
                        # at previous column else at previous row
-                       elif key == "BackSpace":
+                       elif key == "backspace":
                                self.delete_prev_guess ()
                                drawarea.queue_draw ()
 
@@ -290,6 +409,17 @@ class MainWindow:
                                                        ctx.move_to (col*30+10, row*30+20)
                                                        ctx.show_text (self.puzzle.data[row][col].guess)
 
+                                               # if there is a revealed solution character at the location
+                                               if (self.puzzle.data[row][col].revealed is True and
+                                                       (self.puzzle.data[row][col].occupied_across is True
+                                                       or self.puzzle.data[row][col].occupied_down is True)):
+                                                       ctx.set_source_rgb (0, 0, 0.8)
+                                                       ctx.select_font_face ("Serif", cairo.FONT_SLANT_NORMAL,
+                                                                       cairo.FONT_WEIGHT_BOLD)
+                                                       ctx.set_font_size (16)
+                                                       ctx.move_to (col*30+10, row*30+20)
+                                                       ctx.show_text (self.puzzle.data[row][col].char)
+
                        return False
 
        # load clues to the list