fixes compile
[platform/upstream/ibus-libpinyin.git] / setup / dicttreeview.py
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
22 import gettext
23 from gi.repository import GObject
24 from gi.repository import Gtk
25
26 gettext.install('ibus-libpinyin')
27
28 (
29     RESERVED,
30     GB_DICTIONARY,
31     GBK_DICTIONARY,
32     MERGED_DICTIONARY,
33     ART_DICTIONARY,
34     CULTURE_DICTIONARY,
35     ECONOMY_DICTIONARY,
36     GEOLOGY_DICTIONARY,
37     HISTORY_DICTIONARY,
38     LIFE_DICTIONARY,
39     NATURE_DICTIONARY,
40     SCITECH_DICTIONARY,
41     SOCIETY_DICTIONARY,
42     SPORT_DICTIONARY,
43     RESERVED1,
44     USER_DICTIONARY
45 ) = range(16)
46
47 (
48 COLUMN_SENSITIVE,
49 COLUMN_PHRASE_INDEX,
50 COLUMN_DESCRIPTION,
51 COLUMN_ACTIVE
52 ) = range(4)
53
54 dictionaries = \
55     (
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)
67     )
68
69
70 class DictionaryTreeView(Gtk.TreeView):
71     __gtype_name__ = 'DictionaryTreeView'
72     __gproperties__ = {
73         'dictionaries': (
74             str,
75             'dictionaries',
76             'Enabled Dictionaries',
77             "",
78             GObject.PARAM_READWRITE
79         )
80     }
81
82     def __init__(self):
83         super(DictionaryTreeView, self).__init__()
84
85         self.__changed = False
86
87         self.set_headers_visible(True)
88
89         self.__model = self.__create_model()
90         self.set_model(self.__model)
91
92         self.__add_columns()
93
94     def __create_model(self):
95         model = Gtk.ListStore(bool, int, str, bool)
96
97         model.connect("row-changed", self.__emit_changed, "row-changed")
98
99         for dict in dictionaries:
100             iter = model.append()
101             model.set(iter,
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])
106
107         return model
108
109     def __add_columns(self):
110         # column for toggles
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)
115
116         # column for description
117         render = Gtk.CellRendererText()
118         column = Gtk.TreeViewColumn(_('Description'), render, text=COLUMN_DESCRIPTION)
119         self.append_column(column)
120
121     def __active_toggled(self, cell, path, model):
122         # get toggled iter
123         iter = model.get_iter((int(path),))
124         active = model.get_value(iter, COLUMN_ACTIVE)
125
126         # toggle active
127         active = not active
128
129         # save value
130         model.set(iter, COLUMN_ACTIVE, active)
131
132         # notify changed
133         self.__changed = True
134         self.__emit_changed()
135
136     def __emit_changed(self, *args):
137         if self.__changed:
138             self.__changed = False
139             self.notify("dictionaries")
140
141     def get_dictionaries(self):
142         dicts = []
143         for row in self.__model:
144             if (not row[COLUMN_SENSITIVE]):
145                 continue;
146             if (row[COLUMN_ACTIVE]):
147                 dicts.append(str(row[COLUMN_PHRASE_INDEX]))
148
149         return ';'.join(dicts)
150
151     def set_dictionaries(self, dicts):
152         # clean dictionaries
153         for row in self.__model:
154             if not row[COLUMN_SENSITIVE]:
155                 continue
156             row[COLUMN_ACTIVE] = False
157
158         if not dicts:
159             self.__emit_changed()
160             return
161
162         for dict in dicts.split(";"):
163             dict = int(dict)
164             for row in self.__model:
165                 if not row[COLUMN_SENSITIVE]:
166                     continue
167                 if dict == row[COLUMN_PHRASE_INDEX]:
168                     row[COLUMN_ACTIVE] = True
169         self.__emit_changed()
170
171     def do_get_property(self, prop):
172         if prop.name == 'dictionaries':
173             return self.get_dictionaries()
174         else:
175             raise AttributeError, 'unknown property %s' % prop.name
176
177     def do_set_property(self, prop, value):
178         if prop.name == "dictionaries":
179             self.set_dictionaries(value)
180         else:
181             raise AttributeError, 'unknown property %s' % prop.name
182
183
184 GObject.type_register(DictionaryTreeView)
185
186
187 if __name__ == "__main__":
188     tree = DictionaryTreeView()
189     tree.set_dictionaries("")
190     w = Gtk.Window()
191     w.add(tree)
192     w.show_all()
193     Gtk.main()