-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialogs.py
More file actions
1451 lines (1157 loc) · 51.4 KB
/
Copy pathdialogs.py
File metadata and controls
1451 lines (1157 loc) · 51.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Schach is a basic chess application that uses the Stockfish chess engine.
# Copyright (C) 2021 Samuel Matzko
# This file is part of Schach.
# This program 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.
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
# or see <http://www.gnu.org/licenses/>
"""The main dialogs."""
import gi
import io
import json
import string
import chess
import chess.dcn
import chessboards
import messagedialogs
gi.require_version("Gdk", "3.0")
gi.require_version("Gtk", "3.0")
from constants import *
from gi.repository import Gtk, Gdk, GdkPixbuf
# The text for the KeybindingsDialog's expander label
EXPANDER_TEXT = """Enter a keyboard shortcut.
Do be aware that these will not be checked for validity!
Gtk uses keyboard shortcuts in this format: <Primary>R ("Primary" means
"Control"), F11, <Primary><Alt>R, <Primary><Shift><Alt>R."""
class _Dialog(Gtk.Dialog):
"""The base class for the dialogs."""
def __init__(self, *args, **kwargs):
Gtk.Dialog.__init__(self, *args, **kwargs)
self.set_resizable(False)
# Connect "escape" so that the dialog closes
self.connect("key-press-event", self._on_key_press)
self.connect("delete-event", self._destroy)
def _destroy(self, *args):
"""Destroy the dialog."""
self.destroy()
def _on_key_press(self, widget, event):
"""Close the dialog if the key pressed was "escape"."""
if event.keyval == Gdk.KEY_Escape:
self._destroy()
class _ShortcutsListBoxRow(Gtk.ListBoxRow):
"""A ListBoxRow for the keyboard shortcuts."""
def __init__(self, *args, parent, action, keybinding, func, **kwargs):
Gtk.ListBoxRow.__init__(self, *args, **kwargs)
self.parent = parent
self.action = action
# The main box
self.box = Gtk.HBox()
self.add(self.box)
# The label
actions = json.load(open(MENU_OPTIONS))
action = actions[action]
self.label = Gtk.Label(label=action)
self.box.pack_start(self.label, False, True, 0)
# The function to call when the user sets a shortcut
self.func = func
# The model button's label
try:
self.modelbutton_label = keybinding[0].upper()
except IndexError:
self.modelbutton_label = ""
self.modelbutton_label = self.modelbutton_label.replace("<PRIMARY>", "Ctrl+")
self.modelbutton_label = self.modelbutton_label.replace("<SHIFT>", "Shift+")
self.modelbutton_label = self.modelbutton_label.replace("<ALT>", "Alt+")
# The shortcut button
self.modelbutton = Gtk.ModelButton(label=self.modelbutton_label)
self.modelbutton.connect("clicked", self.show_shortcut_dialog)
self.box.pack_end(self.modelbutton, False, True, 0)
self.box.show_all()
def set_shortcut(self, shortcut):
"""Set the shortcut to SHORTCUT."""
self.modelbutton_label = shortcut.upper()
self.modelbutton_label = self.modelbutton_label.replace("<PRIMARY>", "Ctrl+")
self.modelbutton_label = self.modelbutton_label.replace("<SHIFT>", "Shift+")
self.modelbutton_label = self.modelbutton_label.replace("<ALT>", "Alt+")
self.modelbutton.set_label(self.modelbutton_label)
self.func(self.action, shortcut)
def show_shortcut_dialog(self, event):
response, shortcut = KeybindingsDialog(self.parent).show_dialog()
if response == Gtk.ResponseType.OK:
humanreadable_shortcut = shortcut.upper()
humanreadable_shortcut = humanreadable_shortcut.replace("<PRIMARY>", "Ctrl+")
humanreadable_shortcut = humanreadable_shortcut.replace("<SHIFT>", "Shift+")
humanreadable_shortcut = humanreadable_shortcut.replace("<ALT>", "Alt+")
if not humanreadable_shortcut.endswith("+"):
self.set_shortcut(shortcut)
else:
messagedialogs.show_info(
self.parent,
"Invalid Shortcut",
'Shortcut "%s" does not have a final keystroke.' % humanreadable_shortcut
)
class AboutDialog:
"""The class that handles the about dialog."""
def __init__(self, parent, info):
self.dialog = Gtk.AboutDialog(
transient_for=parent,
modal=True
)
self.dialog.set_artists(info["artists"])
self.dialog.set_authors(info["authors"])
self.dialog.set_comments(info["comments"])
self.dialog.set_copyright(info["copyright"])
self.dialog.set_license(info["license"])
pixbuf = GdkPixbuf.Pixbuf().new_from_file_at_size(info["logo"], 100, 100)
pixbuf
self.dialog.set_logo(pixbuf)
self.dialog.set_program_name(info["program_name"])
self.dialog.set_version(info["version"])
def present(self):
self.dialog.present()
class BoardSetupDialog(_Dialog):
"""A dialog to get a setup board from the user."""
def __init__(self, parent):
_Dialog.__init__(
self,
title="Board Setup",
transient_for=parent,
modal=True
)
# The area to which we can add everything
self.area = self.get_content_area()
self.BOARD_ORDER = []
for r in NUMBERS:
for c in LETTERS:
self.BOARD_ORDER.append("%s%s" % (c, r))
# Add the buttons
buttons = (
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL),
(Gtk.STOCK_OK, Gtk.ResponseType.OK)
)
for button in buttons:
self.add_button(*button)
# The current piece to be adding to the board
self.current_piece = "P"
# Whether the board is valid
self.board_is_valid = True
# The board
self.board = chess.Board()
# Create the dialog
self._create_dialog()
self.show_all()
def _create_board_settings(self):
"""Add all the settings widgets."""
# The box
self.board_settings_box = Gtk.VBox()
self.secondary_box.pack_end(self.board_settings_box, True, True, 5)
# The piece buttons and boxes
self.b1 = Gtk.HBox()
self.board_settings_box.pack_start(self.b1, False, True, 5)
self.b2 = Gtk.HBox()
self.board_settings_box.pack_start(self.b2, False, True, 5)
self.b3 = Gtk.HBox()
self.board_settings_box.pack_start(self.b3, False, True, 5)
self.b4 = Gtk.HBox()
self.board_settings_box.pack_start(self.b4, False, True, 5)
self.b5 = Gtk.HBox()
self.board_settings_box.pack_start(self.b5, False, True, 5)
self.b6 = Gtk.HBox()
self.board_settings_box.pack_start(self.b6, False, True, 5)
self.pawn_w_button = Gtk.Button()
self.pawn_w_button_image = Gtk.Image.new_from_file(IMAGE_P)
self.pawn_w_button.set_image(self.pawn_w_button_image)
self.pawn_w_button.connect("clicked", self._on_button_toggle, "P")
self.b1.pack_start(self.pawn_w_button, False, False, 3)
self.knight_w_button = Gtk.Button()
self.knight_w_button_image = Gtk.Image.new_from_file(IMAGE_N)
self.knight_w_button.set_image(self.knight_w_button_image)
self.knight_w_button.connect("clicked", self._on_button_toggle, "N")
self.b2.pack_start(self.knight_w_button, False, False, 3)
self.bishop_w_button = Gtk.Button()
self.bishop_w_button_image = Gtk.Image.new_from_file(IMAGE_B)
self.bishop_w_button.set_image(self.bishop_w_button_image)
self.bishop_w_button.connect("clicked", self._on_button_toggle, "B")
self.b3.pack_start(self.bishop_w_button, False, False, 3)
self.rook_w_button = Gtk.Button()
self.rook_w_button_image = Gtk.Image.new_from_file(IMAGE_R)
self.rook_w_button.set_image(self.rook_w_button_image)
self.rook_w_button.connect("clicked", self._on_button_toggle, "R")
self.b4.pack_start(self.rook_w_button, False, False, 3)
self.queen_w_button = Gtk.Button()
self.queen_w_button_image = Gtk.Image.new_from_file(IMAGE_Q)
self.queen_w_button.set_image(self.queen_w_button_image)
self.queen_w_button.connect("clicked", self._on_button_toggle, "Q")
self.b5.pack_start(self.queen_w_button, False, False, 3)
self.king_w_button = Gtk.Button()
self.king_w_button_image = Gtk.Image.new_from_file(IMAGE_K)
self.king_w_button.set_image(self.king_w_button_image)
self.king_w_button.connect("clicked", self._on_button_toggle, "K")
self.b6.pack_start(self.king_w_button, False, False, 3)
self.pawn_b_button = Gtk.Button()
self.pawn_b_button_image = Gtk.Image.new_from_file(IMAGE_p)
self.pawn_b_button.set_image(self.pawn_b_button_image)
self.pawn_b_button.connect("clicked", self._on_button_toggle, "p")
self.b1.pack_start(self.pawn_b_button, False, False, 3)
self.knight_b_button = Gtk.Button()
self.knight_b_button_image = Gtk.Image.new_from_file(IMAGE_n)
self.knight_b_button.set_image(self.knight_b_button_image)
self.knight_b_button.connect("clicked", self._on_button_toggle, "n")
self.b2.pack_start(self.knight_b_button, False, False, 3)
self.bishop_b_button = Gtk.Button()
self.bishop_b_button_image = Gtk.Image.new_from_file(IMAGE_b)
self.bishop_b_button.set_image(self.bishop_b_button_image)
self.bishop_b_button.connect("clicked", self._on_button_toggle, "b")
self.b3.pack_start(self.bishop_b_button, False, False, 3)
self.rook_b_button = Gtk.Button()
self.rook_b_button_image = Gtk.Image.new_from_file(IMAGE_r)
self.rook_b_button.set_image(self.rook_b_button_image)
self.rook_b_button.connect("clicked", self._on_button_toggle, "r")
self.b4.pack_start(self.rook_b_button, False, False, 3)
self.queen_b_button = Gtk.Button()
self.queen_b_button_image = Gtk.Image.new_from_file(IMAGE_q)
self.queen_b_button.set_image(self.queen_b_button_image)
self.queen_b_button.connect("clicked", self._on_button_toggle, "q")
self.b5.pack_start(self.queen_b_button, False, False, 3)
self.king_b_button = Gtk.Button()
self.king_b_button_image = Gtk.Image.new_from_file(IMAGE_k)
self.king_b_button.set_image(self.king_b_button_image)
self.king_b_button.connect("clicked", self._on_button_toggle, "k")
self.b6.pack_start(self.king_b_button, False, False, 3)
# The turn box and its widgets
self.turn_box = Gtk.VBox()
self.b1.pack_end(self.turn_box, True, True, 3)
self.turn_label = Gtk.Label(label="Turn")
self.turn_box.pack_start(self.turn_label, True, True, 3)
self.white_radiobutton = Gtk.RadioButton.new_with_label_from_widget(None, "White")
self.white_radiobutton.connect("toggled", self._on_radiobutton_toggle, "w")
self.turn_box.pack_start(self.white_radiobutton, False, False, 3)
self.black_radiobutton = Gtk.RadioButton.new_with_label_from_widget(self.white_radiobutton, "Black")
self.black_radiobutton.connect("toggled", self._on_radiobutton_toggle, "b")
self.turn_box.pack_end(self.black_radiobutton, False, False, 3)
# The list of buttons
self.buttons = [
self.pawn_w_button,
self.knight_w_button,
self.bishop_w_button,
self.rook_w_button,
self.queen_w_button,
self.king_w_button,
self.pawn_b_button,
self.knight_b_button,
self.bishop_b_button,
self.rook_b_button,
self.queen_b_button,
self.king_b_button
]
for button in self.buttons:
button.set_relief(Gtk.ReliefStyle.NONE)
self.pawn_w_button.set_relief(Gtk.ReliefStyle.HALF)
def _create_dialog(self):
"""Add all the widgets to the dialog."""
# The main box
self.main_box = Gtk.VBox()
self.area.add(self.main_box)
# The secondary box
self.secondary_box = Gtk.HBox()
self.main_box.add(self.secondary_box)
# The chessboard
self.chessboard = chessboards.SetupChessBoard(self)
self.chessboard.from_board(self.board)
self.chessboard.bind_place(self._place_func)
# The extra box for the chessboard
self.chessboard_box = Gtk.VBox()
self.secondary_box.pack_start(self.chessboard_box, False, False, 5)
self.chessboard_box.pack_start(self.chessboard, False, False, 5)
self.fen_box = Gtk.HBox()
self.main_box.pack_end(self.fen_box, True, True, 3)
# The buttons for pasting a fen to the fen entry
self.paste_fen_button = Gtk.Button(label="Paste fen")
self.paste_fen_button.connect("clicked", self._paste_fen)
self.fen_box.pack_start(self.paste_fen_button, False, False, 3)
# The entry containing the fen
self.fen_entry = Gtk.Entry()
self.fen_entry.set_editable(False)
self.fen_entry.set_tooltip_text("Board FEN")
self.fen_box.pack_start(self.fen_entry, True, True, 3)
# The board settings buttons
self._create_board_settings()
self._update()
def _get_chess_piece(self, piece):
"""Return a chess.Piece instance for string PIECE."""
if piece == "K":
return chess.Piece(chess.KING, chess.WHITE)
elif piece == "Q":
return chess.Piece(chess.QUEEN, chess.WHITE)
elif piece == "R":
return chess.Piece(chess.ROOK, chess.WHITE)
elif piece == "B":
return chess.Piece(chess.BISHOP, chess.WHITE)
elif piece == "N":
return chess.Piece(chess.KNIGHT, chess.WHITE)
elif piece == "P":
return chess.Piece(chess.PAWN, chess.WHITE)
elif piece == "k":
return chess.Piece(chess.KING, chess.BLACK)
elif piece == "q":
return chess.Piece(chess.QUEEN, chess.BLACK)
elif piece == "r":
return chess.Piece(chess.ROOK, chess.BLACK)
elif piece == "b":
return chess.Piece(chess.BISHOP, chess.BLACK)
elif piece == "n":
return chess.Piece(chess.KNIGHT, chess.BLACK)
elif piece == "p":
return chess.Piece(chess.PAWN, chess.BLACK)
else:
return chess.Piece(chess.BB_EMPTY, chess.WHITE)
def _on_button_toggle(self, button, piece):
"""Handle events when a button is toggled."""
for b in self.buttons:
b.set_relief(Gtk.ReliefStyle.NONE)
button.set_relief(Gtk.ReliefStyle.HALF)
self.current_piece = piece
def _on_radiobutton_toggle(self, button, color):
"""Set the board's turn to the color given."""
if color == "w":
self.board.turn = chess.WHITE
else:
self.board.turn = chess.BLACK
self._update()
def _paste_fen(self, *args):
"""Paste a fen into the fen entry."""
clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
fen = clipboard.wait_for_text()
self.board = chess.Board(fen)
self.chessboard.from_board(self.board)
self._update()
def _place_func(self, square):
"""The method to be called when the chessboard is clicked."""
ignore, piece = self.chessboard.convert_square_to_image(square)
if piece == self.current_piece:
self.chessboard.place(".", square)
self.board.set_piece_at(self.BOARD_ORDER.index(square), self._get_chess_piece("."))
else:
self.chessboard.place(self.current_piece, square)
self.board.set_piece_at(self.BOARD_ORDER.index(square), self._get_chess_piece(self.current_piece))
self._update()
def _update(self):
"""Update everything."""
self.fen_entry.set_text(self.board.fen())
status = self.board.status()
if chess.Status.TOO_MANY_KINGS in status:
self.fen_entry.set_text("Too many kings")
self.board_is_valid = False
elif chess.Status.TOO_MANY_WHITE_PIECES in status:
self.fen_entry.set_text("Too many white pieces")
elif chess.Status.TOO_MANY_BLACK_PIECES in status:
self.fen_entry.set_text("Too many black pieces")
elif chess.Status.TOO_MANY_WHITE_PAWNS in status:
self.fen_entry.set_text("Too many white pawns")
elif chess.Status.TOO_MANY_BLACK_PAWNS in status:
self.fen_entry.set_text("Too many black pawns")
elif chess.Status.PAWNS_ON_BACKRANK in status:
self.fen_entry.set_text("Pawns are not allowed on backrank")
elif chess.Status.NO_WHITE_KING in status:
self.fen_entry.set_text("White must have one king")
self.board_is_valid = False
elif chess.Status.NO_BLACK_KING in status:
self.fen_entry.set_text("Black must have one king")
self.board_is_valid = False
else:
self.board_is_valid = True
def show_dialog(self):
"""Show the dilaog."""
response = self.run()
self.destroy()
return response, self.board, self.board_is_valid
class CalendarDialog(_Dialog):
"""A dialog to get a date response from the user using a calendar."""
def __init__(self, parent):
_Dialog.__init__(
self,
title="Choose a date",
transient_for=parent,
modal=True
)
# The area to which we can add the calendar
self.area = self.get_content_area()
# The calendar
self.calendar = Gtk.Calendar()
self.area.add(self.calendar)
# The buttons
buttons = (
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL),
(Gtk.STOCK_OK, Gtk.ResponseType.OK)
)
# Add the buttons
for button in buttons:
self.add_button(button[0], button[1])
self.show_all()
def show_dialog(self):
"""Show the dialog."""
# Run the dialog
response = self.run()
# Get the date from the calendar
dateorg = self.calendar.get_date()
# Set the month to one ahead, because the calendar returns
# January as 0, February as 1, etc.
date = (dateorg[0], dateorg[1] + 1, dateorg[2])
# Destory us
self.destroy()
# Return the response and date
return response, date
class FileOpen:
"""An Open File dialog
Valid options: initialdir, parent, title"""
def __init__(self, **kw):
try: self.title = kw["title"]
except:
self.title = "Open File"
try: self.initialdir = kw["initialdir"]
except:
self.initialdir = None
try: self.parent = kw["parent"]
except:
self.parent = None
try: self.filters = kw["filters"]
except:
self.filters = None
# The dialog
self.dialog = Gtk.FileChooserDialog(title=self.title, parent=self.parent, transient_for=self.parent,
modal=True, action=Gtk.FileChooserAction.OPEN)
self.dialog.add_buttons("Cancel", Gtk.ResponseType.CANCEL, "OK", Gtk.ResponseType.ACCEPT)
self.dialog.set_action(Gtk.FileChooserAction.OPEN)
if self.initialdir:
self.dialog.set_current_folder(self.initialdir)
if self.filters:
for f in self.filters:
self.dialog.add_filter(f)
self.dialog.set_select_multiple(False)
self.dialog.connect("response", self.callback)
def callback(self, dialog=None, response=None, whoknows=None):
"""The method to be called when the user responds to the file dialog."""
self.filenames = self.dialog.get_filenames()
self.response = response
self.dialog.destroy()
def show(self):
"""Show the dialog."""
self.dialog.show()
while self.dialog.get_mapped():
Gtk.main_iteration()
if self.response == Gtk.ResponseType.ACCEPT:
return self.filenames[0]
else:
return None
class FileSaveAs:
"""A Save File As dialog.
Valid options: initialdir, parent"""
def __init__(self, **kw):
try: self.initialdir = kw["initialdir"]
except:
self.initialdir = None
try: self.parent = kw["parent"]
except:
self.parent = None
try: self.filters = kw["filters"]
except:
self.filters = None
self.dialog = Gtk.FileChooserDialog(parent=self.parent, transient_for=self.parent,
modal=True, action=Gtk.FileChooserAction.SAVE)
self.dialog.add_buttons("Cancel", Gtk.ResponseType.CANCEL, "Save", Gtk.ResponseType.ACCEPT)
self.dialog.set_action(Gtk.FileChooserAction.SAVE)
self.dialog.set_do_overwrite_confirmation(True)
if self.initialdir:
self.dialog.set_current_folder(self.initialdir)
if self.filters:
for f in self.filters:
self.dialog.add_filter(f)
self.dialog.set_select_multiple(False)
self.dialog.connect('response', self.callback)
self.dialog.show()
def callback(self, dialog=None, response=None, whoknows=None):
"""The method to be called when the user responds to the file dialog."""
self.filenames = self.dialog.get_filenames()
self.response = response
self.dialog.destroy()
def show(self):
"""Show the dialog."""
self.dialog.show()
while self.dialog.get_mapped():
Gtk.main_iteration()
if self.response == Gtk.ResponseType.ACCEPT:
return self.filenames[0]
else:
return None
class GameSelectorDialog(_Dialog):
"""The dialog to prompt the user to choose a game from a loaded file."""
def __init__(self, parent, games):
_Dialog.__init__(
self,
title="Select a Game",
transient_for=parent,
modal=True
)
self.set_default_size(500, 300)
# The area to which we can add the list of games in the file
self.area = self.get_content_area()
# The games file
self.games_file = games
# The list of chess.dcn.Game instances
self.games = games
# The listbox for the games
self.listbox = Gtk.ListBox()
self.listbox.set_selection_mode(Gtk.SelectionMode.SINGLE)
self.listbox.connect("row-activated", self._on_row_activated)
self.listbox_box = Gtk.ScrolledWindow()
self.listbox_box.add(self.listbox)
self.area.pack_start(self.listbox_box, True, True, 0)
# The list of rows
self.rows = []
# Add the games to the listbox
for game in self.games:
row = Gtk.ListBoxRow()
self.rows.append(row)
row.game_instance = game
hbox = Gtk.HBox()
hbox.add(
Gtk.Label(
label="%s vs. %s" % (game.headers["White"], game.headers["Black"])
)
)
hbox.show_all()
row.add(hbox)
self.listbox.add(row)
self.listbox.select_row(self.rows[0])
self.game_selected = self.games[0]
# Add the buttons
buttons = (
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL),
(Gtk.STOCK_OK, Gtk.ResponseType.OK)
)
for button in buttons:
self.add_button(button[0], button[1])
self.show_all()
def _on_row_activated(self, listbox, row):
self.game_selected = row.game_instance
def show_dialog(self):
# Run us
response = self.run()
# Destroy us
self.destroy()
return response, self.game_selected
class HeadersDialog(_Dialog):
"""The dialog in which users can edit the game's headers.
Valid results for OVERRIDE_RESULT are:
1 - 0, 0 - 1, 1/2 - 1/2, and *"""
def __init__(self, parent, title, override_result=None, headers=None):
_Dialog.__init__(
self,
title=title,
transient_for=parent,
modal=True)
# The area to which we can add everything
self.area = self.get_content_area()
# The box containing the headers
self.headers_box = Gtk.VBox()
# The dictionary containing the headers given by the user
self.headers = None
# The default result vaule; this is to prevent errors
self.result = "*"
# Whether the result of the game has already been determined
self.override_result = override_result
# Create the headers box
self._create_headers_box()
# Set the buttons
self._create_buttons()
# Set the headers
if headers is not None:
self.set_headers(headers)
self.show_all()
def _create_buttons(self):
"""Create the buttons of the dialog."""
# The buttons
buttons = (
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL),
(Gtk.STOCK_SAVE, Gtk.ResponseType.OK)
)
# Add the buttons
for button in buttons:
self.add_button(button[0], button[1])
def _create_headers_box(self):
"""Create all the header editing elements and add them to the header box."""
# The event header
self.event_header_box = Gtk.HBox()
self.area.pack_start(self.event_header_box, True, True, 2)
self.event_header_label = Gtk.Label(label="Event: ")
self.event_header_entry = Gtk.Entry()
self.event_header_box.pack_start(self.event_header_label, False, False, 2)
self.event_header_box.pack_end(self.event_header_entry, False, False, 2)
# The site header
self.site_header_box = Gtk.HBox()
self.area.pack_start(self.site_header_box, True, True, 2)
self.site_header_label = Gtk.Label(label="Site: ")
self.site_header_entry = Gtk.Entry()
self.site_header_box.pack_start(self.site_header_label, False, False, 2)
self.site_header_box.pack_end(self.site_header_entry, False, False, 2)
# The date header
self.date_header_box = Gtk.HBox()
self.area.pack_start(self.date_header_box, True, True, 2)
self.date_header_label = Gtk.Label(label="Date:")
self.date_header_box.pack_start(self.date_header_label, False, False, 2)
# The date header entries
self.date_header_entry_box = Gtk.HBox()
Gtk.StyleContext.add_class(self.date_header_entry_box.get_style_context(), "linked")
self.date_header_box.pack_end(self.date_header_entry_box, False, False, 2)
self.date_header_year_entry = Gtk.Entry()
self.date_header_year_entry.set_width_chars(4)
self.date_header_year_entry.set_max_length(4)
self.date_header_year_entry.insert_text("????", 0)
self.date_header_year_entry.connect("event", self._on_entry_edit)
self.date_header_entry_box.pack_start(self.date_header_year_entry, False, False, 0)
self.date_header_entry_box.pack_start(Gtk.Label(label="/"), False, False, 1)
self.date_header_month_entry = Gtk.Entry()
self.date_header_month_entry.set_width_chars(2)
self.date_header_month_entry.set_max_length(2)
self.date_header_month_entry.insert_text("??", 0)
self.date_header_month_entry.connect("event", self._on_entry_edit)
self.date_header_entry_box.pack_start(self.date_header_month_entry, False, False, 0)
self.date_header_entry_box.pack_start(Gtk.Label(label="/"), False, False, 1)
self.date_header_day_entry = Gtk.Entry()
self.date_header_day_entry.set_width_chars(2)
self.date_header_day_entry.set_max_length(2)
self.date_header_day_entry.insert_text("??", 0)
self.date_header_day_entry.connect("event", self._on_entry_edit)
self.date_header_entry_box.pack_start(self.date_header_day_entry, False, False, 0)
# The calendar button
self.calendar_button = Gtk.Button.new_from_icon_name("x-office-calendar-symbolic", 1)
self.calendar_button.connect("clicked", self._on_calendar_clicked)
self.date_header_entry_box.pack_start(self.calendar_button, False, False, 0)
# The round header
self.round_header_box = Gtk.HBox()
self.area.pack_start(self.round_header_box, True, True, 2)
self.round_header_label = Gtk.Label(label="Round: ")
self.round_header_entry = Gtk.Entry()
self.round_header_box.pack_start(self.round_header_label, False, False, 2)
self.round_header_box.pack_end(self.round_header_entry, False, False, 2)
# The white header
self.white_header_box = Gtk.HBox()
self.area.pack_start(self.white_header_box, True, True, 2)
self.white_header_label = Gtk.Label(label="White: ")
self.white_header_entry = Gtk.Entry()
self.white_header_box.pack_start(self.white_header_label, False, False, 2)
self.white_header_box.pack_end(self.white_header_entry, False, False, 2)
# The black header
self.black_header_box = Gtk.HBox()
self.area.pack_start(self.black_header_box, True, True, 2)
self.black_header_label = Gtk.Label(label="Black: ")
self.black_header_entry = Gtk.Entry()
self.black_header_box.pack_start(self.black_header_label, False, False, 2)
self.black_header_box.pack_end(self.black_header_entry, False, False, 2)
# The result header
self.result_header_box = Gtk.HBox()
self.area.pack_start(self.result_header_box, True, True, 2)
self.result_header_label = Gtk.Label(label="Result: ")
self.result_header_box.pack_start(self.result_header_label, False, False, 2)
# The result options
self.results_box = Gtk.HBox()
self.result_header_box.pack_end(self.results_box, True, True, 2)
self.result_w_b = Gtk.RadioButton.new_with_label_from_widget(None, "1 - 0")
self.result_w_b.connect("toggled", self._on_result_set, "0")
self.results_box.pack_start(self.result_w_b, False, False, 2)
self.result_b_w = Gtk.RadioButton.new_from_widget(self.result_w_b)
self.result_b_w.set_label("0 - 1")
self.result_b_w.connect("toggled", self._on_result_set, "1")
self.results_box.pack_start(self.result_b_w, False, False, 2)
self.result_1_2 = Gtk.RadioButton.new_from_widget(self.result_w_b)
self.result_1_2.set_label("1/2 - 1/2")
self.result_1_2.connect("toggled", self._on_result_set, "2")
self.results_box.pack_start(self.result_1_2, False, False, 2)
self.result_unfinished = Gtk.RadioButton.new_from_widget(self.result_w_b)
self.result_unfinished.set_label("*")
self.result_unfinished.connect("toggled", self._on_result_set, "3")
self.results_box.pack_start(self.result_unfinished, False, False, 2)
# Set whether the user is allowed to set the result
if self.override_result is not None:
self.result = self.override_result
# Set the result radiobutton
if self.result == "1 - 0":
self.result_w_b.set_active(True)
elif self.result == "0 - 1":
self.result_b_w.set_active(True)
elif self.result == "1/2 - 1/2":
self.result_1_2.set_active(True)
else:
self.result_unfinished.set_active(True)
# Set the radiobuttons insensitive
self.result_header_box.set_sensitive(False)
def _destroy(self, *args):
"""Close the dialog properly."""
# Get all the info for the headers
self.headers["Event"] = self.event_header_entry.get_text()
self.headers["Site"] = self.site_header_entry.get_text()
self.headers["Date"] = "%s.%s.%s" % (
self.date_header_year_entry.get_text(),
self.date_header_month_entry.get_text(),
self.date_header_day_entry.get_text()
)
self.headers["Round"] = self.round_header_entry.get_text()
self.headers["White"] = self.white_header_entry.get_text()
self.headers["Black"] = self.black_header_entry.get_text()
self.headers["Result"] = self.result
# Destroy us
self.destroy()
def _on_calendar_clicked(self, button):
"""Get the date from the calendar."""
# The calendar dialog
dialog = CalendarDialog(self)
response, date = dialog.show_dialog()
# Set the date labels
self.date_header_year_entry.set_text(str(date[0]))
self.date_header_month_entry.set_text(str(date[1]))
self.date_header_day_entry.set_text(str(date[2]))
def _on_entry_edit(self, w, e):
"""Remove invalid characters from the date entries."""
# If the user has left the entry...
if e.type == Gdk.EventType.LEAVE_NOTIFY or e.type == Gdk.EventType.FOCUS_CHANGE:
# ...for each character in the entry's text...
for l in w.get_text():
# ...if the character is not a digit...
if l not in string.digits and l != "?":
# ...set the entry to the default question marks
w.set_text("?" *w.get_max_length())
break
def _on_result_set(self, radio, name):
if radio.get_active():
if name == "0":
self.result = "1 - 0"
elif name == "1":
self.result = "0 - 1"
elif name == "2":
self.result = "1/2 1/2"
elif name == "3":
self.result = "*"
def set_headers(self, headers):
"""Set the headers to HEADERS."""
self.headers = headers
self.event_header_entry.set_text(self.headers["Event"])
self.site_header_entry.set_text(self.headers["Site"])
year, month, day = self.headers["Date"].split(".")
self.date_header_year_entry.set_text(year)
self.date_header_month_entry.set_text(month)
self.date_header_day_entry.set_text(day)
self.round_header_entry.set_text(self.headers["Round"])
self.white_header_entry.set_text(self.headers["White"])
self.black_header_entry.set_text(self.headers["Black"])
result = self.headers["Result"]
if result == "*":
self.result_unfinished.set_active(True)
elif result == "1-0":
self.result_w_b.set_active(True)
elif result == "0-1":
self.result_b_w.set_active(True)
elif result == "1/2":
self.result_1_2.set_active(True)
self.show_all()
def show_dialog(self):
"""Run the dilaog and return the user's response."""
# Run the dialog
response = self.run()
# Destroy the dialog
self._destroy()
# Return the reply
return response, self.headers
class KeybindingsDialog(_Dialog):
"""The dialog to get a keyboard shortcut from the user."""
def __init__(self, parent):
_Dialog.__init__(
self,
title="Enter a shortcut",
transient_for=parent,
modal=True
)
# The shortcut we're set to
self.shortcut = ""
# Whether the expander is open or not
self.expander_open = False
# The area
self.area = self.get_content_area()
# The boxes
self.back_box = Gtk.VBox()
self.box = Gtk.HBox()
self.back_box.pack_start(self.box, True, True, 3)
self.back_box.show_all()
self.area.add(self.back_box)
self.shift_on = False
self.control_on = False
self.alt_on = False
self.entry_text = ""
buttons = (
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL),
(Gtk.STOCK_OK, Gtk.ResponseType.OK)
)
for button in buttons:
self.add_button(button[0], button[1])
# Create the buttons
self._create_buttons()
# Create the expander
self._create_expander()