1 # vim:set et ts=4 sts=4:
2 # -*- coding: utf-8 -*-
4 # ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus
6 # Copyright (c) 2012 Peng Wu <alexepico@gmail.com>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 from gi.repository import GObject
24 from gi.repository import Gtk
26 gettext.install('ibus-libpinyin')
56 (True, GBK_DICTIONARY, _("Low Frequent Characters"), True),
57 (True, ART_DICTIONARY, _("Art"), True),
58 (True, CULTURE_DICTIONARY, _("Culture"), True),
59 (True, ECONOMY_DICTIONARY, _("Economy"), True),
60 (True, GEOLOGY_DICTIONARY, _("Geology"), True),
61 (True, HISTORY_DICTIONARY, _("History"), True),
62 (True, LIFE_DICTIONARY, _("Life"), True),
63 (True, NATURE_DICTIONARY, _("Nature"), True),
64 (True, SCITECH_DICTIONARY, _("SciTech"), True),
65 (True, SOCIETY_DICTIONARY, _("Society"), True),
66 (True, SPORT_DICTIONARY, _("Sport"), True)
70 class DictionaryTreeView(Gtk.TreeView):
71 __gtype_name__ = 'DictionaryTreeView'
76 'Enabled Dictionaries',
78 GObject.PARAM_READWRITE
83 super(DictionaryTreeView, self).__init__()
85 self.__changed = False
87 self.set_headers_visible(True)
89 self.__model = self.__create_model()
90 self.set_model(self.__model)
94 def __create_model(self):
95 model = Gtk.ListStore(bool, int, str, bool)
97 model.connect("row-changed", self.__emit_changed, "row-changed")
99 for dict in dictionaries:
100 iter = model.append()
102 COLUMN_SENSITIVE, dict[COLUMN_SENSITIVE],
103 COLUMN_PHRASE_INDEX, dict[COLUMN_PHRASE_INDEX],
104 COLUMN_DESCRIPTION, dict[COLUMN_DESCRIPTION],
105 COLUMN_ACTIVE, dict[COLUMN_ACTIVE])
109 def __add_columns(self):
111 renderer = Gtk.CellRendererToggle()
112 renderer.connect('toggled', self.__active_toggled, self.__model)
113 column = Gtk.TreeViewColumn(_('Active'), renderer, active=COLUMN_ACTIVE, sensitive=COLUMN_SENSITIVE)
114 self.append_column(column)
116 # column for description
117 render = Gtk.CellRendererText()
118 column = Gtk.TreeViewColumn(_('Description'), render, text=COLUMN_DESCRIPTION)
119 self.append_column(column)
121 def __active_toggled(self, cell, path, model):
123 iter = model.get_iter((int(path),))
124 active = model.get_value(iter, COLUMN_ACTIVE)
130 model.set(iter, COLUMN_ACTIVE, active)
133 self.__changed = True
134 self.__emit_changed()
136 def __emit_changed(self, *args):
138 self.__changed = False
139 self.notify("dictionaries")
141 def get_dictionaries(self):
143 for row in self.__model:
144 if (not row[COLUMN_SENSITIVE]):
146 if (row[COLUMN_ACTIVE]):
147 dicts.append(str(row[COLUMN_PHRASE_INDEX]))
149 return ';'.join(dicts)
151 def set_dictionaries(self, dicts):
153 for row in self.__model:
154 if not row[COLUMN_SENSITIVE]:
156 row[COLUMN_ACTIVE] = False
159 self.__emit_changed()
162 for dict in dicts.split(";"):
164 for row in self.__model:
165 if not row[COLUMN_SENSITIVE]:
167 if dict == row[COLUMN_PHRASE_INDEX]:
168 row[COLUMN_ACTIVE] = True
169 self.__emit_changed()
171 def do_get_property(self, prop):
172 if prop.name == 'dictionaries':
173 return self.get_dictionaries()
175 raise AttributeError, 'unknown property %s' % prop.name
177 def do_set_property(self, prop, value):
178 if prop.name == "dictionaries":
179 self.set_dictionaries(value)
181 raise AttributeError, 'unknown property %s' % prop.name
184 GObject.type_register(DictionaryTreeView)
187 if __name__ == "__main__":
188 tree = DictionaryTreeView()
189 tree.set_dictionaries("")