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

Attempt at a clearer example of data channels #785

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
70 changes: 52 additions & 18 deletions examples/data_view/row_table_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@

import logging

from traits.api import Bool, Dict, HasStrictTraits, Instance, Int, Str, List
from traits.api import Bool, Dict, HasStrictTraits, Instance, Int, List, Str

from pyface.api import ApplicationWindow, GUI, Image, ImageResource
from pyface.ui_traits import PyfaceColor
from pyface.data_view.data_models.api import (
AttributeDataAccessor, RowTableDataModel
)
from pyface.data_view.api import DataViewWidget, IDataViewWidget
from pyface.data_view.api import (
DataViewSetError, DataViewWidget, IDataViewWidget
)
from pyface.data_view.value_types.api import (
BoolValue, ColorValue, IntValue, TextValue
BoolValue, EditableValue, ColorValue, IntValue, TextValue
)

from example_data import (
Expand All @@ -30,22 +32,29 @@
logger = logging.getLogger(__name__)


flags = {
'Canada': ImageResource('ca.png'),
'UK': ImageResource('gb.png'),
'USA': ImageResource('us.png'),
}
# The data model

class Country(HasStrictTraits):

name = Str()

flag = Image()


countries = {
'Canada': Country(name='Canada', flag=ImageResource('ca.png')),
'UK': Country(name='UK', flag=ImageResource('gb.png')),
'USA': Country(name='USA', flag=ImageResource('us.png')),
}

# The data model

class Address(HasStrictTraits):

street = Str()

city = Str()

country = Str()
country = Instance(Country, allow_none=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
country = Instance(Country, allow_none=True)
country = Instance(Country, allow_none=False)

D'oh!



class Person(HasStrictTraits):
Expand All @@ -61,17 +70,42 @@ class Person(HasStrictTraits):
address = Instance(Address, ())


class CountryValue(TextValue):
class CountryValue(EditableValue):

countries = Dict(Str, Instance(Country))

def is_valid(self, model, row, column, value):
return value in self.countries

def get_editor_value(self, model, row, column):
country = model.get_value(row, column)
return country.name

def set_editor_value(self, model, row, column, value):
if self.is_valid(model, row, column, value):
country = self.countries[value]
model.set_value(row, column, country)
else:
raise DataViewSetError("Invalid value set: {!r}".format(value))

def get_text(self, model, row, column):
country = model.get_value(row, column)
return country.name

flags = Dict(Str, Image, update_value_type=True)
def set_text(self, model, row, column):
if self.is_valid(model, row, column, value):
country = self.countries[value]
model.set_value(row, column, country)
else:
raise DataViewSetError("Invalid value set: {!r}".format(value))

def has_image(self, model, row, column):
value = model.get_value(row, column)
return value in self.flags
country = model.get_value(row, column)
return country.flag is not None

def get_image(self, model, row, column):
value = model.get_value(row, column)
return self.flags[value]
country = model.get_value(row, column)
return country.flag


row_header_data = AttributeDataAccessor(
Expand Down Expand Up @@ -103,7 +137,7 @@ def get_image(self, model, row, column):
),
AttributeDataAccessor(
attr="address.country",
value_type=CountryValue(flags=flags),
value_type=CountryValue(countries=countries),
),
]

Expand Down Expand Up @@ -141,7 +175,7 @@ def _data_default(self):
address=Address(
street=street(),
city=city(),
country=country(),
country=countries[country()],
),
)
for i in range(10000)
Expand Down