1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
# (C) Copyright 2005-2023 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!
""" The base interface for all pyface widgets. """
from traits.api import Any, Bool, HasTraits, Interface, Instance, Str
class IWidget(Interface):
""" The base interface for all pyface widgets.
Pyface widgets delegate to a toolkit specific control.
"""
#: The toolkit specific control that represents the widget.
control = Any()
#: The control's optional parent control.
parent = Any()
#: Whether or not the control is visible
visible = Bool(True)
#: Whether or not the control is enabled
enabled = Bool(True)
#: A tooltip for the widget.
tooltip = Str()
#: An optional context menu for the widget.
context_menu = Instance("pyface.action.menu_manager.MenuManager")
# ------------------------------------------------------------------------
# 'IWidget' interface.
# ------------------------------------------------------------------------
def show(self, visible):
""" Show or hide the widget.
Parameters
----------
visible : bool
Visible should be ``True`` if the widget should be shown.
"""
def enable(self, enabled):
""" Enable or disable the widget.
Parameters
----------
enabled : bool
The enabled state to set the widget to.
"""
def focus(self):
""" Set the keyboard focus to this widget.
"""
def has_focus(self):
""" Does the widget currently have keyboard focus?
Returns
-------
focus_state : bool
Whether or not the widget has keyboard focus.
"""
def create(self, parent=None):
""" Creates the toolkit specific control.
This method should create the control and assign it to the
:py:attr:`control` trait.
"""
def destroy(self):
""" Destroy the control if it exists. """
# ------------------------------------------------------------------------
# Protected 'IWidget' interface.
# ------------------------------------------------------------------------
def _create_control(self, parent):
""" Create toolkit specific control that represents the widget.
Parameters
----------
parent : toolkit control
The toolkit control to be used as the parent for the widget's
control.
Returns
-------
control : toolkit control
A control for the widget.
"""
def _add_event_listeners(self):
""" Set up toolkit-specific bindings for events """
def _remove_event_listeners(self):
""" Remove toolkit-specific bindings for events """
class MWidget(HasTraits):
""" The mixin class that contains common code for toolkit specific
implementations of the IWidget interface.
"""
#: A tooltip for the widget.
tooltip = Str()
#: An optional context menu for the widget.
context_menu = Instance("pyface.action.menu_manager.MenuManager")
def create(self, parent=None):
""" Creates the toolkit specific control.
This method should create the control and assign it to the
:py:attr:``control`` trait.
"""
if parent is not None:
self.parent = parent
self.control = self._create_control(self.parent)
self._initialize_control()
self._add_event_listeners()
def destroy(self):
""" Call clean-up code and destroy toolkit objects.
Subclasses should override to perform any additional clean-up, ensuring
that they call super() after that clean-up.
"""
if self.control is not None:
self._remove_event_listeners()
self.control = None
# ------------------------------------------------------------------------
# Protected 'IWidget' interface.
# ------------------------------------------------------------------------
def _create(self):
""" Creates the toolkit specific control.
The default implementation simply calls create()
"""
from warnings import warn
warn(
"The _create() method will be removed in a future version of "
"Pyface. Use create() instead.",
DeprecationWarning,
stacklevel=2,
)
self.create()
def _create_control(self, parent):
""" Create toolkit specific control that represents the widget.
Parameters
----------
parent : toolkit control
The toolkit control to be used as the parent for the widget's
control.
Returns
-------
control : toolkit control
A control for the widget.
"""
raise NotImplementedError()
def _initialize_control(self):
""" Perform any post-creation initialization for the control.
"""
self._set_control_tooltip(self.tooltip)
def _add_event_listeners(self):
""" Set up toolkit-specific bindings for events """
self.observe(self._tooltip_updated, "tooltip", dispatch="ui")
self.observe(
self._context_menu_updated, "context_menu", dispatch="ui"
)
if self.control is not None and self.context_menu is not None:
self._observe_control_context_menu()
def _remove_event_listeners(self):
""" Remove toolkit-specific bindings for events """
if self.control is not None and self.context_menu is not None:
self._observe_control_context_menu(remove=True)
self.observe(
self._context_menu_updated,
"context_menu",
dispatch="ui",
remove=True,
)
self.observe(
self._tooltip_updated, "tooltip", dispatch="ui", remove=True
)
# Toolkit control interface ---------------------------------------------
def _get_control_tooltip(self):
""" Toolkit specific method to get the control's tooltip. """
raise NotImplementedError()
def _set_control_tooltip(self, tooltip):
""" Toolkit specific method to set the control's tooltip. """
raise NotImplementedError()
def _observe_control_context_menu(self, remove=False):
""" Toolkit specific method to change the context menu observer.
This should use _handle_control_context_menu as the event handler.
Parameters
----------
remove : bool
Whether the context menu handler should be removed or added.
"""
raise NotImplementedError()
def _handle_control_context_menu(self, event):
""" Handle a context menu event.
Implementations should override this with a method suitable to be used
as a toolkit event handler that invokes a context menu.
The function signature will likely vary from toolkit to toolkit.
"""
raise NotImplementedError()
# Trait change handlers -------------------------------------------------
def _tooltip_updated(self, event):
tooltip = event.new
if self.control is not None:
self._set_control_tooltip(tooltip)
def _context_menu_updated(self, event):
if self.control is not None:
if event.new is None:
self._observe_control_context_menu(remove=True)
if event.old is None:
self._observe_control_context_menu()
|