summaryrefslogtreecommitdiffstats
path: root/ibus/lookuptable.py
blob: 358981c3c07069eb319df4eed422a3549849b3df (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# vim:set et sts=4 sw=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2007-2009 Huang Peng <shawn.p.huang@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA

__all__ = (
        "LookupTable",
    )

import dbus
from serializable import *
from exception import *

class LookupTable(Serializable):
    __NAME__ = "IBusLookupTable"
    def __init__(self, page_size=5, cursor_pos=0, coursor_visible=True, round=False, candidates=None):
        super(LookupTable, self).__init__()
        self.__cursor_pos = cursor_pos
        self.__cursor_visible = True
        self.__round = round
        if candidates == None:
            self.__candidates = list()
        else:
            self.__candidates = candidates
        self.set_page_size(page_size)
        self.set_labels(None)

    def set_page_size(self, page_size):
        self.__page_size = page_size

    def get_page_size(self):
        return self.__page_size

    def get_current_page_size(self):
        nr_candidate = len(self.__candidates)
        nr_page, last_page_size = divmod(nr_candidate, self.__page_size)
        if self.__cursor_pos / self.__page_size == nr_page:
            return last_page_size
        else:
            return self.__page_size

    def set_labels(self, labels):
        self.__labels = labels

    def get_labels(self):
        return self.__labels

    def show_cursor(self, show = True):
        self.__cursor_visible = show

    def is_cursor_visible(self):
        return self.__cursor_visible

    def get_current_page_start(self):
        return (self.__cursor_pos / self.__page_size) * self.__page_size

    def set_cursor_pos(self, pos):
        if pos >= len(self.__candidates) or pos < 0:
            return False
        self.__cursor_pos = pos
        return True

    def get_cursor_pos(self):
        return self.__cursor_pos

    def get_cursor_pos_in_current_page(self):
        page, pos_in_page = divmod(self.__cursor_pos, self.__page_size)
        return pos_in_page

    def set_cursor_pos_in_current_page(self, pos):
        if pos < 0 or pos >= self.__page_size:
            return False
        pos += self.get_current_page_start()
        if pos >= len(self.__candidates):
            return False
        self.__cursor_pos = pos
        return True


    def page_up(self):
        if self.__cursor_pos < self.__page_size:
            if self.__round:
                nr_candidates = len(self.__candidates)
                max_page = nr_candidates / self.__page_size
                self.__cursor_pos += max_page * self.__page_size
                if self.__cursor_pos > nr_candidates - 1:
                    self.__cursor_pos = nr_candidates - 1
                return True
            else:
                return False

        self.__cursor_pos -= self.__page_size
        return True

    def page_down(self):
        current_page = self.__cursor_pos / self.__page_size
        nr_candidates = len(self.__candidates)
        max_page = nr_candidates / self.__page_size

        if current_page >= max_page:
            if self.__round:
                self.__cursor_pos %= self.__page_size
                return True
            else:
                return False

        pos = self.__cursor_pos + self.__page_size
        if pos >= nr_candidates:
            pos = nr_candidates - 1
        self.__cursor_pos = pos

        return True

    def cursor_up(self):
        if self.__cursor_pos == 0:
            if self.__round:
                self.__cursor_pos = len(self.__candidates) - 1
                return True
            else:
                return False

        self.__cursor_pos -= 1
        return True

    def cursor_down(self):
        if self.__cursor_pos == len(self.__candidates) - 1:
            if self.__round:
                self.__cursor_pos = 0
                return True
            else:
                return False

        self.__cursor_pos += 1
        return True

    def clean(self):
        self.__candidates = list()
        self.__cursor_pos = 0

    def append_candidate(self, text):
        self.__candidates.append(text)

    def get_candidate(self, index):
        return self.__candidates[index]

    def get_candidates_in_current_page(self):
        page = self.__cursor_pos / self.__page_size
        start_index = page * self.__page_size
        end_index = min((page + 1) * self.__page_size, len(self.__candidates))
        return self.__candidates[start_index:end_index]

    def get_current_candidate(self):
        return self.__candidates [self.__cursor_pos]

    def get_number_of_candidates(self):
        return len(self.__candidates)

    def __len__(self):
        return self.get_number_of_candidates()

    def serialize(self, struct):
        super(LookupTable, self).serialize(struct)
        struct.append(dbus.UInt32(self.__page_size))
        struct.append(dbus.UInt32(self.__cursor_pos))
        struct.append(dbus.Boolean(self.__cursor_visible))
        struct.append(dbus.Boolean(self.__round))
        candidates = map(lambda c: serialize_object(c), self.__candidates)
        struct.append(dbus.Array(candidates, signature="v"))

    def get_current_page_as_lookup_table(self):
        candidates = self.get_candidates_in_current_page()
        return LookupTable(self.__page_size,
                           self.__cursor_pos % self.__page_size,
                           self.__cursor_visible,
                           self.__round,
                           candidates)

    def deserialize(self, struct):
        super(LookupTable, self).deserialize(struct)

        self.__page_size = struct.pop(0)
        self.__cursor_pos = struct.pop(0)
        self.__cursor_visible = struct.pop(0)
        self.__round = struct.pop(0)
        self.__candidates = map(deserialize_object, struct.pop(0))


serializable_register(LookupTable)

def test():
    t = LookupTable()
    # attrs = AttrList()
    # attrs.append(AttributeBackground(RGB(233, 0,1), 0, 3))
    # attrs.append(AttributeUnderline(1, 3, 5))
    t.append_candidate("Hello")
    value = serialize_object(t)
    t = deserialize_object(value)
    t = t.get_current_page_as_lookup_table()
    value = serialize_object(t)
    t = deserialize_object(value)

if __name__ == "__main__":
    test()