Skip to content

Commit

Permalink
Change filter to be applied as AND
Browse files Browse the repository at this point in the history
  • Loading branch information
jfeil committed Mar 21, 2022
1 parent 27fad94 commit f79f857
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/question_table.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union, Any, List, Callable, Tuple
from typing import Union, Any, List

import PySide6
from PySide6.QtCore import Qt, QPoint, QAbstractTableModel, QSortFilterProxyModel
Expand Down Expand Up @@ -271,23 +271,15 @@ def startDrag(self, supportedActions: Qt.DropActions) -> None:


class RuleSortFilterProxyModel(QSortFilterProxyModel):
filters = [] # List[Tuple[dict_key, Callable]]
filters = [] # List[Tuple[Tuple[dict_key, Callable], Tuple[str, FilterOption, Any]]]

def filterAcceptsRow(self, source_row: int, source_parent: Union[
PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex]) -> bool:
if not RuleSortFilterProxyModel.filters:
return True

cur_question = self.sourceModel().data(source_row, Qt.UserRole)
for target, filter_function in RuleSortFilterProxyModel.filters:
if filter_function(cur_question.__dict__[target]):
return True
return False

@staticmethod
def add_filter(filter_param: Tuple[dict_key, Callable]):
RuleSortFilterProxyModel.filters += [filter_param]

@staticmethod
def remove_filter(index: int):
RuleSortFilterProxyModel.filters.pop(index)
answer = True
for ((target, filter_function), _) in RuleSortFilterProxyModel.filters:
answer = answer & filter_function(cur_question.__dict__[target])
return answer

0 comments on commit f79f857

Please sign in to comment.