Skip to content

Commit 031f18b

Browse files
committed
feat(lazy-images): add file counts to numberPopulated signal
1 parent c0080d3 commit 031f18b

2 files changed

Lines changed: 36 additions & 20 deletions

File tree

‎sqlite3__examples/view_many_images__lazy__delegates__async_load_images/utils/FileListModel.py‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# SOURCE: https://github.com/gil9red/SimplePyScripts/blob/f49a0c3462176ccc34bf31dffbe6fd88d1baa0bd/qt__pyqt__pyside__pyqode/lazy__qtwidgets_itemviews_fetchmore_example__QAbstractListModel.py#L19
1111
class FileListModel(QAbstractListModel):
12-
numberPopulated = pyqtSignal(int)
12+
numberPopulated = pyqtSignal(int, int, int)
1313

1414
def __init__(self, batch_size=50, parent=None):
1515
super().__init__(parent)
@@ -21,11 +21,15 @@ def __init__(self, batch_size=50, parent=None):
2121
def rowCount(self, parent: QModelIndex = None) -> int:
2222
return self.current_file_count
2323

24+
@property
25+
def total_file_count(self) -> int:
26+
return len(self.total_file_list)
27+
2428
def data(self, index: QModelIndex, role=Qt.DisplayRole) -> QVariant:
2529
if not index.isValid():
2630
return QVariant()
2731

28-
if index.row() >= len(self.total_file_list) or index.row() < 0:
32+
if index.row() >= self.total_file_count or index.row() < 0:
2933
return QVariant()
3034

3135
if role == Qt.DisplayRole or role == Qt.ToolTipRole:
@@ -41,29 +45,33 @@ def data(self, index: QModelIndex, role=Qt.DisplayRole) -> QVariant:
4145
return QVariant()
4246

4347
def canFetchMore(self, parent: QModelIndex = None) -> bool:
44-
return self.current_file_count < len(self.total_file_list)
48+
return self.current_file_count < self.total_file_count
4549

4650
def fetchMore(self, parent: QModelIndex = None):
47-
remainder = len(self.total_file_list) - self.current_file_count
48-
itemsToFetch = min(self.batch_size, remainder)
49-
if itemsToFetch <= 0:
51+
remainder = self.total_file_count - self.current_file_count
52+
items_to_fetch = min(self.batch_size, remainder)
53+
if items_to_fetch <= 0:
5054
return
5155

5256
self.beginInsertRows(
53-
QModelIndex(), self.current_file_count, self.current_file_count + itemsToFetch - 1
57+
QModelIndex(),
58+
self.current_file_count,
59+
self.current_file_count + items_to_fetch - 1,
5460
)
5561

56-
self.current_file_count += itemsToFetch
62+
self.current_file_count += items_to_fetch
5763

5864
self.endInsertRows()
5965

60-
self.numberPopulated.emit(itemsToFetch)
66+
self.numberPopulated.emit(
67+
items_to_fetch, self.current_file_count, self.total_file_count
68+
)
6169

6270
def set_total_file_list(self, fileList: list):
6371
self.beginResetModel()
6472

6573
self.total_file_list = fileList
6674
self.current_file_count = 0
67-
self.numberPopulated.emit(0)
75+
self.numberPopulated.emit(0, 0, 0)
6876

6977
self.endResetModel()

‎sqlite3__examples/view_many_images__lazy__delegates__async_load_images/utils_PyQt6/FileListModel.py‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
class FileListModel(QAbstractListModel):
18-
numberPopulated = pyqtSignal(int)
18+
numberPopulated = pyqtSignal(int, int, int)
1919

2020
IsMainRole: int = Qt.ItemDataRole.UserRole
2121
IsMatchedRole: int = Qt.ItemDataRole.UserRole + 1
@@ -35,6 +35,10 @@ def __init__(self, batch_size: int = 50, parent: QObject | None = None) -> None:
3535
def rowCount(self, parent: QModelIndex | None = None) -> int:
3636
return self.current_file_count
3737

38+
@property
39+
def total_file_count(self) -> int:
40+
return len(self.total_file_list)
41+
3842
def data(
3943
self,
4044
index: QModelIndex,
@@ -43,7 +47,7 @@ def data(
4347
if not index.isValid():
4448
return QVariant()
4549

46-
if index.row() >= len(self.total_file_list) or index.row() < 0:
50+
if index.row() >= self.total_file_count or index.row() < 0:
4751
return QVariant()
4852

4953
file_name = self.total_file_list[index.row()]
@@ -71,30 +75,34 @@ def data(
7175
return QVariant()
7276

7377
def canFetchMore(self, parent: QModelIndex | None = None) -> bool:
74-
return self.current_file_count < len(self.total_file_list)
78+
return self.current_file_count < self.total_file_count
7579

7680
def fetchMore(self, parent: QModelIndex | None = None) -> None:
77-
remainder = len(self.total_file_list) - self.current_file_count
78-
itemsToFetch = min(self.batch_size, remainder)
79-
if itemsToFetch <= 0:
81+
remainder: int = self.total_file_count - self.current_file_count
82+
items_to_fetch: int = min(self.batch_size, remainder)
83+
if items_to_fetch <= 0:
8084
return
8185

8286
self.beginInsertRows(
83-
QModelIndex(), self.current_file_count, self.current_file_count + itemsToFetch - 1
87+
QModelIndex(),
88+
self.current_file_count,
89+
self.current_file_count + items_to_fetch - 1,
8490
)
8591

86-
self.current_file_count += itemsToFetch
92+
self.current_file_count += items_to_fetch
8793

8894
self.endInsertRows()
8995

90-
self.numberPopulated.emit(itemsToFetch)
96+
self.numberPopulated.emit(
97+
items_to_fetch, self.current_file_count, self.total_file_count
98+
)
9199

92100
def set_total_file_list(self, file_list: list[str]) -> None:
93101
self.beginResetModel()
94102

95103
self.total_file_list = file_list
96104
self.current_file_count = 0
97-
self.numberPopulated.emit(0)
105+
self.numberPopulated.emit(0, 0, 0)
98106

99107
self.endResetModel()
100108

0 commit comments

Comments
 (0)