Skip to content

Commit

Permalink
Add a constraint list with selection to the constraints demo.
Browse files Browse the repository at this point in the history
This should help with the layout helper debugging...
  • Loading branch information
jwiggins authored and brett-patterson committed Jun 25, 2014
1 parent 1618698 commit 520167e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
2 changes: 0 additions & 2 deletions enable/constraints_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ def __layout_constraints_changed(self, name, old, new):
""" Invalidate the layout when the private constraints list changes.
"""
self._layout_manager.replace_constraints(old, new)
if self.debug:
self._debug_overlay.selected_constraints = new
self.relayout()

def __components_items_changed(self, event):
Expand Down
4 changes: 3 additions & 1 deletion enable/layout/debug_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DebugConstraintsOverlay(AbstractOverlay):
boxes = Any()

# Style options for the lines.
term_color = ColorTrait('lightblue')
term_color = ColorTrait('orange')
term_line_style = LineStyle('solid')

def update_from_constraints(self, layout_mgr):
Expand All @@ -76,7 +76,9 @@ def overlay(self, other_component, gc, view_bounds=None, mode="normal"):
"""
if len(self.selected_constraints) == 0:
return
origin = other_component.position
with gc:
gc.translate_ctm(*origin)
gc.set_stroke_color(self.term_color_)
gc.set_line_dash(self.term_line_style_)
gc.set_line_width(3)
Expand Down
51 changes: 44 additions & 7 deletions examples/enable/constraints_demo.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,51 @@

from enable.api import Component, ComponentEditor, ConstraintsContainer
from enable.layout.layout_helpers import hbox, align, grid
from traits.api import HasTraits, Instance
from traitsui.api import Item, View
from traits.api import HasTraits, Any, Instance, List, Property
from traitsui.api import Item, View, HGroup, TabularEditor
from traitsui.tabular_adapter import TabularAdapter


class ConstraintAdapter(TabularAdapter):
""" Display Constraints in a TabularEditor.
"""
columns = [('Constraint', 'id')]
id_text = Property
def _get_id_text(self):
return self.item.__repr__()


class Demo(HasTraits):
canvas = Instance(Component)

constraints = Property(List)

selected_constraints = Any

traits_view = View(
Item('canvas',
editor=ComponentEditor(),
show_label=False,
HGroup(
Item('constraints',
editor=TabularEditor(
adapter=ConstraintAdapter(),
editable=False,
multi_select=True,
selected='selected_constraints',
),
show_label=False,
),
Item('canvas',
editor=ComponentEditor(),
show_label=False,
),
),
resizable=True,
title="Constraints Demo",
width=500,
width=1000,
height=500,
)

def _canvas_default(self):
parent = ConstraintsContainer(bounds=(500,500), debug=True)
parent = ConstraintsContainer(bounds=(500,500), padding=20, debug=True)

hugs = {'hug_width':'weak', 'hug_height':'weak'}
one = Component(id="one", bgcolor=0xFF0000, **hugs)
Expand All @@ -39,6 +64,18 @@ def _canvas_default(self):

return parent

def _get_constraints(self):
return list(self.canvas._layout_manager._constraints)

def _selected_constraints_changed(self, new):
if new is None or new == []:
return

if self.canvas.debug:
canvas = self.canvas
canvas._debug_overlay.selected_constraints = new
canvas.request_redraw()


if __name__ == "__main__":
Demo().configure_traits()

0 comments on commit 520167e

Please sign in to comment.