Buckets:

download
raw
6.47 kB
############################################################################
##
## Copyright (C) 2013 Riverbank Computing Limited.
## Copyright (C) 2020 The Qt Company Ltd.
## Contact: https://www.qt.io/licensing/
##
## This file is part of the Qt for Python examples of the Qt Toolkit.
##
## $QT_BEGIN_LICENSE:BSD$
## You may use this file under the terms of the BSD license as follows:
##
## "Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are
## met:
## * Redistributions of source code must retain the above copyright
## notice, this list of conditions and the following disclaimer.
## * Redistributions in binary form must reproduce the above copyright
## notice, this list of conditions and the following disclaimer in
## the documentation and/or other materials provided with the
## distribution.
## * Neither the name of The Qt Company Ltd nor the names of its
## contributors may be used to endorse or promote products derived
## from this software without specific prior written permission.
##
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
##
## $QT_END_LICENSE$
##
#############################################################################
"""PySide6 port of the widgets/layouts/flowlayout example from Qt v6.x"""
from __future__ import annotations
from typing import TYPE_CHECKING
from PySide6.QtCore import QMargins, QPoint, QRect, QSize, Qt
from PySide6.QtWidgets import QLayout, QLayoutItem, QSizePolicy, QWidgetItem
if TYPE_CHECKING:
from PySide6 import QtWidgets
class FlowLayout(QLayout):
"""
A custom layout that adjusts the position of widgets within depending on the width of the application window.
"""
def __init__(self, parent: QtWidgets.QWidget | None = None, center: bool = False) -> None:
super().__init__(parent)
if parent is not None:
self.setContentsMargins(QMargins(0, 0, 0, 0))
self._item_list: list[QWidgetItem] = []
self.center = center
def __del__(self) -> None:
item = self.takeAt(0)
while item:
item = self.takeAt(0)
def addItem(self, item: QLayoutItem) -> None:
assert isinstance(item, QWidgetItem)
self._item_list.append(item)
def count(self) -> int:
return len(self._item_list)
def itemAt(self, index: int) -> QWidgetItem | None:
if 0 <= index < len(self._item_list):
return self._item_list[index]
return None
def takeAt(self, index: int) -> QWidgetItem | None:
if 0 <= index < len(self._item_list):
return self._item_list.pop(index)
return None
def expandingDirections(self) -> Qt.Orientation:
return Qt.Orientation(0)
def hasHeightForWidth(self) -> bool:
return True
def heightForWidth(self, width: int) -> int:
height = self._do_layout(QRect(0, 0, width, 0), True)
return height
def setGeometry(self, rect: QRect) -> None:
super().setGeometry(rect)
self._do_layout(rect, False)
def sizeHint(self) -> QSize:
return self.minimumSize()
def minimumSize(self) -> QSize:
size = QSize()
for item in self._item_list:
size = size.expandedTo(item.minimumSize())
size += QSize(2 * self.contentsMargins().top(), 2 * self.contentsMargins().top())
return size
def _do_layout(self, rect: QRect, test_only: bool) -> int:
x = rect.x()
y = rect.y()
line_height = 0
spacing = self.spacing()
rows: list[tuple[list[QWidgetItem], int, int]] = []
row: list[QWidgetItem] = []
for item in self._item_list:
widget = item.widget()
assert widget is not None
style = widget.style()
layout_spacing_x = style.layoutSpacing(
QSizePolicy.ControlType.PushButton, QSizePolicy.ControlType.PushButton, Qt.Orientation.Horizontal
)
layout_spacing_y = style.layoutSpacing(
QSizePolicy.ControlType.PushButton, QSizePolicy.ControlType.PushButton, Qt.Orientation.Vertical
)
space_x = (
spacing + layout_spacing_x
) * widget.isVisible() # If an item isn't visible, it does not have any influence on the next
space_y = spacing + layout_spacing_y
next_x = x + item.sizeHint().width() + space_x
if next_x - space_x > rect.right() and line_height > 0:
rows.append(
(row, rect.right() - x - space_x, line_height)
) # We need to remove the unnecessary extra padding from all rows, not just the last
row = []
x = rect.x()
y = y + line_height + space_y
next_x = x + item.sizeHint().width() + space_x
line_height = 0
if not test_only:
item.setGeometry(QRect(QPoint(x, y), item.sizeHint()))
x = next_x
line_height = max(line_height, item.sizeHint().height())
row.append(item)
rows.append((row, rect.right() - x - space_x, line_height))
if not test_only and self.center:
for items, x_margin, y_size in rows:
x_margin //= 2
for item in items:
r = item.geometry()
new_y = r.y()
if r.height() < y_size:
new_y += (y_size - r.height()) // 2
item.setGeometry(QRect(QPoint(r.x() + x_margin, new_y), item.sizeHint()))
return y + line_height - rect.y()

Xet Storage Details

Size:
6.47 kB
·
Xet hash:
7bccf47299ab397d1c5cceebaf38a0f568ae7e6bc07c2ecc9d04316408d94cd8

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.