Commit eb5c5842 authored by Flavio TARSETTI's avatar Flavio TARSETTI Committed by Samuel GAIST
Browse files

[widgets][spinboxes] refactored/renamed class and added Qt property for type setting. Fixes #212

parent b39fabc6
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
import pytest import pytest
import numpy as np import numpy as np
from ..widgets.spinboxes import GenericSpinBox from ..widgets.spinboxes import NumpySpinBox
@pytest.fixture() @pytest.fixture()
...@@ -46,10 +46,8 @@ def expected_values(): ...@@ -46,10 +46,8 @@ def expected_values():
class SpinBoxBaseTest: class SpinBoxBaseTest:
klass = None
def test_range(self, qtbot): def test_range(self, qtbot):
spinbox = self.klass(self.numpy_type) spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox) qtbot.addWidget(spinbox)
minimum = 0 minimum = 0
...@@ -65,7 +63,7 @@ class SpinBoxBaseTest: ...@@ -65,7 +63,7 @@ class SpinBoxBaseTest:
assert spinbox.value() == spinbox.maximum() assert spinbox.value() == spinbox.maximum()
def test_valid_input(self, qtbot): def test_valid_input(self, qtbot):
spinbox = self.klass(self.numpy_type) spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox) qtbot.addWidget(spinbox)
value = "42" value = "42"
...@@ -74,7 +72,7 @@ class SpinBoxBaseTest: ...@@ -74,7 +72,7 @@ class SpinBoxBaseTest:
assert spinbox.value() == spinbox.numpy_type(value) assert spinbox.value() == spinbox.numpy_type(value)
def test_invalid_input(self, qtbot, expected_values): def test_invalid_input(self, qtbot, expected_values):
spinbox = self.klass(self.numpy_type) spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox) qtbot.addWidget(spinbox)
value = "44444444444444444444444444444444444444444444444444444444444" value = "44444444444444444444444444444444444444444444444444444444444"
expected_value = None expected_value = None
...@@ -90,18 +88,16 @@ class SpinBoxBaseTest: ...@@ -90,18 +88,16 @@ class SpinBoxBaseTest:
assert spinbox.value() == spinbox.numpy_type(expected_value) assert spinbox.value() == spinbox.numpy_type(expected_value)
class UinSpinBoxBaseTest(SpinBoxBaseTest): class UintSpinBoxBaseTest(SpinBoxBaseTest):
klass = GenericSpinBox
def test_invalid_range(self, qtbot): def test_invalid_range(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type) spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox) qtbot.addWidget(spinbox)
spinbox.setRange(-10, 10) spinbox.setRange(-10, 10)
assert spinbox.minimum() == 0 assert spinbox.minimum() == 0
def test_values(self, qtbot): def test_values(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type) spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox) qtbot.addWidget(spinbox)
spinbox.setRange(0, 10) spinbox.setRange(0, 10)
...@@ -113,11 +109,9 @@ class UinSpinBoxBaseTest(SpinBoxBaseTest): ...@@ -113,11 +109,9 @@ class UinSpinBoxBaseTest(SpinBoxBaseTest):
assert spinbox.value() == spinbox.maximum() assert spinbox.value() == spinbox.maximum()
class InSpinBoxBaseTest(SpinBoxBaseTest): class IntSpinBoxBaseTest(SpinBoxBaseTest):
klass = GenericSpinBox
def test_values(self, qtbot): def test_values(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type) spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox) qtbot.addWidget(spinbox)
spinbox.setRange(0, 10) spinbox.setRange(0, 10)
...@@ -130,10 +124,8 @@ class InSpinBoxBaseTest(SpinBoxBaseTest): ...@@ -130,10 +124,8 @@ class InSpinBoxBaseTest(SpinBoxBaseTest):
class FloatSpinBoxBaseTest(SpinBoxBaseTest): class FloatSpinBoxBaseTest(SpinBoxBaseTest):
klass = GenericSpinBox
def test_values(self, qtbot): def test_values(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type) spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox) qtbot.addWidget(spinbox)
spinbox.setRange(-10.45, 10.45) spinbox.setRange(-10.45, 10.45)
...@@ -151,35 +143,35 @@ class FloatSpinBoxBaseTest(SpinBoxBaseTest): ...@@ -151,35 +143,35 @@ class FloatSpinBoxBaseTest(SpinBoxBaseTest):
assert spinbox.value() == spinbox.maximum() assert spinbox.value() == spinbox.maximum()
class TestUin8SpinBox(UinSpinBoxBaseTest): class TestUint8SpinBox(UintSpinBoxBaseTest):
numpy_type = np.uint8 numpy_type = np.uint8
class TestUin16SpinBox(UinSpinBoxBaseTest): class TestUint16SpinBox(UintSpinBoxBaseTest):
numpy_type = np.uint16 numpy_type = np.uint16
class TestUin32SpinBox(UinSpinBoxBaseTest): class TestUint32SpinBox(UintSpinBoxBaseTest):
numpy_type = np.uint32 numpy_type = np.uint32
class TestUin64SpinBox(UinSpinBoxBaseTest): class TestUint64SpinBox(UintSpinBoxBaseTest):
numpy_type = np.uint64 numpy_type = np.uint64
class TestIn8SpinBox(InSpinBoxBaseTest): class TestInt8SpinBox(IntSpinBoxBaseTest):
numpy_type = np.int8 numpy_type = np.int8
class TestIn16SpinBox(InSpinBoxBaseTest): class TestInt16SpinBox(IntSpinBoxBaseTest):
numpy_type = np.int16 numpy_type = np.int16
class TestIn32SpinBox(InSpinBoxBaseTest): class TestInt32SpinBox(IntSpinBoxBaseTest):
numpy_type = np.int32 numpy_type = np.int32
class TestIn64SpinBox(InSpinBoxBaseTest): class TestInt64SpinBox(IntSpinBoxBaseTest):
numpy_type = np.int64 numpy_type = np.int64
...@@ -192,18 +184,17 @@ class TestFloat64SpinBox(FloatSpinBoxBaseTest): ...@@ -192,18 +184,17 @@ class TestFloat64SpinBox(FloatSpinBoxBaseTest):
class TestTypeChange(SpinBoxBaseTest): class TestTypeChange(SpinBoxBaseTest):
klass = GenericSpinBox
numpy_type = np.uint8 numpy_type = np.uint8
def test_invalid_range_uin8(self, qtbot): def test_invalid_range_uint8(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type) spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox) qtbot.addWidget(spinbox)
spinbox.setRange(-10, 10) spinbox.setRange(-10, 10)
assert spinbox.minimum() == 0 assert spinbox.minimum() == 0
def test_values_uin8(self, qtbot): def test_values_uint8(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type) spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox) qtbot.addWidget(spinbox)
spinbox.setRange(0, 10) spinbox.setRange(0, 10)
...@@ -214,12 +205,12 @@ class TestTypeChange(SpinBoxBaseTest): ...@@ -214,12 +205,12 @@ class TestTypeChange(SpinBoxBaseTest):
spinbox.setValue(20) spinbox.setValue(20)
assert spinbox.value() == spinbox.maximum() assert spinbox.value() == spinbox.maximum()
def test_values_from_uin8_to_float_64(self, qtbot): def test_values_from_uint8_to_float_64(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type) spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox) qtbot.addWidget(spinbox)
self.numpy_type = np.float64 self.numpy_type = np.float64
spinbox.setType(self.numpy_type) spinbox.setSpinboxType(self.numpy_type)
spinbox.setRange(-10.45, 10.45) spinbox.setRange(-10.45, 10.45)
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
import numpy as np import numpy as np
from PyQt5.QtCore import pyqtSignal from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import pyqtProperty
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtGui import QValidator from PyQt5.QtGui import QValidator
...@@ -36,7 +37,7 @@ from ..utils import frozen ...@@ -36,7 +37,7 @@ from ..utils import frozen
@frozen @frozen
class GenericSpinBox(QAbstractSpinBox): class NumpySpinBox(QAbstractSpinBox):
"""Generic spinbox base class using numpy types """Generic spinbox base class using numpy types
Requires initialization with the numpy Requires initialization with the numpy
...@@ -49,6 +50,7 @@ class GenericSpinBox(QAbstractSpinBox): ...@@ -49,6 +50,7 @@ class GenericSpinBox(QAbstractSpinBox):
""" """
numpy_type = None numpy_type = None
typeChanged = pyqtSignal()
valueChanged = pyqtSignal() valueChanged = pyqtSignal()
minimumChanged = pyqtSignal() minimumChanged = pyqtSignal()
maximumChanged = pyqtSignal() maximumChanged = pyqtSignal()
...@@ -59,9 +61,9 @@ class GenericSpinBox(QAbstractSpinBox): ...@@ -59,9 +61,9 @@ class GenericSpinBox(QAbstractSpinBox):
:param parent QWidget: parent widget :param parent QWidget: parent widget
""" """
super(GenericSpinBox, self).__init__(parent) super(NumpySpinBox, self).__init__(parent)
self.setType(numpy_type) self.setSpinboxType(numpy_type)
self.lineEdit().textEdited.connect(self.__on_text_edited) self.lineEdit().textEdited.connect(self.__on_text_edited)
...@@ -232,22 +234,37 @@ class GenericSpinBox(QAbstractSpinBox): ...@@ -232,22 +234,37 @@ class GenericSpinBox(QAbstractSpinBox):
return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled
def setType(self, numpy_type): def spinboxType(self):
"""Returns the numpy type of the spinbox
:return: the numpy type used
"""
return self.numpy_type
def setSpinboxType(self, numpy_type):
"""Sets the input type of the spin box """Sets the input type of the spin box
:param value numpy_type: value to set :param value numpy_type: value to set
""" """
self.numpy_type = numpy_type if self.numpy_type != numpy_type:
if np.issubdtype(self.numpy_type, np.floating): self.numpy_type = numpy_type
self._info = np.finfo(self.numpy_type) if np.issubdtype(self.numpy_type, np.floating):
else: self._info = np.finfo(self.numpy_type)
self._info = np.iinfo(self.numpy_type) else:
self._info = np.iinfo(self.numpy_type)
self._minimum = self._info.min
self._maximum = self._info.max
self._value = None
self.setInputMethodHints(Qt.ImhFormattedNumbersOnly)
self._minimum = self._info.min self.setValue(0)
self._maximum = self._info.max
self._value = None
self.setInputMethodHints(Qt.ImhFormattedNumbersOnly) self.typeChanged.emit()
self.setValue(0) spinbox_type = pyqtProperty(
str, fget=spinboxType, fset=setSpinboxType, notify=typeChanged
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment