From e9d3ba9b7402458852606d9e050ada4268b567f5 Mon Sep 17 00:00:00 2001 From: Ghostkeeper Date: Sat, 6 Mar 2021 14:15:48 +0100 Subject: [PATCH] Simplify limits on [minimum] layer/path number Just a few calls to min() or max() do the trick, rather than if statements. I consider this more semantic, because we just intend to clamp values here, and min() and max() are commonly used to do that. It should also be slightly faster because it's less Python and more internal in CPython, but considering that this happens at best like 60 times per second the performance impact of this will be practically nil. --- plugins/SimulationView/SimulationView.py | 36 ++++++------------------ 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/plugins/SimulationView/SimulationView.py b/plugins/SimulationView/SimulationView.py index 38e831d1ec..48269aae8c 100644 --- a/plugins/SimulationView/SimulationView.py +++ b/plugins/SimulationView/SimulationView.py @@ -249,52 +249,32 @@ class SimulationView(CuraView): def setLayer(self, value: int) -> None: if self._current_layer_num != value: - self._current_layer_num = value - if self._current_layer_num < 0: - self._current_layer_num = 0 - if self._current_layer_num > self._max_layers: - self._current_layer_num = self._max_layers - if self._current_layer_num < self._minimum_layer_num: - self._minimum_layer_num = self._current_layer_num + self._current_layer_num = min(max(value, 0), self._max_layers) + self._minimum_layer_num = min(self._current_layer_num, self._minimum_layer_num) self._startUpdateTopLayers() self.currentLayerNumChanged.emit() def setMinimumLayer(self, value: int) -> None: if self._minimum_layer_num != value: - self._minimum_layer_num = value - if self._minimum_layer_num < 0: - self._minimum_layer_num = 0 - if self._minimum_layer_num > self._max_layers: - self._minimum_layer_num = self._max_layers - if self._minimum_layer_num > self._current_layer_num: - self._current_layer_num = self._minimum_layer_num + self._minimum_layer_num = min(max(value, 0), self._max_layers) + self._current_layer_num = max(self._current_layer_num, self._minimum_layer_num) self._startUpdateTopLayers() self.currentLayerNumChanged.emit() def setPath(self, value: int) -> None: if self._current_path_num != value: - self._current_path_num = value - if self._current_path_num < 0: - self._current_path_num = 0 - if self._current_path_num > self._max_paths: - self._current_path_num = self._max_paths - if self._current_path_num < self._minimum_path_num: - self._minimum_path_num = self._current_path_num + self._current_path_num = min(max(value, 0), self._max_paths) + self._minimum_path_num = min(self._minimum_path_num, self._current_path_num) self._startUpdateTopLayers() self.currentPathNumChanged.emit() def setMinimumPath(self, value: int) -> None: if self._minimum_path_num != value: - self._minimum_path_num = value - if self._minimum_path_num < 0: - self._minimum_path_num = 0 - if self._minimum_path_num > self._max_paths: - self._minimum_path_num = self._max_paths - if self._minimum_path_num > self._current_path_num: - self._current_path_num = self._minimum_path_num + self._minimum_path_num = min(max(value, 0), self._max_paths) + self._current_path_num = max(self._current_path_num, self._minimum_path_num) self._startUpdateTopLayers() self.currentPathNumChanged.emit()