PageRenderTime 25ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/python/setupui/ZhengJu/ZhengJuUI.py

http://scim-python.googlecode.com/
Python | 186 lines | 140 code | 21 blank | 25 comment | 19 complexity | 702a2e078c4c1a9c03d504989fea57bf 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. }
  65. class ZhengJuSetupUI (gtk.VBox):
  66. def __init__ (self):
  67. gtk.VBox.__init__ (self)
  68. self._values = {}
  69. self.orgin_value = {}
  70. self._create_ui ()
  71. self.show_all ()
  72. def _create_ui (self):
  73. pathname = os.path.dirname (__file__)
  74. path = os.path.abspath(pathname)
  75. self.xml = gtk.glade.XML(path+'/ZhengJuUI.glade',root="top")
  76. self.pack_start (self.xml.get_widget("top"))
  77. gtk_pinin_schema = self.xml.get_widget("PinYinSchema")
  78. liststore = gtk.ListStore(str)
  79. for k in pinyin_schema_list:
  80. liststore.append([_(k)])
  81. gtk_pinin_schema.set_model(liststore)
  82. gtk_shuangpin_schema = self.xml.get_widget("ShuangPinSchema")
  83. liststore = gtk.ListStore(str)
  84. for k in shuangpin_schema_list:
  85. liststore.append([_(k)])
  86. gtk_shuangpin_schema.set_model(liststore)
  87. train_button = self.xml.get_widget("Train")
  88. train_button.connect('clicked', self.train_callback)
  89. def get_name (self):
  90. return _("ZhengJu")
  91. def train_callback(self,button):
  92. dialog = gtk.FileChooserDialog(_("Choose A Plain Text File"),buttons=
  93. (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT))
  94. if dialog.run() == gtk.RESPONSE_ACCEPT:
  95. files = dialog.get_filenames()
  96. else:
  97. files = []
  98. dialog.destroy()
  99. for i in files:
  100. pathname = os.path.dirname (__file__)
  101. path = os.path.abspath(pathname)
  102. path = os.path.join (path, "../../engine/PinYin/ZhengJu.py -f ")
  103. result = commands.getstatusoutput("python " +path + i)
  104. if result[0]==0:
  105. message = gtk.MessageDialog(buttons=gtk.BUTTONS_OK,
  106. message_format = _("Train Successful"))
  107. else:
  108. message = gtk.MessageDialog(buttons=gtk.BUTTONS_OK,
  109. message_format = _("Train Failed"))
  110. message.format_secondary_text(result[1])
  111. message.run()
  112. message.destroy()
  113. def load_config(self,config):
  114. for k, v in Config.items():
  115. value = config.read (ConfigBase+k, v[2])
  116. self.xml.get_widget(k).set_active(v[0](value))
  117. self.orgin_value[k] = value
  118. def save_config (self, config):
  119. for k, v in Config.items():
  120. value = v[1](self.xml.get_widget(k).get_active())
  121. config.write (ConfigBase+k, value)
  122. def query_changed (self):
  123. for k, v in Config.items():
  124. if v[1](self.xml.get_widget(k).get_active()) != self.orgin_value[k]:
  125. return True
  126. return False
  127. window = None
  128. def mod_create_ui ():
  129. global window
  130. if window != None:
  131. return window
  132. try:
  133. window = ZhengJuSetupUI()
  134. window.show_all ()
  135. return window
  136. except:
  137. import traceback
  138. traceback.print_exc ()
  139. return None
  140. def mod_load_config (config):
  141. try:
  142. window.load_config (config)
  143. except:
  144. import traceback
  145. traceback.print_exc ()
  146. def mod_save_config (config):
  147. try:
  148. window.save_config (config)
  149. except:
  150. import traceback
  151. traceback.print_exc ()
  152. def mod_query_changed ():
  153. try:
  154. if window.query_changed ():
  155. return True
  156. except:
  157. import traceback
  158. traceback.print_exc ()
  159. return False
  160. if __name__ == "__main__":
  161. w = gtk.Window()
  162. w.set_title(_("ZhengJu"))
  163. w.add(ZhengJuSetupUI())
  164. w.show()
  165. gtk.main()