moninj: handler create, delete widgets

This commit is contained in:
Simon Renblad 2024-04-15 17:50:37 +08:00
parent 3b8f6edd52
commit aeaa337b1a

View File

@ -115,14 +115,12 @@ class _TTLHandler:
self.channel = channel self.channel = channel
self.force_out = force_out self.force_out = force_out
self.set_mode = dm.ttl_set_mode self.set_mode = dm.ttl_set_mode
self.title = title
self.cur_level = False self.cur_level = False
self.cur_oe = False self.cur_oe = False
self.cur_override = False self.cur_override = False
self.cur_override_level = False self.cur_override_level = False
self.widget = _TTLWidget(title) self.widget = None
self.widget.override_toggled.connect(self.override_toggled)
self.widget.level_toggled.connect(self.level_toggled)
self.refresh_display()
def override_toggled(self, override): def override_toggled(self, override):
if override: if override:
@ -141,12 +139,23 @@ class _TTLHandler:
self.set_mode(self.channel, "0") self.set_mode(self.channel, "0")
def refresh_display(self): def refresh_display(self):
level = self.cur_override_level if self.cur_override else self.cur_level if self.widget is not None:
oe = self.cur_oe or self.force_out level = self.cur_override_level if self.cur_override else self.cur_level
self.widget.setValueText(self.cur_override, level) oe = self.cur_oe or self.force_out
self.widget.setDirectionText(oe) self.widget.setValueText(self.cur_override, level)
with QtCore.QSignalBlocker(self.widget): self.widget.setDirectionText(oe)
self.widget.setButtonsState(self.cur_override, self.cur_level) with QtCore.QSignalBlocker(self.widget):
self.widget.setButtonsState(self.cur_override, self.cur_level)
def delete_widget(self):
self.widget.deleteLater()
self.widget = None
def create_widget(self):
self.widget = _TTLWidget(self.title)
self.widget.override_toggled.connect(self.override_toggled)
self.widget.level_toggled.connect(self.level_toggled)
self.refresh_display()
def sort_key(self): def sort_key(self):
return self.channel return self.channel
@ -282,6 +291,7 @@ class _DDSHandler:
def __init__(self, dm, title, bus_channel, channel, dds_type, def __init__(self, dm, title, bus_channel, channel, dds_type,
ref_clk, cpld=None, pll=1, clk_div=0): ref_clk, cpld=None, pll=1, clk_div=0):
self.dm = dm self.dm = dm
self.title = title
self.bus_channel = bus_channel self.bus_channel = bus_channel
self.channel = channel self.channel = channel
self.cur_frequency = 0 self.cur_frequency = 0
@ -305,7 +315,10 @@ class _DDSHandler:
raise NotImplementedError raise NotImplementedError
sysclk = ref_clk / clk_mult[clk_div] * pll sysclk = ref_clk / clk_mult[clk_div] * pll
self.ftw_per_hz = 1 / sysclk * max_freq self.ftw_per_hz = 1 / sysclk * max_freq
self.widget = _DDSWidget(title, self.is_urukul) self.widget = None
def create_widget(self):
self.widget = _DDSWidget(self.title, self.is_urukul)
self.widget.apply_changes.connect(self.apply_changes) self.widget.apply_changes.connect(self.apply_changes)
self.widget.off_clicked.connect(self.off_clicked) self.widget.off_clicked.connect(self.off_clicked)
self.widget.set_clicked.connect(self.set_clicked) self.widget.set_clicked.connect(self.set_clicked)
@ -319,8 +332,13 @@ class _DDSHandler:
def _ftw_to_freq(self, ftw): def _ftw_to_freq(self, ftw):
return ftw / self.ftw_per_hz return ftw / self.ftw_per_hz
def delete_widget(self):
self.widget.deleteLater()
self.widget = None
def refresh_display(self): def refresh_display(self):
self.widget.set_value(self.cur_frequency / 1e6) if self.widget is not None:
self.widget.set_value(self.cur_frequency / 1e6)
def apply_changes(self): def apply_changes(self):
self.widget.set_page(0) self.widget.set_page(0)
@ -370,14 +388,23 @@ class _DACWidget(QtWidgets.QFrame):
class _DACHandler: class _DACHandler:
def __init__(self, dm, spi_channel, channel, title): def __init__(self, dm, spi_channel, channel, title):
self.widget = _DACWidget(channel, title) self.widget = None
self.cur_value = 0 self.cur_value = 0
self.spi_channel = spi_channel self.spi_channel = spi_channel
self.channel = channel self.channel = channel
self.title = title
def create_widget(self):
self.widget = _DACWidget(self.channel, self.title)
self.refresh_display() self.refresh_display()
def delete_widget(self):
self.widget.deleteLater()
self.widget = None
def refresh_display(self): def refresh_display(self):
self.widget.set_value(self.cur_value * 100 / 2**16) if self.widget is not None:
self.widget.set_value(self.cur_value * 100 / 2**16)
def sort_key(self): def sort_key(self):
return (self.spi_channel, self.channel) return (self.spi_channel, self.channel)
@ -484,17 +511,17 @@ class _DeviceManager:
if isinstance(handler, _TTLHandler): if isinstance(handler, _TTLHandler):
self.setup_ttl_monitoring(False, handler.channel) self.setup_ttl_monitoring(False, handler.channel)
handler.widget.deleteLater() handler.delete_widget()
del self.ttl_handlers[handler.channel] del self.ttl_handlers[handler.channel]
self.ttl_cb() self.ttl_cb()
elif isinstance(handler, _DDSHandler): elif isinstance(handler, _DDSHandler):
self.setup_dds_monitoring(False, handler.bus_channel, handler.channel) self.setup_dds_monitoring(False, handler.bus_channel, handler.channel)
handler.widget.deleteLater() handler.delete_widget()
del self.dds_handlers[(handler.bus_channel, handler.channel)] del self.dds_handlers[(handler.bus_channel, handler.channel)]
self.dds_cb() self.dds_cb()
elif isinstance(handler, _DACHandler): elif isinstance(handler, _DACHandler):
self.setup_dac_monitoring(False, handler.spi_channel, handler.channel) self.setup_dac_monitoring(False, handler.spi_channel, handler.channel)
handler.widget.deleteLater() handler.delete_widget()
del self.dac_handlers[(handler.spi_channel, handler.channel)] del self.dac_handlers[(handler.spi_channel, handler.channel)]
self.dac_cb() self.dac_cb()
else: else:
@ -502,6 +529,7 @@ class _DeviceManager:
for to_add in description - self.description: for to_add in description - self.description:
handler = to_add.cls(self, *to_add.arguments) handler = to_add.cls(self, *to_add.arguments)
handler.create_widget()
if to_add.comment is not None: if to_add.comment is not None:
handler.widget.setToolTip(to_add.comment) handler.widget.setToolTip(to_add.comment)
self.handlers_by_uid[to_add.uid] = handler self.handlers_by_uid[to_add.uid] = handler