Skip to content

Commit

Permalink
add VGroup and HGroup convenience widgets
Browse files Browse the repository at this point in the history
The are convenience Container sublasses which make it easier to create
horizontal and vertical layouts without resort to custom 'constraint'
declarations.
  • Loading branch information
sccolbert committed Mar 9, 2014
1 parent a15e0f1 commit aed5ddd
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
2 changes: 2 additions & 0 deletions enaml/widgets/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from .form import Form
from .frame import Border
from .group_box import GroupBox
from .h_group import HGroup
from .html import Html
from .image_view import ImageView
from .label import Label
Expand Down Expand Up @@ -57,6 +58,7 @@
from .time_selector import TimeSelector
from .timer import Timer
from .tool_bar import ToolBar
from .v_group import VGroup
from .vtk_canvas import VTKCanvas
from .web_view import WebView
from .window import Window
65 changes: 65 additions & 0 deletions enaml/widgets/h_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#------------------------------------------------------------------------------
# Copyright (c) 2014, 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.
#------------------------------------------------------------------------------
from atom.api import Bool, Int, Typed, observe

from enaml.core.declarative import d_
from enaml.layout.layout_helpers import align, hbox
from enaml.layout.spacers import Spacer

from .container import Container


class HGroup(Container):
""" A Container subclass which groups child widgets horizontally.
User constraints are applied *in addition* to the horizontal group
constraints. Widgets are aligned along their vertical center.
"""
#: The horizontal spacing to place between widgets.
spacing = d_(Int(10))

#: The optional spacer to add as the first layout item.
leading_spacer = d_(Typed(Spacer))

#: The optional spacer to add as the last layout item.
trailing_spacer = d_(Typed(Spacer))

#: Whether to align the widths of the widgets.
align_widths = d_(Bool(True))

#--------------------------------------------------------------------------
# Observers
#--------------------------------------------------------------------------
@observe('spacing', 'leading_spacer', 'trailing_spacer', 'align_widths')
def _layout_invalidated(self, change):
""" A private observer which invalidates the layout.
"""
# The superclass handler is sufficient.
super(HGroup, self)._layout_invalidated(change)

#--------------------------------------------------------------------------
# Layout Constraints
#--------------------------------------------------------------------------
def layout_constraints(self):
""" The constraints generation for a HGroup.
This method supplies horizontal group constraints for the
children of the container in addition to any user-supplied
constraints.
"""
widgets = self.visible_widgets()
items = [self.leading_spacer] + widgets + [self.trailing_spacer]
cns = self.constraints[:]
cns.append(hbox(*items, spacing=self.spacing))
cns.append(align('v_center', *widgets))
if self.align_widths:
cns.append(align('width', *widgets))
return cns
60 changes: 60 additions & 0 deletions enaml/widgets/v_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#------------------------------------------------------------------------------
# Copyright (c) 2014, 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.
#------------------------------------------------------------------------------
from atom.api import Int, Typed, observe

from enaml.core.declarative import d_
from enaml.layout.layout_helpers import align, vbox
from enaml.layout.spacers import Spacer

from .container import Container


class VGroup(Container):
""" A Container subclass which groups child widgets vertically.
User constraints are applied *in addition* to the vertical group
constraints. Widgets are aligned along their left edge.
"""
#: The vertical spacing to place between widgets.
spacing = d_(Int(10))

#: The optional spacer to add as the first layout item.
leading_spacer = d_(Typed(Spacer))

#: The optional spacer to add as the last layout item.
trailing_spacer = d_(Typed(Spacer))

#--------------------------------------------------------------------------
# Observers
#--------------------------------------------------------------------------
@observe('spacing', 'leading_spacer', 'trailing_spacer')
def _layout_invalidated(self, change):
""" A private observer which invalidates the layout.
"""
# The superclass handler is sufficient.
super(VGroup, self)._layout_invalidated(change)

#--------------------------------------------------------------------------
# Layout Constraints
#--------------------------------------------------------------------------
def layout_constraints(self):
""" The constraints generation for a VGroup.
This method supplies left-aligned vertical group constraints for
the children of the container in addition to any user-supplied
constraints.
"""
widgets = self.visible_widgets()
items = [self.leading_spacer] + widgets + [self.trailing_spacer]
cns = self.constraints[:]
cns.append(vbox(*items, spacing=self.spacing))
cns.append(align('left', *widgets))
return cns

0 comments on commit aed5ddd

Please sign in to comment.