1# vim:set et ts=4 sts=4: 2# -*- coding: utf-8 -*- 3# 4# ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus 5# 6# Copyright (c) 2012 Peng Wu <alexepico@gmail.com> 7# 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) 11# any later version. 12# 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. 17# 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. 21 22import gettext 23from gi.repository import GObject 24from gi.repository import Gtk 25 26gettext.install('ibus-libpinyin') 27 28( 29 ART_DICTIONARY, 30 CULTURE_DICTIONARY, 31 ECONOMY_DICTIONARY, 32 GEOLOGY_DICTIONARY, 33 HISTORY_DICTIONARY, 34 LIFE_DICTIONARY, 35 NATURE_DICTIONARY, 36 PEOPLE_DICTIONARY, 37 SCIENCE_DICTIONARY, 38 SOCIETY_DICTIONARY, 39 SPORT_DICTIONARY, 40 TECHNOLOGY_DICTIONARY, 41) = range(4, 16) 42 43( 44COLUMN_SENSITIVE, 45COLUMN_PHRASE_INDEX, 46COLUMN_DESCRIPTION, 47COLUMN_ACTIVE 48) = range(4) 49 50dictionaries = \ 51 ( 52 (True, ART_DICTIONARY, _("Art"), True), 53 (True, CULTURE_DICTIONARY, _("Culture"), True), 54 (True, ECONOMY_DICTIONARY, _("Economy"), True), 55 (True, GEOLOGY_DICTIONARY, _("Geology"), True), 56 (True, HISTORY_DICTIONARY, _("History"), True), 57 (True, LIFE_DICTIONARY, _("Life"), True), 58 (True, NATURE_DICTIONARY, _("Nature"), True), 59 (True, PEOPLE_DICTIONARY, _("People"), True), 60 (True, SCIENCE_DICTIONARY, _("Science"), True), 61 (True, SOCIETY_DICTIONARY, _("Society"), True), 62 (True, SPORT_DICTIONARY, _("Sport"), True), 63 (True, TECHNOLOGY_DICTIONARY, _("Technology"), True), 64 ) 65 66 67class DictionaryTreeView(Gtk.TreeView): 68 __gtype_name__ = 'DictionaryTreeView' 69 __gproperties__ = { 70 'dictionaries': ( 71 str, 72 'dictionaries', 73 'Enabled Dictionaries', 74 "", 75 GObject.PARAM_READWRITE 76 ) 77 } 78 79 def __init__(self): 80 super(DictionaryTreeView, self).__init__() 81 82 self.__changed = False 83 84 self.set_headers_visible(True) 85 86 self.__model = self.__create_model() 87 self.set_model(self.__model) 88 89 self.__add_columns() 90 91 def __create_model(self): 92 model = Gtk.ListStore(bool, int, str, bool) 93 94 model.connect("row-changed", self.__emit_changed, "row-changed") 95 96 for dict in dictionaries: 97 iter = model.append() 98 model.set(iter, 99 COLUMN_SENSITIVE, dict[COLUMN_SENSITIVE], 100 COLUMN_PHRASE_INDEX, dict[COLUMN_PHRASE_INDEX], 101 COLUMN_DESCRIPTION, dict[COLUMN_DESCRIPTION], 102 COLUMN_ACTIVE, dict[COLUMN_ACTIVE]) 103 104 return model 105 106 def __add_columns(self): 107 # column for toggles 108 renderer = Gtk.CellRendererToggle() 109 renderer.connect('toggled', self.__active_toggled, self.__model) 110 column = Gtk.TreeViewColumn(_('Active'), renderer, active=COLUMN_ACTIVE, sensitive=COLUMN_SENSITIVE) 111 self.append_column(column) 112 113 # column for description 114 render = Gtk.CellRendererText() 115 column = Gtk.TreeViewColumn(_('Description'), render, text=COLUMN_DESCRIPTION) 116 self.append_column(column) 117 118 def __active_toggled(self, cell, path, model): 119 # get toggled iter 120 iter = model.get_iter((int(path),)) 121 active = model.get_value(iter, COLUMN_ACTIVE) 122 123 # toggle active 124 active = not active 125 126 # save value 127 model.set(iter, COLUMN_ACTIVE, active) 128 129 # notify changed 130 self.__changed = True 131 self.__emit_changed() 132 133 def __emit_changed(self, *args): 134 if self.__changed: 135 self.__changed = False 136 self.notify("dictionaries") 137 138 def get_dictionaries(self): 139 dicts = [] 140 for row in self.__model: 141 if (not row[COLUMN_SENSITIVE]): 142 continue; 143 if (row[COLUMN_ACTIVE]): 144 dicts.append(str(row[COLUMN_PHRASE_INDEX])) 145 146 return ';'.join(dicts) 147 148 def set_dictionaries(self, dicts): 149 # clean dictionaries 150 for row in self.__model: 151 if not row[COLUMN_SENSITIVE]: 152 continue 153 row[COLUMN_ACTIVE] = False 154 155 if not dicts: 156 self.__emit_changed() 157 return 158 159 for dict in dicts.split(";"): 160 dict = int(dict) 161 for row in self.__model: 162 if not row[COLUMN_SENSITIVE]: 163 continue 164 if dict == row[COLUMN_PHRASE_INDEX]: 165 row[COLUMN_ACTIVE] = True 166 self.__emit_changed() 167 168 def do_get_property(self, prop): 169 if prop.name == 'dictionaries': 170 return self.get_dictionaries() 171 else: 172 raise AttributeError('unknown property %s' % prop.name) 173 174 def do_set_property(self, prop, value): 175 if prop.name == "dictionaries": 176 self.set_dictionaries(value) 177 else: 178 raise AttributeError('unknown property %s' % prop.name) 179 180 181GObject.type_register(DictionaryTreeView) 182 183 184if __name__ == "__main__": 185 tree = DictionaryTreeView() 186 tree.set_dictionaries("") 187 w = Gtk.Window() 188 w.add(tree) 189 w.show_all() 190 Gtk.main() 191