1
0
Fork 0
mirror of synced 2024-06-02 18:54:41 +12:00

FlowLayout: Fix overlapping widgets and remove default arguments

This commit is contained in:
loathingKernel 2023-12-14 00:49:22 +02:00
parent 23716e40d8
commit 5abae7ee16
No known key found for this signature in database
GPG key ID: CE0C72D0B53821FD
3 changed files with 57 additions and 34 deletions

View file

@ -69,6 +69,7 @@ class GamesTab(QStackedWidget):
self.icon_view = QWidget(self.icon_view_scroll) self.icon_view = QWidget(self.icon_view_scroll)
icon_view_layout = LibraryLayout(self.icon_view) icon_view_layout = LibraryLayout(self.icon_view)
icon_view_layout.setSpacing(9)
icon_view_layout.setContentsMargins(0, 13, 0, 13) icon_view_layout.setContentsMargins(0, 13, 0, 13)
icon_view_layout.setAlignment(Qt.AlignTop) icon_view_layout.setAlignment(Qt.AlignTop)

View file

@ -1,33 +1,55 @@
from typing import Optional from typing import Optional, List, overload
from PyQt5.QtCore import ( from PyQt5.QtCore import Qt, QRect, QSize, QPoint
Qt, from PyQt5.QtWidgets import QLayout, QStyle, QSizePolicy, QLayoutItem, QWidget
QRect,
QSize,
QPoint,
)
from PyQt5.QtWidgets import (
QLayout,
QStyle,
QSizePolicy,
QLayoutItem,
)
class FlowLayout(QLayout): class FlowLayout(QLayout):
def __init__(self, parent=None, margin=-1, hspacing=-1, vspacing=-1): def __init__(self, parent=None):
super(FlowLayout, self).__init__(parent) super(FlowLayout, self).__init__(parent)
self._hspacing = hspacing
self._vspacing = vspacing
self._items = []
self.setContentsMargins(margin, margin, margin, margin)
self.setObjectName(type(self).__name__) self.setObjectName(type(self).__name__)
self._hspacing = -1
self._vspacing = -1
self._items: List[QLayoutItem] = []
def __del__(self): def __del__(self):
del self._items[:] del self._items[:]
@overload
def indexOf(self, a0: QWidget) -> int:
try:
return next(idx for idx, item in enumerate(self._items) if item.widget() is a0)
except:
return -1
def indexOf(self, a0: QLayoutItem) -> int:
try:
return self._items.index(a0)
except:
return -1
def addItem(self, a0: QLayoutItem) -> None: def addItem(self, a0: QLayoutItem) -> None:
self._items.append(a0) self._items.append(a0)
self.invalidate()
def removeItem(self, a0: QLayoutItem) -> None:
self._items.remove(a0)
self.invalidate()
def spacing(self) -> int:
hspacing = self.horizontalSpacing()
if hspacing == self.verticalSpacing():
return hspacing
else:
return -1
def setSpacing(self, a0: int) -> None:
self._hspacing = self._vspacing = a0
self.invalidate()
def setHorizontalSpacing(self, a0: int) -> None:
self._hspacing = a0
self.invalidate()
def horizontalSpacing(self): def horizontalSpacing(self):
if self._hspacing >= 0: if self._hspacing >= 0:
@ -35,6 +57,10 @@ class FlowLayout(QLayout):
else: else:
return self.smartSpacing(QStyle.PM_LayoutHorizontalSpacing) return self.smartSpacing(QStyle.PM_LayoutHorizontalSpacing)
def setVerticalSpacing(self, a0: int) -> None:
self._vspacing = a0
self.invalidate()
def verticalSpacing(self): def verticalSpacing(self):
if self._vspacing >= 0: if self._vspacing >= 0:
return self._vspacing return self._vspacing
@ -51,11 +77,14 @@ class FlowLayout(QLayout):
def takeAt(self, index: int) -> Optional[QLayoutItem]: def takeAt(self, index: int) -> Optional[QLayoutItem]:
if 0 <= index < len(self._items): if 0 <= index < len(self._items):
return self._items.pop(index) item = self._items.pop(index)
self.invalidate()
return item
return None return None
def expandingDirections(self) -> Qt.Orientations: def expandingDirections(self) -> Qt.Orientations:
return Qt.Horizontal | Qt.Vertical return Qt.Orientations(Qt.Orientation(0))
# return Qt.Horizontal | Qt.Vertical
def hasHeightForWidth(self) -> bool: def hasHeightForWidth(self) -> bool:
return True return True
@ -93,8 +122,6 @@ class FlowLayout(QLayout):
if item.isEmpty(): if item.isEmpty():
continue continue
widget = item.widget() widget = item.widget()
if not widget.isVisible():
continue
hspace = self.horizontalSpacing() hspace = self.horizontalSpacing()
if hspace == -1: if hspace == -1:
hspace = widget.style().layoutSpacing( hspace = widget.style().layoutSpacing(

View file

@ -1,23 +1,18 @@
from typing import Callable from typing import Callable
from PyQt5.QtCore import ( from PyQt5.QtCore import Qt, QRect, QPoint
Qt, from PyQt5.QtWidgets import QSizePolicy
QRect,
QPoint,
)
from PyQt5.QtWidgets import (
QSizePolicy,
)
from .flow_layout import FlowLayout from .flow_layout import FlowLayout
class LibraryLayout(FlowLayout): class LibraryLayout(FlowLayout):
def __init__(self, parent=None, margin=6, spacing=11): def __init__(self, parent=None):
super(LibraryLayout, self).__init__(parent=parent, margin=margin, hspacing=spacing, vspacing=spacing) super(LibraryLayout, self).__init__(parent)
def expandingDirections(self) -> Qt.Orientations: def expandingDirections(self) -> Qt.Orientations:
return Qt.Horizontal | Qt.Vertical return Qt.Orientations(Qt.Orientation(0))
# return Qt.Horizontal | Qt.Vertical
def setGeometry(self, a0: QRect) -> None: def setGeometry(self, a0: QRect) -> None:
super(FlowLayout, self).setGeometry(a0) super(FlowLayout, self).setGeometry(a0)
@ -127,4 +122,4 @@ class LibraryLayout(FlowLayout):
def sort(self, key: Callable, reverse=False) -> None: def sort(self, key: Callable, reverse=False) -> None:
self._items.sort(key=key, reverse=reverse) self._items.sort(key=key, reverse=reverse)
self.setGeometry(self.parent().contentsRect()) self.setGeometry(self.parent().contentsRect().adjusted(*self.parent().getContentsMargins()))