Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
Update the TraitsItem community PR for merge with master.
Browse files Browse the repository at this point in the history
  • Loading branch information
sccolbert committed Dec 8, 2012
1 parent 944d549 commit 9cb9126
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 122 deletions.
111 changes: 74 additions & 37 deletions enaml/qt/qt_traits_item.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,57 @@
from .qt.QtCore import Qt, QMargins
from .qt.QtGui import QWidget, QVBoxLayout, QFrame
#------------------------------------------------------------------------------
# Copyright (c) 2012, Enthought, Inc.
# All rights reserved.
#
# Special thanks to Steven Silvester for contributing this module!
#------------------------------------------------------------------------------
from .qt.QtGui import QFrame
from .q_single_widget_layout import QSingleWidgetLayout
from .qt_control import QtControl


class QtTraitsItem(QtControl):
""" A Qt implementation of an Enaml TraitsItem.
"""
#: Internal storage for the traits model
_model = None

#: Internal storage for the traits view
_view = None

#: Internal storage for the traits handler
_handler = None

#: Internal storage for the generated traits UI object.
_ui = None

#--------------------------------------------------------------------------
# Setup Methods
#--------------------------------------------------------------------------
def create_widget(self, parent, tree):
""" Create the underlying label widget.
""" Create the underlying widget.
"""
widget = QFrame(parent)
layout = QVBoxLayout(widget)
layout.setContentsMargins(QMargins(0, 0, 0, 0))
layout = QSingleWidgetLayout()
widget.setLayout(layout)
return widget

def create(self, tree):
""" Create and initialize the underlying widget.
"""
super(QtTraitsItem, self).create(tree)
self.model = tree['model']
self.view = tree['view']
self.handler = tree['handler']
self.ui = None
self._model = tree['model']
self._view = tree['view']
self._handler = tree['handler']

def init_layout(self):
'''Create the Traits UI widget and add to our layout
'''
super(QtTraitsItem, self).init_layout()
# guard against using a named view that is not supported by the model
if isinstance(self.view, (str, unicode)):
if self.model.trait_view(self.view) is None:
self.view = ''
# remove any previous widget before adding a new one
if self.ui:
self.widget().layout().removeWidget(self.ui.control)
self.ui.control.hide()
self.ui = self.model.edit_traits(parent=self.widget(), view=self.view,
handler=self.handler, kind='subpanel')
# on qt, we must set this explicitly
self.ui.control.setParent(self.widget())
self.widget().layout().addWidget(self.ui.control)
# allow the widget to resize when the view is changed
size = self.ui.control.sizeHint()
self.set_minimum_size((size.width(), size.height()))
self.size_hint_updated()
""" Initialize the layout for the widget.
"""
super(QtTraitsItem, self).init_layout()
self.refresh_traits_widget(notify=False)

#--------------------------------------------------------------------------
# Message Handlers
Expand All @@ -58,19 +60,54 @@ def on_action_set_model(self, content):
""" Handle the 'set_model' action from the Enaml widget.
"""
self.model = content['model']
self.init_layout()
self._model = content['model']
self.refresh_traits_widget()

def on_action_set_view(self, content):
""" Handle the 'set_view' action from the Enaml widget.
"""
self._view = content['view']
self.refresh_traits_widget()

def on_action_set_handler(self, content):
""" Handle the 'set_handler' action from the Enaml widget.
"""
self.handler = content['handler']
self.init_layout()
self._handler = content['handler']
self.refresh_traits_widget()

def on_action_set_view(self, content):
""" Handle the 'set_view' action from the Enaml widget.
#--------------------------------------------------------------------------
# Widget Update Methods
#--------------------------------------------------------------------------
def refresh_traits_widget(self, notify=True):
""" Create the traits widget and update the underlying control.
Parameters
----------
notify : bool, optional
Whether to notify the layout system if the size hint of the
widget has changed. The default is True.
"""
self.view = content['view']
self.init_layout()
widget = self.widget()
model = self._model
if model is None:
control = None
else:
view = self._view
handler = self._handler
self._ui = ui = model.edit_traits(
parent=widget, view=view, handler=handler, kind='subpanel',
)
control = ui.control
if notify:
item = self.widget_item()
old_hint = item.sizeHint()
widget.layout().setWidget(control)
new_hint = item.sizeHint()
if old_hint != new_hint:
self.size_hint_updated()
else:
widget.layout().setWidget(control)

31 changes: 18 additions & 13 deletions enaml/widgets/traits_item.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
#------------------------------------------------------------------------------
# Copyright (c) 2011, Enthought, Inc.
# Copyright (c) 2012, Enthought, Inc.
# All rights reserved.
#
# Special thanks to Steven Silvester for contributing this module!
#------------------------------------------------------------------------------
from traits.api import HasTraits, Instance, Any, Trait, Str
# NOTE: There shall be no imports from traitsui in this module. Doing so
# will create an import dependency on traitsui for the rest of Enaml!
from traits.api import HasTraits, Instance

from .control import Control


