PageRenderTime 69ms CodeModel.GetById 33ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

/python/setupui/PinYin/PinYinUI.py

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