PageRenderTime 35ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/python/helper/ZhengJuSetup/ZhengJuUI.py

http://scim-python.googlecode.com/
Python | 240 lines | 190 code | 25 blank | 25 comment | 21 complexity | 0aca1ead35f37dda16496f17e1244093 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. # vim:set noet ts=4:
  3. #
  4. # scim-python
  5. #
  6. # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  7. #
  8. #
  9. # This library is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU Lesser General Public
  11. # License as published by the Free Software Foundation; either
  12. # version 2 of the License, or (at your option) any later version.
  13. #
  14. # This library is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU Lesser General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser General Public
  20. # License along with this program; if not, write to the
  21. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  22. # Boston, MA 02111-1307 USA
  23. #
  24. # $Id: $
  25. #
  26. import scim
  27. import gtk
  28. import os
  29. import sys
  30. import gtk.glade
  31. import gobject
  32. import commands
  33. try:
  34. from PYDict import *
  35. except:
  36. pathname = os.path.dirname (__file__)
  37. path = os.path.abspath(pathname)
  38. print path
  39. path = os.path.join (path, "../../engine/PinYin")
  40. path = os.path.abspath(path)
  41. print path
  42. sys.path.append(path)
  43. from PYDict import *
  44. from gettext import dgettext
  45. _ = lambda a : dgettext ("scim-python", a)
  46. N_ = lambda x : x
  47. gtk.glade.textdomain("scim-python")
  48. shuangpin_schema_list = SHUANGPIN_SCHEMAS.keys()
  49. pinyin_schema_list = [N_("QuanPin"),N_("JianPin"),N_("ShuangPin")]
  50. ConfigBase = "/IMEngine/Python/ZhengJu/"
  51. Pass = lambda get: get
  52. Config = {
  53. "ShuangPinSchema" : ( lambda get: shuangpin_schema_list.index(get) ,
  54. lambda set: shuangpin_schema_list[set],
  55. "MSPY"),
  56. "PinYinSchema" : ( lambda get: pinyin_schema_list.index(get) ,
  57. lambda set: pinyin_schema_list[set],
  58. "JianPin"),
  59. "LargeCharset" :( Pass, Pass, False),
  60. "CreateUserWords" :( Pass, Pass, True),
  61. "CreateUserPhrases" :( Pass, Pass, True),
  62. "AdjustWordFreq" :( Pass, Pass, True),
  63. "ProgressivePrompt" : ( Pass, Pass, True),
  64. "LogConvError": ( Pass, Pass, True),
  65. "SplitPinyin": ( Pass, Pass, True),
  66. "TranslateShuangPin": ( Pass, Pass, True),
  67. "MixedInput": ( Pass, Pass, True),
  68. "FuzzyPinyin": ( Pass, Pass, False),
  69. "FuzzyS_Sh": ( Pass, Pass, True),
  70. "FuzzyC_Ch": ( Pass, Pass, True),
  71. "FuzzyZ_Zh": ( Pass, Pass, True),
  72. "FuzzyL_N": ( Pass, Pass, True),
  73. "FuzzyIn_Ing": ( Pass, Pass, True),
  74. "FuzzyEn_Eng": ( Pass, Pass, True),
  75. "FuzzyAn_Ang": ( Pass, Pass, True)
  76. }
  77. class ZhengJuSetupUI ():
  78. def __init__ (self, helper_info):
  79. self._helper_info = helper_info
  80. self._helper_agent = scim.HelperAgent ()
  81. self.orgin_value = {}
  82. def _create_ui (self):
  83. pathname = os.path.dirname (__file__)
  84. path = os.path.abspath(pathname)
  85. self.xml = gtk.glade.XML(path+'/ZhengJuUI.glade')
  86. self.xml.signal_autoconnect (self)
  87. self.window = self.xml.get_widget ("window_main")
  88. gtk_pinin_schema = self.xml.get_widget("PinYinSchema")
  89. liststore = gtk.ListStore(str)
  90. for k in pinyin_schema_list:
  91. liststore.append([_(k)])
  92. gtk_pinin_schema.set_model(liststore)
  93. gtk_shuangpin_schema = self.xml.get_widget("ShuangPinSchema")
  94. liststore = gtk.ListStore(str)
  95. for k in shuangpin_schema_list:
  96. liststore.append([_(k)])
  97. gtk_shuangpin_schema.set_model(liststore)
  98. self.window.show_all ()
  99. def get_name (self):
  100. return _("ZhengJu")
  101. def on_ClearUserWords_clicked(self, button):
  102. import ZhengJuDB
  103. db = ZhengJuDB.ZhengJuDB( self.config )
  104. db.remove_all_user_words()
  105. def on_ClearAdjustedWordFreq_clicked(self, button):
  106. import ZhengJuDB
  107. db = ZhengJuDB.ZhengJuDB( self.config )
  108. db.remvoe_all_user_freq()
  109. def on_ClearUserPhrases_clicked(self, button):
  110. import ZhengJuDB
  111. db = ZhengJuDB.ZhengJuDB( self.config )
  112. db.remove_all_user_phrase()
  113. def on_SendLog_clicked(self, button):
  114. attach = os.path.expanduser ("~/.scim/zhengju-conv-error.log")
  115. os.system("gnome-open \"mailto:yufanyufan@gmail.com?subject=%s&body=%s&attach=file://%s\"" % (
  116. _("Conversion Error Log"),
  117. _("Check attachment\nIf you have any suggestion, I'm glad to hear!" )
  118. ,attach))
  119. def on_train_clicked(self,button):
  120. dialog = gtk.FileChooserDialog(_("Choose A Plain Text File"),buttons=
  121. (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT))
  122. if dialog.run() == gtk.RESPONSE_ACCEPT:
  123. files = dialog.get_filenames()
  124. else:
  125. files = []
  126. dialog.destroy()
  127. for i in files:
  128. pathname = os.path.dirname (__file__)
  129. path = os.path.abspath(pathname)
  130. path = os.path.join (path, "../../engine/PinYin/ZhengJu.py -f ")
  131. result = commands.getstatusoutput("python " + path + +'"' + i + '"')
  132. if result[0]==0:
  133. message = gtk.MessageDialog(buttons=gtk.BUTTONS_OK,
  134. message_format = _("Train Successful"))
  135. else:
  136. message = gtk.MessageDialog(buttons=gtk.BUTTONS_OK,
  137. message_format = _("Train Failed"))
  138. message.format_secondary_text(result[1])
  139. message.run()
  140. message.destroy()
  141. def run (self, uuid, config, display):
  142. self.config = config
  143. self._uuid = uuid
  144. self._display = display
  145. self._init_agent ()
  146. self._create_ui ()
  147. self.load_config (config)
  148. gtk.main ()
  149. self.config.flush ()
  150. self._helper_agent.reload_config ()
  151. self._helper_agent.close_connection ()
  152. def load_config(self,config):
  153. for k, v in Config.items():
  154. value = config.read (ConfigBase+k, v[2])
  155. self.xml.get_widget(k).set_active (v[0](value))
  156. self.orgin_value[k] = value
  157. def save_config (self, config):
  158. for k, v in Config.items():
  159. value = v[1](self.xml.get_widget(k).get_active())
  160. config.write (ConfigBase+k, value)
  161. self.orgin_value[k] = value
  162. def query_changed (self):
  163. for k, v in Config.items():
  164. if v[1](self.xml.get_widget(k).get_active()) != self.orgin_value[k]:
  165. return True
  166. return False
  167. def _init_agent (self):
  168. fd = self._helper_agent.open_connection (self._helper_info, self._display)
  169. if fd >= 0:
  170. condition = gobject.IO_IN | gobject.IO_ERR | gobject.IO_HUP
  171. gobject.io_add_watch (fd, condition, self.on_agent_event)
  172. def _quit (self, need_confirm ):
  173. if self.query_changed() == False:
  174. gtk.main_quit ()
  175. return True
  176. else:
  177. dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION,
  178. gtk.BUTTONS_YES_NO, _("Are you sure to close Python PinYin Setup without save configure?"))
  179. id = dlg.run ()
  180. dlg.destroy ()
  181. if id == gtk.RESPONSE_YES:
  182. gtk.main_quit ()
  183. return True
  184. return False
  185. def on_window_main_delete_event (self, widget, event):
  186. result = self._quit (True)
  187. return True
  188. def on_button_ok_clicked (self, button):
  189. self.save_config (self.config)
  190. self._quit (False)
  191. def on_button_apply_clicked (self, button):
  192. self.save_config (self.config)
  193. def on_button_cancel_clicked (self, button):
  194. self._quit (True)
  195. def on_agent_event (self, fd, condition):
  196. if condition == gobject.IO_IN:
  197. while self._helper_agent.has_pending_event ():
  198. self._helper_agent.filter_event ()
  199. return True
  200. elif condition == gobject.IO_ERR or condition == gobject.IO_HUP:
  201. gtk.main_quit ()
  202. return False
  203. return False
  204. if __name__ == "__main__":
  205. class CC:
  206. def __init__ (self):
  207. pass
  208. def read (self, name, v):
  209. return v
  210. def write (self, name, v):
  211. pass
  212. helper_info = ("eebeecd7-cb22-48f4-8ced-70e42dad1a79", "", "", "", 1)
  213. __UUID__ = "eebeecd7-cb22-58f4-8ced-70e42dad1a79"
  214. ZhengJuSetupUI(helper_info).run (__UUID__, CC (), ":0.0")