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

Promote LEDEditor to a first-class editor #779

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions docs/source/api/traitsui.editors.led_editor.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
traitsui\.editors\.led\_editor module
=====================================

.. automodule:: traitsui.editors.led_editor
:members:
:undoc-members:
:show-inheritance:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API documentation is now being generated automatically. So this file and traitsui.editors.rst are not needed any more.

1 change: 1 addition & 0 deletions docs/source/api/traitsui.editors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Submodules
traitsui.editors.image_enum_editor
traitsui.editors.instance_editor
traitsui.editors.key_binding_editor
traitsui.editors.led_editor
traitsui.editors.list_editor
traitsui.editors.list_str_editor
traitsui.editors.null_editor
Expand Down
18 changes: 5 additions & 13 deletions examples/demo/Extras/LED_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,16 @@
using a simulated LED display control.
"""

from threading import Thread

from threading import Thread
from time import sleep

from traits.api import HasTraits, Instance, Int, Bool, Float

from traitsui.api import View, Item, HGroup, Handler, UIInfo, spring

from traits.etsconfig.api import ETSConfig
if ETSConfig.toolkit == 'wx':
from traitsui.wx.extra.led_editor import LEDEditor
else:
from traitsui.qt4.extra.led_editor import LEDEditor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we keep traitsui.qt4.extra.led_editor and traitsui.wx.extra.led_editor as aliases? It costs almost nothing to maintain aliases. Otherwise this would be a breaking change.


# Handler class for the LEDDemo class view:
from traitsui.api import View, Item, HGroup, Handler, LEDEditor, UIInfo, spring


class LEDDemoHandler(Handler):
""" Handler class for the LEDDemo class view. """

# The UIInfo object associated with the UI:
info = Instance(UIInfo)
Expand All @@ -55,10 +47,9 @@ def _update_counter(self):
sleep(.01)
self.alive = False

# The main demo class:


class LEDDemo(HasTraits):
""" The main class for the LED Demo. """

# A counter to display:
counter1 = Int()
Expand Down Expand Up @@ -119,6 +110,7 @@ class LEDDemo(HasTraits):
handler=LEDDemoHandler
)


# Create the demo:
demo = LEDDemo()

Expand Down
1 change: 1 addition & 0 deletions traitsui/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
ImageEnumEditor,
InstanceEditor,
KeyBindingEditor,
LEDEditor,
ListEditor,
ListStrEditor,
NullEditor,
Expand Down
1 change: 1 addition & 0 deletions traitsui/editors/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from .image_editor import ImageEditor
from .image_enum_editor import ImageEnumEditor
from .instance_editor import InstanceEditor
from .led_editor import LEDEditor
from .list_editor import ListEditor
from .list_str_editor import ListStrEditor
from .null_editor import NullEditor
Expand Down
20 changes: 20 additions & 0 deletions traitsui/editors/led_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# (C) Copyright 2020 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!

""" A Traits UI editor that wraps a LED-style integer display.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "a" -> "an"

"""

from traitsui.editor_factory import EditorFactory
from traits.api import Enum


class LEDEditor(EditorFactory):
""" Editor factory for an LED editor. """

#: The alignment of the numeric text within the control. (Wx only)
alignment = Enum("right", "center", "left")
20 changes: 10 additions & 10 deletions traitsui/qt4/extra/led_editor.py → traitsui/qt4/led_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
# Thanks for using Enthought open source!

from pyface.qt import QtGui
from traitsui.qt4.editor import Editor
from traitsui.basic_editor_factory import BasicEditorFactory
from traits.api import Any, Undefined

from .editor import Editor


class LEDEditor(Editor):
""" Traits UI 'display only' LED numeric editor.
"""

class _LEDEditor(Editor):
def init(self, parent):
self.control = QtGui.QLCDNumber()
self.control.setSegmentStyle(QtGui.QLCDNumber.Flat)
Expand All @@ -22,9 +24,7 @@ def update_editor(self):
self.control.display(self.str_value)


class LEDEditor(BasicEditorFactory):

#: The editor class to be created
klass = _LEDEditor
#: Alignment is not supported for QT backend
alignment = Any(Undefined)
# editor names for factory to find
ReadonlyEditor = LEDEditor
SimpleEditor = LEDEditor
CustomEditor = LEDEditor
40 changes: 14 additions & 26 deletions traitsui/wx/extra/led_editor.py → traitsui/wx/led_editor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -------------------------------------------------------------------------
#
# Copyright (c) 2007, Enthought, Inc.
# Copyright (c) 2007-2020, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
Expand All @@ -9,11 +7,6 @@
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
#
# Author: David C. Morrill
# Date: 03/02/2007
#
# -------------------------------------------------------------------------

""" Traits UI 'display only' LED numeric editor.
"""
Expand All @@ -28,9 +21,7 @@

from traits.api import Enum

from traitsui.wx.editor import Editor

from traitsui.basic_editor_factory import BasicEditorFactory
from .editor import Editor


# LED alignment styles:
Expand All @@ -41,16 +32,19 @@
}


class _LEDEditor(Editor):
class LEDEditor(Editor):
""" Traits UI 'display only' LED numeric editor.
"""

#: The alignment of the numeric text within the control.
alignment = Enum("right", "center", "left")

def init(self, parent):
""" Finishes initializing the editor by creating the underlying toolkit
widget.
"""
self.control = LEDNumberCtrl(parent, -1)
self.control.SetAlignment(LEDStyles[self.factory.alignment])
self.control.SetAlignment(LEDStyles[self.alignment])
Copy link
Contributor

@kitchoi kitchoi Jan 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, I think we can defer making alignment mutable in a separate PR / issue and restore the original code which uses the factory value.

(I bet one probably needs to sync_value method like this

self.sync_value(factory.adapter_name, "adapter", "from")
so that the changes are handled on the event loop and so that the value on the editor factory is actually transferred to the editor.) (edited: cross out this half of the comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I bet one probably needs to sync_value method like this...

Please ignore this half of the comment. The sync_value is used for synchronizing a trait on the editor with a trait on the object being edited. But the first half of the comment about defer making alignment mutable still stands.

self.set_tooltip()

def update_editor(self):
Expand All @@ -59,18 +53,12 @@ def update_editor(self):
"""
self.control.SetValue(self.str_value)

def _alignment_changed(self):
if self.control is not None:
self.control.SetAlignment(LEDStyles[self.alignment])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make such a change in a separate PR such that this PR is more focused about exposing the existing editor as first-class / built-in / whatever editor?

Furthermore I expect there being very limited use case to mutate the alignment after the editor is created - we can wait for this feature request to arise. The fewer mutables there are, the simpler and more maintainable the code is.


# -------------------------------------------------------------------------
# Create the editor factory object:
# -------------------------------------------------------------------------

# wxPython editor factory for LED editors:


class LEDEditor(BasicEditorFactory):

#: The editor class to be created:
klass = _LEDEditor

#: The alignment of the numeric text within the control:
alignment = Enum("right", "left", "center")
# editor names for factory to find
ReadonlyEditor = LEDEditor
SimpleEditor = LEDEditor
CustomEditor = LEDEditor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need to populate all the three styles? If the custom/readonly editors are not implemented, then in the future we will be certain that they are never used because attempts to set style='custom' or style='readonly' will be met with an error (I think). If we populate them, it will add constraint to future extensions when we do want the custom / readonly style to behave completely differently.

e.g. The TableEditor does not have a custom editor. ButtonEditor does not have a readonly editor. They don't seem to be missed.