Skip to content

Instantly share code, notes, and snippets.

@geojeff
Created August 31, 2013 15:55
Show Gist options
  • Save geojeff/6399106 to your computer and use it in GitHub Desktop.
Save geojeff/6399106 to your computer and use it in GitHub Desktop.
from kivy.models import SelectableDataItem
from kivy.uix.gridlayout import GridLayout
from kivy.uix.listview import ListView
from kivy.uix.listview import ListItemButton
from kivy.uix.objectview import ObjectView
from fixtures import fruit_categories, fruit_data_list_of_dicts
class CategoryItem(SelectableDataItem):
def __init__(self, **kwargs):
super(CategoryItem, self).__init__(**kwargs)
self.name = kwargs.get('name', '')
self.fruits = kwargs.get('fruits', [])
self.is_selected = kwargs.get('is_selected', False)
class FruitItem(SelectableDataItem):
def __init__(self, **kwargs):
super(FruitItem, self).__init__(**kwargs)
self.name = kwargs.get('name', '')
self.serving_size = kwargs.get('Serving Size', '')
self.data = kwargs.get('data', [])
self.is_selected = kwargs.get('is_selected', False)
# To instantiate CategoryItem and FruitItem instances, we use the dictionary-
# style fixtures data in fruit_data (See import above), which is
# also used by other list examples. The double asterisk usage here is for
# setting arguments from a dict in calls to instantiate the custom data item
# classes defined above.
# fruit_categories is a dict of dicts.
category_data_items = \
[CategoryItem(**fruit_categories[c]) for c in sorted(fruit_categories)]
# fruit_data_list_of_dicts is a list of dicts, already sorted.
fruit_data_items = \
[FruitItem(**fruit_dict) for fruit_dict in fruit_data_list_of_dicts]
# We end up with two normal lists of objects, to be used for two list views
# defined below.
class CascadingView(GridLayout):
'''Implementation of a master-detail style view, with a scrollable list
of fruit categories on the left, a list of fruits for the selected
category in the middle, and a detail view on the right.
'''
def __init__(self, **kwargs):
kwargs['cols'] = 3
super(CascadingView, self).__init__(**kwargs)
list_item_args_converter = \
lambda row_index, selectable: {'text': selectable.name,
'size_hint_y': None,
'height': 25}
fruit_categories_list_view = \
ListView(data=category_data_items,
args_converter=list_item_args_converter,
selection_mode='single',
allow_empty_selection=False,
cls=ListItemButton,
size_hint=(.2, 1.0))
fruits_list_view = \
ListView(data=Binding(
source=fruit_categories_list_view.adapter,
transform=(binding_transforms.TRANSFORM,
lambda v: [f in fruit_data_items
if f.name in category_data_items[v.index].fruits])),
args_converter=list_item_args_converter,
selection_mode='single',
allow_empty_selection=False,
cls=ListItemButton,
size_hint=(.2, 1.0))
fruit_view = ObjectView(
data=Binding(
source=fruit_view.adapter,
prop='selection',
mode=bindings.FIRST_ITEM,
transform=(binding_transforms.TRANSFORM,
lambda v: App.app().current_fruits_adapter.data[v.index])),
size_hint=(.6, 1.0))
self.add_widget(fruit_categories_list_view)
self.add_widget(fruits_list_view)
self.add_widget(fruit_view)
if __name__ == '__main__':
from kivy.base import runTouchApp
runTouchApp(CascadingView(width=800))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment