Skip to content

Commit

Permalink
MNT: remove widget classes inheriting from enum, as preperation for q…
Browse files Browse the repository at this point in the history
…t6 enum support

The need for inheritance was b/c  when an enum gets written to a ui file by
designer, its in the form of <widget class name>::<enum value> (ex='PyDMLabel::STRING').

On loading the ui file, unless the widget class had subclassed the enum class it
would not be able to find the definition of the enum.

The issue with subclassing the enum is:  in qt6 enums must inherit
from python Enum (ex = class MyNewEnum(Enum)), and these classes can not be
inherited from.

So instead of inheriting the enum we can just re-declare the values of the enum
in the class itself.
  • Loading branch information
nstelter-slac committed Jan 11, 2025
1 parent dd6f38a commit d5794c6
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/label/label.ui
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<bool>true</bool>
</property>
<property name="displayFormat" stdset="0">
<enum>PyDMLabel::String</enum>
<enum>PyDMLabel::Binary</enum>
</property>
</widget>
</widget>
Expand Down
14 changes: 12 additions & 2 deletions pydm/widgets/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ class TimeBase(object):
Seconds = 1


class PyDMDateTimeEdit(QtWidgets.QDateTimeEdit, PyDMWritableWidget, TimeBase):
class PyDMDateTimeEdit(QtWidgets.QDateTimeEdit, PyDMWritableWidget):
QtCore.Q_ENUMS(TimeBase)

# Make enum definitions known to this class
Milliseconds = TimeBase.Milliseconds
Seconds = TimeBase.Seconds

returnPressed = QtCore.Signal()
"""
A QDateTimeEdit with support for setting the text via a PyDM Channel, or
Expand Down Expand Up @@ -107,8 +112,13 @@ def value_changed(self, new_val):
self.setDateTime(val)


class PyDMDateTimeLabel(QtWidgets.QLabel, PyDMWidget, TimeBase):
class PyDMDateTimeLabel(QtWidgets.QLabel, PyDMWidget):
QtCore.Q_ENUMS(TimeBase)

# Make enum definitions known to this class
Milliseconds = TimeBase.Milliseconds
Seconds = TimeBase.Seconds

"""
A QLabel with support for setting the text via a PyDM Channel, or
through the PyDM Rules system.
Expand Down
1 change: 0 additions & 1 deletion pydm/widgets/display_format.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import math
import numpy as np
from typing import Any

import logging
import warnings

Expand Down
6 changes: 5 additions & 1 deletion pydm/widgets/enum_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class WidgetType(object):
logger = logging.getLogger(__name__)