class TraitsItem(Control):
""" A control to display a traitsui item
""" A control which can be used to embded a traits ui view.
"""
#: The item being displayed
#: The traits model being displayed. If no other view is given, the
#: view will be retrieved by calling `model.edit_traits()`.
model = Instance(HasTraits)

# optional : the desired handler for the model
handler = Instance('traitsui.api.Handler')
#: An optional traits ui View definition to use in lieu of the
#: default view generated by the model.
view = Instance('traitsui.api.View')

# optional: the desired view for the model
view = Trait('', Str, Instance('traitsui.api.View'))
#: An optional traits ui Handler definition to use in lieu of the
#: default handler generated by the model.
handler = Instance('traitsui.api.Handler')

#: How strongly a component hugs it's contents' width. TraitsItems ignore
#: the width hug by default, so they expand freely in width.
#: TraitsItem widgets expand freely in height and width by default.
hug_width = 'ignore'
hug_height = 'ignore'

#--------------------------------------------------------------------------
# Initialization
#--------------------------------------------------------------------------
def snapshot(self):
""" Returns the dict of creation attributes for the control.
""" Get the snapshot dictionary for the TraitsItem widget.
"""
snap = super(TraitsItem, self).snapshot()
Expand All @@ -39,8 +45,7 @@ def snapshot(self):
return snap

def bind(self):
""" A method called after initialization which allows the widget
to bind any event handlers necessary.
""" Bind the change handlers for the control.
"""
super(TraitsItem, self).bind()
Expand Down
112 changes: 75 additions & 37 deletions enaml/wx/wx_traits_item.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,58 @@
#------------------------------------------------------------------------------
# Copyright (c) 2012, Enthought, Inc.
# All rights reserved.
#
# Special thanks to Steven Silvester for contributing this module!
#------------------------------------------------------------------------------
import wx

from .wx_control import WxControl
from .wx_single_widget_sizer import wxSingleWidgetSizer


class WxTraitsItem(WxControl):
""" A Wx implementation of an Enaml TraitsUIItem.
""" A Wx implementation of an Enaml TraitsItem.
"""
#: Internal storage for the traits model
_model = None

#: Internal storage for the traits view
_view = None

#: Internal storage for the traits handler
_handler = None

#: Internal storage for the generated traits UI object.
_ui = None

#--------------------------------------------------------------------------
# Setup Methods
#--------------------------------------------------------------------------
def create_widget(self, parent, tree):
""" Create the underlying label widget.
""" Create the underlying widget.
"""
widget = wx.Panel(parent, -1, style=wx.CLIP_CHILDREN)
vbox = wx.BoxSizer(wx.VERTICAL)
widget.SetSizer(vbox)
return widget
widget = wx.Panel(parent)
sizer = wxSingleWidgetSizer()
widget.SetSizer(sizer)
return widget

def create(self, tree):
""" Create and initialize the underlying widget.
"""
super(WxTraitsItem, self).create(tree)
self.model = tree['model']
self.view = tree['view']
self.handler = tree['handler']
self.ui = None
self._model = tree['model']
self._view = tree['view']
self._handler = tree['handler']

def init_layout(self):
'''Create the Traits UI widget and add to our layout
'''
super(WxTraitsItem, self).init_layout()
# guard against using a named view that is not supported by the model
if isinstance(self.view, (str, unicode)):
if self.model.trait_view(self.view) is None:
self.view = ''
# remove any previous widget before adding a new one
if self.ui:
self.widget().GetSizer().Remove(self.ui.control)
self.ui.control.Hide()
self.ui = self.model.edit_traits(parent=self.widget(), view=self.view,
handler=self.handler, kind='subpanel')
self.widget().GetSizer().Add(self.ui.control, 1,
wx.LEFT | wx.TOP | wx.GROW)
# allow the widget to resize when the view is changed
size = self.ui.control.GetSize()
self.set_minimum_size((size.width, size.height))
self.size_hint_updated()
""" Initialize the layout for the widget.
"""
super(WxTraitsItem, self).init_layout()
self.refresh_traits_widget(notify=False)

#--------------------------------------------------------------------------
# Message Handlers
Expand All @@ -57,19 +61,53 @@ def on_action_set_model(self, content):
""" Handle the 'set_model' action from the Enaml widget.
"""
self.model = content['model']
self.init_layout()
self._model = content['model']
self.refresh_traits_widget()

def on_action_set_view(self, content):
""" Handle the 'set_view' action from the Enaml widget.
"""
self._view = content['view']
self.refresh_traits_widget()

def on_action_set_handler(self, content):
""" Handle the 'set_handler' action from the Enaml widget.
"""
self.handler = content['handler']
self.init_layout()
self._handler = content['handler']
self.refresh_traits_widget()

def on_action_set_view(self, content):
""" Handle the 'set_view' action from the Enaml widget.
#--------------------------------------------------------------------------
# Widget Update Methods
#--------------------------------------------------------------------------
def refresh_traits_widget(self, notify=True):
""" Create the traits widget and update the underlying control.
Parameters
----------
notify : bool, optional
Whether to notify the layout system if the size hint of the
widget has changed. The default is True.
"""
self.view = content['view']
self.init_layout()
widget = self.widget()
model = self._model
if model is None:
control = None
else:
view = self._view
handler = self._handler
self._ui = ui = model.edit_traits(
parent=widget, view=view, handler=handler, kind='subpanel',
)
control = ui.control
if notify:
old_hint = widget.GetBestSize()
widget.GetSizer().Add(control)
new_hint = widget.GetBestSize()
if old_hint != new_hint:
self.size_hint_updated()
else:
widget.GetSizer().Add(control)

Loading

0 comments on commit 9cb9126

Please sign in to comment.