Skip to content

Commit

Permalink
Add possibility to create filter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jfeil committed Mar 7, 2022
1 parent fc439f0 commit 83512d9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Question(Base):
last_edited = Column(Date, default=date.today)
signature = Column(String, default=(lambda: uuid.uuid4().hex), primary_key=True)

table_headers = {
dict_to_header = {
'group_id': "Regelgruppe",
'rule_id': "Regelnummer",
'question': "Frage",
Expand All @@ -114,6 +114,8 @@ class Question(Base):
'signature': "Signatur"
}

header_to_dict = {y: x for x, y in dict_to_header.items()}

# noinspection PyTypeChecker
def table_checkbox(self, dict_key):
return defaultdict(lambda: None, {
Expand Down
9 changes: 5 additions & 4 deletions src/main_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ def create_ruletabs(self, rulegroups: List[Rulegroup]):
self.ui.tabWidget.addTab(tab, "")
self.ui.tabWidget.setTabText(self.ui.tabWidget.indexOf(tab), f"{rulegroup.id:02d} {rulegroup.name}")
# self.filter_column(3, 'FaD')
# self.filter_column(3, 'VW')

def setup_regeltest(self):
regeltest_setup = RegeltestSetup(self)
Expand All @@ -181,10 +180,12 @@ def setup_regeltest(self):
self.ui.regeltest_list.add_question(question)

def filter_column(self, column, keyword, mode=FilterMode.Include):
# RuleSortFilterProxyModel.add_filter(('answer_text', lambda x: 'FaD' in x))
# RuleSortFilterProxyModel.add_filter(('last_edited', lambda x: datetime.date.fromisoformat('2020-06-01') < x))

for (filter_model, _) in self.ruletabs.values():
filter_model = filter_model # type: QSortFilterProxyModel
filter_model.setFilterFixedString(keyword)
filter_model.setFilterKeyColumn(column)
filter_model = filter_model # type: RuleSortFilterProxyModel
filter_model.invalidateFilter()

def create_regeltest(self):
question_set = []
Expand Down
34 changes: 28 additions & 6 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
from typing import Union, Any, List, Callable, Tuple

import PySide6
from PySide6.QtCore import Qt, QPoint, QAbstractTableModel, QSortFilterProxyModel
Expand All @@ -10,6 +10,8 @@
from src.datatypes import Question
from src.question_editor import QuestionEditor

dict_key = str


class RuleDataModel(QAbstractTableModel):
# When subclassing QAbstractTableModel, you must implement rowCount(), columnCount(), and data(). Default
Expand Down Expand Up @@ -64,10 +66,15 @@ def removeColumns(self, column: int, count: int,
self.endRemoveColumns()
return True

def data(self, index: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex],
def data(self, index: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex, int],
role: int = ...) -> Any:
col = index.column()
row = index.row()
if type(index) == int:
row = index
col = 0
else:
col = index.column()
row = index.row()

if role == Qt.UserRole:
return self.questions[row]

Expand Down Expand Up @@ -96,7 +103,7 @@ def headerData(self, section: int, orientation: PySide6.QtCore.Qt.Orientation, r
if orientation == Qt.Vertical:
return None
if role == Qt.DisplayRole:
return Question.table_headers[self.headers[section]]
return Question.dict_to_header[self.headers[section]]

def insertRows(self, row: int, count: int,
parent: Union[PySide6.QtCore.QModelIndex, PySide6.QtCore.QPersistentModelIndex] = ...) -> bool:
Expand Down Expand Up @@ -262,4 +269,19 @@ def startDrag(self, supportedActions: Qt.DropActions) -> None:


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

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]

0 comments on commit 83512d9

Please sign in to comment.