Skip to content

Commit

Permalink
Cleaner handling of separators in the flow toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Nov 13, 2021
1 parent b604611 commit 6ca3986
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/calibre/gui2/flow_toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def update_state(self):
if self.isVisible() != old:
self.layout_needed.emit()

def __repr__(self):
return f'Button({self.toolTip()})'


class SingleLineToolBar(QToolBar):

Expand Down Expand Up @@ -137,45 +140,45 @@ def layout_spacing(wid, horizontal=True):

lines, current_line = [], []
gmap = {}
for i, wid in enumerate(self.items):
prev_wid = self.items[i - 1] if i else None
if isinstance(wid, Button) and not wid.isVisible():
if apply_geometry:
for item in self.items:
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
isz = wid.sizeHint()
hs, vs = layout_spacing(wid), layout_spacing(wid, False)

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 isinstance(prev_wid, Separator):
prev_wid.setVisible(False)
if isinstance(wid, Separator):
wid.setVisible(False)
continue
x = rect.x()
y = y + line_height + vs
next_x = x + isz.width() + hs
while current_line and isinstance(current_line[-1], Separator):
current_line.pop()
lines.append((line_height, current_line))
current_line = []
line_height = 0
if apply_geometry:
gmap[wid] = x, y, isz
x = next_x
line_height = max(line_height, isz.height())
current_line.append((wid, isz.height()))
current_line.append(wid)

lines.append((line_height, current_line))

if apply_geometry:
self.applied_geometry = rect
for line_height, items in lines:
for wid, item_height in items:
for wid in items:
x, wy, isz = gmap[wid]
if item_height < line_height:
wy += (line_height - item_height) // 2
wid.setGeometry(QRect(QPoint(x, wy), isz))
if isz.height() < line_height:
wy += (line_height - isz.height()) // 2
if wid.isVisible():
wid.setGeometry(QRect(QPoint(x, wy), isz))

return y + line_height - rect.y()

Expand Down

0 comments on commit 6ca3986

Please sign in to comment.