Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LayoutWidget class and use it for newer widget classes #1016

Merged
merged 6 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 2 additions & 30 deletions pyface/data_view/tests/test_data_view_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from pyface.gui import GUI
from pyface.toolkit import toolkit
from pyface.testing.layout_widget_mixin import LayoutWidgetMixin
from pyface.window import Window

# This import results in an error without numpy installed
Expand All @@ -37,20 +38,7 @@


@requires_numpy
class TestWidget(unittest.TestCase, UnittestTools):

def setUp(self):
self.gui = GUI()

self.parent = Window()
self.parent._create()
self.addCleanup(self._destroy_parent)
self.gui.process_events()

self.widget = self._create_widget()

self.parent.open()
self.gui.process_events()
class TestWidget(LayoutWidgetMixin, unittest.TestCase):

def _create_widget(self):
self.data = np.arange(120.0).reshape(4, 5, 6)
Expand All @@ -60,22 +48,6 @@ def _create_widget(self):
data_model=self.model
)

def _create_widget_control(self):
self.widget._create()
self.addCleanup(self._destroy_widget)
self.parent.show(True)
self.gui.process_events()

def _destroy_parent(self):
self.parent.destroy()
self.gui.process_events()
self.parent = None

def _destroy_widget(self):
self.widget.destroy()
self.gui.process_events()
self.widget = None

def test_defaults(self):
self.assertTrue(self.widget.header_visible)

Expand Down
4 changes: 2 additions & 2 deletions pyface/fields/i_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

from traits.api import Any, HasTraits, Instance, Str

from pyface.i_widget import IWidget
from pyface.i_layout_widget import ILayoutWidget


class IField(IWidget):
class IField(ILayoutWidget):
""" The field interface.

A field is a widget that displays a value and (potentially) allows a user
Expand Down
38 changes: 2 additions & 36 deletions pyface/fields/tests/field_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,16 @@
# Thanks for using Enthought open source!


from traits.testing.api import UnittestTools
from pyface.testing.layout_widget_mixin import LayoutWidgetMixin

from pyface.action.api import Action, MenuManager
from pyface.gui import GUI
from pyface.window import Window


class FieldMixin(UnittestTools):
class FieldMixin(LayoutWidgetMixin):
""" Mixin which provides standard methods for all fields. """

def setUp(self):
self.gui = GUI()

self.parent = Window()
self.parent._create()
self.addCleanup(self._destroy_parent)
self.gui.process_events()

self.widget = self._create_widget()

self.parent.open()
self.gui.process_events()

def _create_widget(self):
raise NotImplementedError()

def _create_widget_control(self):
self.widget._create()
self.addCleanup(self._destroy_widget)
self.widget.show(True)
self.gui.process_events()

def _destroy_parent(self):
self.parent.destroy()
self.gui.process_events()
self.parent = None

def _destroy_widget(self):
self.widget.destroy()
self.gui.process_events()
self.widget = None

# Tests ------------------------------------------------------------------

def test_field_tooltip(self):
self._create_widget_control()
self.widget.tooltip = "New tooltip."
Expand Down
36 changes: 36 additions & 0 deletions pyface/i_layout_item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

from traits.api import Enum, Int, Interface, Tuple

#: Value that indicates the default size values should be used.
DEFAULT_SIZE = -1

#: Trait for sizes of widgets.
Size = Tuple(Int(DEFAULT_SIZE), Int(DEFAULT_SIZE))

#: Trait for size policy values.
SizePolicy = Enum("default", "fixed", "preferred", "expand")


class ILayoutItem(Interface):
""" An item that can participate in layout. """

#: The minimum size that the item can take.
minimum_size = Size

#: The maximum size that the item can take.
maximum_size = Size

#: Weight factor used to distribute extra space between widgets.
stretch = Tuple(Int, Int)

#: How the item should behave when more space is available.
size_policy = Tuple(SizePolicy, SizePolicy)
183 changes: 183 additions & 0 deletions pyface/i_layout_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

from traits.api import HasTraits, Int, Tuple

from pyface.i_layout_item import ILayoutItem, Size, SizePolicy
from pyface.i_widget import IWidget


class ILayoutWidget(IWidget, ILayoutItem):
""" Interface for widgets that can participate in layout.

