# -*- coding: utf-8 -*- # vim:set noet ts=4: # # fitx # # Copyright (c) 2008 Loya Liu # # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2 of the License, or (at your option) any later version. # # This library 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 Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this program; if not, write to the # Free Software Foundation, Inc., 59 Temple Place, Suite 330, # Boston, MA 02111-1307 USA # # $Id$ # import scim import gtk from gtk import gdk import os import sys try: import FitxConfig except: pathname = os.path.dirname (__file__) path = os.path.abspath(pathname) path = os.path.join (path, "../../engine/FunInputToy") #path = os.path.join (path, "../python-engine") path = os.path.abspath(path) sys.path.append(path) import FitxConfig import FitxUtil from gettext import dgettext _ = lambda a : dgettext ("fitx", a) RGB_COLOR = lambda c : ((c.red & 0xff00) << 8) + (c.green & 0xff00) + ((c.blue & 0xff00) >> 8) GDK_COLOR = lambda c : gdk.Color (((c >> 16) & 0xff) * 256, ((c >> 8) & 0xff) * 256, (c & 0xff) * 256) RGB = lambda r, g, b : ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff) class FitxSetupUI (gtk.VBox): def __init__ (self): gtk.VBox.__init__ (self) # init value config_path=os.path.expanduser("~/.fit/fitd.conf") #config_path=os.path.expanduser("../python-engine/fitd.conf") self._fitx_config=FitxConfig.FitxConfig(config_path) self._modes=['PY','BH','WBX'] self._fuzzy_pinyin=['an-ang','c-ch','en-eng','f-h','ian-iang','in-ing','l-n','s-sh','uan-uang','z-zh'] self._sp_mode_tmp=None # self._phrase_color = RGB (0, 0, 0) # self._user_phrase_color = RGB (0, 0, 0xef) # self._new_phrase_color = RGB (0xef, 0, 0) # self._special_phrase_color = RGB (0, 0xbf, 0) # self._english_phrase_color = RGB (0, 0xbf, 0) # self._error_eng_phrase_color = RGB (0xef, 0, 0) self._create_ui () self.show_all () def get_name (self): return _("FunInputToy") def _create_ui (self): notebook = gtk.Notebook() vbox_inputmode=gtk.VBox() self._rb_inputmode_py = gtk.RadioButton(None, _('PinYin')) #self._rb_inputmode_py.connect('toggled',self.toggle_pinyin,'PY') vbox_inputmode.pack_start(self._rb_inputmode_py,False,False,2) self._rb_inputmode_bh = gtk.RadioButton(self._rb_inputmode_py, _('BiHua')) #self._rb_inputmode_bh.connect('toggled',self.toggle_pinyin,'BH') vbox_inputmode.pack_start(self._rb_inputmode_bh,False,False,2) self._rb_inputmode_wb = gtk.RadioButton(self._rb_inputmode_py, _('WuBi')) #self._rb_inputmode_wb.connect('toggled',self.toggle_pinyin,'WBX') vbox_inputmode.pack_start(self._rb_inputmode_wb,False,False,2) self._rb_inputmode_py.set_active(True) notebook.append_page(vbox_inputmode, gtk.Label(_("Input Mode"))) vbox_pymode_i=gtk.VBox() self._rb_pymode_qp = gtk.RadioButton(None, _('QuanPin')) vbox_pymode_i.pack_start(self._rb_pymode_qp,False,False,2) self._rb_pymode_sp = gtk.RadioButton(self._rb_pymode_qp, _('ShuangPin')) vbox_pymode_i.pack_start(self._rb_pymode_sp,False,False,2) self._rb_pymode_qpsp = gtk.RadioButton(self._rb_pymode_sp, _('QuanPinShuangPin')) vbox_pymode_i.pack_start(self._rb_pymode_qpsp,False,False,2) self._rb_pymode_qp.set_active(True) notebook.append_page(vbox_pymode_i, gtk.Label(_("Pinyin Mode"))) vbox_spmode=gtk.VBox() rb_spmode=None self._rb_spmode_dict={} for x in self._fitx_config._sp_scheme_list: rb_spmode=gtk.RadioButton(rb_spmode, x) rb_spmode.connect('toggled',self.toggle_spmode,x) self._rb_spmode_dict[x]=rb_spmode if not self._sp_mode_tmp: self._sp_mode_tmp=x rb_spmode.set_active(True) vbox_spmode.pack_start(rb_spmode,False,False,2) notebook.append_page(vbox_spmode, gtk.Label(_("ShuangPin Mode"))) vbox_wbmode=gtk.VBox() self._rb_wbmode_normal=gtk.RadioButton(None, _('Normal')) self._rb_wbmode_mix=gtk.RadioButton(self._rb_wbmode_normal, _('Mix')) vbox_wbmode.pack_start(self._rb_wbmode_normal,False,False,2) vbox_wbmode.pack_start(self._rb_wbmode_mix,False,False,2) self._rb_wbmode_normal.set_active(True) notebook.append_page(vbox_wbmode, gtk.Label(_("WuBi Mode"))) self._cb_fuzzy_pinyin_dict={} vbox_funzzy_pinyin=gtk.VBox() for x in self._fuzzy_pinyin: cb_fp=gtk.CheckButton(x) self._cb_fuzzy_pinyin_dict[x]=cb_fp vbox_funzzy_pinyin.pack_start(cb_fp) notebook.append_page(vbox_funzzy_pinyin,gtk.Label(_('Fuzzy PinYin'))) vbox_userdb=gtk.VBox() hbox_userdb_export=gtk.HBox() btn_export_userdb=gtk.Button(_('Export User Dictionary')) btn_export_userdb.connect("clicked", self.export_user_db) hbox_userdb_export.pack_start(btn_export_userdb,False,False,2) hbox_userdb_export.pack_start(gtk.Label(_('Export user dictionary to txt format')),False,False,2) vbox_userdb.pack_start(hbox_userdb_export,False,False,2) hbox_userdb_import=gtk.HBox() btn_import_userdb=gtk.Button(_('Import User Dictionary')) btn_import_userdb.connect("clicked", self.import_user_db) hbox_userdb_import.pack_start(btn_import_userdb,False,False,2) hbox_userdb_import.pack_start(gtk.Label(_('Import user dictionary from txt format')),False,False,2) vbox_userdb.pack_start(hbox_userdb_import,False,False,2) hbox_userdb_clear=gtk.HBox() btn_clear_userdb=gtk.Button(_('Empty User Dictionary')) btn_clear_userdb.connect("clicked", self.clear_user_db) hbox_userdb_clear.pack_start(btn_clear_userdb,False,False,2) hbox_userdb_clear.pack_start(gtk.Label(_('Empty user dictionary')),False,False,2) vbox_userdb.pack_start(hbox_userdb_clear,False,False,2) notebook.append_page(vbox_userdb,gtk.Label(_('User Dictionary'))) vbox_misc = gtk.VBox() self._cb_tradchinese=gtk.CheckButton(_('Tradition chinese output')) vbox_misc.pack_start(self._cb_tradchinese,False,False,2) self._cb_half_punct_after_number=gtk.CheckButton(_('Use half punct after number')) vbox_misc.pack_start(self._cb_half_punct_after_number,False,False,2) self._cb_disable_v_mode=gtk.CheckButton(_('Disable V mode (SP mode will auto disable)')) vbox_misc.pack_start(self._cb_disable_v_mode,False,False,2) self._cb_disable_i_mode=gtk.CheckButton(_('Disable I mode (SP mode will auto disable)')) vbox_misc.pack_start(self._cb_disable_i_mode,False,False,2) self._cb_disable_punct_commit=gtk.CheckButton(_('Disable punct commit')) self._cb_disable_punct_commit.connect('toggled',self._on_disable_punct_commit) vbox_misc.pack_start(self._cb_disable_punct_commit,False,False,2) self._cb_enable_comma_period_commit=gtk.CheckButton(_('Enable commit by comma and period')) vbox_misc.pack_start(self._cb_enable_comma_period_commit,False,False,2) self._cb_enable_minus_equal_commit=gtk.CheckButton(_('Enable commit by minus and equal')) vbox_misc.pack_start(self._cb_enable_minus_equal_commit,False,False,2) self._cb_enable_bracket_page=gtk.CheckButton(_('Enable bracket page up/down')) vbox_misc.pack_start(self._cb_enable_bracket_page,False,False,2) ad_lk = gtk.Adjustment(value=5, lower=4, upper=9, step_incr=1, page_incr=0, page_size=0) vbox_lookup_page_size=gtk.HBox() vbox_lookup_page_size.pack_start(gtk.Label(_('Candidate words page size:')),False,False,2) self._sb_lookup_table_page_size=gtk.SpinButton(adjustment=ad_lk, climb_rate=0.0, digits=0) vbox_lookup_page_size.pack_start(self._sb_lookup_table_page_size,False,False,2) vbox_misc.pack_start(vbox_lookup_page_size,False,False,2) notebook.append_page(vbox_misc,gtk.Label(_('Misc'))) self.pack_start(notebook) def _on_disable_punct_commit(self,w,data=None): if self._cb_disable_punct_commit.get_active(): self._cb_enable_comma_period_commit.hide() self._cb_enable_minus_equal_commit.hide() else: self._cb_enable_comma_period_commit.show() self._cb_enable_minus_equal_commit.show() def toggle_spmode(self,w,data): if w.get_active(): self._sp_mode_tmp=data def export_user_db(self,w,data=None): chooser = gtk.FileChooserDialog(title=_('Export User Dictionary'),action=gtk.FILE_CHOOSER_ACTION_SAVE,buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) chooser.set_current_name('fitx_user_db.txt') ok = chooser.run() filename=None if ok== gtk.RESPONSE_OK: filename=chooser.get_filename() chooser.destroy() if filename:FitxUtil.export_db_txt(filename) def import_user_db(self,w,data=None): chooser = gtk.FileChooserDialog(title=_('Import User Dictionary'),action=gtk.FILE_CHOOSER_ACTION_OPEN,buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) ok = chooser.run() filename=None if ok== gtk.RESPONSE_OK: filename=chooser.get_filename() chooser.destroy() if filename:FitxUtil.import_db_txt(filename) def clear_user_db(self,w,data=None): chooser = gtk.Dialog(title=_('Clear User Dictionary'),parent=None,flags=gtk.DIALOG_MODAL,buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OK,gtk.RESPONSE_OK)) info=gtk.Label(_(' Are you sure to empty your user dictionary? \n\tNote: This action can\'t recover!')) chooser.vbox.pack_start(info,True,True,10) info.show() ok = chooser.run() chooser.destroy() if ok== gtk.RESPONSE_OK: FitxUtil.clear_user_db() def save_config (self, config): if self._rb_inputmode_wb.get_active():self._fitx_config._input_mode='WBX' elif self._rb_inputmode_bh.get_active():self._fitx_config._input_mode='BH' else: self._fitx_config._input_mode='PY' if(self._rb_pymode_qpsp.get_active()):self._fitx_config._pymode='QPSP' elif(self._rb_pymode_sp.get_active()):self._fitx_config._pymode='SP' else: self._fitx_config._pymode='QP' if self._rb_wbmode_mix.get_active():self._fitx_config._wbxmode='Mix' else:self._fitx_config._wbxmode='Normal' for fz,cb_fp in self._cb_fuzzy_pinyin_dict.iteritems(): self._fitx_config._mhpy_setting[fz]=int(cb_fp.get_active()) self._fitx_config._trad_chinese=self._cb_tradchinese.get_active() self._fitx_config._half_punct_after_number = self._cb_half_punct_after_number.get_active() self._fitx_config._enable_i_mode_internal=not self._cb_disable_i_mode.get_active() self._fitx_config._enable_v_mode_internal=not self._cb_disable_v_mode.get_active() self._fitx_config._sp_scheme=self._sp_mode_tmp self._fitx_config._disable_punct_commit=self._cb_disable_punct_commit.get_active() self._fitx_config._enable_comma_period_commit_internal = self._cb_enable_comma_period_commit.get_active() self._fitx_config._enable_minus_equal_commit_internal = self._cb_enable_minus_equal_commit.get_active() self._fitx_config._enable_bracket_page=self._cb_enable_bracket_page.get_active() self._fitx_config._lookup_table_page_size = self._sb_lookup_table_page_size.get_value_as_int() self._fitx_config.save() def load_config (self, config): # read values self._fitx_config.load() if self._fitx_config._input_mode=='WBX':self._rb_inputmode_wb.set_active(True) elif self._fitx_config._input_mode=='BH':self._rb_inputmode_bh.set_active(True) else: self._rb_inputmode_py.set_active(True) if self._fitx_config._pymode=='SP':self._rb_pymode_sp.set_active(True) elif self._fitx_config._pymode=='QPSP':self._rb_pymode_qpsp.set_active(True) else: self._rb_pymode_qp.set_active(True) if self._fitx_config._wbxmode=='Mix':self._rb_wbmode_mix.set_active(True) else:self._rb_wbmode_normal.set_active(True) self._cb_disable_i_mode.set_active(not self._fitx_config._enable_i_mode_internal) self._cb_disable_v_mode.set_active(not self._fitx_config._enable_v_mode_internal) self._sb_lookup_table_page_size.set_value(self._fitx_config._lookup_table_page_size) self._cb_enable_comma_period_commit.set_active(self._fitx_config._enable_comma_period_commit_internal) self._cb_enable_minus_equal_commit.set_active(self._fitx_config._enable_minus_equal_commit_internal) self._cb_enable_bracket_page.set_active(self._fitx_config._enable_bracket_page) self._cb_disable_punct_commit.set_active(self._fitx_config._disable_punct_commit) self._on_disable_punct_commit(self._cb_disable_punct_commit) self._sp_mode_tmp=self._fitx_config._sp_scheme self._rb_spmode_dict[self._sp_mode_tmp].set_active(True) for fz, st in self._fitx_config._mhpy_setting.iteritems(): if st : self._cb_fuzzy_pinyin_dict[fz].set_active(True) else : self._cb_fuzzy_pinyin_dict[fz].set_active(False) self._cb_tradchinese.set_active(self._fitx_config._trad_chinese) self._cb_half_punct_after_number.set_active(self._fitx_config._half_punct_after_number) def query_changed (self): if self._fitx_config._input_mode=='WBX': if not self._rb_inputmode_wb.get_active() : return True elif self._fitx_config._input_mode=='BH': if not self._rb_inputmode_bh.get_active() : return True else: if not self._rb_inputmode_py.get_active() : return True if self._fitx_config._pymode=='SP': if not self._rb_pymode_sp.get_active() :return True elif self._fitx_config._pymode=='QPSP': if not self._rb_pymode_qpsp.get_active() :return True else: if not self._rb_pymode_qp.get_active() :return True if self._fitx_config._wbxmode=='Mix': if not self._rb_wbmode_mix.get_active() :return True else: if not self._rb_wbmode_normal.get_active() :return True if self._sp_mode_tmp<>self._fitx_config._sp_scheme:return True if self._cb_enable_bracket_page.get_active()<>self._fitx_config._enable_bracket_page:return True for fz, st in self._fitx_config._mhpy_setting.iteritems(): if st and (not self._cb_fuzzy_pinyin_dict[fz].get_active()) :return True elif (not st) and self._cb_fuzzy_pinyin_dict[fz].get_active() : return True if self._cb_tradchinese.get_active() <> self._fitx_config._trad_chinese:return True if self._cb_half_punct_after_number.get_active() <> self._fitx_config._half_punct_after_number:return True if self._cb_disable_i_mode.get_active() <> (not self._fitx_config._enable_i_mode_internal):return True if self._cb_disable_v_mode.get_active() <> (not self._fitx_config._enable_v_mode_internal):return True if self._cb_disable_punct_commit.get_active <> self._fitx_config._disable_punct_commit : return True if self._cb_enable_comma_period_commit.get_active <> self._fitx_config._enable_comma_period_commit_internal: return True if self._cb_enable_minus_equal_commit.get_active <> self._fitx_config._enable_minus_equal_commit_internal: return True if(self._sb_lookup_table_page_size.get_value_as_int()<>self._fitx_config._lookup_table_page_size): return True return False def test_save(widget,ui): ui.query_changed() ui.save_config(None) if __name__ == "__main__": window = gtk.Window () setupui = FitxSetupUI () setupui.load_config(None) b=gtk.VBox() b.pack_start(setupui,False,False,2) save=gtk.Button('Save') save.connect("clicked", test_save, setupui) b.pack_start(save,False,False,2) window.add (b) window.show_all () gtk.main ()