2 # -*- coding: utf-8 -*-
4 # Project: J-Ben, Python front-end
5 # File: jben/gui/dialog/kanjilisteditor.py
7 # Created on: 26 Nov 2008
9 from __future__
import absolute_import
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
):
22 gtk
.TextView
.__init
__(self
)
23 self
.set_accepts_tab(False)
24 self
.set_wrap_mode(gtk
.WRAP_WORD_CHAR
)
28 self
.get_buffer().connect("changed", self
.on_text_changed
)
30 def on_text_changed(self
, widget
):
31 print "EditBox.on_text_changed"
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()
44 if isinstance(parent
, gtk
.Window
):
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(
65 gtk
.STOCK_CANCEL
, gtk
.RESPONSE_CANCEL
,
66 gtk
.STOCK_OK
, gtk
.RESPONSE_OK
))
68 if result
== gtk
.RESPONSE_OK
:
69 filename
= dialog
.get_filename()
70 if isinstance(filename
, str):
71 print "FILE SELECTED:", repr(filename
)
73 # Make a set of all unique characters
74 # Limit characters to Japanese characters
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
)
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
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
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." \
127 # Get kanji in specified range
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
)
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"))
195 for button_class
in (AddFromFile
, AddByJouyou
, AddByJlpt
, AddByFreq
):
196 button
= button_class(edit_box
)
197 box
.pack_start(button
, expand
=False)
201 class SortFrame(ShadowedFrame
):
203 def __init__(self
, edit_box
):
204 ShadowedFrame
.__init
__(self
, _("Sort Kanji"))
206 for button_class
in (SortByJouyou
, SortByJlpt
, SortByFreq
):
207 button
= button_class(edit_box
)
208 box
.pack_start(button
, expand
=False)
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
)
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
)
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)
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
):
270 def on_ok_clicked(self
, widget
):
272 self
.response(gtk
.RESPONSE_OK
)