def _do_layout(self, origin): left, top, right, bottom = self.getContentsMargins() width_hint = left + right height_hint = top + bottom if self.item_list: item_size = self.item_list[0].sizeHint() item_h = item_size.height() item_w = item_size.width() multi_row = self.viewport_size.height() - height_hint > item_h if multi_row: columns = max( (self.viewport_size.width() - width_hint) // item_w, 1) rows = (len(self.item_list) + columns - 1) // columns else: columns = len(self.item_list) rows = 1 width_hint += columns * item_w height_hint += rows * item_h self.size_hint = QtCore.QSize(width_hint, height_hint) if not self.item_list: return x = origin.x() + left y = origin.y() + top for n, item in enumerate(self.item_list): i, j = n % columns, n // columns item.setGeometry( QtCore.QRect(QtCore.QPoint(x + (i * item_w), y + (j * item_h)), item_size)) self.scroll_area.set_multi_row(multi_row)
def _do_layout(self, rect, test_only): left, top, right, bottom = self.getContentsMargins() effective_rect = rect.adjusted(left, top, -right, -bottom) x = effective_rect.x() y = effective_rect.y() row_height = 0 for item in self.item_list: item_size = item.sizeHint() if x + item_size.width() > effective_rect.right() and row_height > 0: x = effective_rect.x() y += row_height row_height = 0 if not test_only: item.setGeometry(QtCore.QRect(QtCore.QPoint(x, y), item_size)) x += item_size.width() row_height = max(row_height, item_size.height()) return y + row_height - rect.y() + bottom
def heightForWidth(self, width): return self._do_layout(QtCore.QRect(0, 0, width, 0), True)