Add Makefile.am in ibus
[ibus.git] / ibus / lookuptable.py
blob15545350c115142f290f08dc77236e4e5d3927fe
1 import dbus
2 from attribute import *
3 from exception import *
5 class StringList (list):
6 def clean (self):
7 del self[:]
9 def to_dbus_value (self):
10 value = dbus.Array ([], signature="v")
11 for text, attrs in self:
12 value.append (dbus.Struct ((dbus.String (text), attrs.to_dbus_value ())))
13 return value
15 def from_dbus_value (self, value):
16 candidates = []
17 if not isinstance (value, dbus.Array):
18 raise dbus.Exception ("Candidates must from dbus.Array (a(sa(...))")
19 for candidate in value:
20 if not isinstance (candidate, dbus.Struct):
21 raise IBusException ("Candidates must from dbus.Array (a(sa(...)))")
22 if len (candidate) != 2 or \
23 not isinstance (candidate[0], dbus.String):
24 raise IBusException ("Candidates must from dbus.Array (a(sa(...)))")
25 text = candidate[0]
26 attrs = attr_list_from_dbus_value (candidate[1])
27 candidates.append ((text, attrs))
29 self.clean ()
30 self[:] = candidates
32 class LookupTable:
33 def __init__ (self, page_size = 5, labels = None):
34 self._cursor_visible = False
35 self._cursor_pos = 0
36 self._candidates = StringList ()
37 self.set_page_size (page_size)
39 def set_page_size (self, page_size):
40 self._page_size = page_size
42 def get_page_size (self):
43 return self._page_size
45 def get_current_page_size (self):
46 nr_candidate = len (self._candidates)
47 nr_page, last_page_size = divmod (nr_candidate, self._page_size)
48 if self._cursor_pos / self._page_size == nr_page:
49 return last_page_size
50 else:
51 return self._page_size
53 def set_labels (self, labels):
54 self._labels == labels
56 def show_cursor (self, show = True):
57 self._cursor_visible = show
59 def is_cursor_visible (self):
60 return self._cursor_visible
62 def get_current_page_start (self):
63 return (self._cursor_pos / self._page_size) * self._page_size
65 def set_cursor_pos (self, pos):
66 if pos >= len (self._candidates) or pos < 0:
67 return False
68 self._cursor_pos = pos
69 return True
71 def get_cursor_pos (self):
72 return self._cursor_pos
74 def get_cursor_pos_in_current_page (self):
75 page, pos_in_page = divmod (self._cursor_pos, self._page_size)
76 return pos_in_page
78 def set_cursor_pos_in_current_page (self, pos):
79 if pos < 0 or pos >= self._page_size:
80 return False
81 pos += self.get_current_page_start ()
82 if pos >= len (self._candidates):
83 return False
84 self._cursor_pos = pos
85 return True
88 def page_up (self):
89 if self._cursor_pos < self._page_size:
90 return False
92 self._cursor_pos -= self._page_size
93 return True
95 def page_down (self):
96 current_page = self._cursor_pos / self._page_size
97 nr_candidates = len (self._candidates)
98 max_page = nr_candidates / self._page_size
100 if current_page >= max_page:
101 return False
103 pos = self._cursor_pos + self._page_size
104 if pos >= nr_candidates:
105 return False
106 self._cursor_pos = pos
108 return True
110 def cursor_up (self):
111 if self._cursor_pos == 0:
112 return False
114 self._cursor_pos -= 1
115 return True
117 def cursor_down (self):
118 if self._cursor_pos == len (self._candidates) - 1:
119 return False
121 self._cursor_pos += 1
122 return True
124 def clean (self):
125 self._candidates.clean ()
126 self._cursor_pos = 0
128 def append_candidate (self, candidate, attrs = None):
129 if attrs == None:
130 attrs = AttrList ()
131 self._candidates.append ((candidate, attrs))
133 def get_candidate (self, index):
134 return self._candidates[index]
136 def get_canidates_in_current_page (self):
137 page = self._cursor_pos / self._page_size
138 start_index = page * self._page_size
139 end_index = min ((page + 1) * self._page_size, len (self._candidates))
140 return self._candidates[start_index:end_index]
142 def get_current_candidate (self):
143 return self._candidates [self._cursor_pos]
145 def get_number_of_candidates (self):
146 return len (self._candidates)
148 def to_dbus_value (self):
149 value = (dbus.Int32 (self._page_size),
150 dbus.Int32 (self._cursor_pos),
151 dbus.Boolean (self._cursor_visible),
152 self._candidates.to_dbus_value ())
153 return dbus.Struct (value)
155 def from_dbus_value (self, value):
156 if not isinstance (value, dbus.Struct):
157 raise dbus.Exception ("LookupTable must from dbus.Struct (uuba(...))")
159 if len (value) != 4 or \
160 not isinstance (value[0], dbus.Int32) or \
161 not isinstance (value[1], dbus.Int32) or \
162 not isinstance (value[2], dbus.Boolean):
163 raise dbus.Exception ("LookupTable must from dbus.Struct (uuba(...))")
165 self._candidates.from_dbus_value (value[3])
166 self._page_size = value[0]
167 self._cursor_pos = value[1]
168 self._cursor_visible = value[2]
170 def lookup_table_from_dbus_value (value):
171 lookup_table = LookupTable ()
172 lookup_table.from_dbus_value (value)
173 return lookup_table
175 def unit_test ():
176 t = LookupTable ()
177 # attrs = AttrList ()
178 # attrs.append (AttributeBackground (RGB (233, 0,1), 0, 3))
179 # attrs.append (AttributeUnderline (1, 3, 5))
180 t.append_candidate ("Hello")
181 value = t.to_dbus_value ()
182 print value
183 t = lookup_table_from_dbus_value (value)
185 if __name__ == "__main__":
186 unit_test ()