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
|
# Copyright (c) 2007, Riverbank Computing Limited
# All rights reserved.
# Copyright (c) 2017, Enthought, Inc
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD license.
# However, when used with the GPL version of PyQt the additional terms described in the PyQt GPL exception also apply
#
# Author: Riverbank Computing Limited
# Description: <Enthought pyface package component>
# Major package imports.
from pyface.qt import QtCore, QtGui
# Enthought library imports.
from traits.api import Any, Bool, HasTraits, Instance, provides
# Local imports.
from pyface.i_widget import IWidget, MWidget
@provides(IWidget)
class Widget(MWidget, HasTraits):
""" The toolkit specific implementation of a Widget. See the IWidget
interface for the API documentation.
"""
# 'IWidget' interface ----------------------------------------------------
#: 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)
# Private interface ----------------------------------------------------
#: The event filter for the widget.
_event_filter = Instance(QtCore.QObject)
# ------------------------------------------------------------------------
# 'IWidget' interface.
# ------------------------------------------------------------------------
def show(self, visible):
""" Show or hide the widget.
Parameter
---------
visible : bool
Visible should be ``True`` if the widget should be shown.
"""
self.visible = visible
if self.control is not None:
self.control.setVisible(visible)
def enable(self, enabled):
""" Enable or disable the widget.
Parameter
---------
enabled : bool
The enabled state to set the widget to.
"""
self.enabled = enabled
if self.control is not None:
self.control.setEnabled(enabled)
def destroy(self):
self._remove_event_listeners()
if self.control is not None:
self.control.hide()
self.control.deleteLater()
self.control = None
def _add_event_listeners(self):
self.control.installEventFilter(self._event_filter)
def _remove_event_listeners(self):
if self._event_filter is not None:
if self.control is not None:
self.control.removeEventFilter(self._event_filter)
self._event_filter = None
# Trait change handlers --------------------------------------------------
def _visible_changed(self, new):
if self.control is not None:
self.show(new)
def _enabled_changed(self, new):
if self.control is not None:
self.enable(new)
def __event_filter_default(self):
return WidgetEventFilter(self)
class WidgetEventFilter(QtCore.QObject):
""" An internal class that watches for certain events on behalf of the
Widget instance.
"""
def __init__(self, widget):
""" Initialise the event filter. """
QtCore.QObject.__init__(self)
self._widget = widget
def eventFilter(self, obj, event):
""" Adds any event listeners required by the window. """
widget = self._widget
# Sanity check.
if obj is not widget.control:
return False
event_type = event.type()
if event_type in {QtCore.QEvent.Show, QtCore.QEvent.Hide}:
widget.visible = widget.control.isVisible()
return False
|