Line data Source code
1 : # SPDX-FileCopyrightText: 2025 Pairinteraction Developers
2 : # SPDX-License-Identifier: LGPL-3.0-or-later
3 :
4 1 : from typing import Optional
5 :
6 1 : from PySide6.QtWidgets import (
7 : QDoubleSpinBox,
8 : QSpinBox,
9 : QWidget,
10 : )
11 :
12 :
13 1 : class IntSpinBox(QSpinBox):
14 : """Custom spin box for integer values."""
15 :
16 1 : def __init__(
17 : self,
18 : parent: Optional[QWidget] = None,
19 : vmin: int = 0,
20 : vmax: int = 999,
21 : vdefault: int = 0,
22 : vstep: Optional[int] = None,
23 : suffix: Optional[str] = None,
24 : tooltip: Optional[str] = None,
25 : ) -> None:
26 : """Initialize the integer spin box."""
27 1 : super().__init__(parent)
28 :
29 1 : self.setRange(int(vmin), int(vmax))
30 1 : self.setValue(int(vdefault))
31 1 : self.setSingleStep(vstep if vstep is not None else 1)
32 :
33 1 : if suffix:
34 0 : self.setSuffix(suffix)
35 1 : if tooltip:
36 1 : self.setToolTip(tooltip)
37 :
38 :
39 1 : class HalfIntSpinBox(QDoubleSpinBox):
40 : """Custom spin box for half int values."""
41 :
42 1 : def __init__(
43 : self,
44 : parent: Optional[QWidget] = None,
45 : vmin: float = 0.5,
46 : vmax: float = 999.5,
47 : vdefault: float = 0.5,
48 : vstep: Optional[int] = None,
49 : suffix: Optional[str] = None,
50 : tooltip: Optional[str] = None,
51 : ) -> None:
52 : """Initialize the double spin box."""
53 1 : super().__init__(parent)
54 :
55 1 : vstep = vstep if vstep is not None else 1
56 1 : assert vdefault % 1 == 0.5, "Default value must be a half integer." # NOSONAR
57 1 : assert vstep % 1 == 0, "Step value must be an integer."
58 :
59 1 : self.setRange(vmin, vmax)
60 1 : self.setValue(vdefault)
61 1 : self.setSingleStep(vstep)
62 1 : self.setDecimals(1)
63 :
64 1 : if suffix:
65 0 : self.setSuffix(suffix)
66 1 : if tooltip:
67 1 : self.setToolTip(tooltip)
68 :
69 1 : def valueFromText(self, text: str) -> float:
70 : """Convert text to value, ensuring it's a half integer."""
71 0 : value = super().valueFromText(text)
72 0 : return round(value - 0.49) + 0.5
73 :
74 :
75 1 : class DoubleSpinBox(QDoubleSpinBox):
76 : """Custom spin box for double values."""
77 :
78 1 : def __init__(
79 : self,
80 : parent: Optional[QWidget] = None,
81 : vmin: float = 0,
82 : vmax: float = 999.9,
83 : vdefault: float = 0,
84 : vstep: Optional[float] = None,
85 : suffix: Optional[str] = None,
86 : decimals: int = 1,
87 : tooltip: Optional[str] = None,
88 : ) -> None:
89 : """Initialize the double spin box."""
90 1 : super().__init__(parent)
91 :
92 1 : self.setDecimals(decimals)
93 1 : self.setSingleStep(vstep if vstep is not None else 10**-decimals)
94 1 : self.setRange(vmin, vmax)
95 1 : self.setValue(vdefault)
96 :
97 1 : if suffix:
98 0 : self.setSuffix(suffix)
99 1 : if tooltip:
100 1 : self.setToolTip(tooltip)
|