|
57 | 57 | get_cathode_labels, |
58 | 58 | ) |
59 | 59 | from ..ui.clinical_scales_settings_dialog import ClinicalScalesSettingsDialog |
| 60 | +from ..ui.widgets import line_edit_min_width_for_text, push_button_min_width_for_label |
60 | 61 | from ..utils.program_config_manager import ( |
61 | 62 | ProgramConfigManager, |
62 | 63 | get_program_config_manager, |
@@ -1805,14 +1806,24 @@ def _add_clinical_scale_row( |
1805 | 1806 |
|
1806 | 1807 | name_edit = QLineEdit() |
1807 | 1808 | name_edit.setPlaceholderText(PLACEHOLDERS["scale_name"]) |
1808 | | - name_edit.setMaximumWidth(80) |
1809 | 1809 | name_edit.setText(name) |
| 1810 | + line_edit_min_width_for_text(name_edit, name, floor=56) |
| 1811 | + name_edit.textChanged.connect( |
| 1812 | + lambda text, edit=name_edit: line_edit_min_width_for_text( |
| 1813 | + edit, text, floor=56 |
| 1814 | + ) |
| 1815 | + ) |
1810 | 1816 |
|
1811 | 1817 | score_edit = QLineEdit() |
1812 | 1818 | score_edit.setPlaceholderText(PLACEHOLDERS["scale_score"]) |
1813 | | - score_edit.setMaximumWidth(50) |
1814 | 1819 | score_edit.setValidator(QIntValidator()) |
1815 | 1820 | score_edit.setText(value) |
| 1821 | + line_edit_min_width_for_text(score_edit, value, floor=44) |
| 1822 | + score_edit.textChanged.connect( |
| 1823 | + lambda text, edit=score_edit: line_edit_min_width_for_text( |
| 1824 | + edit, text, floor=44 |
| 1825 | + ) |
| 1826 | + ) |
1816 | 1827 |
|
1817 | 1828 | btn = None |
1818 | 1829 | if with_plus: |
@@ -1908,14 +1919,39 @@ def _on_presets_changed(self, new_presets: dict[str, list[str]]): |
1908 | 1919 | # Preset was deleted - clear scales |
1909 | 1920 | self._apply_preset_scales([]) |
1910 | 1921 |
|
| 1922 | + def _preset_buttons_content_size(self) -> QSize: |
| 1923 | + """Measure preset row size from buttons. |
| 1924 | +
|
| 1925 | + Container ``sizeHint()`` can be 0 immediately after a rebuild. |
| 1926 | + """ |
| 1927 | + layout = self.preset_row_layout |
| 1928 | + if layout is None or not self.preset_buttons: |
| 1929 | + return QSize(0, 0) |
| 1930 | + |
| 1931 | + margins = layout.contentsMargins() |
| 1932 | + spacing = layout.spacing() |
| 1933 | + width = margins.left() + margins.right() |
| 1934 | + height = margins.top() + margins.bottom() |
| 1935 | + for index, btn in enumerate(self.preset_buttons): |
| 1936 | + hint = btn.sizeHint() |
| 1937 | + width += hint.width() |
| 1938 | + height = max(height, hint.height() + margins.top() + margins.bottom()) |
| 1939 | + if index > 0: |
| 1940 | + width += spacing |
| 1941 | + return QSize(width, height) |
| 1942 | + |
1911 | 1943 | def _update_preset_buttons_geometry(self) -> None: |
1912 | 1944 | """Size the preset strip so horizontal scrolling appears when needed.""" |
1913 | 1945 | if not hasattr(self, "preset_scroll_content"): |
1914 | 1946 | return |
| 1947 | + if self.preset_row_layout is not None: |
| 1948 | + self.preset_row_layout.activate() |
| 1949 | + |
| 1950 | + measured = self._preset_buttons_content_size() |
1915 | 1951 | self.preset_scroll_content.adjustSize() |
1916 | 1952 | hint = self.preset_scroll_content.sizeHint() |
1917 | | - width = max(hint.width(), 1) |
1918 | | - content_height = max(hint.height(), 1) |
| 1953 | + width = max(measured.width(), hint.width(), 1) |
| 1954 | + content_height = max(measured.height(), hint.height(), 1) |
1919 | 1955 | self.preset_scroll_content.setMinimumSize(width, content_height) |
1920 | 1956 | self.preset_scroll_content.resize(width, content_height) |
1921 | 1957 |
|
@@ -1953,10 +1989,13 @@ def _refresh_preset_buttons(self): |
1953 | 1989 | for preset_name in ordered_names: |
1954 | 1990 | btn = QPushButton(preset_name) |
1955 | 1991 | btn.setObjectName(f"preset_{preset_name}") |
| 1992 | + push_button_min_width_for_label(btn, preset_name) |
1956 | 1993 | self.preset_buttons.append(btn) |
1957 | 1994 | preset_row.addWidget(btn) |
1958 | 1995 |
|
1959 | | - self._update_preset_buttons_geometry() |
| 1996 | + # Defer sizing until Qt has laid out the new buttons; immediate sizeHint() |
| 1997 | + # on the scroll content is often 0 and collapses the strip to 1px wide. |
| 1998 | + QTimer.singleShot(0, self._update_preset_buttons_geometry) |
1960 | 1999 |
|
1961 | 2000 | if hasattr(self, "on_add_callback") and hasattr(self, "on_remove_callback"): |
1962 | 2001 | self._connect_preset_buttons() |
0 commit comments