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 @@
import pytest
import numpy as np
from ..widgets.spinboxes import GenericSpinBox
from ..widgets.spinboxes import NumpySpinBox
@pytest.fixture()
......@@ -46,10 +46,8 @@ def expected_values():
class SpinBoxBaseTest:
klass = None
def test_range(self, qtbot):
spinbox = self.klass(self.numpy_type)
spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox)
minimum = 0
......@@ -65,7 +63,7 @@ class SpinBoxBaseTest:
assert spinbox.value() == spinbox.maximum()
def test_valid_input(self, qtbot):
spinbox = self.klass(self.numpy_type)
spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox)
value = "42"
......@@ -74,7 +72,7 @@ class SpinBoxBaseTest:
assert spinbox.value() == spinbox.numpy_type(value)
def test_invalid_input(self, qtbot, expected_values):
spinbox = self.klass(self.numpy_type)
spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox)
value = "44444444444444444444444444444444444444444444444444444444444"
expected_value = None
......@@ -90,18 +88,16 @@ class SpinBoxBaseTest:
assert spinbox.value() == spinbox.numpy_type(expected_value)
class UinSpinBoxBaseTest(SpinBoxBaseTest):
klass = GenericSpinBox
class UintSpinBoxBaseTest(SpinBoxBaseTest):
def test_invalid_range(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type)
spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox)
spinbox.setRange(-10, 10)
assert spinbox.minimum() == 0
def test_values(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type)
spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox)
spinbox.setRange(0, 10)
......@@ -113,11 +109,9 @@ class UinSpinBoxBaseTest(SpinBoxBaseTest):
assert spinbox.value() == spinbox.maximum()
class InSpinBoxBaseTest(SpinBoxBaseTest):
klass = GenericSpinBox
class IntSpinBoxBaseTest(SpinBoxBaseTest):
def test_values(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type)
spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox)
spinbox.setRange(0, 10)
......@@ -130,10 +124,8 @@ class InSpinBoxBaseTest(SpinBoxBaseTest):
class FloatSpinBoxBaseTest(SpinBoxBaseTest):
klass = GenericSpinBox
def test_values(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type)
spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox)
spinbox.setRange(-10.45, 10.45)
......@@ -151,35 +143,35 @@ class FloatSpinBoxBaseTest(SpinBoxBaseTest):
assert spinbox.value() == spinbox.maximum()
class TestUin8SpinBox(UinSpinBoxBaseTest):
class TestUint8SpinBox(UintSpinBoxBaseTest):
numpy_type = np.uint8
class TestUin16SpinBox(UinSpinBoxBaseTest):
class TestUint16SpinBox(UintSpinBoxBaseTest):
numpy_type = np.uint16
class TestUin32SpinBox(UinSpinBoxBaseTest):
class TestUint32SpinBox(UintSpinBoxBaseTest):
numpy_type = np.uint32
class TestUin64SpinBox(UinSpinBoxBaseTest):
class TestUint64SpinBox(UintSpinBoxBaseTest):
numpy_type = np.uint64
class TestIn8SpinBox(InSpinBoxBaseTest):
class TestInt8SpinBox(IntSpinBoxBaseTest):
numpy_type = np.int8
class TestIn16SpinBox(InSpinBoxBaseTest):
class TestInt16SpinBox(IntSpinBoxBaseTest):
numpy_type = np.int16
class TestIn32SpinBox(InSpinBoxBaseTest):
class TestInt32SpinBox(IntSpinBoxBaseTest):
numpy_type = np.int32
class TestIn64SpinBox(InSpinBoxBaseTest):
class TestInt64SpinBox(IntSpinBoxBaseTest):
numpy_type = np.int64
......@@ -192,18 +184,17 @@ class TestFloat64SpinBox(FloatSpinBoxBaseTest):
class TestTypeChange(SpinBoxBaseTest):
klass = GenericSpinBox
numpy_type = np.uint8
def test_invalid_range_uin8(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type)
def test_invalid_range_uint8(self, qtbot):
spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox)
spinbox.setRange(-10, 10)
assert spinbox.minimum() == 0
def test_values_uin8(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type)
def test_values_uint8(self, qtbot):
spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox)
spinbox.setRange(0, 10)
......@@ -214,12 +205,12 @@ class TestTypeChange(SpinBoxBaseTest):
spinbox.setValue(20)
assert spinbox.value() == spinbox.maximum()
def test_values_from_uin8_to_float_64(self, qtbot):
spinbox = GenericSpinBox(self.numpy_type)
def test_values_from_uint8_to_float_64(self, qtbot):
spinbox = NumpySpinBox(self.numpy_type)
qtbot.addWidget(spinbox)
self.numpy_type = np.float64
spinbox.setType(self.numpy_type)
spinbox.setSpinboxType(self.numpy_type)
spinbox.setRange(-10.45, 10.45)
......
......@@ -26,6 +26,7 @@
import numpy as np
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import pyqtProperty
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QValidator
......@@ -36,7 +37,7 @@ from ..utils import frozen
@frozen
class GenericSpinBox(QAbstractSpinBox):
class NumpySpinBox(QAbstractSpinBox):
"""Generic spinbox base class using numpy types
Requires initialization with the numpy
......@@ -49,6 +50,7 @@ class GenericSpinBox(QAbstractSpinBox):
"""
numpy_type = None
typeChanged = pyqtSignal()
valueChanged = pyqtSignal()
minimumChanged = pyqtSignal()
maximumChanged = pyqtSignal()
......@@ -59,9 +61,9 @@ class GenericSpinBox(QAbstractSpinBox):
: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)
......@@ -232,12 +234,21 @@ class GenericSpinBox(QAbstractSpinBox):
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
:param value numpy_type: value to set
"""
if self.numpy_type != numpy_type:
self.numpy_type = numpy_type
if np.issubdtype(self.numpy_type, np.floating):
self._info = np.finfo(self.numpy_type)
......@@ -251,3 +262,9 @@ class GenericSpinBox(QAbstractSpinBox):
self.setInputMethodHints(Qt.ImhFormattedNumbersOnly)
self.setValue(0)
self.typeChanged.emit()
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