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