Skip to content

Commit 41e5785

Browse files
committed
PERF: delay updating cell sizes on scroll
1 parent f095bd6 commit 41e5785

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

larray_editor/arraywidget.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
import numpy as np
6464

6565
from qtpy import QtCore
66-
from qtpy.QtCore import Qt, QPoint, QItemSelection, QItemSelectionModel, Signal, QSize, QModelIndex
66+
from qtpy.QtCore import (Qt, QPoint, QItemSelection, QItemSelectionModel,
67+
Signal, QSize, QModelIndex, QTimer)
6768
from qtpy.QtGui import (QDoubleValidator, QIntValidator, QKeySequence, QFontMetrics, QCursor, QPixmap, QPainter, QIcon,
6869
QWheelEvent, QMouseEvent)
6970
from qtpy.QtWidgets import (QApplication, QTableView, QItemDelegate, QLineEdit, QCheckBox,
@@ -1290,6 +1291,8 @@ def get_numbers_width(self, int_digits, frac_digits=0, need_sign=False, scientif
12901291

12911292
class ArrayEditorWidget(QWidget):
12921293
dataChanged = Signal(list)
1294+
# milliseconds between a scroll event and updating cell sizes
1295+
UPDATE_SIZES_FROM_CONTENT_DELAY = 100
12931296

12941297
def __init__(self, parent, data=None, readonly=False, attributes=None, bg_gradient='blue-red',
12951298
minvalue=None, maxvalue=None, digits=None):
@@ -1512,6 +1515,12 @@ def hidden_vscroll_range_changed(min_value: int, max_value: int):
15121515
self._updating_vlabels_row_heights = False
15131516
self._updating_axes_row_heights = False
15141517

1518+
update_timer = QTimer(self)
1519+
update_timer.setSingleShot(True)
1520+
update_timer.setInterval(self.UPDATE_SIZES_FROM_CONTENT_DELAY)
1521+
update_timer.timeout.connect(self.update_cell_sizes_from_content)
1522+
self.update_cell_sizes_timer = update_timer
1523+
15151524
def visible_cols(self, include_partial=True):
15161525
"""number of visible columns *including* partially visible ones"""
15171526

@@ -1650,20 +1659,24 @@ def visible_vscroll_changed(self, value):
16501659
model_data.set_v_offset(new_v_offset)
16511660
self.model_vlabels.set_v_offset(new_v_offset)
16521661
self._update_selection(model_data.h_offset, new_v_offset)
1653-
# TODO: scrolling feels *much* snappier (especially for in-memory
1654-
# datasets) without these, but not having them at all is
1655-
# annoying. Can we make that faster?
1656-
# * Would computing the sizeHint ourselves help?
1657-
# * For many in-memory (especially numerical) containers,
1658-
# it would be cheaper to compute that once for the
1659-
# whole array instead
1660-
self._update_hlabels_column_widths_from_content()
1661-
# we do not need to update axes cell size on scroll but vlabels
1662-
# width can change on scroll (and they are linked to axes widths)
1663-
self._update_axes_column_widths_from_content()
1664-
self._update_vlabels_row_heights_from_content()
1662+
self.update_cell_sizes_timer.start()
1663+
16651664
hidden_vscroll.setValue(new_hidden_offset)
16661665

1666+
def update_cell_sizes_from_content(self):
1667+
logger.debug("ArrayEditorWidget.update_cell_sizes_from_content()")
1668+
# TODO: having this in a timer alleviates the scrolling speed issue
1669+
# but we could also try to make this faster:
1670+
# * Would computing the sizeHint ourselves help?
1671+
# * For many in-memory (especially numerical) containers,
1672+
# it would be cheaper to compute that once for the
1673+
# whole array instead of after each scroll
1674+
self._update_hlabels_column_widths_from_content()
1675+
# we do not need to update axes cell size on scroll but vlabels
1676+
# width can change on scroll (and they are linked to axes widths)
1677+
self._update_axes_column_widths_from_content()
1678+
self._update_vlabels_row_heights_from_content()
1679+
16671680
def visible_hscroll_changed(self, value):
16681681
# 'value' will be the first visible column
16691682
assert value >= 0, f"value must be >= 0 but is {value!r}"
@@ -1715,11 +1728,7 @@ def visible_hscroll_changed(self, value):
17151728
model_data.set_h_offset(new_h_offset)
17161729
self.model_hlabels.set_h_offset(new_h_offset)
17171730
self._update_selection(new_h_offset, model_data.v_offset)
1718-
self._update_hlabels_column_widths_from_content()
1719-
# we do not need to update axes cell size on scroll but vlabels
1720-
# width can change on scroll (and they are linked to axes widths)
1721-
self._update_axes_column_widths_from_content()
1722-
self._update_vlabels_row_heights_from_content()
1731+
self.update_cell_sizes_timer.start()
17231732
hidden_hscroll.setValue(new_hidden_offset)
17241733

17251734
def on_axes_column_resized(self, logical_index, old_size, new_size):

0 commit comments

Comments
 (0)