Skip to content

Commit 7f7d861

Browse files
refactor: fix type hints and overrides in flowlayout.py (#880)
* refactor and fixes * type annotations and parameter name changes * Update src/tagstudio/qt/flowlayout.py Co-authored-by: Jann Stute <46534683+Computerdores@users.noreply.github.com> * Update src/tagstudio/qt/flowlayout.py Co-authored-by: Jann Stute <46534683+Computerdores@users.noreply.github.com> * ruff format --------- Co-authored-by: Jann Stute <46534683+Computerdores@users.noreply.github.com>
1 parent cccd858 commit 7f7d861

File tree

1 file changed

+42
-28
lines changed

1 file changed

+42
-28
lines changed

src/tagstudio/qt/flowlayout.py

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,71 +5,85 @@
55

66
"""PySide6 port of the widgets/layouts/flowlayout example from Qt v6.x."""
77

8+
from typing import Literal, override
9+
810
from PySide6.QtCore import QMargins, QPoint, QRect, QSize, Qt
9-
from PySide6.QtWidgets import QLayout, QSizePolicy, QWidget
11+
from PySide6.QtWidgets import QLayout, QLayoutItem, QSizePolicy, QWidget
12+
13+
IGNORE_SIZE = "ignore_size"
1014

1115

1216
class FlowWidget(QWidget):
13-
def __init__(self, parent=None) -> None:
17+
def __init__(self, parent: QWidget | None = None) -> None:
1418
super().__init__(parent)
15-
self.ignore_size: bool = False
19+
self.setProperty(IGNORE_SIZE, False) # noqa: FBT003
1620

1721

1822
class FlowLayout(QLayout):
19-
def __init__(self, parent=None):
23+
def __init__(self, parent: QWidget | None = None) -> None:
2024
super().__init__(parent)
2125

2226
if parent is not None:
2327
self.setContentsMargins(QMargins(0, 0, 0, 0))
2428

25-
self._item_list = []
29+
self._item_list: list[QLayoutItem] = []
2630
self.grid_efficiency = False
2731

2832
def __del__(self):
2933
item = self.takeAt(0)
3034
while item:
3135
item = self.takeAt(0)
3236

33-
def addItem(self, item): # noqa: N802
34-
self._item_list.append(item)
37+
@override
38+
def addItem(self, arg__1: QLayoutItem) -> None:
39+
self._item_list.append(arg__1)
3540

36-
def count(self):
41+
@override
42+
def count(self) -> int:
3743
return len(self._item_list)
3844

39-
def itemAt(self, index): # noqa: N802
45+
@override
46+
def itemAt(self, index: int) -> QLayoutItem | None: # pyright: ignore[reportIncompatibleMethodOverride]
4047
if 0 <= index < len(self._item_list):
4148
return self._item_list[index]
4249

4350
return None
4451

45-
def takeAt(self, index): # noqa: N802
52+
@override
53+
def takeAt(self, index: int) -> QLayoutItem | None: # pyright: ignore[reportIncompatibleMethodOverride]
4654
if 0 <= index < len(self._item_list):
4755
return self._item_list.pop(index)
4856

4957
return None
5058

51-
def expandingDirections(self): # noqa: N802
52-
return Qt.Orientation(0)
59+
@override
60+
def expandingDirections(self) -> Qt.Orientation:
61+
return Qt.Orientation.Horizontal
5362

54-
def hasHeightForWidth(self): # noqa: N802
63+
@override
64+
def hasHeightForWidth(self) -> Literal[True]:
5565
return True
5666

57-
def heightForWidth(self, width): # noqa: N802
58-
height = self._do_layout(QRect(0, 0, width, 0), test_only=True)
59-
return height
67+
@override
68+
def heightForWidth(self, arg__1: int) -> int:
69+
height = self._do_layout(QRect(0, 0, arg__1, 0), test_only=True)
70+
return int(height)
6071

61-
def setGeometry(self, rect): # noqa: N802
62-
super().setGeometry(rect)
63-
self._do_layout(rect, test_only=False)
72+
@override
73+
def setGeometry(self, arg__1: QRect) -> None:
74+
super().setGeometry(arg__1)
75+
self._do_layout(arg__1, test_only=False)
6476

65-
def enable_grid_optimizations(self, value: bool):
77+
def enable_grid_optimizations(self, value: bool) -> None:
6678
"""Enable or Disable efficiencies when all objects are equally sized."""
6779
self.grid_efficiency = value
6880

69-
def sizeHint(self): # noqa: N802
81+
@override
82+
def sizeHint(self) -> QSize:
7083
return self.minimumSize()
7184

72-
def minimumSize(self): # noqa: N802
85+
@override
86+
def minimumSize(self) -> QSize:
7387
if self.grid_efficiency:
7488
if self._item_list:
7589
return self._item_list[0].minimumSize()
@@ -89,8 +103,8 @@ def _do_layout(self, rect: QRect, test_only: bool) -> float:
89103
y = rect.y()
90104
line_height = 0
91105
spacing = self.spacing()
92-
layout_spacing_x = None
93-
layout_spacing_y = None
106+
layout_spacing_x = 0
107+
layout_spacing_y = 0
94108

95109
if self.grid_efficiency and self._item_list:
96110
item = self._item_list[0]
@@ -108,12 +122,12 @@ def _do_layout(self, rect: QRect, test_only: bool) -> float:
108122

109123
for item in self._item_list:
110124
skip_count = 0
111-
if issubclass(type(item.widget()), FlowWidget) and item.widget().ignore_size:
125+
ignore_size: bool | None = item.widget().property(IGNORE_SIZE)
126+
127+
if ignore_size:
112128
skip_count += 1
113129

114-
if (issubclass(type(item.widget()), FlowWidget) and not item.widget().ignore_size) or (
115-
not issubclass(type(item.widget()), FlowWidget)
116-
):
130+
else:
117131
if not self.grid_efficiency:
118132
style = item.widget().style()
119133
layout_spacing_x = style.layoutSpacing(

0 commit comments

Comments
 (0)