Put some menu requests in menu itself

This commit is contained in:
atse 2024-08-26 18:01:01 +08:00
parent 8a13ce2b47
commit 260a466078
2 changed files with 56 additions and 65 deletions

View File

@ -1,20 +1,18 @@
from PyQt6 import QtWidgets, QtGui, QtCore from PyQt6 import QtWidgets, QtGui, QtCore
from PyQt6.QtCore import pyqtSignal, pyqtSlot from PyQt6.QtCore import pyqtSignal, pyqtSlot, QSignalBlocker
from qasync import asyncSlot
from pytec.gui.view.net_settings_input_diag import NetSettingsInputDiag
class ThermostatCtrlMenu(QtWidgets.QMenu): class ThermostatCtrlMenu(QtWidgets.QMenu):
fan_set_act = pyqtSignal(int)
fan_auto_set_act = pyqtSignal(int)
connect_act = pyqtSignal()
reset_act = pyqtSignal(bool) reset_act = pyqtSignal(bool)
dfu_act = pyqtSignal(bool)
load_cfg_act = pyqtSignal(int) load_cfg_act = pyqtSignal(int)
save_cfg_act = pyqtSignal(int) save_cfg_act = pyqtSignal(int)
net_cfg_act = pyqtSignal(bool)
def __init__(self, style): def __init__(self, thermostat, style):
super().__init__() super().__init__()
self._thermostat = thermostat
self._style = style self._style = style
self.setTitle("Thermostat settings") self.setTitle("Thermostat settings")
@ -45,8 +43,8 @@ class ThermostatCtrlMenu(QtWidgets.QMenu):
self.fan_pwm_warning.setMinimumSize(QtCore.QSize(16, 0)) self.fan_pwm_warning.setMinimumSize(QtCore.QSize(16, 0))
self.fan_layout.addWidget(self.fan_pwm_warning) self.fan_layout.addWidget(self.fan_pwm_warning)
self.fan_power_slider.valueChanged.connect(self.fan_set_act) self.fan_power_slider.valueChanged.connect(self.fan_set_request)
self.fan_auto_box.stateChanged.connect(self.fan_auto_set_act) self.fan_auto_box.stateChanged.connect(self.fan_auto_set_request)
self.fan_lbl.setToolTip("Adjust the fan") self.fan_lbl.setToolTip("Adjust the fan")
self.fan_lbl.setText("Fan:") self.fan_lbl.setText("Fan:")
@ -62,11 +60,11 @@ class ThermostatCtrlMenu(QtWidgets.QMenu):
self.addAction(self.actionReset) self.addAction(self.actionReset)
self.actionEnter_DFU_Mode = QtGui.QAction("Enter DFU Mode", self) self.actionEnter_DFU_Mode = QtGui.QAction("Enter DFU Mode", self)
self.actionEnter_DFU_Mode.triggered.connect(self.dfu_act) self.actionEnter_DFU_Mode.triggered.connect(self.dfu_request)
self.addAction(self.actionEnter_DFU_Mode) self.addAction(self.actionEnter_DFU_Mode)
self.actionnet_settings_input_diag = QtGui.QAction("Set IPV4 Settings", self) self.actionnet_settings_input_diag = QtGui.QAction("Set IPV4 Settings", self)
self.actionnet_settings_input_diag.triggered.connect(self.net_cfg_act) self.actionnet_settings_input_diag.triggered.connect(self.net_settings_request)
self.addAction(self.actionnet_settings_input_diag) self.addAction(self.actionnet_settings_input_diag)
@pyqtSlot(bool) @pyqtSlot(bool)
@ -143,3 +141,48 @@ class ThermostatCtrlMenu(QtWidgets.QMenu):
def hw_rev(self, hw_rev): def hw_rev(self, hw_rev):
self.hw_rev_data = hw_rev self.hw_rev_data = hw_rev
self.fan_group.setEnabled(self.hw_rev_data["settings"]["fan_available"]) self.fan_group.setEnabled(self.hw_rev_data["settings"]["fan_available"])
@asyncSlot(int)
async def fan_set_request(self, value):
assert self._thermostat.connected()
if self.fan_auto_box.isChecked():
with QSignalBlocker(self.fan_auto_box):
self.fan_auto_box.setChecked(False)
await self._thermostat.set_fan(value)
if not self.hw_rev_data["settings"]["fan_pwm_recommended"]:
self.set_fan_pwm_warning()
@asyncSlot(int)
async def fan_auto_set_request(self, enabled):
assert self._thermostat.connected()
if enabled:
await self._thermostat.set_fan("auto")
self.fan_update(await self._thermostat.get_fan())
else:
await self.thermostat.set_fan(
self.fan_power_slider.value()
)
@asyncSlot(bool)
async def dfu_request(self, _):
assert self._thermostat.connected()
await self._thermostat.dfu()
await self._thermostat.end_session()
@asyncSlot(bool)
async def net_settings_request(self, _):
assert self._thermostat.connected()
ipv4 = await self._thermostat.get_ipv4()
self.net_settings_input_diag = NetSettingsInputDiag(ipv4["addr"])
self.net_settings_input_diag.set_ipv4_act.connect(self.set_net_settings_request)
@asyncSlot(str)
async def set_net_settings_request(self, ipv4_settings):
assert self._thermostat.connected()
await self._thermostat.set_ipv4(ipv4_settings)
await self._thermostat.end_session()

