#------------------------------------------------------------------------------
# Copyright (c) 2013, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
""" An example of using Conditional to generate the views.
This example shows how Conditional can be used to generate views for
objects which are not known until runtime.
<< autodoc-me >>
"""
import random
from enaml.core.api import Conditional, Looper
from enaml.widgets.api import (
Window, Container, Field, PushButton, CheckBox, Separator, ScrollArea
)
class Foo(object): pass
class Bar(object): pass
class Baz(object): pass
enamldef FooView(Container):
attr model: Foo
PushButton: text = str(model)
PushButton: text = str(model)
PushButton: text = str(model)
enamldef BarView(Container):
attr model: Bar
Field: text = str(model)
Field: text = str(model)
Field: text = str(model)
enamldef BazView(Container):
attr model: Baz
CheckBox: text = str(model)
CheckBox: text = str(model)
CheckBox: text = str(model)
CLASSES = [Foo, Bar, Baz]
def generate(n):
return [random.choice(CLASSES)() for _ in xrange(n)]
enamldef Main(Window): main:
attr models = generate(5)
Container:
CheckBox: show_bar:
text = 'Show Bar Views'
checked = True
PushButton:
text = 'Regenerate Models'
clicked :: main.models = generate(random.randint(1, 10))
ScrollArea:
Container:
Looper:
iterable << models
Conditional:
condition = isinstance(loop_item, Foo)
FooView:
model = loop_item
Conditional:
condition << isinstance(loop_item, Bar) and show_bar.checked
BarView:
model = loop_item
Conditional:
condition = isinstance(loop_item, Baz)
BazView:
model = loop_item
Conditional:
condition << (
(loop_index != len(main.models) - 1) and
(show_bar.checked if isinstance(loop_item, Bar) else True)
)
Separator:
orientation = 'horizontal'