LCOV - code coverage report
Current view: top level - src/pairinteraction_gui/qobjects - spin_boxes.py (source / functions) Hit Total Coverage
Test: coverage.info Lines: 36 41 87.8 %
Date: 2025-09-29 10:28:29 Functions: 3 8 37.5 %

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

Generated by: LCOV version 1.16