Просмотр исходного кода

- fixed the FCDoubleSpinner to only allow the amount of decimals already set with set_precision()

Marius Stanciu 6 лет назад
Родитель
Сommit
6c205eb37e
2 измененных файлов с 36 добавлено и 20 удалено
  1. 1 0
      README.md
  2. 35 20
      flatcamGUI/GUIElements.py

+ 1 - 0
README.md

@@ -13,6 +13,7 @@ CAD program, and create G-Code for Isolation routing.
 
 
 - updated the Rules Check Tool - solved some issues
 - updated the Rules Check Tool - solved some issues
 - made FCDoubleSpinner to use either comma or dot as a decimal separator
 - made FCDoubleSpinner to use either comma or dot as a decimal separator
+- fixed the FCDoubleSpinner to only allow the amount of decimals already set with set_precision()
 
 
 8.10.2019
 8.10.2019
 
 

+ 35 - 20
flatcamGUI/GUIElements.py

@@ -571,17 +571,14 @@ class FCDoubleSpinner(QtWidgets.QDoubleSpinBox):
 
 
         self.editingFinished.connect(self.on_edit_finished)
         self.editingFinished.connect(self.on_edit_finished)
         self.lineEdit().installEventFilter(self)
         self.lineEdit().installEventFilter(self)
-        self.lineEdit().setValidator(QtGui.QRegExpValidator(QtCore.QRegExp("-?[0-9]*[.,]?[0-9]*"), self))
 
 
-    def valueFromText(self, p_str):
-        text = p_str.replace(',', '.')
-        try:
-            return float(text)
-        except ValueError:
-            return 0.0
+        # by default don't allow the minus sign to be entered as the default for QDoubleSpinBox is the positive range
+        # between 0.00 and 99.00 (2 decimals)
+        self.lineEdit().setValidator(
+            QtGui.QRegExpValidator(QtCore.QRegExp("[0-9]*[.,]?[0-9]{%d}" % self.decimals()), self))
 
 
-    def validate(self, p_str, p_int):
-        return QtGui.QValidator.Acceptable, p_str, p_int
+    def on_edit_finished(self):
+        self.clearFocus()
 
 
     def eventFilter(self, object, event):
     def eventFilter(self, object, event):
         if event.type() == QtCore.QEvent.MouseButtonPress:
         if event.type() == QtCore.QEvent.MouseButtonPress:
@@ -593,22 +590,11 @@ class FCDoubleSpinner(QtWidgets.QDoubleSpinBox):
             return True
             return True
         return False
         return False
 
 
-    def on_edit_finished(self):
-        self.clearFocus()
-
     def wheelEvent(self, *args, **kwargs):
     def wheelEvent(self, *args, **kwargs):
         # should work only there is a focus in the lineedit of the SpinBox
         # should work only there is a focus in the lineedit of the SpinBox
         if self.readyToEdit is False:
         if self.readyToEdit is False:
             super().wheelEvent(*args, **kwargs)
             super().wheelEvent(*args, **kwargs)
 
 
-    # def keyPressEvent(self, in_event):  # 46 = dot; 44 = comma:
-    #     if in_event.key() == 44:
-    #         print(in_event, "ahaaa")
-    #         event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, 46, Qt.NoModifier, 0, 0, 0)
-    #         QtWidgets.QApplication.sendEvent(self.parent(), event)
-    #     else:
-    #         super().keyPressEvent(in_event)
-
     def focusOutEvent(self, e):
     def focusOutEvent(self, e):
         # don't focus out if the user requests an popup menu
         # don't focus out if the user requests an popup menu
         if e.reason() != QtCore.Qt.PopupFocusReason:
         if e.reason() != QtCore.Qt.PopupFocusReason:
@@ -616,6 +602,23 @@ class FCDoubleSpinner(QtWidgets.QDoubleSpinBox):
             self.lineEdit().deselect()
             self.lineEdit().deselect()
             self.readyToEdit = True
             self.readyToEdit = True
 
 
+    def valueFromText(self, p_str):
+        text = p_str.replace(',', '.')
+        try:
+            ret_val = float(text)
+        except ValueError:
+            ret_val = 0.0
+
+        return ret_val
+
+    def validate(self, p_str, p_int):
+        try:
+            if float(p_str) < self.minimum() or float(p_str) > self.maximum():
+                return QtGui.QValidator.Intermediate, p_str, p_int
+        except ValueError:
+            pass
+        return QtGui.QValidator.Acceptable, p_str, p_int
+
     def get_value(self):
     def get_value(self):
         return float(self.value())
         return float(self.value())
 
 
@@ -630,7 +633,19 @@ class FCDoubleSpinner(QtWidgets.QDoubleSpinBox):
     def set_precision(self, val):
     def set_precision(self, val):
         self.setDecimals(val)
         self.setDecimals(val)
 
 
+        # make sure that the user can't type more decimals than the set precision
+        if self.minimum() < 0 or self.maximum() < 0:
+            self.lineEdit().setValidator(
+                QtGui.QRegExpValidator(QtCore.QRegExp("-?[0-9]*[.,]?[0-9]{%d}" % self.decimals()), self))
+        else:
+            self.lineEdit().setValidator(
+                QtGui.QRegExpValidator(QtCore.QRegExp("[0-9]*[.,]?[0-9]{%d}" % self.decimals()), self))
+
     def set_range(self, min_val, max_val):
     def set_range(self, min_val, max_val):
+        if min_val < 0 or max_val < 0:
+            self.lineEdit().setValidator(
+                QtGui.QRegExpValidator(QtCore.QRegExp("-?[0-9]*[.,]?[0-9]{%d}" % self.decimals()), self))
+
         self.setRange(min_val, max_val)
         self.setRange(min_val, max_val)