PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/python/setupui/ZhengJu/ZhengJuUI.py

http://scim-python.googlecode.com/
Python | 186 lines | 140 code | 21 blank | 25 comment | 18 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#
 26import scim
 27import gtk 
 28import os
 29import sys
 30import gtk.glade
 31import gobject
 32import commands
 33try:
 34	from PYDict import *
 35except:
 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
 45from gettext import dgettext
 46_ = lambda a : dgettext ("scim-python", a)
 47N_ = lambda x : x
 48
 49gtk.glade.textdomain("scim-python")
 50
 51shuangpin_schema_list = SHUANGPIN_SCHEMAS.keys()
 52pinyin_schema_list = [N_("QuanPin"),N_("JianPin"),N_("ShuangPin")]
 53ConfigBase = "/IMEngine/Python/ZhengJu/"
 54Pass = lambda get: get
 55Config = {
 56"ShuangPinSchema" : ( lambda get: shuangpin_schema_list.index(get) ,
 57				lambda set: shuangpin_schema_list[set],
 58				"MSPY"),
 59"PinYinSchema" : ( lambda get: pinyin_schema_list.index(get) ,
 60				lambda set: pinyin_schema_list[set],
 61				"JianPin"),
 62"LargeCharset" :( Pass,Pass,False),
 63"CreateUserWords" :( Pass,Pass,True),
 64"CreateUserPhrases" :( Pass,Pass,True),
 65"AdjustWordFreq" :( Pass,Pass,True),
 66"ProgressivePrompt" : ( Pass,Pass,True)
 67}
 68
 69class ZhengJuSetupUI (gtk.VBox):
 70
 71	def __init__ (self):
 72		gtk.VBox.__init__ (self)
 73		self._values = {}
 74		self.orgin_value = {}
 75		self._create_ui ()
 76		self.show_all ()
 77		
 78	def _create_ui (self):
 79		pathname = os.path.dirname (__file__)
 80		path = os.path.abspath(pathname)
 81		self.xml = gtk.glade.XML(path+'/ZhengJuUI.glade',root="top")
 82		self.pack_start (self.xml.get_widget("top"))
 83		
 84		gtk_pinin_schema = self.xml.get_widget("PinYinSchema")
 85		liststore = gtk.ListStore(str)
 86		for k in pinyin_schema_list:
 87			liststore.append([_(k)])
 88		gtk_pinin_schema.set_model(liststore)
 89		
 90		gtk_shuangpin_schema = self.xml.get_widget("ShuangPinSchema")
 91		liststore = gtk.ListStore(str)
 92		for k in shuangpin_schema_list:
 93			liststore.append([_(k)])
 94		gtk_shuangpin_schema.set_model(liststore)
 95
 96		train_button = self.xml.get_widget("Train")
 97		train_button.connect('clicked', self.train_callback)
 98		
 99	def get_name (self):
100		return _("ZhengJu")
101	
102	def train_callback(self,button):
103		dialog = gtk.FileChooserDialog(_("Choose A Plain Text File"),buttons=
104			(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT))
105		if dialog.run() == gtk.RESPONSE_ACCEPT:
106			files = dialog.get_filenames()
107		else:
108			files = []
109		dialog.destroy()
110		for i in files:
111			pathname = os.path.dirname (__file__)
112			path = os.path.abspath(pathname)
113			path = os.path.join (path, "../../engine/PinYin/ZhengJu.py -f ")
114			result = commands.getstatusoutput("python " +path + i)
115			if result[0]==0:
116				message = gtk.MessageDialog(buttons=gtk.BUTTONS_OK,
117					message_format = _("Train Successful"))
118			else:
119				message = gtk.MessageDialog(buttons=gtk.BUTTONS_OK,
120					message_format = _("Train Failed"))
121				message.format_secondary_text(result[1])
122			message.run()
123			message.destroy()
124
125	
126	def load_config(self,config):
127		for k, v in Config.items():
128			value = config.read (ConfigBase+k, v[2])
129			self.xml.get_widget(k).set_active(v[0](value))
130			self.orgin_value[k] = value
131	
132	def save_config (self, config):
133		for k, v in Config.items():
134			value = v[1](self.xml.get_widget(k).get_active())
135			config.write (ConfigBase+k, value)
136	
137	def query_changed (self):
138		for k, v in Config.items():
139			if v[1](self.xml.get_widget(k).get_active()) != self.orgin_value[k]:
140				return True
141		return False
142
143window = None
144
145def mod_create_ui ():
146	global window
147	if window != None:
148		return window
149	try:
150		window = ZhengJuSetupUI()
151		window.show_all ()
152		return window
153	except:
154		import traceback
155		traceback.print_exc ()
156		return None
157
158def mod_load_config (config):
159	try:
160		window.load_config (config)
161	except:
162		import traceback
163		traceback.print_exc ()
164
165def mod_save_config (config):
166	try:
167		window.save_config (config)
168	except:
169		import traceback
170		traceback.print_exc ()
171
172def mod_query_changed ():
173	try:
174		if window.query_changed ():
175			return True
176	except:
177		import traceback
178		traceback.print_exc ()
179		return False
180
181if __name__ == "__main__":
182	w = gtk.Window()
183	w.set_title(_("ZhengJu"))
184	w.add(ZhengJuSetupUI())
185	w.show()
186	gtk.main()