Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from __future__ import annotations | ||
|
||
from collections.abc import Container | ||
|
||
from prettyqt import core | ||
from prettyqt.utils import get_repr | ||
|
||
|
||
def is_in_slice(a_slice: slice | range, idx: int) -> bool: | ||
start = a_slice.start or 0 | ||
if idx < start or (a_slice.stop is not None and idx >= a_slice.stop): | ||
return False | ||
step = a_slice.step or 1 | ||
return (idx - start) % step == 0 | ||
|
||
|
||
class SubsetFilterProxyModel(core.SortFilterProxyModel): | ||
def __init__( | ||
self, | ||
row_filter: slice | range | int | Container[int] | None, | ||
column_filter: slice | range | int | Container[int] | None, | ||
**kwargs, | ||
): | ||
self.row_filter = row_filter | ||
self.column_filter = column_filter | ||
super().__init__(**kwargs) | ||
|
||
def __repr__(self): | ||
return get_repr(self, self.row_filter, self.column_filter) | ||
|
||
def filterAcceptsColumn(self, source_column: int, parent: core.ModelIndex) -> bool: | ||
match self.column_filter: | ||
case slice() | range(): | ||
return is_in_slice(self.column_filter, source_column) | ||
case int(): | ||
return source_column == self.column_filter | ||
case Container(): | ||
return source_column in self.column_filter | ||
case None: | ||
return True | ||
case _: | ||
raise ValueError(self.column_filter) | ||
|
||
def filterAcceptsRow(self, source_row: int, parent: core.ModelIndex) -> bool: | ||
match self.row_filter: | ||
case slice() | range(): | ||
return is_in_slice(self.row_filter, source_row) | ||
case int(): | ||
return source_row == self.row_filter | ||
case Container(): | ||
return source_row in self.row_filter | ||
case None: | ||
return True | ||
case _: | ||
raise ValueError(self.row_filter) | ||
|
||
|
||
if __name__ == "__main__": | ||
from prettyqt import widgets | ||
|
||
app = widgets.app() | ||
proxy = SubsetFilterProxyModel(1, 2) |