diff --git a/sloth/gui/buttonarea.py b/sloth/gui/buttonarea.py index 2f2566d..632ad04 100644 --- a/sloth/gui/buttonarea.py +++ b/sloth/gui/buttonarea.py @@ -88,7 +88,6 @@ class ButtonArea(QWidget): self.vlayout = QVBoxLayout() self.vlayout.setAlignment(Qt.AlignTop) self.vlayout.addWidget(self.label_button_list) - self.setLayout(self.vlayout) self.stateChanged.connect(self.stateHasChanged) if labels is not None: @@ -99,6 +98,7 @@ class ButtonArea(QWidget): self.add_hotkey(choice, name, hotkey) self.init_button_lists() self.vlayout.addStretch(1) + self.setLayout(self.vlayout) def stateHasChanged(self, state): print "stateChanged(object)", state diff --git a/sloth/gui/floatinglayout.py b/sloth/gui/floatinglayout.py index 5d2a3f4..d918ff2 100644 --- a/sloth/gui/floatinglayout.py +++ b/sloth/gui/floatinglayout.py @@ -5,6 +5,7 @@ class FloatingLayout(QLayout): def __init__(self, parent=None): QLayout.__init__(self, parent) self._items = [] + self._last_min_size = self.minimumSize() def addItem(self, item): self._items.append(item) @@ -31,37 +32,58 @@ class FloatingLayout(QLayout): def setGeometry(self, r): QLayout.setGeometry(self, r) self.layoutChildren(r) + min_size = self.minimumSize() + if self._last_min_size != min_size: + self._last_min_size = min_size + self.parentWidget().updateGeometry() def minimumSize(self): - sz = QSize(0, 20) + w = 0 + h = 0 for item in self._items: - sz.rwidth = max(sz.width(), item.minimumSize().width()) - return sz + w = max(w, item.minimumSize().width()) + h = max(h, item.minimumSize().height()) + + current_width = self.contentsRect().width() + if current_width > 0: + h = self.heightForWidth(current_width) + + left, top, right, bottom = self.getContentsMargins() + w += left + right + h += top + bottom + + return QSize(w, h) def hasHeightForWidth(self): return True def heightForWidth(self, width): - height = self.layoutChildren(QRect(0, 0, width, 100), False) - return height + height = self.layoutChildren(QRect(0, 0, width, 0), False) + left, top, right, bottom = self.getContentsMargins() + return height + top + bottom - def layoutChildren(self, r, appl=True): - line_width = r.x() - line_height = 0 - line_top = r.y() - max_width = r.x() + r.width() + def layoutChildren(self, rect, appl=True): + left, top, right, bottom = self.getContentsMargins() + r = rect.adjusted(+left, +top, -right, -bottom) + x = r.x(); + y = r.y(); + lineHeight = 0 for item in self._items: + wid = item.widget() + spaceX = wid.style().layoutSpacing(QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Horizontal) + spaceY = wid.style().layoutSpacing(QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Vertical) + sz_hint = item.sizeHint() - if line_width != r.x() and line_width + sz_hint.width() > max_width: + if x != r.x() and x + sz_hint.width() >= r.right(): # start new line - line_top += line_height - line_width = r.x() - line_height = 0 + x = r.x() + y += lineHeight + spaceY + lineHeight = 0 if appl: - item.setGeometry(QRect(QPoint(line_width, line_top), sz_hint)) + item.setGeometry(QRect(QPoint(x, y), sz_hint)) - line_width += sz_hint.width() - line_height = max(line_height, sz_hint.height()) + x += sz_hint.width() + spaceX + lineHeight = max(lineHeight, sz_hint.height()) - return line_top + line_height + return y + lineHeight - r.y()