Skip to content

Commit 441da95

Browse files
committed
Merge PR #1337 into feature/pr1337-local-base
2 parents 7f4e967 + b3f4594 commit 441da95

2 files changed

Lines changed: 56 additions & 6 deletions

File tree

anylabeling/views/labeling/label_widget.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@
9797
LABEL_OPACITY = 128
9898

9999

100+
def _measure_text_width(font_metrics, text):
101+
if hasattr(font_metrics, "horizontalAdvance"):
102+
return font_metrics.horizontalAdvance(text)
103+
return font_metrics.width(text)
104+
105+
100106
class LabelingWidget(LabelDialog):
101107
"""The main widget for labeling images"""
102108

@@ -3720,12 +3726,12 @@ def update_attributes(self, shape_index):
37203726
if hasattr(self, "grid_layout_container"):
37213727
font_metrics = QFontMetrics(self.grid_layout_container.font())
37223728
else:
3723-
font_metrics = QLabel().font()
3729+
font_metrics = QFontMetrics(QLabel().font())
37243730
available_width = self.scroll_area.width() - 30
37253731
property_display = property
3726-
if font_metrics.width(property) > available_width:
3732+
if _measure_text_width(font_metrics, property) > available_width:
37273733
while (
3728-
font_metrics.width(property_display + "...")
3734+
_measure_text_width(font_metrics, property_display + "...")
37293735
> available_width
37303736
and len(property_display) > 1
37313737
):
@@ -3747,18 +3753,19 @@ def update_attributes(self, shape_index):
37473753
main_layout.setSpacing(2)
37483754

37493755
def get_truncated_text(text, max_width):
3750-
if font_metrics.width(text) <= max_width:
3756+
if _measure_text_width(font_metrics, text) <= max_width:
37513757
return text, text
37523758
truncated = text
37533759
while (
3754-
font_metrics.width(truncated + "...") > max_width
3760+
_measure_text_width(font_metrics, truncated + "...")
3761+
> max_width
37553762
and len(truncated) > 1
37563763
):
37573764
truncated = truncated[:-1]
37583765
return truncated + "...", text
37593766

37603767
def get_button_width(text):
3761-
return font_metrics.width(text) + 30
3768+
return _measure_text_width(font_metrics, text) + 30
37623769

37633770
def create_radio_button_with_handler(
37643771
display_text, original_text, prop, shape_idx

tests/test_label_widget_metrics.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
import unittest
3+
4+
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
5+
6+
try:
7+
from PyQt6 import QtGui, QtWidgets
8+
9+
PYQT_AVAILABLE = True
10+
except Exception:
11+
PYQT_AVAILABLE = False
12+
13+
14+
@unittest.skipUnless(
15+
PYQT_AVAILABLE, "PyQt6 is required for label widget metrics tests"
16+
)
17+
class TestLabelWidgetMetrics(unittest.TestCase):
18+
19+
def setUp(self):
20+
self.app = QtWidgets.QApplication.instance()
21+
if self.app is None:
22+
self.app = QtWidgets.QApplication([])
23+
24+
def test_measure_text_width_matches_horizontal_advance(self):
25+
from anylabeling.views.labeling.label_widget import _measure_text_width
26+
27+
font = self.app.font()
28+
metrics = QtGui.QFontMetrics(font)
29+
30+
self.assertEqual(
31+
_measure_text_width(metrics, "bodyColor"),
32+
metrics.horizontalAdvance("bodyColor"),
33+
)
34+
35+
def test_measure_text_width_falls_back_to_width(self):
36+
from anylabeling.views.labeling.label_widget import _measure_text_width
37+
38+
class LegacyFontMetrics:
39+
def width(self, text):
40+
return len(text) * 7
41+
42+
metrics = LegacyFontMetrics()
43+
self.assertEqual(_measure_text_width(metrics, "vehicle"), 49)

0 commit comments

Comments
 (0)