PageRenderTime 19ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/python/setupui/PinYin/PinYinUI.py

http://scim-python.googlecode.com/
Python | 248 lines | 181 code | 32 blank | 35 comment | 19 complexity | f374c8cbdbb51fac60b3bfd1f0fc154b 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. from gtk import gdk
  29. import os
  30. import sys
  31. try:
  32. from PYDict import *
  33. except:
  34. pathname = os.path.dirname (__file__)
  35. path = os.path.abspath(pathname)
  36. path = os.path.join (path, "../../engine/PinYin")
  37. path = os.path.abspath(path)
  38. sys.path.append(path)
  39. from PYDict import *
  40. from gettext import dgettext
  41. _ = lambda a : dgettext ("scim-python", a)
  42. RGB_COLOR = lambda c : ((c.red & 0xff00) << 8) + (c.green & 0xff00) + ((c.blue & 0xff00) >> 8)
  43. GDK_COLOR = lambda c : gdk.Color (((c >> 16) & 0xff) * 256, ((c >> 8) & 0xff) * 256, (c & 0xff) * 256)
  44. RGB = lambda r, g, b : ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff)
  45. class PinYinSetupUI (gtk.VBox):
  46. def __init__ (self):
  47. gtk.VBox.__init__ (self)
  48. # init value
  49. self._shuangpin_schema = "MSPY"
  50. self._fuzzy_pinyin = False
  51. self._auto_correct = True
  52. self._spell_check = True
  53. self._page_size = 5
  54. self._phrase_color = RGB (0, 0, 0)
  55. self._user_phrase_color = RGB (0, 0, 0xef)
  56. self._new_phrase_color = RGB (0xef, 0, 0)
  57. self._special_phrase_color = RGB (0, 0xbf, 0)
  58. self._english_phrase_color = RGB (0, 0xbf, 0)
  59. self._error_eng_phrase_color = RGB (0xef, 0, 0)
  60. self._create_ui ()
  61. self.show_all ()
  62. def _create_ui (self):
  63. line = 0
  64. table = gtk.Table (3, 10)
  65. # Create combobox for Shuang Pin Schemas
  66. label = gtk.Label (_("ShuangPin Schema"))
  67. table.attach (label, 0, 1, line, line + 1)
  68. self._combo_schema = gtk.combo_box_new_text ()
  69. table.attach (self._combo_schema, 1, 2, line, line + 1)
  70. i = 0
  71. self._schemas = []
  72. for schema in SHUANGPIN_SCHEMAS.keys ():
  73. self._combo_schema.append_text(_(schema))
  74. self._schemas.append (schema)
  75. i += 1
  76. self._combo_schema.set_active (0)
  77. line += 1
  78. # Create combobox for The number for lookup table page size
  79. label = gtk.Label (_("Lookup table page size"))
  80. table.attach (label, 0, 1, line, line + 1)
  81. self._combo_page_size = gtk.combo_box_new_text ()
  82. table.attach (self._combo_page_size, 1, 2, line, line + 1)
  83. for i in xrange (1, 10):
  84. self._combo_page_size.append_text (str (i))
  85. self._combo_page_size.set_active (0)
  86. line += 1
  87. # Create checkbox for MoHu PinYin
  88. label = gtk.Label (_("Enable fuzzy PinYin support"))
  89. table.attach (label, 0, 1, line, line + 1)
  90. self._button_fuzzy_pinyin = gtk.CheckButton ()
  91. table.attach (self._button_fuzzy_pinyin, 1, 2, line, line + 1)
  92. line += 1
  93. # Create Autocorrect wrong PinYin
  94. label = gtk.Label (_("Enable wrong PinYin auto correct"))
  95. table.attach (label, 0, 1, line, line + 1)
  96. self._button_auto_correct = gtk.CheckButton ()
  97. table.attach (self._button_auto_correct, 1, 2, line, line + 1)
  98. line += 1
  99. # Create English spelling checking
  100. label = gtk.Label (_("Enable English spell check"))
  101. table.attach (label, 0, 1, line, line + 1)
  102. self._button_spell_check = gtk.CheckButton ()
  103. table.attach (self._button_spell_check, 1, 2, line, line + 1)
  104. line += 1
  105. # Create Colors choices
  106. self._button_colors = []
  107. self.pack_start (table)
  108. i = 0
  109. colors = (_("Color of Normal Phrases"), _("Color of New Phrase"),
  110. _("Color of User Phrase"), _("Color of Special Phrase"),
  111. _("Color of English Candidates"), _("Color of Spelling Error"))
  112. for color in colors:
  113. label = gtk.Label (color)
  114. table.attach (label, 0, 1, line, line + 1)
  115. button_color = gtk.ColorButton ()
  116. table.attach (button_color, 1, 2, line, line + 1)
  117. self._button_colors.append (button_color)
  118. i += 1
  119. line += 1
  120. def get_name (self):
  121. return _("Python Pin Yin")
  122. def save_config (self, config):
  123. self._shuangpin_schema = self._schemas [self._combo_schema.get_active ()]
  124. self._fuzzy_pinyin = self._button_fuzzy_pinyin.get_active ()
  125. self._auto_correct = self._button_auto_correct.get_active ()
  126. self._spell_check = self._button_auto_correct.get_active ()
  127. self._page_size = self._combo_page_size.get_active () + 1
  128. self._phrase_color = RGB_COLOR (self._button_colors[0].get_color())
  129. self._new_phrase_color = RGB_COLOR (self._button_colors[1].get_color())
  130. self._user_phrase_color = RGB_COLOR (self._button_colors[2].get_color())
  131. self._special_phrase_color = RGB_COLOR (self._button_colors[3].get_color())
  132. self._english_phrase_color = RGB_COLOR (self._button_colors[4].get_color())
  133. self._error_eng_phrase_color = RGB_COLOR (self._button_colors[5].get_color())
  134. config.write ("/IMEngine/Python/PinYin/ShuangPinSchema", self._shuangpin_schema)
  135. config.write ("/IMEngine/Python/PinYin/FuzzyPinYin", self._fuzzy_pinyin)
  136. config.write ("/IMEngine/Python/PinYin/AutoCorrect", self._auto_correct)
  137. config.write ("/IMEngine/Python/PinYin/SpellCheck", self._spell_check)
  138. config.write ("/IMEngine/Python/PinYin/PageSize", self._page_size)
  139. # write colors
  140. config.write ("/IMEngine/Python/PinYin/PhraseColor", self._phrase_color)
  141. config.write ("/IMEngine/Python/PinYin/NewPhraseColor", self._new_phrase_color)
  142. config.write ("/IMEngine/Python/PinYin/UserPhraseColor", self._user_phrase_color)
  143. config.write ("/IMEngine/Python/PinYin/SpecialPhraseColor", self._special_phrase_color)
  144. config.write ("/IMEngine/Python/PinYin/EnglishPhraseColor", self._english_phrase_color)
  145. config.write ("/IMEngine/Python/PinYin/ErrorEnglishPhraseColor", self._error_eng_phrase_color)
  146. def load_config (self, config):
  147. # read values
  148. self._shuangpin_schema = \
  149. config.read ("/IMEngine/Python/PinYin/ShuangPinSchema", "MSPY")
  150. if self._shuangpin_schema not in self._schemas:
  151. self._shuangpin_schema = "MSPY"
  152. self._fuzzy_pinyin = \
  153. config.read ("/IMEngine/Python/PinYin/FuzzyPinYin", False)
  154. self._auto_correct = \
  155. config.read ("/IMEngine/Python/PinYin/AutoCorrect", True)
  156. self._spell_check = \
  157. config.read ("/IMEngine/Python/PinYin/SpellCheck", True)
  158. self._page_size = \
  159. config.read ("/IMEngine/Python/PinYin/PageSize", 5)
  160. if self._page_size < 1 or self._page_size > 9:
  161. self._page_size = 5
  162. self._phrase_color = \
  163. config.read ("/IMEngine/Python/PinYin/PhraseColor", self._phrase_color)
  164. self._new_phrase_color = \
  165. config.read ("/IMEngine/Python/PinYin/NewPhraseColor", self._new_phrase_color)
  166. self._user_phrase_color = \
  167. config.read ("/IMEngine/Python/PinYin/UserPhraseColor", self._user_phrase_color)
  168. self._special_phrase_color = \
  169. config.read ("/IMEngine/Python/PinYin/SpecialPhraseColor", self._special_phrase_color)
  170. self._english_phrase_color = \
  171. config.read ("/IMEngine/Python/PinYin/EnglishPhraseColor", self._english_phrase_color)
  172. self._error_eng_phrase_color = \
  173. config.read ("/IMEngine/Python/PinYin/ErrorEnglishPhraseColor", self._error_eng_phrase_color)
  174. # set widgets
  175. self._combo_schema.set_active (self._schemas.index (self._shuangpin_schema))
  176. self._button_fuzzy_pinyin.set_active (self._fuzzy_pinyin)
  177. self._button_auto_correct.set_active (self._auto_correct)
  178. self._button_spell_check.set_active (self._spell_check)
  179. self._combo_page_size.set_active (self._page_size - 1)
  180. self._button_colors[0].set_color (GDK_COLOR (self._phrase_color))
  181. self._button_colors[1].set_color (GDK_COLOR (self._new_phrase_color))
  182. self._button_colors[2].set_color (GDK_COLOR (self._user_phrase_color))
  183. self._button_colors[3].set_color (GDK_COLOR (self._special_phrase_color))
  184. self._button_colors[4].set_color (GDK_COLOR (self._english_phrase_color))
  185. self._button_colors[5].set_color (GDK_COLOR (self._error_eng_phrase_color))
  186. def query_changed (self):
  187. if self._schemas.index (self._shuangpin_schema) != self._combo_schema.get_active ():
  188. return True
  189. if self._fuzzy_pinyin != self._button_fuzzy_pinyin.get_active ():
  190. return True
  191. if self._auto_correct != self._button_auto_correct.get_active ():
  192. return True
  193. if self._spell_check != self._button_spell_check.get_active ():
  194. return True
  195. if self._page_size != self._combo_page_size.get_active () + 1:
  196. return True
  197. if self._phrase_color != RGB_COLOR (self._button_colors[0].get_color()):
  198. return True
  199. if self._new_phrase_color != RGB_COLOR (self._button_colors[1].get_color()):
  200. return True
  201. if self._user_phrase_color != RGB_COLOR (self._button_colors[2].get_color()):
  202. return True
  203. if self._special_phrase_color != RGB_COLOR (self._button_colors[3].get_color()):
  204. return True
  205. if self._english_phrase_color != RGB_COLOR (self._button_colors[4].get_color()):
  206. return True
  207. if self._error_eng_phrase_color != RGB_COLOR (self._button_colors[5].get_color()):
  208. return True
  209. return False
  210. if __name__ == "__main__":
  211. window = gtk.Window ()
  212. setupui = PinYinSetupUI ()
  213. window.add (setupui)
  214. window.show_all ()
  215. print setupui.query_changed ()
  216. gtk.main ()