Added kanji frequency selector and linked it into KanjiListEditor.
[jben2_gui.git] / python / jben / interface / gtk / dialog / kanjilisteditor.py
blob5166a7f990b1e6f95717e12d4863b1ecf884d662
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 parent = self._get_parent_window()
121 dialog = AddKanjiByFreqDialog(parent)
122 result = dialog.run()
123 if result == gtk.RESPONSE_OK:
124 low, high = dialog.get_freq_range()
125 print "TODO: Add kanji between frequency rankings %d and %d." \
126 % (low, high)
127 # Get kanji in specified range
128 # Merge lists
129 # Update edit box
130 dialog.destroy()
133 class SortByJouyou(BaseButton):
135 def __init__(self, edit_box):
136 BaseButton.__init__(self, _("By Jouyou G_rade"), edit_box)
138 def on_clicked(self, widget, edit_box):
139 print "sort.by_jouyou clicked"
142 class SortByJlpt(BaseButton):
144 def __init__(self, edit_box):
145 BaseButton.__init__(self, _("By J_LPT Level"), edit_box)
147 def on_clicked(self, widget, edit_box):
148 print "sort.by_jlpt clicked"
151 class SortByFreq(BaseButton):
153 def __init__(self, edit_box):
154 BaseButton.__init__(self, _("By Freq_uency"), edit_box)
156 def on_clicked(self, widget, edit_box):
157 print "sort.by_freq clicked"
160 class DirectEdit(gtk.CheckButton):
162 def __init__(self, edit_box):
163 gtk.CheckButton.__init__(self, _("_Edit directly"))
164 self.connect("toggled", self.on_toggled, edit_box)
166 # Call once to set initial status appropriately.
167 self.on_toggled(self, edit_box)
169 def on_toggled(self, widget, edit_box):
170 state = widget.get_active()
171 edit_box.set_sensitive(state)
174 class EditWindow(gtk.ScrolledWindow):
176 def __init__(self, edit_box):
177 gtk.ScrolledWindow.__init__(self)
178 self.set_shadow_type(gtk.SHADOW_IN)
179 self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
180 self.add(edit_box)
183 class ShadowedFrame(gtk.Frame):
185 def __init__(self, *args, **kwargs):
186 gtk.Frame.__init__(self, *args, **kwargs)
187 self.set_shadow_type(gtk.SHADOW_IN)
190 class AddFrame(ShadowedFrame):
192 def __init__(self, edit_box):
193 ShadowedFrame.__init__(self, _("Add Kanji"))
194 box = gtk.VBox()
195 for button_class in (AddFromFile, AddByJouyou, AddByJlpt, AddByFreq):
196 button = button_class(edit_box)
197 box.pack_start(button, expand=False)
198 self.add(box)
201 class SortFrame(ShadowedFrame):
203 def __init__(self, edit_box):
204 ShadowedFrame.__init__(self, _("Sort Kanji"))
205 box = gtk.VBox()
206 for button_class in (SortByJouyou, SortByJlpt, SortByFreq):
207 button = button_class(edit_box)
208 box.pack_start(button, expand=False)
209 self.add(box)
212 class SideButtons(gtk.VBox):
214 def __init__(self, edit_box):
215 gtk.VBox.__init__(self, spacing=5)
217 add_frame = AddFrame(edit_box)
218 sort_frame = SortFrame(edit_box)
220 for frame in (add_frame, sort_frame):
221 self.pack_start(frame, expand=False)
223 direct_edit = DirectEdit(edit_box)
224 align = gtk.Alignment(xalign=1.0)
225 align.add(direct_edit)
226 self.pack_start(align)
229 class DialogKanjiListEditor(StoredSizeDialog):
230 def __init__(self, parent):
231 StoredSizeDialog.__init__(self, "gui.kanjilisteditor.size", -1, -1,
232 _("Kanji List Editor"), parent)
234 edit_box = EditBox()
236 edit_window = EditWindow(edit_box)
237 side_buttons = SideButtons(edit_box)
239 main_box = gtk.HBox(spacing=5)
240 main_box.pack_start(side_buttons, expand=False)
241 main_box.pack_start(edit_window)
243 self.vbox.set_spacing(5)
244 self.vbox.pack_start(main_box)
245 self.vbox.show_all()
247 ok_button = gtk.Button(stock=gtk.STOCK_OK)
248 cancel_button = gtk.Button(stock=gtk.STOCK_CANCEL)
249 apply_button = gtk.Button(stock=gtk.STOCK_APPLY)
251 ok_button.connect("clicked", self.on_ok_clicked)
252 cancel_button.connect("clicked", self.on_cancel_clicked)
253 apply_button.connect("clicked", self.on_apply_clicked)
255 for button in cancel_button, apply_button, ok_button:
256 self.action_area.pack_start(button)
257 self.action_area.show_all()
259 self.set_has_separator(False)
261 def apply(self):
262 print "apply()ing changes"
264 def on_cancel_clicked(self, widget):
265 self.response(gtk.RESPONSE_CANCEL)
267 def on_apply_clicked(self, widget):
268 self.apply()
270 def on_ok_clicked(self, widget):
271 self.apply()
272 self.response(gtk.RESPONSE_OK)