Skip to content

Commit

Permalink
Merge pull request #604 from ZLLentz/perf_resize_text
Browse files Browse the repository at this point in the history
PERF: set text size on resize event, not on paint event, to avoid 100% cpu
  • Loading branch information
ZLLentz authored Jun 4, 2024
2 parents df47c8f + a6a4645 commit baedbb8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
22 changes: 22 additions & 0 deletions docs/source/upcoming_release_notes/604-perf_resize_text.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
604 perf_resize_text
#################

API Breaks
----------
- N/A

Features
--------
- N/A

Bugfixes
--------
- Fix an issue where the row positioner widget's resizing would peg the cpu to 100%

Maintenance
-----------
- N/A

Contributors
------------
- zllentz
26 changes: 13 additions & 13 deletions typhos/dynamic_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def patch_widget(
maximum font size. Content margin settings determine the content
rectangle, and this padding is applied as a percentage on top of that.
"""
def paintEvent(event: QtGui.QPaintEvent) -> None:
def resizeEvent(event: QtGui.QResizeEvent) -> None:
font = widget.font()
font_size = get_widget_maximum_font_size(
widget, widget.text(),
Expand All @@ -129,27 +129,27 @@ def paintEvent(event: QtGui.QPaintEvent) -> None:
if abs(font.pointSizeF() - font_size) > 0.1:
font.setPointSizeF(font_size)
widget.setFont(font)
return orig_paint_event(event)
return orig_resize_event(event)

def minimumSizeHint() -> QtCore.QSize:
# Do not give any size hint as it it changes during paintEvent
# Do not give any size hint as it it changes during resizeEvent
return QtWidgets.QWidget.minimumSizeHint(widget)

def sizeHint() -> QtCore.QSize:
# Do not give any size hint as it it changes during paintEvent
# Do not give any size hint as it it changes during resizeEvent
return QtWidgets.QWidget.sizeHint(widget)

if hasattr(widget.paintEvent, "_patched_methods_"):
if hasattr(widget.resizeEvent, "_patched_methods_"):
return

orig_paint_event = widget.paintEvent
orig_resize_event = widget.resizeEvent

paintEvent._patched_methods_ = (
widget.paintEvent,
resizeEvent._patched_methods_ = (
widget.resizeEvent,
widget.sizeHint,
widget.minimumSizeHint,
)
widget.paintEvent = paintEvent
widget.resizeEvent = resizeEvent
widget.sizeHint = sizeHint
widget.minimumSizeHint = minimumSizeHint

Expand All @@ -163,14 +163,14 @@ def unpatch_widget(widget: QtWidgets.QWidget) -> None:
widget : QtWidgets.QWidget
The widget to unpatch.
"""
if not hasattr(widget.paintEvent, "_patched_methods_"):
if not hasattr(widget.resizeEvent, "_patched_methods_"):
return

(
widget.paintEvent,
widget.resizeEvent,
widget.sizeHint,
widget.minimumSizeHint,
) = widget.paintEvent._patched_methods_
) = widget.resizeEvent._patched_methods_


def is_patched(widget: QtWidgets.QWidget) -> bool:
Expand All @@ -187,4 +187,4 @@ def is_patched(widget: QtWidgets.QWidget) -> bool:
bool
True if the widget has been patched.
"""
return hasattr(widget.paintEvent, "_patched_methods_")
return hasattr(widget.resizeEvent, "_patched_methods_")
8 changes: 6 additions & 2 deletions typhos/tests/test_dynamic_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ def test_patching(
)
print("Starting font size", original_font_size)

event = QtGui.QPaintEvent(QtCore.QRect(0, 0, widget.width(), widget.height()))
old_size = widget.size()
event = QtGui.QResizeEvent(
QtCore.QSize(old_size.width() * 2, old_size.height() * 2),
old_size,
)

assert not is_patched(widget)
patch_widget(widget)
assert is_patched(widget)

widget.paintEvent(event)
widget.resizeEvent(event)
new_font_size = widget.font().pointSizeF()
print("Patched font size", new_font_size)
assert original_font_size != new_font_size
Expand Down

0 comments on commit baedbb8

Please sign in to comment.