Skip to content

Commit

Permalink
Merge pull request #654 from mprib/auto-fit-spinbox-width
Browse files Browse the repository at this point in the history
set width based on check of font metrics and include padding to avoid…
  • Loading branch information
mprib authored Oct 24, 2024
2 parents 8788cc5 + 9464524 commit 9ec7a08
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
27 changes: 20 additions & 7 deletions caliscope/gui/charuco_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import caliscope.logger
from caliscope.calibration.charuco import Charuco
from caliscope.controller import Controller
from caliscope.gui.utils.spinbox_utils import setup_spinbox_sizing

logger = caliscope.logger.get(__name__)

Expand Down Expand Up @@ -135,7 +136,7 @@ def build_charuco(self):
rows = self.charuco_config.row_spin.value()
board_height = self.charuco_config.length_spin.value()
board_width = self.charuco_config.width_spin.value()
aruco_scale = 0.75
aruco_scale = self.params["aruco_scale"]
units = self.charuco_config.units.currentText()
square_edge_length = self.printed_edge_length.value()
# a
Expand Down Expand Up @@ -199,22 +200,34 @@ def __init__(self, controller: Controller):
self.params = self.controller.config.dict["charuco"]

self.column_spin = QSpinBox()
self.column_spin.setMinimum(3)
setup_spinbox_sizing(self.column_spin, min_value=3,max_value=999, padding=10)
self.column_spin.setValue(self.params["columns"])
# self.column_spin.setMinimum(3)
# self.column_spin.setMinimumWidth(50)
# self.column_spin.setAlignment(Qt.AlignmentFlag.AlignCenter)


self.row_spin = QSpinBox()
self.row_spin.setMinimum(4)
self.row_spin.setValue(self.params["rows"])
setup_spinbox_sizing(self.row_spin, min_value=4,max_value=999, padding=10)
# self.row_spin.setMinimum(4)
# self.row_spin.setMinimumWidth(50)
# self.row_spin.setAlignment(Qt.AlignmentFlag.AlignCenter)

self.width_spin = QDoubleSpinBox()
self.width_spin.setMinimum(1)
self.width_spin.setMaximum(10000)
self.width_spin.setValue(self.params["board_width"])
setup_spinbox_sizing(self.width_spin,min_value=1, max_value=10000,padding=10)
# self.width_spin.setMinimum(1)
# self.width_spin.setMaximum(10000)
# self.width_spin.setMinimumWidth(70)
# self.width_spin.setAlignment(Qt.AlignmentFlag.AlignCenter)


self.length_spin = QDoubleSpinBox()
self.length_spin.setMaximum(10000)
self.length_spin.setMinimum(1)
self.length_spin.setValue(self.params["board_height"])
setup_spinbox_sizing(self.length_spin,min_value=1, max_value=10000,padding=10)
# self.length_spin.setMinimumWidth(70)
# self.length_spin.setAlignment(Qt.AlignmentFlag.AlignCenter)

self.units = QComboBox()
self.units.addItems(["cm", "inch"])
Expand Down
67 changes: 67 additions & 0 deletions caliscope/gui/utils/spinbox_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QDoubleSpinBox


def calculate_spinbox_width(spin_box, min_width=None, padding=None):
"""
Calculate appropriate width for a spin box based on its content.
Args:
spin_box: QSpinBox or QDoubleSpinBox instance
min_width: Optional minimum width to enforce
padding: Optional padding to add to calculated width
Returns:
Calculated minimum width in pixels
"""
fm = spin_box.fontMetrics()

# Get the maximum possible string length
if isinstance(spin_box, QDoubleSpinBox):
# For double, consider decimal places
decimals = spin_box.decimals()
# Format with maximum digits: -1234.56
max_str = f"{spin_box.minimum():.{decimals}f}"
min_str = f"{spin_box.maximum():.{decimals}f}"
else:
max_str = str(spin_box.minimum())
min_str = str(spin_box.maximum())

# Get width of the longest possible string
max_width = max(fm.horizontalAdvance(max_str),
fm.horizontalAdvance(min_str))

# Add space for spin arrows and frame
if padding is None:
padding = fm.height() + 20 # Default padding based on font height

width = max_width + padding

# Enforce minimum width if specified
if min_width is not None:
width = max(width, min_width)

return width

def setup_spinbox_sizing(spin_box, centered=True, min_value=None, max_value=None, min_width=None, padding=None):
"""
Configure sizing and alignment for a spin box.
Args:
spin_box: QSpinBox or QDoubleSpinBox instance
centered: Whether to center-align the text
min_width: Optional minimum width to enforce
padding: Optional padding to add to calculated width
"""
if centered:
spin_box.setAlignment(Qt.AlignmentFlag.AlignCenter)

if min_value is not None:
spin_box.setMinimum(min_value)
if max_value is not None:
spin_box.setMaximum(max_value)

width = calculate_spinbox_width(spin_box, min_width, padding)
spin_box.setMinimumWidth(width)

return width
2 changes: 2 additions & 0 deletions caliscope/gui/workspace_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import caliscope.logger
from caliscope.controller import Controller
from caliscope.gui.synched_frames_display import SynchedFramesDisplay
from caliscope.gui.utils.spinbox_utils import setup_spinbox_sizing

logger = caliscope.logger.get(__name__)

Expand All @@ -26,6 +27,7 @@ def __init__(self, controller: Controller):

self.camera_count_spin = QSpinBox()
self.camera_count_spin.setValue(self.controller.get_camera_count())
setup_spinbox_sizing(self.camera_count_spin,min_value=1,max_value=100,padding=30)
self.camera_count_spin.setMaximumWidth(40)

self.status_HTML = QTextBrowser()
Expand Down

0 comments on commit 9ec7a08

Please sign in to comment.