Skip to content

Commit

Permalink
add limit_width and limit_height virtual constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
sccolbert committed Oct 30, 2013
1 parent ba937da commit 8722be9
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 18 deletions.
4 changes: 4 additions & 0 deletions enaml/qt/qt_constraints_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,15 @@ def _default_size_hint_cns(self):
cns.append((d.width == width_hint) | d.hug_width)
if d.resist_width != 'ignore':
cns.append((d.width >= width_hint) | d.resist_width)
if d.limit_width != 'ignore':
cns.append((d.width <= width_hint) | d.limit_width)
if height_hint >= 0:
if d.hug_height != 'ignore':
cns.append((d.height == height_hint) | d.hug_height)
if d.resist_height != 'ignore':
cns.append((d.height >= height_hint) | d.resist_height)
if d.limit_height != 'ignore':
cns.append((d.height <= height_hint) | d.limit_height)
return cns

#--------------------------------------------------------------------------
Expand Down
25 changes: 21 additions & 4 deletions enaml/qt/qt_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ def hard_constraints(d):
return [d.left >= 0, d.top >= 0, d.width >= 0, d.height >= 0]


def can_expand_in_width(d):
""" Get whether a declarative container can expand in width.
"""
expand = ('ignore', 'weak')
return d.hug_width in expand and d.limit_width in expand


def can_expand_in_height(d):
""" Get whether a declarative container can expand in height.
"""
expand = ('ignore', 'weak')
return d.hug_height in expand and d.limit_height in expand


class QtContainer(QtFrame, ProxyContainer):
""" A Qt implementation of an Enaml ProxyContainer.
Expand Down Expand Up @@ -574,14 +590,15 @@ def compute_max_size(self):
"""
d = self.declaration
expanding = ('ignore', 'weak')
if d.hug_width in expanding and d.hug_height in expanding:
expand_w = can_expand_in_width(d)
expand_h = can_expand_in_height(d)
if expand_w and expand_h:
return QSize(16777215, 16777215)
if self._owns_layout and self._layout_manager is not None:
w, h = self._layout_manager.get_max_size(d.width, d.height)
if w < 0 or d.hug_width in expanding:
if w < 0 or expand_w:
w = 16777215
if h < 0 or d.hug_height in expanding:
if h < 0 or expand_h:
h = 16777215
return QSize(w, h)
return QSize(16777215, 16777215)
46 changes: 36 additions & 10 deletions enaml/widgets/constraints_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,27 +115,53 @@ def _default_h_center(self):
return self.left + self.width / 2.0

#: How strongly a component hugs it's width hint. Valid strengths
#: are 'weak', 'medium', 'strong', 'required' and 'ignore'. Default
#: is 'strong'. This can be overridden on a per-control basis to
#: specify a logical default for the given control.
#: are 'weak', 'medium', 'strong', 'required' and 'ignore'. The
#: default is 'strong'. This can be overridden on a per-control
#: basis to specify a logical default for the given control. This
#: is equivalent to the following constraint:
#: (width == hint) | hug_width
hug_width = d_(PolicyEnum('strong'))

#: How strongly a component hugs it's height hint. Valid strengths
#: are 'weak', 'medium', 'strong', 'required' and 'ignore'. Default
#: is 'strong'. This can be overridden on a per-control basis to
#: specify a logical default for the given control.
#: are 'weak', 'medium', 'strong', 'required' and 'ignore'. The
#: default is 'strong'. This can be overridden on a per-control
#: basis to specify a logical default for the given control. This
#: is equivalent to the following constraint:
#: (height == hint) | hug_height
hug_height = d_(PolicyEnum('strong'))

#: How strongly a component resists clipping its contents. Valid
#: How strongly a component resists clipping its width hint. Valid
#: strengths are 'weak', 'medium', 'strong', 'required' and 'ignore'.
#: The default is 'strong' for width.
#: The default is 'strong'. This can be overridden on a per-control
#: basis to specify a logical default for the given control. This
#: is equivalent to the following constraint:
#: (width >= hint) | resist_width
resist_width = d_(PolicyEnum('strong'))

#: How strongly a component resists clipping its contents. Valid
#: How strongly a component resists clipping its height hint. Valid
#: strengths are 'weak', 'medium', 'strong', 'required' and 'ignore'.
#: The default is 'strong' for height.
#: The default is 'strong'. This can be overridden on a per-control
#: basis to specify a logical default for the given control. This
#: is equivalent to the following constraint:
#: (height >= hint) | resist_height
resist_height = d_(PolicyEnum('strong'))

#: How strongly a component resists expanding its width hint. Valid
#: strengths are 'weak', 'medium', 'strong', 'required' and 'ignore'.
#: The default is 'ignore'. This can be overridden on a per-control
#: basis to specify a logical default for the given control. This
#: is equivalent to the following constraint:
#: (width <= hint) | limit_width
limit_width = d_(PolicyEnum('ignore'))

#: How strongly a component resists expanding its height hint. Valid
#: strengths are 'weak', 'medium', 'strong', 'required' and 'ignore'.
#: The default is 'strong'. This can be overridden on a per-control
#: basis to specify a logical default for the given control. This
#: is equivalent to the following constraint:
#: (height <= hint) | limit_height
limit_height = d_(PolicyEnum('ignore'))

#: A reference to the ProxyConstraintsWidget object.
proxy = Typed(ProxyConstraintsWidget)

Expand Down
4 changes: 4 additions & 0 deletions enaml/wx/wx_constraints_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,15 @@ def _default_size_hint_cns(self):
cns.append((d.width == width_hint) | d.hug_width)
if d.resist_width != 'ignore':
cns.append((d.width >= width_hint) | d.resist_width)
if d.limit_width != 'ignore':
cns.append((d.width <= width_hint) | d.limit_width)
if height_hint >= 0:
if d.hug_height != 'ignore':
cns.append((d.height == height_hint) | d.hug_height)
if d.resist_height != 'ignore':
cns.append((d.height >= height_hint) | d.resist_height)
if d.limit_height != 'ignore':
cns.append((d.height <= height_hint) | d.limit_height)
return cns

#--------------------------------------------------------------------------
Expand Down
25 changes: 21 additions & 4 deletions enaml/wx/wx_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ def hard_constraints(d):
return [d.left >= 0, d.top >= 0, d.width >= 0, d.height >= 0]


def can_expand_in_width(d):
""" Get whether a declarative container can expand in width.
"""
expand = ('ignore', 'weak')
return d.hug_width in expand and d.limit_width in expand


def can_expand_in_height(d):
""" Get whether a declarative container can expand in height.
"""
expand = ('ignore', 'weak')
return d.hug_height in expand and d.limit_height in expand


class WxContainer(WxFrame, ProxyContainer):
""" A Wx implementation of an Enaml ProxyContainer.
Expand Down Expand Up @@ -560,14 +576,15 @@ def compute_max_size(self):
"""
d = self.declaration
expanding = ('ignore', 'weak')
if d.hug_width in expanding and d.hug_height in expanding:
expand_w = can_expand_in_width(d)
expand_h = can_expand_in_height(d)
if expand_w and expand_h:
return wx.Size(-1, -1)
if self._owns_layout and self._layout_manager is not None:
w, h = self._layout_manager.get_max_size(d.width, d.height)
if w < 0 or d.hug_width in expanding:
if w < 0 or expand_w:
w = -1
if h < 0 or d.hug_height in expanding:
if h < 0 or expand_h:
h = -1
return wx.Size(w, h)
return wx.Size(-1, -1)

0 comments on commit 8722be9

Please sign in to comment.