forked from beeware/toga
/
table.py
223 lines (189 loc) · 8.01 KB
/
table.py
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
220
221
222
223
from travertino.size import at_least
from ..libs.activity import MainActivity
from ..libs.android import R__attr
from ..libs.android.graphics import Rect, Typeface
from ..libs.android.view import Gravity, OnClickListener, OnLongClickListener
from ..libs.android.widget import (
LinearLayout__LayoutParams,
ScrollView,
TableLayout,
TableLayout__Layoutparams,
TableRow,
TableRow__Layoutparams,
TextView,
)
from .base import Widget
class TogaOnClickListener(OnClickListener):
def __init__(self, impl):
super().__init__()
self.impl = impl
def onClick(self, view):
tr_id = view.getId()
if self.impl.interface.multiple_select:
if tr_id in self.impl.selection:
self.impl.remove_selection(tr_id)
else:
self.impl.add_selection(tr_id, view)
else:
self.impl.clear_selection()
self.impl.add_selection(tr_id, view)
self.impl.interface.on_select(None)
class TogaOnLongClickListener(OnLongClickListener):
def __init__(self, impl):
super().__init__()
self.impl = impl
def onLongClick(self, view):
self.impl.clear_selection()
index = view.getId()
self.impl.add_selection(index, view)
self.impl.interface.on_select(None)
self.impl.interface.on_activate(None, row=self.impl.interface.data[index])
return True
class Table(Widget):
table_layout = None
color_selected = None
color_unselected = None
_font_impl = None
def create(self):
# get the selection color from the current theme
attrs = [R__attr.colorBackground, R__attr.colorControlHighlight]
typed_array = self._native_activity.obtainStyledAttributes(attrs)
self.color_unselected = typed_array.getColor(0, 0)
self.color_selected = typed_array.getColor(1, 0)
typed_array.recycle()
# add vertical scroll view
self.native = vscroll_view = ScrollView(self._native_activity)
vscroll_view_layout_params = LinearLayout__LayoutParams(
LinearLayout__LayoutParams.MATCH_PARENT,
LinearLayout__LayoutParams.MATCH_PARENT,
)
vscroll_view_layout_params.gravity = Gravity.TOP
vscroll_view.setLayoutParams(vscroll_view_layout_params)
self.table_layout = TableLayout(MainActivity.singletonThis)
table_layout_params = TableLayout__Layoutparams(
TableLayout__Layoutparams.MATCH_PARENT,
TableLayout__Layoutparams.WRAP_CONTENT,
)
# add table layout to scrollbox
self.table_layout.setLayoutParams(table_layout_params)
vscroll_view.addView(self.table_layout)
def change_source(self, source):
self.selection = {}
self.table_layout.removeAllViews()
# StretchAllColumns mode causes a divide by zero error if there are no columns.
self.table_layout.setStretchAllColumns(bool(self.interface.accessors))
if source is not None:
if self.interface.headings is not None:
self.table_layout.addView(self.create_table_header())
for row_index in range(len(source)):
table_row = self.create_table_row(row_index)
self.table_layout.addView(table_row)
self.table_layout.invalidate()
def add_selection(self, index, table_row):
self.selection[index] = table_row
table_row.setBackgroundColor(self.color_selected)
def remove_selection(self, index):
table_row = self.selection.pop(index)
table_row.setBackgroundColor(self.color_unselected)
def clear_selection(self):
for index in list(self.selection):
self.remove_selection(index)
def create_table_header(self):
table_row = TableRow(MainActivity.singletonThis)
table_row_params = TableRow__Layoutparams(
TableRow__Layoutparams.MATCH_PARENT, TableRow__Layoutparams.WRAP_CONTENT
)
table_row.setLayoutParams(table_row_params)
for col_index in range(len(self.interface._accessors)):
text_view = TextView(MainActivity.singletonThis)
text_view.setText(self.interface.headings[col_index])
self._font_impl.apply(
text_view, text_view.getTextSize(), text_view.getTypeface()
)
text_view.setTypeface(
Typeface.create(
text_view.getTypeface(),
text_view.getTypeface().getStyle() | Typeface.BOLD,
)
)
text_view_params = TableRow__Layoutparams(
TableRow__Layoutparams.MATCH_PARENT, TableRow__Layoutparams.WRAP_CONTENT
)
text_view_params.setMargins(10, 5, 10, 5) # left, top, right, bottom
text_view_params.gravity = Gravity.START
text_view.setLayoutParams(text_view_params)
table_row.addView(text_view)
return table_row
def create_table_row(self, row_index):
table_row = TableRow(MainActivity.singletonThis)
table_row_params = TableRow__Layoutparams(
TableRow__Layoutparams.MATCH_PARENT, TableRow__Layoutparams.WRAP_CONTENT
)
table_row.setLayoutParams(table_row_params)
table_row.setClickable(True)
table_row.setOnClickListener(TogaOnClickListener(impl=self))
table_row.setLongClickable(True)
table_row.setOnLongClickListener(TogaOnLongClickListener(impl=self))
table_row.setId(row_index)
for col_index in range(len(self.interface._accessors)):
text_view = TextView(MainActivity.singletonThis)
text_view.setText(self.get_data_value(row_index, col_index))
self._font_impl.apply(
text_view, text_view.getTextSize(), text_view.getTypeface()
)
text_view_params = TableRow__Layoutparams(
TableRow__Layoutparams.MATCH_PARENT, TableRow__Layoutparams.WRAP_CONTENT
)
text_view_params.setMargins(10, 5, 10, 5) # left, top, right, bottom
text_view_params.gravity = Gravity.START
text_view.setLayoutParams(text_view_params)
table_row.addView(text_view)
return table_row
def get_data_value(self, row_index, col_index):
row_object = self.interface.data[row_index]
value = getattr(
row_object,
self.interface._accessors[col_index],
None,
)
if isinstance(value, tuple): # TODO: support icons
value = value[1]
if value is None:
value = self.interface.missing_value
return str(value)
def get_selection(self):
selection = sorted(self.selection)
if self.interface.multiple_select:
return selection
elif len(selection) == 0:
return None
else:
return selection[0]
def insert(self, index, item):
self.change_source(self.interface.data)
def clear(self):
self.change_source(self.interface.data)
def change(self, item):
self.change_source(self.interface.data)
def remove(self, index, item):
self.change_source(self.interface.data)
def scroll_to_row(self, index):
if (index != 0) and (self.interface.headings is not None):
index += 1
table_row = self.table_layout.getChildAt(index)
table_row.requestRectangleOnScreen(
Rect(0, 0, 0, table_row.getHeight()),
True, # Immediate, not animated
)
def insert_column(self, index, heading, accessor):
self.change_source(self.interface.data)
def remove_column(self, index):
self.change_source(self.interface.data)
def set_background_color(self, value):
self.set_background_simple(value)
def set_font(self, font):
self._font_impl = font._impl
self.change_source(self.interface.data)
def rehint(self):
self.interface.intrinsic.width = at_least(self.interface._MIN_WIDTH)
self.interface.intrinsic.height = at_least(self.interface._MIN_HEIGHT)