PageRenderTime 729ms CodeModel.GetById 121ms app.highlight 397ms RepoModel.GetById 198ms app.codeStats 1ms

/python/helper/ZhengJuSetup/ZhengJuUI.py

http://scim-python.googlecode.com/
Python | 240 lines | 190 code | 25 blank | 25 comment | 22 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#
 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	"LogConvError": ( Pass, Pass, True),
 68	"SplitPinyin":  ( Pass, Pass, True),
 69	"TranslateShuangPin":  ( Pass, Pass, True),
 70	"MixedInput": ( Pass, Pass, True),
 71	"FuzzyPinyin": ( Pass, Pass, False),
 72	"FuzzyS_Sh": ( Pass, Pass, True),
 73	"FuzzyC_Ch": ( Pass, Pass, True),
 74	"FuzzyZ_Zh": ( Pass, Pass, True),
 75	"FuzzyL_N": ( Pass, Pass, True),
 76	"FuzzyIn_Ing": ( Pass, Pass, True),
 77	"FuzzyEn_Eng": ( Pass, Pass, True),
 78	"FuzzyAn_Ang": ( Pass, Pass, True)
 79}
 80
 81class ZhengJuSetupUI ():
 82
 83	def __init__ (self, helper_info):
 84		self._helper_info = helper_info
 85		self._helper_agent = scim.HelperAgent ()
 86		self.orgin_value = {}
 87
 88	def _create_ui (self):
 89		pathname = os.path.dirname (__file__)
 90		path = os.path.abspath(pathname)
 91		self.xml = gtk.glade.XML(path+'/ZhengJuUI.glade')
 92		self.xml.signal_autoconnect (self)
 93		self.window = self.xml.get_widget ("window_main")
 94		
 95		gtk_pinin_schema = self.xml.get_widget("PinYinSchema")
 96		liststore = gtk.ListStore(str)
 97		for k in pinyin_schema_list:
 98			liststore.append([_(k)])
 99		gtk_pinin_schema.set_model(liststore)
100		
101		gtk_shuangpin_schema = self.xml.get_widget("ShuangPinSchema")
102		liststore = gtk.ListStore(str)
103		for k in shuangpin_schema_list:
104			liststore.append([_(k)])
105		gtk_shuangpin_schema.set_model(liststore)
106
107		self.window.show_all ()
108		
109	def get_name (self):
110		return _("ZhengJu")
111	def on_ClearUserWords_clicked(self, button):
112		import ZhengJuDB
113		db = ZhengJuDB.ZhengJuDB( self.config )
114		db.remove_all_user_words()
115	def on_ClearAdjustedWordFreq_clicked(self, button):
116		import ZhengJuDB
117		db = ZhengJuDB.ZhengJuDB( self.config )
118		db.remvoe_all_user_freq()
119	def on_ClearUserPhrases_clicked(self, button):
120		import ZhengJuDB
121		db = ZhengJuDB.ZhengJuDB( self.config )
122		db.remove_all_user_phrase()
123	def on_SendLog_clicked(self, button):
124		attach = os.path.expanduser ("~/.scim/zhengju-conv-error.log")
125		os.system("gnome-open \"mailto:yufanyufan@gmail.com?subject=%s&body=%s&attach=file://%s\"" % (
126			_("Conversion Error Log"),
127			_("Check attachment\nIf you have any suggestion, I'm glad to hear!" )
128			,attach))
129		
130	def on_train_clicked(self,button):
131		dialog = gtk.FileChooserDialog(_("Choose A Plain Text File"),buttons=
132			(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT))
133		if dialog.run() == gtk.RESPONSE_ACCEPT:
134			files = dialog.get_filenames()
135		else:
136			files = []
137		dialog.destroy()
138		for i in files:
139			pathname = os.path.dirname (__file__)
140			path = os.path.abspath(pathname)
141			path = os.path.join (path, "../../engine/PinYin/ZhengJu.py -f ")
142			result = commands.getstatusoutput("python " + path + +'"' + i + '"')
143			if result[0]==0:
144				message = gtk.MessageDialog(buttons=gtk.BUTTONS_OK,
145					message_format = _("Train Successful"))
146			else:
147				message = gtk.MessageDialog(buttons=gtk.BUTTONS_OK,
148					message_format = _("Train Failed"))
149				message.format_secondary_text(result[1])
150			message.run()
151			message.destroy()
152
153	def run (self, uuid, config, display):
154		self.config = config
155		self._uuid = uuid
156		self._display = display
157		self._init_agent ()
158		self._create_ui ()
159		self.load_config (config)
160		gtk.main ()
161		self.config.flush ()
162		self._helper_agent.reload_config ()
163		self._helper_agent.close_connection ()
164
165	def load_config(self,config):
166		for k, v in Config.items():
167			value = config.read (ConfigBase+k, v[2])
168			self.xml.get_widget(k).set_active (v[0](value))
169			self.orgin_value[k] = value
170	
171	def save_config (self, config):
172		for k, v in Config.items():
173			value = v[1](self.xml.get_widget(k).get_active())
174			config.write (ConfigBase+k, value)
175			self.orgin_value[k] = value
176	
177	def query_changed (self):
178		for k, v in Config.items():
179			if v[1](self.xml.get_widget(k).get_active()) != self.orgin_value[k]:
180				return True
181		return False
182
183	def _init_agent (self):
184		fd = self._helper_agent.open_connection (self._helper_info, self._display)
185		if fd >= 0:
186			condition = gobject.IO_IN | gobject.IO_ERR | gobject.IO_HUP
187			gobject.io_add_watch (fd, condition, self.on_agent_event)
188
189	def _quit (self, need_confirm ):
190		if self.query_changed() == False:
191			gtk.main_quit ()
192			return True
193		else:
194			dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION,
195						gtk.BUTTONS_YES_NO, _("Are you sure to close Python PinYin Setup without save configure?"))
196			id = dlg.run ()
197			dlg.destroy ()
198			if id == gtk.RESPONSE_YES:
199				gtk.main_quit ()
200				return True
201		return False
202
203	def on_window_main_delete_event (self, widget, event):
204		result = self._quit (True)
205		return True
206	
207	def on_button_ok_clicked (self, button):
208		self.save_config (self.config)
209		self._quit (False)
210	
211	def on_button_apply_clicked (self, button):
212		self.save_config (self.config)
213	
214	def on_button_cancel_clicked (self, button):
215		self._quit (True)
216	
217	def on_agent_event (self, fd, condition):
218		if condition == gobject.IO_IN:
219			while self._helper_agent.has_pending_event ():
220				self._helper_agent.filter_event ()
221			return True
222		elif condition == gobject.IO_ERR or condition == gobject.IO_HUP:
223			gtk.main_quit ()
224			return False
225		
226		return False
227
228if __name__ == "__main__":
229	class CC:
230		def __init__ (self):
231			pass
232
233		def read (self, name, v):
234			return v
235		def write (self, name, v):
236			pass
237	helper_info = ("eebeecd7-cb22-48f4-8ced-70e42dad1a79", "", "", "", 1)
238	__UUID__ = "eebeecd7-cb22-58f4-8ced-70e42dad1a79"
239	ZhengJuSetupUI(helper_info).run (__UUID__, CC (), ":0.0")
240