Most widgets implement ILayoutWidget, but widgets like top-level windows,
menus, toolbars, etc. do not.
"""
pass


class MLayoutWidget(HasTraits):
""" A mixin for Widgets that can participate in layouts.

Most widgets implement ILayoutWidget, but widgets like top-level windows,
menus, toolbars, etc. do not.
"""

#: The minimum size that the widget can take.
minimum_size = Size

#: The maximum size that the widget can take.
maximum_size = Size

#: Weight factor used to distribute extra space between widgets.
stretch = Tuple(Int, Int)

#: How the widget should behave when more space is available.
size_policy = Tuple(SizePolicy, SizePolicy)

def _initialize_control(self):
""" Initialize the toolkit control. """
super()._initialize_control()
self._set_control_minimum_size(self.minimum_size)
self._set_control_maximum_size(self.maximum_size)
self._set_control_stretch(self.stretch)
self._set_control_size_policy(self.size_policy)

def _add_event_listeners(self):
""" Add trait observers and toolkit binding. """
super()._add_event_listeners()
self.observe(
self._minimum_size_updated,
"minimum_size",
dispatch="ui",
)
self.observe(
self._maximum_size_updated,
"maximum_size",
dispatch="ui",
)
self.observe(
self._stretch_updated,
"stretch",
dispatch="ui",
)
self.observe(
self._size_policy_updated,
"size_policy",
dispatch="ui",
)

def _remove_event_listeners(self):
""" Remove trait observers and toolkit binding. """
self.observe(
self._minimum_size_updated,
"minimum_size",
dispatch="ui",
remove=True,
)
self.observe(
self._maximum_size_updated,
"maximum_size",
dispatch="ui",
remove=True,
)
self.observe(
self._stretch_updated,
"stretch",
dispatch="ui",
remove=True,
)
self.observe(
self._size_policy_updated,
"size_policy",
dispatch="ui",
remove=True,
)
super()._remove_event_listeners()

def _minimum_size_updated(self, event):
""" Trait observer for minimum size. """
if self.control is not None:
self._set_control_minimum_size(event.new)

def _maximum_size_updated(self, event):
""" Trait observer for maximum size. """
if self.control is not None:
self._set_control_maximum_size(event.new)

def _stretch_updated(self, event):
""" Trait observer for stretch. """
if self.control is not None:
self._set_control_stretch(event.new)

def _size_policy_updated(self, event):
""" Trait observer for size policy. """
if self.control is not None:
self._set_control_size_policy(event.new)

def _set_control_minimum_size(self, size):
""" Set the minimum size of the control.

Toolkit implementations will need to override this method.
"""
raise NotImplementedError()

def _get_control_minimum_size(self):
""" Get the minimum size of the control.

Toolkit implementations will need to override this method.
This method is only used for testing.
"""
raise NotImplementedError()

def _set_control_maximum_size(self, size):
""" Set the maximum size of the control.

Toolkit implementations will need to override this method.
"""
raise NotImplementedError()

def _get_control_maximum_size(self):
""" Get the maximum size of the control.

Toolkit implementations will need to override this method.
This method is only used for testing.
"""
raise NotImplementedError()

def _set_control_stretch(self, stretch):
""" Set the stretch factor of the control.

Toolkit implementations will need to override this method.
"""
raise NotImplementedError()

def _get_control_stretch(self):
""" Get the stretch factor of the control.

Toolkit implementations will need to override this method.
This method is only used for testing.
"""
raise NotImplementedError()

def _set_control_size_policy(self, size_policy):
""" Set the size policy of the control.

Toolkit implementations will need to override this method.
"""
raise NotImplementedError()

def _get_control_size_policy(self):
""" Get the size policy of the control.

Toolkit implementations will need to override this method.
This method is only used for testing.
"""
raise NotImplementedError()
15 changes: 15 additions & 0 deletions pyface/layout_widget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# (C) Copyright 2005-2021 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

""" Multi-pane splitter widget. """

from .toolkit import toolkit_object

layout_widget = toolkit_object("layout_widget:LayoutWidget")
Empty file added pyface/testing/__init__.py
Empty file.
Loading