From d6dd30a8d7d86fc640cd549f5ba7dc612dce72e5 Mon Sep 17 00:00:00 2001 From: j8xixo12 Date: Wed, 15 Nov 2023 08:35:35 +0800 Subject: [PATCH 1/2] add Slot decorator and modified button callback Modify button event callback to fit buttton callback function signature. Also, add Slot decorator, according to PySide6 official documentation recommandation, every callback connected to signal should be indicated by Slot decorator for good reliability and performance. --- modmesh/app/euler1d.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/modmesh/app/euler1d.py b/modmesh/app/euler1d.py index d0dc88e2..22b5b4fb 100644 --- a/modmesh/app/euler1d.py +++ b/modmesh/app/euler1d.py @@ -41,7 +41,7 @@ from PySide6.QtWidgets import (QApplication, QWidget, QScrollArea, QGridLayout, QVBoxLayout, QPushButton, QHBoxLayout, QFileDialog, QLayout, QCheckBox) -from PySide6.QtCore import Qt, QEvent, QMimeData, QTimer +from PySide6.QtCore import Qt, QEvent, QMimeData, QTimer, QObject, Slot from PySide6.QtGui import QDrag, QPixmap import modmesh as mm @@ -145,7 +145,8 @@ def __init__(self, shocktube): self.checkbox_select_num = 3 - def save_all(self): + @Slot(bool) + def save_all(self, checked=False): fig = QPixmap(self.figure_container.size()) self.figure_container.render(fig) @@ -175,7 +176,8 @@ def _update_layout(self): # deleteLater() and a QWidget only accept one layout at # the same time, therefore a object delete callback is # needed to set new layout after old layout deleted. - def del_cb(): + @Slot(QObject) + def del_cb(obj=None): if self.use_grid_layout: self.build_lines_grid_layout() else: @@ -186,7 +188,8 @@ def del_cb(): self.main_layout.update() self.main_container.update() - def switch_layout(self): + @Slot(bool) + def switch_layout(self, checked=False): self.use_grid_layout = not self.use_grid_layout self._update_layout() @@ -448,7 +451,8 @@ def _build_checkbox(self, layout): check_box.clicked.connect(self._checkbox_cb) layout.addWidget(check_box) - def _checkbox_cb(self): + @Slot(bool) + def _checkbox_cb(self, checked=False): # Under a single plot layout, that allow 3 lines on the same chart # simultaneously to avoid it looking too crowded. # I have chosen to use checkboxes for user to select @@ -547,13 +551,16 @@ def show(self): if self.use_sub: self._subwin.show() - def start(self): + @Slot(bool) + def start(self, checked=False): self.timer.start(self.interval) - def stop(self): + @Slot(bool) + def stop(self, checked=False): self.timer.stop() - def step_button_cb(self): + @Slot(bool) + def step_button_cb(self, checked=False): self.step() def step(self, steps=1): @@ -561,7 +568,12 @@ def step(self, steps=1): if self.max_steps and self.current_step > self.max_steps: self.stop() - def reset(self): + @Slot() + def timer_timeout_cb(self): + self.step() + + @Slot(bool) + def reset(self, checked=False): self.stop() self.current_step = 0 self.shocktube = euler1d.ShockTube() @@ -596,7 +608,7 @@ def setup_timer(self, interval): """ self.interval = interval self.timer = QTimer() - self.timer.timeout.connect(self.step) + self.timer.timeout.connect(self.timer_timeout_cb) @staticmethod def log(msg): From 5d51509699cc5d54a09c25fa284a7a22ca676524 Mon Sep 17 00:00:00 2001 From: j8xixo12 Date: Wed, 15 Nov 2023 23:01:29 +0800 Subject: [PATCH 2/2] add docstring in slot callback function --- modmesh/app/euler1d.py | 77 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 9 deletions(-) diff --git a/modmesh/app/euler1d.py b/modmesh/app/euler1d.py index 22b5b4fb..d4778414 100644 --- a/modmesh/app/euler1d.py +++ b/modmesh/app/euler1d.py @@ -147,6 +147,13 @@ def __init__(self, shocktube): @Slot(bool) def save_all(self, checked=False): + """ + This callback function don't care button's checked state, + therefore the checked state is not used in this function. + + :param checked: button is checked or not + :return: nothing + """ fig = QPixmap(self.figure_container.size()) self.figure_container.render(fig) @@ -171,13 +178,22 @@ def _update_layout(self): self.main_layout.deleteLater() self.main_container.update() - # Qt objects are managed by Qt internal event loop, - # hence it will not be deleted immediately when we called - # deleteLater() and a QWidget only accept one layout at - # the same time, therefore a object delete callback is - # needed to set new layout after old layout deleted. @Slot(QObject) def del_cb(obj=None): + """ + Qt objects are managed by Qt internal event loop, + hence it will not be deleted immediately when we called + deleteLater() and a QWidget only accept one layout at + the same time, therefore a object delete callback is + needed to set new layout after old layout deleted. + + This callback will be called before the obj destroyed, + in this callback only update the layout, therefore the obj + is not used in this callback. + + :param obj: The object about to be destroyed + :return: nothing + """ if self.use_grid_layout: self.build_lines_grid_layout() else: @@ -190,6 +206,13 @@ def del_cb(obj=None): @Slot(bool) def switch_layout(self, checked=False): + """ + This callback function don't care button's checked state, + therefore the checked state is not used in this function. + + :param checked: button is checked or not + :return: nothing + """ self.use_grid_layout = not self.use_grid_layout self._update_layout() @@ -453,10 +476,18 @@ def _build_checkbox(self, layout): @Slot(bool) def _checkbox_cb(self, checked=False): - # Under a single plot layout, that allow 3 lines on the same chart - # simultaneously to avoid it looking too crowded. - # I have chosen to use checkboxes for user to select - # the parameters they want to plot on the chart. + """ + This callback function don't care button's checked state, + therefore the checked state is not used in this function. + + Under a single plot layout, that allow 3 lines on the same chart + simultaneously to avoid it looking too crowded. + I have chosen to use checkboxes for user to select + the parameters they want to plot on the chart. + + :param checked: button is checked or not + :return: nothing + """ checkbox = self.sender() if self.checkbox_select_num == 3: if checkbox.isChecked(): @@ -553,14 +584,35 @@ def show(self): @Slot(bool) def start(self, checked=False): + """ + This callback function don't care button's checked state, + therefore the checked state is not used in this function. + + :param checked: button is checked or not + :return: nothing + """ self.timer.start(self.interval) @Slot(bool) def stop(self, checked=False): + """ + This callback function don't care button's checked state, + therefore the checked state is not used in this function. + + :param checked: button is checked or not + :return: nothing + """ self.timer.stop() @Slot(bool) def step_button_cb(self, checked=False): + """ + This callback function don't care button's checked state, + therefore the checked state is not used in this function. + + :param checked: button is checked or not + :return: nothing + """ self.step() def step(self, steps=1): @@ -574,6 +626,13 @@ def timer_timeout_cb(self): @Slot(bool) def reset(self, checked=False): + """ + This callback function don't care button's checked state, + therefore the checked state is not used in this function. + + :param checked: button is checked or not + :return: nothing + """ self.stop() self.current_step = 0 self.shocktube = euler1d.ShockTube()