1 # Get A Clue (C) 2010 V. Harishankar
2 # Crossword puzzle maker program
3 # Licensed under the GNU GPL v3
5 # Main window class for GetAClue player
15 import crosswordpuzzle
18 # typing mode constants
21 # license text to be displayed in the about dialog
22 LICENSE_TEXT
= """GetAClue is free software: you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation, either version 3 of the License, or
25 (at your option) any later version.
27 GetAClue is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with GetAClue. If not, see <http://www.gnu.org/licenses/>."""
36 def verify_quit (self
):
38 dlg
= gtk
.MessageDialog (self
.window
, gtk
.DIALOG_MODAL
,
39 gtk
.MESSAGE_QUESTION
, gtk
.BUTTONS_YES_NO
,
40 "Puzzle is open. Are you sure you wish to quit?")
41 if dlg
.run () <> gtk
.RESPONSE_YES
:
47 # callback for menu item open activated event
48 def on_open_activate (self
, menuitem
):
49 dlg
= gtk
.FileChooserDialog ("Open a GetAClue puzzle", self
.window
,
50 gtk
.FILE_CHOOSER_ACTION_OPEN
,
51 (gtk
.STOCK_CANCEL
, gtk
.RESPONSE_CANCEL
, gtk
.STOCK_OPEN
, gtk
.RESPONSE_OK
))
53 if dlg
.run () == gtk
.RESPONSE_OK
:
54 puzzlefile
= dlg
.get_filename ()
55 self
.open_file (puzzlefile
)
59 # callback for menu item save as activated event
60 def on_save_as_activate (self
, menuitem
):
62 dlg
= gtk
.FileChooserDialog ("Save GetAClue puzzle as", self
.window
,
63 gtk
.FILE_CHOOSER_ACTION_SAVE
,
64 (gtk
.STOCK_CANCEL
, gtk
.RESPONSE_CANCEL
, gtk
.STOCK_SAVE
,
66 if dlg
.run () == gtk
.RESPONSE_OK
:
67 puzzlefile
= dlg
.get_filename ()
68 self
.save_file (puzzlefile
)
72 # callback for main window destroy
73 def on_mainwindow_destroy (self
, args
):
77 # callback for window closing dialog
78 def on_mainwindow_delete_event (self
, window
, event
):
79 # verify whether really to quit or not if a puzzle is open
80 v
= self
.verify_quit ()
81 # return False for deleting and True for not deleting
84 # callback for menu item quit activated event
85 def on_quit_activate (self
, menuitem
):
86 # verify whether really to quit or not if a puzzle is open
87 v
= self
.verify_quit ()
88 # if verified, then quit
90 self
.window
.destroy ()
92 # callback for menu item clear grid activated event
93 def on_cleargrid_activate (self
, menuitem
):
95 dlg
= gtk
.MessageDialog (self
.window
, gtk
.DIALOG_MODAL
,
96 gtk
.MESSAGE_QUESTION
, gtk
.BUTTONS_YES_NO
,
97 "Are you sure you wish to clear your entries?")
98 if dlg
.run () == gtk
.RESPONSE_YES
:
100 self
.puzzle
.clear_guesses ()
102 puzgrid
= self
.ui
.get_object ("puzzlegrid")
103 puzgrid
.queue_draw ()
106 # callback for menu item verify board activated event
107 def on_verify_activate (self
, menuitem
):
110 ans
= self
.puzzle
.is_solution_correct ()
111 # if the solution is correct
113 dlg
= gtk
.MessageDialog (self
.window
, gtk
.DIALOG_MODAL
,
114 gtk
.MESSAGE_INFO
, gtk
.BUTTONS_CLOSE
,
115 "Success! Your entries are correct.")
118 dlg
= gtk
.MessageDialog (self
.window
, gtk
.DIALOG_MODAL
,
119 gtk
.MESSAGE_INFO
, gtk
.BUTTONS_CLOSE
,
120 "Your solution has some errors. Fix them and try again")
122 except crosswordpuzzle
.IncompleteSolutionException
:
123 dlg
= gtk
.MessageDialog (self
.window
, gtk
.DIALOG_MODAL
,
124 gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_CLOSE
,
125 "You've not completed the board yet. Cannot verify")
129 # callback for menu item hide solution activated event
130 def on_hidesolution_activate (self
, menuitem
):
133 self
.puzzle
.reveal_solution (False)
134 puzgrid
= self
.ui
.get_object ("puzzlegrid")
136 puzgrid
.queue_draw ()
138 # callback for menu item reveal solution activated event
139 def on_revealsolution_activate (self
, menuitem
):
142 dlg
= gtk
.MessageDialog (self
.window
, gtk
.DIALOG_MODAL
,
143 gtk
.MESSAGE_QUESTION
, gtk
.BUTTONS_YES_NO
,
144 "This will reveal all words in the puzzle! Are you sure?")
145 if dlg
.run () == gtk
.RESPONSE_YES
:
146 # reveal the solution
147 self
.puzzle
.reveal_solution ()
149 puzgrid
= self
.ui
.get_object ("puzzlegrid")
150 puzgrid
.queue_draw ()
153 # callback for menu item reveal word activated event
154 def on_revealword_activate (self
, menuitem
):
156 # reveal across/down word if any the position
157 if self
.puzzle
.data
[self
.selected_row
][self
.selected_col
].occupied_across
is True:
158 dlg
= gtk
.MessageDialog (self
.window
, gtk
.DIALOG_MODAL
,
159 gtk
.MESSAGE_QUESTION
, gtk
.BUTTONS_YES_NO
,
160 "Are you sure you wish to reveal across word at current cell?")
161 # confirm that the user wants to reveal
162 if dlg
.run () == gtk
.RESPONSE_YES
:
163 self
.puzzle
.reveal_word_across (self
.selected_row
, self
.selected_col
)
164 # redraw the grid to reveal the word
165 puzgrid
= self
.ui
.get_object ("puzzlegrid")
166 puzgrid
.queue_draw ()
168 if self
.puzzle
.data
[self
.selected_row
][self
.selected_col
].occupied_down
is True:
169 dlg
= gtk
.MessageDialog (self
.window
, gtk
.DIALOG_MODAL
,
170 gtk
.MESSAGE_QUESTION
, gtk
.BUTTONS_YES_NO
,
171 "Are you sure wish to reveal down word at current cell?")
172 if dlg
.run () == gtk
.RESPONSE_YES
:
173 self
.puzzle
.reveal_word_down (self
.selected_row
, self
.selected_col
)
174 # redraw the grid to reveal the word
175 puzgrid
= self
.ui
.get_object ("puzzlegrid")
176 puzgrid
.queue_draw ()
179 # callback for menu help about activated event
180 def on_about_activate (self
, menu_item
):
181 # display the about dialog
184 # function to set the selected row/col based on the number clicked
185 # on the clues list and also set the typing mode
186 def set_selection_of_num (self
, num
, across
= True):
187 # get the row, col of the word
188 row
, col
= self
.puzzle
.get_position_of_num (num
)
190 # set the selected row and column
191 self
.selected_row
= row
192 self
.selected_col
= col
193 # set typing mode to across
195 self
.typing_mode
= self
.ACROSS
197 self
.typing_mode
= self
.DOWN
199 # update the puzzle grid
200 puzgrid
= self
.ui
.get_object ("puzzlegrid")
202 puzgrid
.queue_draw ()
204 # callback for tree view "across" being activated
205 # activated - when double clicked or enter pressed
206 def on_tree_clues_across_row_activated (self
, view
, path
, column
):
207 # get the across list object
208 across_list
= self
.ui
.get_object ("clues_across")
209 # get the number of the across word
210 anum
= int (across_list
.get_value (across_list
.get_iter (path
), 0))
212 self
.set_selection_of_num (anum
)
216 # callback for tree view "down" being activated
217 # activated - when double clicked or enter pressed
218 def on_tree_clues_down_row_activated (self
, view
, path
, column
):
219 # get the down list object
220 down_list
= self
.ui
.get_object ("clues_down")
221 # get the number of the down word
222 dnum
= int (down_list
.get_value (down_list
.get_iter (path
), 0))
224 self
.set_selection_of_num (dnum
, False)
226 # moving the current selection in grid by one up or down
227 def move_selection_updown (self
, step
):
228 # increase or reduce the row by step until an occupied grid is found
231 last_occupied_row
= self
.selected_row
233 self
.selected_row
+= step
234 if self
.selected_row
< 0 or self
.selected_row
>= self
.puzzle
.rows
:
235 self
.selected_row
= last_occupied_row
237 if (self
.puzzle
.data
[self
.selected_row
][self
.selected_col
].occupied_across
is True
238 or self
.puzzle
.data
[self
.selected_row
][self
.selected_col
].occupied_down
is True):
241 # moving the current selection in grid by one across either way
242 def move_selection_across (self
, step
):
243 # increase or reduce the row by step until an occupied grid is found
246 last_occupied_col
= self
.selected_col
248 self
.selected_col
+= step
249 if self
.selected_col
< 0 or self
.selected_col
>= self
.puzzle
.cols
:
250 self
.selected_col
= last_occupied_col
252 if (self
.puzzle
.data
[self
.selected_row
][self
.selected_col
].occupied_across
is True
253 or self
.puzzle
.data
[self
.selected_row
][self
.selected_col
].occupied_down
is True):
256 # set the guessed character in the grid at selected location and move the
257 # selection across or down as the case may be
258 def set_guess (self
, guess_char
):
260 # set a guess only if not revealed
261 if self
.puzzle
.data
[self
.selected_row
][self
.selected_col
].revealed
is False:
262 self
.puzzle
.data
[self
.selected_row
][self
.selected_col
].guess
= guess_char
264 if self
.typing_mode
== self
.ACROSS
:
265 # move by one character across but only if there is no block
267 old_col
= self
.selected_col
268 self
.move_selection_across (1)
269 if abs (self
.selected_col
- old_col
) > 1:
270 self
.selected_col
= old_col
273 # move by one character down but only if there is no block
275 old_row
= self
.selected_row
276 self
.move_selection_updown (1)
277 if abs (self
.selected_row
- old_row
) > 1:
278 self
.selected_row
= old_row
280 # delete the guessed char in the previous row/col depending on the input mode
281 # If input mode is ACROSS then delete guessed char at previous column else
283 def delete_prev_guess (self
):
285 if self
.typing_mode
== self
.ACROSS
:
286 # prevent deleting characters when there is a gap
287 old_sel_col
= self
.selected_col
288 self
.move_selection_across (-1)
289 # only if there is no block inbetween delete
290 if abs (self
.selected_col
- old_sel_col
) <= 1:
291 self
.puzzle
.data
[self
.selected_row
][self
.selected_col
].guess
= None
294 self
.selected_col
= old_sel_col
295 elif self
.typing_mode
== self
.DOWN
:
296 # prevent deleting characters when there is a gap
297 old_sel_row
= self
.selected_row
298 self
.move_selection_updown (-1)
299 # only if there is no block inbetween delete
300 if abs (self
.selected_row
- old_sel_row
) <= 1:
301 self
.puzzle
.data
[self
.selected_row
][self
.selected_col
].guess
= None
304 self
.selected_row
= old_sel_row
306 # callback for puzzle grid mouse button release event
307 def on_puzzlegrid_button_press_event (self
, drawarea
, event
):
308 # set the focus on the puzzle grid
310 self
.window
.set_focus (drawarea
)
312 col
= int (event
.x
/ 30)
313 row
= int (event
.y
/ 30)
315 if col
< self
.puzzle
.cols
and row
< self
.puzzle
.rows
:
316 if (self
.puzzle
.data
[row
][col
].occupied_across
is True or
317 self
.puzzle
.data
[row
][col
].occupied_down
is True):
318 self
.selected_col
= col
319 self
.selected_row
= row
320 drawarea
.queue_draw ()
324 # callback for main window key release event
325 def on_mainwindow_key_press_event (self
, window
, event
):
327 drawarea
= self
.ui
.get_object ("puzzlegrid")
328 key
= gtk
.gdk
.keyval_name (event
.keyval
).lower ()
330 if event
.state
== gtk
.gdk
.SHIFT_MASK
and key
== "up":
331 # reduce the row by 1 until you find an occupied grid and not a
333 self
.move_selection_updown (-1)
334 self
.typing_mode
= self
.DOWN
335 drawarea
.queue_draw ()
336 elif event
.state
== gtk
.gdk
.SHIFT_MASK
and key
== "down":
337 # increase the row by 1 until you find an occupied grid and not a
339 self
.move_selection_updown (1)
340 self
.typing_mode
= self
.DOWN
341 drawarea
.queue_draw ()
342 elif event
.state
== gtk
.gdk
.SHIFT_MASK
and key
== "right":
343 # increase the column by 1 until you find an occupied grid and not
345 self
.move_selection_across (1)
346 self
.typing_mode
= self
.ACROSS
347 drawarea
.queue_draw ()
348 elif event
.state
== gtk
.gdk
.SHIFT_MASK
and key
== "left":
349 # decrease the column by 1 until you find an occupied grid and not
351 self
.move_selection_across (-1)
352 self
.typing_mode
= self
.ACROSS
353 drawarea
.queue_draw ()
354 # if it is A-Z or a-z then
355 elif len (key
) == 1 and key
.isalpha ():
356 guess_char
= key
.upper ()
357 self
.set_guess (guess_char
)
358 drawarea
.queue_draw ()
359 # if it is the delete key then delete character at selected row/col
360 elif key
== "delete":
361 self
.puzzle
.data
[self
.selected_row
][self
.selected_col
].guess
= None
362 drawarea
.queue_draw ()
363 # if the key is space key then delete character and move across or
364 # down one step depending on the mode
366 self
.set_guess (None)
367 drawarea
.queue_draw ()
368 # if it is backspace key then delete character at previous row/col
369 # depending on the input mode. If across editing mode, then delete
370 # at previous column else at previous row
371 elif key
== "backspace":
372 self
.delete_prev_guess ()
373 drawarea
.queue_draw ()
377 # puzzle grid focus in event
378 def on_puzzlegrid_focus_out_event (self
, drawarea
, event
):
380 col
= drawarea
.window
.get_colormap ().alloc_color (gtk
.gdk
.Color ("gray"))
381 drawarea
.window
.set_background (col
)
385 # puzzle grid focus out event
386 def on_puzzlegrid_focus_in_event (self
, drawarea
, event
):
388 col
= drawarea
.window
.get_colormap ().alloc_color (gtk
.gdk
.Color ("white"))
389 drawarea
.window
.set_background (col
)
392 # callback for drawing the puzzle grid
393 def on_puzzlegrid_expose_event (self
, drawarea
, event
):
394 # if puzzle is loaded
397 drawarea
.set_size_request (self
.puzzle
.cols
*30+2, self
.puzzle
.rows
*30+2)
399 ctx
= drawarea
.window
.cairo_create ()
400 ctx
.set_line_width (1.5)
402 # run through the grid
403 for row
in range (self
.puzzle
.rows
):
404 for col
in range (self
.puzzle
.cols
):
405 # (re)set foreground color
406 ctx
.set_source_rgb (0, 0, 0)
407 # if the area is not occupied
408 if (self
.puzzle
.data
[row
][col
].occupied_across
is False and
409 self
.puzzle
.data
[row
][col
].occupied_down
is False):
410 ctx
.rectangle (col
*30, row
*30, 30, 30)
413 # if selected row/column
414 if row
== self
.selected_row
and col
== self
.selected_col
:
415 ctx
.set_source_rgb (1, 1, 0)
416 ctx
.rectangle (col
*30,row
*30, 30, 30)
419 ctx
.set_source_rgb (1, 1, 1)
420 ctx
.rectangle (col
*30, row
*30, 30, 30)
422 ctx
.set_source_rgb (0, 0, 0)
423 ctx
.rectangle (col
*30, row
*30, 30, 30)
427 if self
.puzzle
.data
[row
][col
].numbered
<> 0:
428 ctx
.set_source_rgb (0, 0, 0)
429 ctx
.select_font_face ("Serif")
430 ctx
.set_font_size (10)
431 ctx
.move_to (col
*30+2, row
*30+10)
432 ctx
.show_text (str(self
.puzzle
.data
[row
][col
].numbered
))
434 # if there is a guessed character at the location
435 # and it is not revealed as a solution
436 if (self
.puzzle
.data
[row
][col
].guess
and
437 self
.puzzle
.data
[row
][col
].revealed
is False and
438 (self
.puzzle
.data
[row
][col
].occupied_across
is True
439 or self
.puzzle
.data
[row
][col
].occupied_down
is True)):
440 ctx
.set_source_rgb (0, 0, 0)
441 ctx
.select_font_face ("Serif", cairo
.FONT_SLANT_NORMAL
,
442 cairo
.FONT_WEIGHT_BOLD
)
443 ctx
.set_font_size (16)
444 ctx
.move_to (col
*30+10, row
*30+20)
445 ctx
.show_text (self
.puzzle
.data
[row
][col
].guess
)
447 # if there is a revealed solution character at the location
448 if (self
.puzzle
.data
[row
][col
].revealed
is True and
449 (self
.puzzle
.data
[row
][col
].occupied_across
is True
450 or self
.puzzle
.data
[row
][col
].occupied_down
is True)):
451 ctx
.set_source_rgb (0, 0, 0.8)
452 ctx
.select_font_face ("Serif", cairo
.FONT_SLANT_NORMAL
,
453 cairo
.FONT_WEIGHT_BOLD
)
454 ctx
.set_font_size (16)
455 ctx
.move_to (col
*30+10, row
*30+20)
456 ctx
.show_text (self
.puzzle
.data
[row
][col
].char
)
460 # load clues to the list
461 def load_clues (self
):
462 # get the clues list store objects
463 across
= self
.ui
.get_object ("clues_across")
464 down
= self
.ui
.get_object ("clues_down")
468 # if puzzle is loaded
470 clues_across
= self
.puzzle
.get_clues_across ()
471 clues_down
= self
.puzzle
.get_clues_down ()
472 # insert the numbers and the clues for across
473 for word
, clue
in clues_across
:
474 across
.append ([str(self
.puzzle
.data
[word
[1]][word
[2]].numbered
),
476 # insert the numbers and the clues for down
477 for word
, clue
in clues_down
:
478 down
.append ([ str(self
.puzzle
.data
[word
[1]][word
[2]].numbered
),
481 def save_file (self
, file):
482 # try to save the file
484 cPickle
.dump (self
.puzzle
, open (file, "wb"), cPickle
.HIGHEST_PROTOCOL
)
485 except (IOError, OSError, cPickle
.PicklingError
):
486 dlg
= gtk
.MessageDialog (self
.window
, gtk
.DIALOG_MODAL
,
487 gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_CLOSE
,
488 "Error in saving puzzle")
493 def open_file (self
, file):
494 # try to open the file
497 self
.puzzle
= cPickle
.load (open (file, "rb"))
498 # assert that it is unfrozen otherwise raise frozen grid exception
499 self
.puzzle
.assert_frozen_grid ()
501 # set selected initial row and column to 0
502 self
.selected_row
= 0
503 self
.selected_col
= 0
504 # set the typing mode to default - across
505 self
.typing_mode
= self
.ACROSS
507 self
.window
.set_title ("GetAClue player - " + file)
510 # handle unpickling, and file errors
511 except (cPickle
.UnpicklingError
, IOError, OSError):
512 dlg
= gtk
.MessageDialog (self
.window
, gtk
.DIALOG_MODAL
,
513 gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_CLOSE
,
514 "Invalid file. Cannot be loaded")
517 # if the puzzle has no words, then it cannot be played obviously
518 except crosswordpuzzle
.NoWordsException
:
520 dlg
= gtk
.MessageDialog (self
.window
, gtk
.DIALOG_MODAL
,
521 gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_CLOSE
,
522 "Word grid has no words. Cannot play")
525 # if the puzzle is not frozen then it cannot be played
526 except crosswordpuzzle
.FrozenGridException
:
528 dlg
= gtk
.MessageDialog (self
.window
, gtk
.DIALOG_MODAL
,
529 gtk
.MESSAGE_ERROR
, gtk
.BUTTONS_CLOSE
,
530 "Word grid is not finalized/frozen. Cannot play")
536 dlg
= gtk
.AboutDialog ()
537 dlg
.set_name ("GetAClue Player")
538 dlg
.set_copyright ("Copyright 2010 V.Harishankar")
539 dlg
.set_website ("http://harishankar.org/software")
540 dlg
.set_authors (("Harishankar",))
541 dlg
.set_logo (self
.window
.get_icon())
542 dlg
.set_license (self
.LICENSE_TEXT
)
543 dlg
.set_comments ("Create and play Crossword puzzles")
547 def __init__ (self
, file_to_play
= None):
548 # load the user interface
549 self
.ui
= gtk
.Builder ()
551 # Path for the interface file - change this if you are distributing
552 # the application and put the icon, interface file in a different
554 gladepath
= os
.path
.join (sys
.path
[0], "playerwindow.glade")
555 self
.ui
.add_from_file (gladepath
)
558 self
.window
= self
.ui
.get_object ("mainwindow")
561 # set the cell renderer for tree views
562 cell
= gtk
.CellRendererText ()
563 tree_acol1
= self
.ui
.get_object ("tree_clues_across").get_column (0)
564 tree_acol2
= self
.ui
.get_object ("tree_clues_across").get_column (1)
565 tree_acol1
.pack_start (cell
)
566 tree_acol1
.add_attribute (cell
, "text", 0)
567 tree_acol2
.pack_start (cell
)
568 tree_acol2
.add_attribute (cell
, "text", 1)
570 tree_down
= self
.ui
.get_object ("tree_clues_down")
571 tree_dcol1
= self
.ui
.get_object ("tree_clues_down").get_column (0)
572 tree_dcol2
= self
.ui
.get_object ("tree_clues_down").get_column (1)
573 tree_dcol1
.pack_start (cell
)
574 tree_dcol1
.add_attribute (cell
, "text", 0)
575 tree_dcol2
.pack_start (cell
)
576 tree_dcol2
.add_attribute (cell
, "text", 1)
578 # connect the signals
579 self
.ui
.connect_signals (self
)
581 # set the puzzle to None
584 # open the file if it is set
586 self
.open_file (file_to_play
)