Fixed JLPT dialog. Linked in dialog to the KanjiListEditor.
[jben2_gui.git] / python / jben / interface / gtk / dialog / kanjilisteditor.py
blob04309876c48a1b79e17a108fc220b91ad8bbe3bb
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # Project: J-Ben, Python front-end
5 # File: jben/gui/dialog/kanjilisteditor.py
6 # Author: Paul Goins
7 # Created on: 26 Nov 2008
9 from __future__ import absolute_import
11 import gtk
12 from ..widget.storedsize import StoredSizeDialog
14 from .addkanjibyjouyou import AddKanjiByJouyouDialog
15 from .addkanjibyjlpt import AddKanjiByJlptDialog
16 #from .addkanjibyfreq import AddKanjiByFreqDialog
19 class EditBox(gtk.TextView):
21 def __init__(self):
22 gtk.TextView.__init__(self)
23 self.set_accepts_tab(False)
24 self.set_wrap_mode(gtk.WRAP_WORD_CHAR)
26 self.modified = False
28 self.get_buffer().connect("changed", self.on_text_changed)
30 def on_text_changed(self, widget):
31 print "EditBox.on_text_changed"
32 self.modified = True
35 class BaseButton(gtk.Button):
37 def __init__(self, label, edit_box):
38 gtk.Button.__init__(self, label)
39 self.connect("clicked", self.on_clicked, edit_box)
41 def _get_parent_window(self):
42 parent = self.get_parent()
43 while True:
44 if isinstance(parent, gtk.Window):
45 return parent
46 elif parent == None:
47 raise Exception(
48 "Expected parent object, but found None instead.")
49 parent = parent.get_parent()
51 def on_clicked(self, widget, edit_box):
52 print "BaseButton clicked"
55 class AddFromFile(BaseButton):
57 def __init__(self, edit_box):
58 BaseButton.__init__(self, _("From _File"), edit_box)
60 def on_clicked(self, widget, edit_box):
61 parent=self._get_parent_window()
62 dialog = gtk.FileChooserDialog(
63 parent=parent,
64 buttons=(
65 gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
66 gtk.STOCK_OK, gtk.RESPONSE_OK))
67 result = dialog.run()
68 if result == gtk.RESPONSE_OK:
69 filename = dialog.get_filename()
70 if isinstance(filename, str):
71 print "FILE SELECTED:", repr(filename)
72 # Open file
73 # Make a set of all unique characters
74 # Limit characters to Japanese characters
75 # Update edit box
76 dialog.destroy()
79 class AddByJouyou(BaseButton):
81 def __init__(self, edit_box):
82 BaseButton.__init__(self, _("By Jouyou _Grade"), edit_box)
84 def on_clicked(self, widget, edit_box):
85 parent = self._get_parent_window()
86 dialog = AddKanjiByJouyouDialog(parent)
87 result = dialog.run()
88 if result == gtk.RESPONSE_OK:
89 low, high = dialog.get_grades()
90 print "TODO: Add kanji from Jouyou grades %d to %d" % (low, high)
91 # Get kanji in specified range
92 # Merge lists
93 # Update edit box
94 dialog.destroy()
96 class AddByJlpt(BaseButton):
98 def __init__(self, edit_box):
99 BaseButton.__init__(self, _("By _JLPT Level"), edit_box)
101 def on_clicked(self, widget, edit_box):
102 parent = self._get_parent_window()
103 dialog = AddKanjiByJlptDialog(parent)
104 result = dialog.run()
105 if result == gtk.RESPONSE_OK:
106 low, high = dialog.get_grades()
107 print "TODO: Add kanji from JLPT grades %d to %d" % (low, high)
108 # Get kanji in specified range
109 # Merge lists
110 # Update edit box
111 dialog.destroy()
114 class AddByFreq(BaseButton):
116 def __init__(self, edit_box):
117 BaseButton.__init__(self, _("By Fre_quency"), edit_box)
119 def on_clicked(self, widget, edit_box):
120 print "add.by_freq clicked"
123 class SortByJouyou(BaseButton):
125 def __init__(self, edit_box):
126 BaseButton.__init__(self, _("By Jouyou G_rade"), edit_box)
128 def on_clicked(self, widget, edit_box):
129 print "sort.by_jouyou clicked"
132 class SortByJlpt(BaseButton):
134 def __init__(self, edit_box):
135 BaseButton.__init__(self, _("By J_LPT Level"), edit_box)
137 def on_clicked(self, widget, edit_box):
138 print "sort.by_jlpt clicked"
141 class SortByFreq(BaseButton):
143 def __init__(self, edit_box):
144 BaseButton.__init__(self, _("By Freq_uency"), edit_box)
146 def on_clicked(self, widget, edit_box):
147 print "sort.by_freq clicked"
150 class DirectEdit(gtk.CheckButton):
152 def __init__(self, edit_box):
153 gtk.CheckButton.__init__(self, _("_Edit directly"))
154 self.connect("toggled", self.on_toggled, edit_box)
156 # Call once to set initial status appropriately.
157 self.on_toggled(self, edit_box)
159 def on_toggled(self, widget, edit_box):
160 state = widget.get_active()
161 edit_box.set_sensitive(state)
164 class EditWindow(gtk.ScrolledWindow):
166 def __init__(self, edit_box):
167 gtk.ScrolledWindow.__init__(self)
168 self.set_shadow_type(gtk.SHADOW_IN)
169 self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
170 self.add(edit_box)
173 class ShadowedFrame(gtk.Frame):
175 def __init__(self, *args, **kwargs):
176 gtk.Frame.__init__(self, *args, **kwargs)
177 self.set_shadow_type(gtk.SHADOW_IN)
180 class AddFrame(ShadowedFrame):
182 def __init__(self, edit_box):
183 ShadowedFrame.__init__(self, _("Add Kanji"))
184 box = gtk.VBox()
185 for button_class in (AddFromFile, AddByJouyou, AddByJlpt, AddByFreq):
186 button = button_class(edit_box)
187 box.pack_start(button, expand=False)
188 self.add(box)
191 class SortFrame(ShadowedFrame):
193 def __init__(self, edit_box):
194 ShadowedFrame.__init__(self, _("Sort Kanji"))
195 box = gtk.VBox()
196 for button_class in (SortByJouyou, SortByJlpt, SortByFreq):
197 button = button_class(edit_box)
198 box.pack_start(button, expand=False)
199 self.add(box)
202 class SideButtons(gtk.VBox):
204 def __init__(self, edit_box):
205 gtk.VBox.__init__(self, spacing=5)
207 add_frame = AddFrame(edit_box)
208 sort_frame = SortFrame(edit_box)
210 for frame in (add_frame, sort_frame):
211 self.pack_start(frame, expand=False)
213 direct_edit = DirectEdit(edit_box)
214 align = gtk.Alignment(xalign=1.0)
215 align.add(direct_edit)
216 self.pack_start(align)
219 class DialogKanjiListEditor(StoredSizeDialog):
220 def __init__(self, parent):
221 StoredSizeDialog.__init__(self, "gui.kanjilisteditor.size", -1, -1,
222 _("Kanji List Editor"), parent)
224 edit_box = EditBox()
226 edit_window = EditWindow(edit_box)
227 side_buttons = SideButtons(edit_box)
229 main_box = gtk.HBox(spacing=5)
230 main_box.pack_start(side_buttons, expand=False)
231 main_box.pack_start(edit_window)
233 self.vbox.set_spacing(5)
234 self.vbox.pack_start(main_box)
235 self.vbox.show_all()
237 ok_button = gtk.Button(stock=gtk.STOCK_OK)
238 cancel_button = gtk.Button(stock=gtk.STOCK_CANCEL)
239 apply_button = gtk.Button(stock=gtk.STOCK_APPLY)
241 ok_button.connect("clicked", self.on_ok_clicked)
242 cancel_button.connect("clicked", self.on_cancel_clicked)
243 apply_button.connect("clicked", self.on_apply_clicked)
245 for button in cancel_button, apply_button, ok_button:
246 self.action_area.pack_start(button)
247 self.action_area.show_all()
249 self.set_has_separator(False)
251 def apply(self):
252 print "apply()ing changes"
254 def on_cancel_clicked(self, widget):
255 self.response(gtk.RESPONSE_CANCEL)
257 def on_apply_clicked(self, widget):
258 self.apply()
260 def on_ok_clicked(self, widget):
261 self.apply()
262 self.response(gtk.RESPONSE_OK)