mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
Cache heightForWIdth calculation as recommended by Qt docs
This commit is contained in:
parent
bbe90eac79
commit
68032fc59a
@ -408,10 +408,22 @@ class FlowLayout(QLayout): # {{{
|
|||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
QLayout.__init__(self, parent)
|
QLayout.__init__(self, parent)
|
||||||
self.items = []
|
self.items = []
|
||||||
|
self.height_for_width_cache = {}
|
||||||
|
|
||||||
|
def clear_caches(self):
|
||||||
|
self.height_for_width_cache.clear()
|
||||||
|
|
||||||
def addItem(self, item):
|
def addItem(self, item):
|
||||||
|
self.clear_caches()
|
||||||
self.items.append(item)
|
self.items.append(item)
|
||||||
|
|
||||||
|
def isEmpty(self):
|
||||||
|
return not bool(self.items)
|
||||||
|
|
||||||
|
def invalidate(self):
|
||||||
|
self.clear_caches()
|
||||||
|
super().invalidate()
|
||||||
|
|
||||||
def itemAt(self, idx):
|
def itemAt(self, idx):
|
||||||
try:
|
try:
|
||||||
return self.items[idx]
|
return self.items[idx]
|
||||||
@ -432,7 +444,9 @@ class FlowLayout(QLayout): # {{{
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def heightForWidth(self, width):
|
def heightForWidth(self, width):
|
||||||
return self.do_layout(QRect(0, 0, width, 0), apply_geometry=False)
|
if (ans := self.height_for_width_cache.get(width)) is None:
|
||||||
|
ans = self.height_for_width_cache[width] = self.do_layout(QRect(0, 0, width, 0), apply_geometry=False)
|
||||||
|
return ans
|
||||||
|
|
||||||
def setGeometry(self, rect):
|
def setGeometry(self, rect):
|
||||||
QLayout.setGeometry(self, rect)
|
QLayout.setGeometry(self, rect)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user