From b5b87172702a5f1eee4d32566ce6b2c3b1e0f67f Mon Sep 17 00:00:00 2001 From: Cyrille Bougot Date: Thu, 8 Oct 2020 22:01:46 +0200 Subject: [PATCH 1/5] When some wx controls (combo-box, edit, etc.) have an associated labels, disable the associated label when the control is disabled. --- source/gui/guiHelper.py | 57 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/source/gui/guiHelper.py b/source/gui/guiHelper.py index e0ccd3bcbc9..fcd0f6b89bd 100644 --- a/source/gui/guiHelper.py +++ b/source/gui/guiHelper.py @@ -154,6 +154,50 @@ def associateElements( firstElement, secondElement): return sizer + +class BaseLabeledControl: + """Base class used to create a labeled control subclass + A labeled control subclass allows to create a control with an associated label. + The associated label is enabled and disabled when the main control is enabled or disabled (respectively). + + Example usage: + + class LabeledChoice(BaseLabeledControl, wx.Choice): + pass + myChoice = LabeledChoice( + parent, + labelText='Choose an option', + wxCtrlClass=wx.Choice, + choices=['Option 1', 'Option 2', 'Option 3'], + ) + """ + + def __init__(self, parent, labelText, wxCtrlClass, **kwargs): + """ @param parent: An instance of the parent wx window. EG wx.Dialog + @param labelText: The text to associate with a wx control. + @type labelText: string + @param wxCtrlClass: The class to associate with the label, eg: wx.TextCtrl + @type wxCtrlClass: class + @param kwargs: The keyword arguments used to instantiate the wxCtrlClass + """ + self.label = wx.StaticText(parent, label=labelText) + if self.__class__ == BaseLabeledControl: + raise TypeError(f"BaseLabeledControl should be subclassed and should also inherit from {wxCtrlClass.__name__}.") + # Check that the final class also inherits from wxCtrlClass + if not isinstance(self, wxCtrlClass): + raise TypeError(f"{self.__class__.__name__} should be a subclass of {wxCtrlClass.__name__}.") + self.controlClass = wxCtrlClass + self.controlClass.__init__(self, parent, **kwargs) + + def Enable(self, state): + self.controlClass.Enable(self, state) + self.label.Enable(state) + + def Disable(self): + self.controlClass.Disable(self) + self.label.Disable() + + class LabeledControlHelper(object): """ Represents a Labeled Control. Provides a class to create and hold on to the objects and automatically associate the two controls together. @@ -168,9 +212,16 @@ def __init__(self, parent, labelText, wxCtrlClass, **kwargs): @param kwargs: The keyword arguments used to instantiate the wxCtrlClass """ object.__init__(self) - self._label = wx.StaticText(parent, label=labelText) - self._ctrl = wxCtrlClass(parent, **kwargs) - self._sizer = associateElements(self._label, self._ctrl) + # Dynamically create a class inheriting from BaseLabeledControl and wxCtrlClass. + # This class acts as wxCtrlClass but overloads the Enable and Disable methods + # in order to also enable or disable self._label at the same time as the control. + LabeledControl = type( + f'Labeled{wxCtrlClass.__name__}', + (BaseLabeledControl, wxCtrlClass), + {}, + ) + self._ctrl = LabeledControl(parent, labelText=labelText, wxCtrlClass=wxCtrlClass, **kwargs) + self._sizer = associateElements(self._ctrl.label, self._ctrl) @property def control(self): From 1bd616933ee6bd2238007a5f1cce07c8fba83a76 Mon Sep 17 00:00:00 2001 From: Cyrille Bougot Date: Thu, 5 Nov 2020 22:14:49 +0100 Subject: [PATCH 2/5] Addressed code linting errors. --- source/gui/guiHelper.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/gui/guiHelper.py b/source/gui/guiHelper.py index fcd0f6b89bd..16c537fb640 100644 --- a/source/gui/guiHelper.py +++ b/source/gui/guiHelper.py @@ -182,7 +182,9 @@ def __init__(self, parent, labelText, wxCtrlClass, **kwargs): """ self.label = wx.StaticText(parent, label=labelText) if self.__class__ == BaseLabeledControl: - raise TypeError(f"BaseLabeledControl should be subclassed and should also inherit from {wxCtrlClass.__name__}.") + raise TypeError( + f"BaseLabeledControl should be subclassed and should also inherit from {wxCtrlClass.__name__}." + ) # Check that the final class also inherits from wxCtrlClass if not isinstance(self, wxCtrlClass): raise TypeError(f"{self.__class__.__name__} should be a subclass of {wxCtrlClass.__name__}.") @@ -192,7 +194,7 @@ def __init__(self, parent, labelText, wxCtrlClass, **kwargs): def Enable(self, state): self.controlClass.Enable(self, state) self.label.Enable(state) - + def Disable(self): self.controlClass.Disable(self) self.label.Disable() From f3558d57a7cf67cf6caacb38c72dba354a1c1099 Mon Sep 17 00:00:00 2001 From: Cyrille Bougot Date: Tue, 17 Nov 2020 17:32:48 +0100 Subject: [PATCH 3/5] Addressing review comments. The code has totally been reworked according to @feerenrut's suggestion. --- source/gui/guiHelper.py | 98 ++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 59 deletions(-) diff --git a/source/gui/guiHelper.py b/source/gui/guiHelper.py index 16c537fb640..8428c4587cf 100644 --- a/source/gui/guiHelper.py +++ b/source/gui/guiHelper.py @@ -45,7 +45,7 @@ def __init__(self,parent): from contextlib import contextmanager import wx -from wx.lib import scrolledpanel +from wx.lib import scrolledpanel, newevent from abc import ABCMeta #: border space to be used around all controls in dialogs @@ -154,76 +154,56 @@ def associateElements( firstElement, secondElement): return sizer - -class BaseLabeledControl: - """Base class used to create a labeled control subclass - A labeled control subclass allows to create a control with an associated label. - The associated label is enabled and disabled when the main control is enabled or disabled (respectively). - - Example usage: - - class LabeledChoice(BaseLabeledControl, wx.Choice): - pass - myChoice = LabeledChoice( - parent, - labelText='Choose an option', - wxCtrlClass=wx.Choice, - choices=['Option 1', 'Option 2', 'Option 3'], - ) - """ - - def __init__(self, parent, labelText, wxCtrlClass, **kwargs): - """ @param parent: An instance of the parent wx window. EG wx.Dialog - @param labelText: The text to associate with a wx control. - @type labelText: string - @param wxCtrlClass: The class to associate with the label, eg: wx.TextCtrl - @type wxCtrlClass: class - @param kwargs: The keyword arguments used to instantiate the wxCtrlClass - """ - self.label = wx.StaticText(parent, label=labelText) - if self.__class__ == BaseLabeledControl: - raise TypeError( - f"BaseLabeledControl should be subclassed and should also inherit from {wxCtrlClass.__name__}." - ) - # Check that the final class also inherits from wxCtrlClass - if not isinstance(self, wxCtrlClass): - raise TypeError(f"{self.__class__.__name__} should be a subclass of {wxCtrlClass.__name__}.") - self.controlClass = wxCtrlClass - self.controlClass.__init__(self, parent, **kwargs) - - def Enable(self, state): - self.controlClass.Enable(self, state) - self.label.Enable(state) - - def Disable(self): - self.controlClass.Disable(self) - self.label.Disable() - - class LabeledControlHelper(object): """ Represents a Labeled Control. Provides a class to create and hold on to the objects and automatically associate the two controls together. Relies on guiHelper.associateElements(), any limitations in guiHelper.associateElements() also apply here. """ - def __init__(self, parent, labelText, wxCtrlClass, **kwargs): + + # When the control is enabled / disabled this event is raised. + # A handler is automatically added to the control to ensure the label is also enabled/disabled. + EnableChanged, EVT_ENABLE_CHANGED = newevent.NewEvent() + + def __init__(self, parent: wx.Window, labelText: str, wxCtrlClass: wx.Control, **kwargs): """ @param parent: An instance of the parent wx window. EG wx.Dialog @param labelText: The text to associate with a wx control. - @type labelText: string @param wxCtrlClass: The class to associate with the label, eg: wx.TextCtrl - @type wxCtrlClass: class @param kwargs: The keyword arguments used to instantiate the wxCtrlClass """ object.__init__(self) - # Dynamically create a class inheriting from BaseLabeledControl and wxCtrlClass. - # This class acts as wxCtrlClass but overloads the Enable and Disable methods - # in order to also enable or disable self._label at the same time as the control. - LabeledControl = type( - f'Labeled{wxCtrlClass.__name__}', - (BaseLabeledControl, wxCtrlClass), - {}, + self._label = _label = wx.StaticText(parent, label=labelText) + + class WxCtrlWithEnableEvnt(wxCtrlClass): + def Enable(self, enable=True): + evt = LabeledControlHelper.EnableChanged(isEnabled=enable) + wx.PostEvent(self, evt) + super().Enable(enable) + + def Disable(self): + evt = LabeledControlHelper.EnableChanged(isEnabled=False) + wx.PostEvent(self, evt) + super().Disable() + + def _onEnableChanged(evt: wx.Event): + _label.Enable(evt.isEnabled) + + self._ctrl = _ctrl = WxCtrlWithEnableEvnt(parent, **kwargs) + _ctrl.Bind(LabeledControlHelper.EVT_ENABLE_CHANGED, _onEnableChanged) + + # prevent _ctrl trying to call events on destroyed objects + def _onDestroy(evt: wx.Event): + try: + _ctrl.Unbind( + LabeledControlHelper.EVT_ENABLE_CHANGED, + handler=_onEnableChanged + ) + except RuntimeError: + pass + self._label.Bind( + wx.EVT_WINDOW_DESTROY, + _onDestroy ) - self._ctrl = LabeledControl(parent, labelText=labelText, wxCtrlClass=wxCtrlClass, **kwargs) - self._sizer = associateElements(self._ctrl.label, self._ctrl) + self._sizer = associateElements(_label, _ctrl) @property def control(self): From 78706548ba962ef246e98d2a8a3f6eba97ffd877 Mon Sep 17 00:00:00 2001 From: Cyrille Bougot Date: Tue, 24 Nov 2020 23:41:24 +0100 Subject: [PATCH 4/5] Applying new review suggestions making the code cleaner. Credit: Reef Turner --- source/gui/guiHelper.py | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/source/gui/guiHelper.py b/source/gui/guiHelper.py index 8428c4587cf..b33edad2875 100644 --- a/source/gui/guiHelper.py +++ b/source/gui/guiHelper.py @@ -171,7 +171,22 @@ def __init__(self, parent: wx.Window, labelText: str, wxCtrlClass: wx.Control, * @param kwargs: The keyword arguments used to instantiate the wxCtrlClass """ object.__init__(self) - self._label = _label = wx.StaticText(parent, label=labelText) + + class LabelEnableChangedListener(wx.StaticText): + isDestroyed = False + isListening = False + + def _onDestroy(self, evt: wx.WindowDestroyEvent): + self.isDestroyed = True + + def listenForEnableChanged(self, _ctrl: wx.Window): + self.Bind(wx.EVT_WINDOW_DESTROY, self._onDestroy) + _ctrl.Bind(LabeledControlHelper.EVT_ENABLE_CHANGED, self._onEnableChanged) + self.isListening = True + + def _onEnableChanged(self, evt: wx.Event): + if self.isListening and not self.isDestroyed: + self.Enable(evt.isEnabled) class WxCtrlWithEnableEvnt(wxCtrlClass): def Enable(self, enable=True): @@ -184,26 +199,10 @@ def Disable(self): wx.PostEvent(self, evt) super().Disable() - def _onEnableChanged(evt: wx.Event): - _label.Enable(evt.isEnabled) - - self._ctrl = _ctrl = WxCtrlWithEnableEvnt(parent, **kwargs) - _ctrl.Bind(LabeledControlHelper.EVT_ENABLE_CHANGED, _onEnableChanged) - - # prevent _ctrl trying to call events on destroyed objects - def _onDestroy(evt: wx.Event): - try: - _ctrl.Unbind( - LabeledControlHelper.EVT_ENABLE_CHANGED, - handler=_onEnableChanged - ) - except RuntimeError: - pass - self._label.Bind( - wx.EVT_WINDOW_DESTROY, - _onDestroy - ) - self._sizer = associateElements(_label, _ctrl) + self._label = LabelEnableChangedListener(parent, label=labelText) + self._ctrl = WxCtrlWithEnableEvnt(parent, **kwargs) + self._label.listenForEnableChanged(self._ctrl) + self._sizer = associateElements(self._label, self._ctrl) @property def control(self): From 39781f7f10c036de5438f5a9b58d8fa6521607b3 Mon Sep 17 00:00:00 2001 From: Reef Turner Date: Wed, 25 Nov 2020 17:07:48 +0800 Subject: [PATCH 5/5] Update changes file for PR #11812 --- user_docs/en/changes.t2t | 1 + 1 file changed, 1 insertion(+) diff --git a/user_docs/en/changes.t2t b/user_docs/en/changes.t2t index e5ba895cf3c..22f845c98a0 100644 --- a/user_docs/en/changes.t2t +++ b/user_docs/en/changes.t2t @@ -27,6 +27,7 @@ What's New in NVDA - In Mozilla Firefox and Chromium based browsers, updates to ARIA live regions are now suppressed when reporting of dynamic content changes is disabled. (#9077) - NVDA will now report "Copied to clipboard" before the copied text. (#6757) - Presentation of graphical view table in disk management has been improved. (#10048) +- Labels for controls are now disabled (greyed out) when the control is disabled. (#11809) == Bug Fixes ==