View File

@ -1,5 +1,4 @@
from pytec.gui.view.zero_limits_warning import ZeroLimitsWarningView from pytec.gui.view.zero_limits_warning import ZeroLimitsWarningView
from pytec.gui.view.net_settings_input_diag import NetSettingsInputDiag
from pytec.gui.view.thermostat_ctrl_menu import ThermostatCtrlMenu from pytec.gui.view.thermostat_ctrl_menu import ThermostatCtrlMenu
from pytec.gui.view.conn_menu import ConnMenu from pytec.gui.view.conn_menu import ConnMenu
from pytec.gui.view.plot_options_menu import PlotOptionsMenu from pytec.gui.view.plot_options_menu import PlotOptionsMenu
@ -141,14 +140,10 @@ class MainWindow(QtWidgets.QMainWindow):
self.conn_menu = ConnMenu() self.conn_menu = ConnMenu()
self.connect_btn.setMenu(self.conn_menu) self.connect_btn.setMenu(self.conn_menu)
self.thermostat_ctrl_menu = ThermostatCtrlMenu(self.style()) self.thermostat_ctrl_menu = ThermostatCtrlMenu(self.thermostat, self.style())
self.thermostat_ctrl_menu.fan_set_act.connect(self.fan_set_request)
self.thermostat_ctrl_menu.fan_auto_set_act.connect(self.fan_auto_set_request)
self.thermostat_ctrl_menu.reset_act.connect(self.reset_request) self.thermostat_ctrl_menu.reset_act.connect(self.reset_request)
self.thermostat_ctrl_menu.dfu_act.connect(self.dfu_request)
self.thermostat_ctrl_menu.save_cfg_act.connect(self.save_cfg_request) self.thermostat_ctrl_menu.save_cfg_act.connect(self.save_cfg_request)
self.thermostat_ctrl_menu.load_cfg_act.connect(self.load_cfg_request) self.thermostat_ctrl_menu.load_cfg_act.connect(self.load_cfg_request)
self.thermostat_ctrl_menu.net_cfg_act.connect(self.net_settings_request)
self.thermostat.hw_rev_update.connect(self.thermostat_ctrl_menu.hw_rev) self.thermostat.hw_rev_update.connect(self.thermostat_ctrl_menu.hw_rev)
self.thermostat_settings.setMenu(self.thermostat_ctrl_menu) self.thermostat_settings.setMenu(self.thermostat_ctrl_menu)
@ -323,30 +318,6 @@ class MainWindow(QtWidgets.QMainWindow):
self.loading_spinner.start() self.loading_spinner.start()
self.loading_spinner.show() self.loading_spinner.show()
@asyncSlot(int)
async def fan_set_request(self, value):
assert self.thermostat.connected()
if self.thermostat_ctrl_menu.fan_auto_box.isChecked():
with QSignalBlocker(self.thermostat_ctrl_menu.fan_auto_box):
self.thermostat_ctrl_menu.fan_auto_box.setChecked(False)
await self.thermostat.set_fan(value)
if not self.hw_rev_data["settings"]["fan_pwm_recommended"]:
self.thermostat_ctrl_menu.set_fan_pwm_warning()
@asyncSlot(int)
async def fan_auto_set_request(self, enabled):
assert self.thermostat.connected()
if enabled:
await self.thermostat.set_fan("auto")
self.fan_update(await self.thermostat.get_fan())
else:
await self.thermostat.set_fan(
self.thermostat_ctrl_menu.fan_power_slider.value()
)
@asyncSlot(int)
async def save_cfg_request(self, ch): async def save_cfg_request(self, ch):
assert self.thermostat.connected() assert self.thermostat.connected()
@ -357,14 +328,6 @@ class MainWindow(QtWidgets.QMainWindow):
assert self.thermostat.connected() assert self.thermostat.connected()
await self.thermostat.load_cfg(str(ch)) await self.thermostat.load_cfg(str(ch))
@asyncSlot(bool)
async def dfu_request(self, _):
assert self.thermostat.connected()
await self.thermostat.dfu()
await self.thermostat.end_session()
@asyncSlot(bool) @asyncSlot(bool)
async def reset_request(self, _): async def reset_request(self, _):
assert self.thermostat.connected() assert self.thermostat.connected()
@ -375,21 +338,6 @@ class MainWindow(QtWidgets.QMainWindow):
self.connect_btn.click() # Reconnect self.connect_btn.click() # Reconnect
@asyncSlot(bool)
async def net_settings_request(self, _):
assert self.thermostat.connected()
ipv4 = await self.thermostat.get_ipv4()
self.net_settings_input_diag = NetSettingsInputDiag(ipv4["addr"])
self.net_settings_input_diag.set_ipv4_act.connect(self.set_net_settings_request)
@asyncSlot(str)
async def set_net_settings_request(self, ipv4_settings):
assert self.thermostat.connected()
await self.thermostat.set_ipv4(ipv4_settings)
await self.thermostat.end_session()
async def coro_main(): async def coro_main():
args = get_argparser().parse_args() args = get_argparser().parse_args()