Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column renderers #1478

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/reference/api/widgets/table.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ Reference
:members:
:undoc-members:
:inherited-members:
:exclude-members: Column
1 change: 1 addition & 0 deletions docs/reference/api/widgets/tree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ Reference
:members:
:undoc-members:
:inherited-members:
:exclude-members: Column
11 changes: 5 additions & 6 deletions examples/table/table/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ def startup(self):
table_data = bee_movies * 1000

self.table1 = toga.Table(
headings=headings,
columns=headings,
data=table_data,
accessors=["title", "year", "rating", "genre"],
style=Pack(
flex=1,
padding_right=5,
Expand All @@ -121,7 +122,7 @@ def startup(self):
)

self.table2 = toga.Table(
headings=headings,
columns=headings,
data=self.table1.data,
multiple_select=True,
style=Pack(flex=1, padding_left=5),
Expand Down Expand Up @@ -171,14 +172,12 @@ def startup(self):
def reduce_fontsize(self, widget):
font_size = int(self.lbl_fontsize.text) - 1
self.lbl_fontsize.text = str(font_size)
font = toga.Font("monospace", font_size, "italic")
self.table1._impl.set_font(font)
self.table1.style.font_size = font_size

def increase_fontsize(self, widget):
font_size = int(self.lbl_fontsize.text) + 1
self.lbl_fontsize.text = str(font_size)
font = toga.Font("monospace", font_size, "italic")
self.table1._impl.set_font(font)
self.table1.style.font_size = font_size


def main():
Expand Down
14 changes: 7 additions & 7 deletions examples/table_source/table_source/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,19 @@ def __getitem__(self, index):
def index(self, entry):
return self._filtered().index(entry)

# A listener that passes on all notifications, but only if the apply
# A listener that passes on all notifications, but only if they apply
# to the filtered data source
def insert(self, index, item):
# If the item exists in the filtered list, propegate the notification
# If the item exists in the filtered list, propagate the notification
for i, filtered_item in enumerate(self._filtered()):
if filtered_item == item:
# Propegate the insertion, with the position in the
# Propagate the insertion, with the position in the
# *filtered* list.
self._notify('insert', index=i, item=item)

def pre_remove(self, index, item):
# If the item exists in the filtered list, track that it is being
# removed; but don't propegate the removal notification until it has
# removed; but don't propagate the removal notification until it has
# been removed from the base data source
for i, filtered_item in enumerate(self._filtered()):
if filtered_item == item:
Expand All @@ -108,7 +108,7 @@ def pre_remove(self, index, item):

def remove(self, index, item):
# If the removed item previously existed in the filtered data source,
# propegate the removal notification.
# propagate the removal notification.
try:
i = self._removals.pop(item)
self._notify('remove', index=i, item=item)
Expand Down Expand Up @@ -150,14 +150,14 @@ def startup(self):
# of the second reads from the first.
# The headings are also in a different order.
self.table1 = toga.Table(
headings=['Year', 'Title', 'Rating', 'Genre'],
columns=['Year', 'Title', 'Rating', 'Genre'],
data=MovieSource(),
style=Pack(flex=1),
on_select=self.on_select_handler
)

self.table2 = toga.Table(
headings=['Rating', 'Title', 'Year', 'Genre'],
columns=['Rating', 'Title', 'Year', 'Genre'],
data=GoodMovieSource(self.table1.data),
style=Pack(flex=1)
)
Expand Down
2 changes: 1 addition & 1 deletion examples/tree/tree/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def startup(self):
self.label = toga.Label('Ready.', style=Pack(padding=10))

self.tree = toga.Tree(
headings=['Year', 'Title', 'Rating', 'Genre'],
columns=['Year', 'Title', 'Rating', 'Genre'],
on_select=self.on_select_handler,
style=Pack(flex=1)
)
Expand Down
23 changes: 20 additions & 3 deletions examples/tree_source/tree_source/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,15 @@ def can_have_children(self):
# this will trigger loading of children, if not yet done
return len(self.children) > 0

# Property that returns the first column value as (icon, label)
# Property that returns the item name
@property
def name(self):
return self._icon, self.path.name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment is now out of date; plus, the demo has now lost the icons.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. It will regain the icons once the API for that is set.

return self.path.name

# Property that returns the item name
@property
def icon(self):
return self._icon

# Property that returns modified date as str
@property
Expand Down Expand Up @@ -147,8 +152,20 @@ def startup(self):

self.fs_source = FileSystemSource(Path.cwd())

columns = [
toga.Tree.Column(
title="Name",
text_accessor="name",
icon_accessor="icon",
),
toga.Tree.Column(
title="Date Modified",
text_accessor="date_modified",
),
]

self.tree = toga.Tree(
headings=['Name', 'Date Modified'],
columns,
data=self.fs_source,
style=Pack(flex=1),
multiple_select=True,
Expand Down
2 changes: 2 additions & 0 deletions src/android/toga_android/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .widgets.slider import Slider
from .widgets.switch import Switch
from .widgets.table import Table
from .widgets.column import Column
from .widgets.textinput import TextInput
from .widgets.timepicker import TimePicker
from .widgets.webview import WebView
Expand All @@ -38,6 +39,7 @@ def not_implemented(feature):
"Button",
"Canvas",
"Command",
"Column",
"DatePicker",
"Font",
"Icon",
Expand Down
12 changes: 12 additions & 0 deletions src/android/toga_android/widgets/column.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from .base import Widget


class Column(Widget):
def create(self):
pass

def set_title(self, value):
pass

def set_editable(self, value):
pass
55 changes: 32 additions & 23 deletions src/android/toga_android/widgets/table.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from travertino.size import at_least
from toga.widgets.internal.column import DataRole

from ..libs.activity import MainActivity
from ..libs.android import R__attr
Expand Down Expand Up @@ -43,14 +44,13 @@ def onClick(self, view):


class Table(Widget):
table_layout = None
color_selected = None
color_unselected = None
selection = {}
_deleted_column = None
_font_impl = None

def create(self):

self.selection = {}
self._deleted_column = None

# get the selection color from the current theme
current_theme = MainActivity.singletonThis.getApplication().getTheme()
attrs = [R__attr.colorBackground, R__attr.colorControlHighlight]
Expand All @@ -68,6 +68,7 @@ def create(self):
parent_layout_params.gravity = Gravity.TOP
parent.setLayoutParams(parent_layout_params)
vscroll_view = ScrollView(self._native_activity)

# add vertical scroll view
vscroll_view_layout_params = LinearLayout__LayoutParams(
LinearLayout__LayoutParams.MATCH_PARENT,
Expand All @@ -79,6 +80,7 @@ def create(self):
TableLayout__Layoutparams.MATCH_PARENT,
TableLayout__Layoutparams.WRAP_CONTENT
)

# add horizontal scroll view
hscroll_view = HorizontalScrollView(self._native_activity)
hscroll_view_layout_params = LinearLayout__LayoutParams(
Expand All @@ -91,14 +93,15 @@ def create(self):
# add table layout to scrollbox
self.table_layout.setLayoutParams(table_layout_params)
hscroll_view.addView(self.table_layout)

# add scroll box to parent layout
parent.addView(vscroll_view, vscroll_view_layout_params)
self.native = parent
if self.interface.data is not None:
self.change_source(self.interface.data)

def change_source(self, source):
self.selection = {}
self.selection.clear()
self.table_layout.removeAllViews()
if source is not None:
self.table_layout.addView(self.create_table_header())
Expand All @@ -111,7 +114,7 @@ def clear_selection(self):
for i in range(self.table_layout.getChildCount()):
row = self.table_layout.getChildAt(i)
row.setBackgroundColor(self.color_unselected)
self.selection = {}
self.selection.clear()

def create_table_header(self):
table_row = TableRow(MainActivity.singletonThis)
Expand All @@ -120,16 +123,20 @@ def create_table_header(self):
TableRow__Layoutparams.WRAP_CONTENT
)
table_row.setLayoutParams(table_row_params)
for col_index in range(len(self.interface._accessors)):
if self.interface._accessors[col_index] == self._deleted_column:

for column in self.interface.columns:
if column is self._deleted_column:
continue

text_view = TextView(MainActivity.singletonThis)
text_view.setText(self.interface.headings[col_index])
text_view.setText(column.title)

if self._font_impl:
text_view.setTextSize(TypedValue.COMPLEX_UNIT_SP, self._font_impl.get_size())
text_view.setTypeface(self._font_impl.get_typeface(), Typeface.BOLD)
else:
text_view.setTypeface(text_view.getTypeface(), Typeface.BOLD)

text_view_params = TableRow__Layoutparams(
TableRow__Layoutparams.MATCH_PARENT,
TableRow__Layoutparams.WRAP_CONTENT
Expand All @@ -138,9 +145,13 @@ def create_table_header(self):
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):

row = self.interface.data[row_index]

table_row = TableRow(MainActivity.singletonThis)
table_row_params = TableRow__Layoutparams(
TableRow__Layoutparams.MATCH_PARENT,
Expand All @@ -150,14 +161,18 @@ def create_table_row(self, row_index):
table_row.setClickable(True)
table_row.setOnClickListener(TogaOnClickListener(impl=self))
table_row.setId(row_index)
for col_index in range(len(self.interface._accessors)):
if self.interface._accessors[col_index] == self._deleted_column:

for column in self.interface.columns:
if column is self._deleted_column:
continue

text_view = TextView(MainActivity.singletonThis)
text_view.setText(self.get_data_value(row_index, col_index))
text_view.setText(column.get_data_for_node(row, DataRole.Text))

if self._font_impl:
text_view.setTextSize(TypedValue.COMPLEX_UNIT_SP, self._font_impl.get_size())
text_view.setTypeface(self._font_impl.get_typeface(), self._font_impl.get_style())

text_view_params = TableRow__Layoutparams(
TableRow__Layoutparams.MATCH_PARENT,
TableRow__Layoutparams.WRAP_CONTENT
Expand All @@ -166,14 +181,8 @@ def create_table_row(self, row_index):
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):
if self.interface.data is None or self.interface._accessors is None:
return None
row_object = self.interface.data[row_index]
value = getattr(row_object, self.interface._accessors[col_index])
return value
return table_row

def get_selection(self):
selection = []
Expand Down Expand Up @@ -210,11 +219,11 @@ def set_on_select(self, handler):
def set_on_double_click(self, handler):
self.interface.factory.not_implemented('Table.set_on_double_click()')

def add_column(self, heading, accessor):
def add_column(self, column):
self.change_source(self.interface.data)

def remove_column(self, accessor):
self._deleted_column = accessor
def remove_column(self, column):
self._deleted_column = column
self.change_source(self.interface.data)
self._deleted_column = None

Expand Down
2 changes: 2 additions & 0 deletions src/cocoa/toga_cocoa/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from .widgets.splitcontainer import SplitContainer
from .widgets.switch import Switch
from .widgets.table import Table
from .widgets.column import Column
from .widgets.textinput import TextInput
from .widgets.tree import Tree
from .widgets.webview import WebView
Expand Down Expand Up @@ -57,6 +58,7 @@ def not_implemented(feature):
'Box',
'Button',
'Canvas',
'Column',
'DetailedList',
'Divider',
'ImageView',
Expand Down
7 changes: 7 additions & 0 deletions src/cocoa/toga_cocoa/libs/appkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,13 @@ class NSLineBreakMode(Enum):
# NSTableCellView.h
NSTableCellView = ObjCClass('NSTableCellView')

######################################################################
# NSTableColumn.h

NSTableColumnNoResizing = 0
NSTableColumnAutoresizingMask = 1 << 0
NSTableColumnUserResizingMask = 1 << 1

######################################################################
# NSTableView.h
NSTableColumn = ObjCClass('NSTableColumn')
Expand Down
22 changes: 22 additions & 0 deletions src/cocoa/toga_cocoa/widgets/column.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from rubicon.objc import objc_property

import uuid
from toga_cocoa.libs import NSTableColumn, at

from .base import Widget


class Column(Widget):
interface = objc_property(object, weak=True)
impl = objc_property(object, weak=True)

def create(self):
self.native = NSTableColumn.alloc().initWithIdentifier(at(str(uuid.uuid4())))
self.native.interface = self.interface
self.native.impl = self

def set_title(self, value):
self.native.headerCell.stringValue = value

def set_editable(self, value):
self.native.editable = value
Loading