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 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
)
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"))
185 for button_class
in (AddFromFile
, AddByJouyou
, AddByJlpt
, AddByFreq
):
186 button
= button_class(edit_box
)
187 box
.pack_start(button
, expand
=False)
191 class SortFrame(ShadowedFrame
):
193 def __init__(self
, edit_box
):
194 ShadowedFrame
.__init
__(self
, _("Sort Kanji"))
196 for button_class
in (SortByJouyou
, SortByJlpt
, SortByFreq
):
197 button
= button_class(edit_box
)
198 box
.pack_start(button
, expand
=False)
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
)
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
)
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)
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
):
260 def on_ok_clicked(self
, widget
):
262 self
.response(gtk
.RESPONSE_OK
)