Browse Source

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.
Ghostkeeper 4 years ago
parent
commit
e9d3ba9b74
1 changed files with 8 additions and 28 deletions
  1. 8 28
      plugins/SimulationView/SimulationView.py

+ 8 - 28
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()