Cleaner handling of separators in the flow toolbar

This commit is contained in:
Kovid Goyal 2021-11-13 15:22:04 +05:30
parent b604611a3a
commit 6ca398619b
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -55,6 +55,9 @@ class Button(QToolButton):
if self.isVisible() != old: if self.isVisible() != old:
self.layout_needed.emit() self.layout_needed.emit()
def __repr__(self):
return f'Button({self.toolTip()})'
class SingleLineToolBar(QToolBar): class SingleLineToolBar(QToolBar):
@ -137,26 +140,25 @@ class FlowToolBar(QWidget):
lines, current_line = [], [] lines, current_line = [], []
gmap = {} gmap = {}
for i, wid in enumerate(self.items): if apply_geometry:
prev_wid = self.items[i - 1] if i else None for item in self.items:
if isinstance(wid, Button) and not wid.isVisible(): if isinstance(item, Separator):
item.setGeometry(0, 0, 0, 0)
for wid in self.items:
if not wid.isVisible() or (not current_line and isinstance(wid, Separator)):
continue continue
isz = wid.sizeHint() isz = wid.sizeHint()
hs, vs = layout_spacing(wid), layout_spacing(wid, False) hs, vs = layout_spacing(wid), layout_spacing(wid, False)
next_x = x + isz.width() + hs next_x = x + isz.width() + hs
wid.setVisible(True)
if isinstance(wid, Separator) and isinstance(prev_wid, Separator):
wid.setVisible(False)
if next_x - hs > rect.right() and line_height > 0: if next_x - hs > rect.right() and line_height > 0:
if isinstance(prev_wid, Separator):
prev_wid.setVisible(False)
if isinstance(wid, Separator): if isinstance(wid, Separator):
wid.setVisible(False)
continue continue
x = rect.x() x = rect.x()
y = y + line_height + vs y = y + line_height + vs
next_x = x + isz.width() + hs next_x = x + isz.width() + hs
while current_line and isinstance(current_line[-1], Separator):
current_line.pop()
lines.append((line_height, current_line)) lines.append((line_height, current_line))
current_line = [] current_line = []
line_height = 0 line_height = 0
@ -164,17 +166,18 @@ class FlowToolBar(QWidget):
gmap[wid] = x, y, isz gmap[wid] = x, y, isz
x = next_x x = next_x
line_height = max(line_height, isz.height()) line_height = max(line_height, isz.height())
current_line.append((wid, isz.height())) current_line.append(wid)
lines.append((line_height, current_line)) lines.append((line_height, current_line))
if apply_geometry: if apply_geometry:
self.applied_geometry = rect self.applied_geometry = rect
for line_height, items in lines: for line_height, items in lines:
for wid, item_height in items: for wid in items:
x, wy, isz = gmap[wid] x, wy, isz = gmap[wid]
if item_height < line_height: if isz.height() < line_height:
wy += (line_height - item_height) // 2 wy += (line_height - isz.height()) // 2
if wid.isVisible():
wid.setGeometry(QRect(QPoint(x, wy), isz)) wid.setGeometry(QRect(QPoint(x, wy), isz))
return y + line_height - rect.y() return y + line_height - rect.y()