Skip to content

Commit

Permalink
Add support for updating the models on a ViewTable
Browse files Browse the repository at this point in the history
  • Loading branch information
sccolbert committed Apr 3, 2013
1 parent 51f3a3c commit 5bc1809
Showing 1 changed file with 68 additions and 23 deletions.
91 changes: 68 additions & 23 deletions enaml/widgets/view_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,27 +262,35 @@ class ViewTable(Control):
proxy = Typed(ProxyViewTable)

#--------------------------------------------------------------------------
# Private API
# Initialization API
#--------------------------------------------------------------------------
def _observe_orientation(self, change):
""" An orientation observer.
This observer will update the orientation of the table model
and then dispatch to the proxy updater.
def initialize(self):
""" Initialize the Table control.
"""
if self.is_initialized:
self.table_model.update(orientation=self.orientation)
self._update_proxy(change)
super(ViewTable, self).initialize()
self.rebuild_table()

#--------------------------------------------------------------------------
# Initialization API
# Public API
#--------------------------------------------------------------------------
def initialize(self):
""" Initialize the Table control.
def headers(self):
""" Get the TableHeaders defined as children of the table.
Returns
-------
result : generator
A generator which yields the child TableHeaders.
"""
for child in self.children:
if isinstance(child, ViewTableHeaders):
yield child

def rebuild_table(self):
""" Rebuild the underlying table model from scratch.
"""
super(ViewTable, self).initialize()
factory = self.factory
views = []
for model in self.models:
Expand All @@ -300,17 +308,54 @@ def initialize(self):
table_model.update(headers, views, self.orientation)

#--------------------------------------------------------------------------
# Public API
# Observers
#--------------------------------------------------------------------------
def headers(self):
""" Get the TableHeaders defined as children of the table.
def _observe_orientation(self, change):
""" Handle a change to the table orientation.
Returns
-------
result : generator
A generator which yields the child TableHeaders.
This observer will update the orientation of the table model
and then dispatch to the proxy updater.
"""
for child in self.children:
if isinstance(child, ViewTableHeaders):
yield child
if self.is_initialized:
self.table_model.update(orientation=self.orientation)
self._update_proxy(change)

def _observe_factory(self, change):
""" Handle and change to the view factory function.
This handler will rebuild the table model from scratch.
"""
if self.is_initialized:
self.rebuild_table()

def _observe_models(self, change):
""" Handle a change to the models list.
This handler will attempt to only rebuild the portion of the
table which has changed and will not create new views for the
models which already exist in the table.
"""
model_map = {}
table_model = self.table_model
for view in table_model.views:
model_map[view.model] = view

index = 0
views = []
factory = self.factory
headers = table_model.headers
for model in self.models:
view = model_map.get(model)
if view is None:
view = factory(model)
view.initialize()
view.resolve(headers)
view.table_model = table_model
view.index = index
views.append(view)
index += 1

table_model.update(views=views)

0 comments on commit 5bc1809

Please sign in to comment.