class PyDMEnumButton(QWidget, PyDMWritableWidget, WidgetType):
class PyDMEnumButton(QWidget, PyDMWritableWidget):
"""
A QWidget that renders buttons for every option of Enum Items.
For now, two types of buttons can be rendered:
Expand All @@ -41,6 +41,10 @@ class PyDMEnumButton(QWidget, PyDMWritableWidget, WidgetType):
Q_ENUMS(WidgetType)
WidgetType = WidgetType

# Make enum definitions known to this class
PushButton = WidgetType.PushButton
RadioButton = WidgetType.RadioButton

def __init__(self, parent=None, init_channel=None):
QWidget.__init__(self, parent)
PyDMWritableWidget.__init__(self, init_channel=init_channel)
Expand Down
17 changes: 16 additions & 1 deletion pydm/widgets/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def run(self):
self.image_view.needs_redraw = False


class PyDMImageView(ImageView, PyDMWidget, PyDMColorMap, ReadingOrder, DimensionOrder):
class PyDMImageView(ImageView, PyDMWidget):
"""
A PyQtGraph ImageView with support for Channels and more from PyDM.
Expand Down Expand Up @@ -130,6 +130,21 @@ class PyDMImageView(ImageView, PyDMWidget, PyDMColorMap, ReadingOrder, Dimension
Q_ENUMS(DimensionOrder)
Q_ENUMS(PyDMColorMap)

# Make enum definitions known to this class
Fortranlike = ReadingOrder.Fortranlike
Clike = ReadingOrder.Clike

HeightFirst = DimensionOrder.HeightFirst
WidthFirst = DimensionOrder.WidthFirst

Magma = PyDMColorMap.Magma
Inferno = PyDMColorMap.Inferno
Plasma = PyDMColorMap.Plasma
Viridis = PyDMColorMap.Viridis
Jet = PyDMColorMap.Jet
Monochrome = PyDMColorMap.Monochrome
Hot = PyDMColorMap.Hot

color_maps = cmaps

def __init__(self, parent=None, image_channel=None, width_channel=None):
Expand Down
9 changes: 8 additions & 1 deletion pydm/widgets/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
_labelRuleProperties = {"Text": ["value_changed", str]}


class PyDMLabel(QLabel, TextFormatter, PyDMWidget, DisplayFormat, new_properties=_labelRuleProperties):
class PyDMLabel(QLabel, TextFormatter, PyDMWidget, new_properties=_labelRuleProperties):
"""
A QLabel with support for setting the text via a PyDM Channel, or
through the PyDM Rules system.
Expand All @@ -27,6 +27,13 @@ class PyDMLabel(QLabel, TextFormatter, PyDMWidget, DisplayFormat, new_properties
The channel to be used by the widget.
"""

Default = DisplayFormat.Default
String = DisplayFormat.String
Decimal = DisplayFormat.Decimal
Exponential = DisplayFormat.Exponential
Hex = DisplayFormat.Hex
Binary = DisplayFormat.Binary

Q_ENUMS(DisplayFormat)
DisplayFormat = DisplayFormat

Expand Down
10 changes: 9 additions & 1 deletion pydm/widgets/line_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
logger = logging.getLogger(__name__)


class PyDMLineEdit(QLineEdit, TextFormatter, PyDMWritableWidget, DisplayFormat):
class PyDMLineEdit(QLineEdit, TextFormatter, PyDMWritableWidget):
"""
A QLineEdit (writable text field) with support for Channels and more
from PyDM.
Expand All @@ -32,6 +32,14 @@ class PyDMLineEdit(QLineEdit, TextFormatter, PyDMWritableWidget, DisplayFormat):
Q_ENUMS(DisplayFormat)
DisplayFormat = DisplayFormat

# Make enum definitions known to this class
Default = DisplayFormat.Default
String = DisplayFormat.String
Decimal = DisplayFormat.Decimal
Exponential = DisplayFormat.Exponential
Hex = DisplayFormat.Hex
Binary = DisplayFormat.Binary

def __init__(self, parent=None, init_channel=None):
QLineEdit.__init__(self, parent)
PyDMWritableWidget.__init__(self, init_channel=init_channel)
Expand Down
11 changes: 10 additions & 1 deletion pydm/widgets/logdisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def as_dict():
return OrderedDict(sorted(entries, key=lambda x: x[1], reverse=False))


class PyDMLogDisplay(QWidget, LogLevels):
class PyDMLogDisplay(QWidget):
"""
Standard display for Log Output
Expand All @@ -131,6 +131,15 @@ class PyDMLogDisplay(QWidget, LogLevels):

Q_ENUMS(LogLevels)
LogLevels = LogLevels

# Make enum definitions known to this class
NOTSET = LogLevels.NOTSET
DEBUG = LogLevels.DEBUG
INFO = LogLevels.INFO
WARNING = LogLevels.WARNING
ERROR = LogLevels.ERROR
CRITICAL = LogLevels.CRITICAL

terminator = "\n"
default_format = "%(asctime)s %(message)s"
default_level = logging.INFO
Expand Down
7 changes: 6 additions & 1 deletion pydm/widgets/template_repeater.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class LayoutType(object):
layout_class_for_type = (QVBoxLayout, QHBoxLayout, FlowLayout)


class PyDMTemplateRepeater(QFrame, PyDMPrimitiveWidget, LayoutType):
class PyDMTemplateRepeater(QFrame, PyDMPrimitiveWidget):
"""
PyDMTemplateRepeater takes a .ui file with macro variables as a template, and a JSON
file (or a list of dictionaries) with a list of values to use to fill in
Expand All @@ -143,6 +143,11 @@ class PyDMTemplateRepeater(QFrame, PyDMPrimitiveWidget, LayoutType):
Q_ENUMS(LayoutType)
LayoutType = LayoutType

# Make enum definitions known to this class
Vertical = LayoutType.Vertical
Horizontal = LayoutType.Horizontal
Flow = LayoutType.Flow

def __init__(self, parent=None):
pydm.data_plugins.initialize_plugins_if_needed()
QFrame.__init__(self, parent)
Expand Down
9 changes: 5 additions & 4 deletions pydm/widgets/timeplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def channels(self):
return [self.channel]


class PyDMTimePlot(BasePlot, updateMode):
class PyDMTimePlot(BasePlot):
"""
PyDMTimePlot is a widget to plot one or more channels vs. time.
Expand All @@ -437,12 +437,13 @@ class PyDMTimePlot(BasePlot, updateMode):
to either a TimeAxisItem if plot_by_timestamps is true, or a regular AxisItem otherwise
"""

OnValueChange = 1
AtFixedRated = 2

Q_ENUMS(updateMode)
updateMode = updateMode

# Make enum definitions known to this class
OnValueChange = updateMode.OnValueChange
AtFixedRated = updateMode.AtFixedRate

plot_redrawn_signal = Signal(TimePlotCurveItem)

def __init__(
Expand Down

0 comments on commit d5794c6

Please sign in to comment.