FlowLayout: Fix overlapping widgets and remove default arguments
This commit is contained in:
parent
23716e40d8
commit
5abae7ee16
|
@ -69,6 +69,7 @@ class GamesTab(QStackedWidget):
|
|||
|
||||
self.icon_view = QWidget(self.icon_view_scroll)
|
||||
icon_view_layout = LibraryLayout(self.icon_view)
|
||||
icon_view_layout.setSpacing(9)
|
||||
icon_view_layout.setContentsMargins(0, 13, 0, 13)
|
||||
icon_view_layout.setAlignment(Qt.AlignTop)
|
||||
|
||||
|
|
|
@ -1,33 +1,55 @@
|
|||
from typing import Optional
|
||||
from typing import Optional, List, overload
|
||||
|
||||
from PyQt5.QtCore import (
|
||||
Qt,
|
||||
QRect,
|
||||
QSize,
|
||||
QPoint,
|
||||
)
|
||||
from PyQt5.QtWidgets import (
|
||||
QLayout,
|
||||
QStyle,
|
||||
QSizePolicy,
|
||||
QLayoutItem,
|
||||
)
|
||||
from PyQt5.QtCore import Qt, QRect, QSize, QPoint
|
||||
from PyQt5.QtWidgets import QLayout, QStyle, QSizePolicy, QLayoutItem, QWidget
|
||||
|
||||
|
||||
class FlowLayout(QLayout):
|
||||
def __init__(self, parent=None, margin=-1, hspacing=-1, vspacing=-1):
|
||||
def __init__(self, parent=None):
|
||||
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._hspacing = -1
|
||||
self._vspacing = -1
|
||||
self._items: List[QLayoutItem] = []
|
||||
|
||||
def __del__(self):
|
||||
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:
|
||||
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):
|
||||
if self._hspacing >= 0:
|
||||
|
@ -35,6 +57,10 @@ class FlowLayout(QLayout):
|
|||
else:
|
||||
return self.smartSpacing(QStyle.PM_LayoutHorizontalSpacing)
|
||||
|
||||
def setVerticalSpacing(self, a0: int) -> None:
|
||||
self._vspacing = a0
|
||||
self.invalidate()
|
||||
|
||||
def verticalSpacing(self):
|
||||
if self._vspacing >= 0:
|
||||
return self._vspacing
|
||||
|
@ -51,11 +77,14 @@ class FlowLayout(QLayout):
|
|||
|
||||
def takeAt(self, index: int) -> Optional[QLayoutItem]:
|
||||
if 0 <= index < len(self._items):
|
||||
return self._items.pop(index)
|
||||
item = self._items.pop(index)
|
||||
self.invalidate()
|
||||
return item
|
||||
return None
|
||||
|
||||
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:
|
||||
return True
|
||||
|
@ -93,8 +122,6 @@ class FlowLayout(QLayout):
|
|||
if item.isEmpty():
|
||||
continue
|
||||
widget = item.widget()
|
||||
if not widget.isVisible():
|
||||
continue
|
||||
hspace = self.horizontalSpacing()
|
||||
if hspace == -1:
|
||||
hspace = widget.style().layoutSpacing(
|
||||
|
|
|
@ -1,23 +1,18 @@
|
|||
from typing import Callable
|
||||
|
||||
from PyQt5.QtCore import (
|
||||
Qt,
|
||||
QRect,
|
||||
QPoint,
|
||||
)
|
||||
from PyQt5.QtWidgets import (
|
||||
QSizePolicy,
|
||||
)
|
||||
from PyQt5.QtCore import Qt, QRect, QPoint
|
||||
from PyQt5.QtWidgets import QSizePolicy
|
||||
|
||||
from .flow_layout import FlowLayout
|
||||
|
||||
|
||||
class LibraryLayout(FlowLayout):
|
||||
def __init__(self, parent=None, margin=6, spacing=11):
|
||||
super(LibraryLayout, self).__init__(parent=parent, margin=margin, hspacing=spacing, vspacing=spacing)
|
||||
def __init__(self, parent=None):
|
||||
super(LibraryLayout, self).__init__(parent)
|
||||
|
||||
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:
|
||||
super(FlowLayout, self).setGeometry(a0)
|
||||
|
@ -127,4 +122,4 @@ class LibraryLayout(FlowLayout):
|
|||
|
||||
def sort(self, key: Callable, reverse=False) -> None:
|
||||
self._items.sort(key=key, reverse=reverse)
|
||||
self.setGeometry(self.parent().contentsRect())
|
||||
self.setGeometry(self.parent().contentsRect().adjusted(*self.parent().getContentsMargins()))
|
||||
|
|
Loading…
Reference in a new issue