summaryrefslogtreecommitdiffstats
path: root/setup/dicttreeview.py
blob: fb33a7cdc059274d18971ca86f089bc4ef44ce4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# vim:set et ts=4 sts=4:
# -*- coding: utf-8 -*-
#
# ibus-libpinyin - Intelligent Pinyin engine based on libpinyin for IBus
#
# Copyright (c) 2012 Peng Wu <alexepico@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import gettext
from gi.repository import GObject
from gi.repository import Gtk

gettext.install('ibus-libpinyin')

(
    ART_DICTIONARY,
    CULTURE_DICTIONARY,
    ECONOMY_DICTIONARY,
    GEOLOGY_DICTIONARY,
    HISTORY_DICTIONARY,
    LIFE_DICTIONARY,
    NATURE_DICTIONARY,
    PEOPLE_DICTIONARY,
    SCIENCE_DICTIONARY,
    SOCIETY_DICTIONARY,
    SPORT_DICTIONARY,
    TECHNOLOGY_DICTIONARY,
) = range(4, 16)

(
COLUMN_SENSITIVE,
COLUMN_PHRASE_INDEX,
COLUMN_DESCRIPTION,
COLUMN_ACTIVE
) = range(4)

dictionaries = \
    (
    (True, ART_DICTIONARY, _("Art"), True),
    (True, CULTURE_DICTIONARY, _("Culture"), True),
    (True, ECONOMY_DICTIONARY, _("Economy"), True),
    (True, GEOLOGY_DICTIONARY, _("Geology"), True),
    (True, HISTORY_DICTIONARY, _("History"), True),
    (True, LIFE_DICTIONARY, _("Life"), True),
    (True, NATURE_DICTIONARY, _("Nature"), True),
    (True, PEOPLE_DICTIONARY, _("People"), True),
    (True, SCIENCE_DICTIONARY, _("Science"), True),
    (True, SOCIETY_DICTIONARY, _("Society"), True),
    (True, SPORT_DICTIONARY, _("Sport"), True),
    (True, TECHNOLOGY_DICTIONARY, _("Technology"), True),
    )


class DictionaryTreeView(Gtk.TreeView):
    __gtype_name__ = 'DictionaryTreeView'
    __gproperties__ = {
        'dictionaries': (
            str,
            'dictionaries',
            'Enabled Dictionaries',
            "",
            GObject.PARAM_READWRITE
        )
    }

    def __init__(self):
        super(DictionaryTreeView, self).__init__()

        self.__changed = False

        self.set_headers_visible(True)

        self.__model = self.__create_model()
        self.set_model(self.__model)

        self.__add_columns()

    def __create_model(self):
        model = Gtk.ListStore(bool, int, str, bool)

        model.connect("row-changed", self.__emit_changed, "row-changed")

        for dict in dictionaries:
            iter = model.append()
            model.set(iter,
                      COLUMN_SENSITIVE, dict[COLUMN_SENSITIVE],
                      COLUMN_PHRASE_INDEX, dict[COLUMN_PHRASE_INDEX],
                      COLUMN_DESCRIPTION, dict[COLUMN_DESCRIPTION],
                      COLUMN_ACTIVE, dict[COLUMN_ACTIVE])

        return model

    def __add_columns(self):
        # column for toggles
        renderer = Gtk.CellRendererToggle()
        renderer.connect('toggled', self.__active_toggled, self.__model)
        column = Gtk.TreeViewColumn(_('Active'), renderer, active=COLUMN_ACTIVE, sensitive=COLUMN_SENSITIVE)
        self.append_column(column)

        # column for description
        render = Gtk.CellRendererText()
        column = Gtk.TreeViewColumn(_('Description'), render, text=COLUMN_DESCRIPTION)
        self.append_column(column)

    def __active_toggled(self, cell, path, model):
        # get toggled iter
        iter = model.get_iter((int(path),))
        active = model.get_value(iter, COLUMN_ACTIVE)

        # toggle active
        active = not active

        # save value
        model.set(iter, COLUMN_ACTIVE, active)

        # notify changed
        self.__changed = True
        self.__emit_changed()

    def __emit_changed(self, *args):
        if self.__changed:
            self.__changed = False
            self.notify("dictionaries")

    def get_dictionaries(self):
        dicts = []
        for row in self.__model:
            if (not row[COLUMN_SENSITIVE]):
                continue;
            if (row[COLUMN_ACTIVE]):
                dicts.append(str(row[COLUMN_PHRASE_INDEX]))

        return ';'.join(dicts)

    def set_dictionaries(self, dicts):
        # clean dictionaries
        for row in self.__model:
            if not row[COLUMN_SENSITIVE]:
                continue
            row[COLUMN_ACTIVE] = False

        if not dicts:
            self.__emit_changed()
            return

        for dict in dicts.split(";"):
            dict = int(dict)
            for row in self.__model:
                if not row[COLUMN_SENSITIVE]:
                    continue
                if dict == row[COLUMN_PHRASE_INDEX]:
                    row[COLUMN_ACTIVE] = True
        self.__emit_changed()

    def do_get_property(self, prop):
        if prop.name == 'dictionaries':
            return self.get_dictionaries()
        else:
            raise AttributeError('unknown property %s' % prop.name)

    def do_set_property(self, prop, value):
        if prop.name == "dictionaries":
            self.set_dictionaries(value)
        else:
            raise AttributeError('unknown property %s' % prop.name)


GObject.type_register(DictionaryTreeView)


if __name__ == "__main__":
    tree = DictionaryTreeView()
    tree.set_dictionaries("")
    w = Gtk.Window()
    w.add(tree)
    w.show_all()
    Gtk.main()