def test_set_list(self): model = FilterCheckboxListModel() model.set_list(self.data) self.assertEqual(model._data, sorted(self.data)) self.assertEqual(model._data_set, set(self.data)) self.assertEqual(model._selected, set(self.data)) self.assertTrue(model._all_selected)
def test_remove_filter(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') model.remove_filter() self.assertFalse(model._is_filtered) self.assertEqual(model._selected, set(self.data)) self.assertEqual(model.rowCount(), 8)
def test_click_select_all_when_all_selected(self): model = FilterCheckboxListModel() model.set_list(self.data) with mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: index = model.index(0, 0) model.click_index(index) self.assertFalse(model._all_selected) self.assertEqual(model._selected, set())
def test_click_selected_item(self): model = FilterCheckboxListModel() model.set_list(self.data) with mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: index = model.index(2, 0) model.click_index(index) self.assertEqual(model._selected, set(self.data).difference(set(['a']))) self.assertFalse(model._all_selected)
def test_remove_items_selected(self): items = set('a') model = FilterCheckboxListModel() model.set_list(self.data) with mock.patch("tabularview_models.FilterCheckboxListModel.beginResetModel") as br, \ mock.patch("tabularview_models.FilterCheckboxListModel.endResetModel") as er: model.remove_items(items) self.assertEqual(model._selected, set(self.data[1:])) self.assertTrue(model._all_selected)
def test_apply_filter_with_replace(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') model.apply_filter() self.assertFalse(model._is_filtered) self.assertEqual(model._selected, set(self.data[3:])) self.assertEqual(model.rowCount(), 8) self.assertFalse(model._empty_selected)
def test_remove_items_filtered_data_midle(self): items = set('bb') model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') with mock.patch("tabularview_models.FilterCheckboxListModel.beginResetModel") as br, \ mock.patch("tabularview_models.FilterCheckboxListModel.endResetModel") as er: model.remove_items(items) self.assertEqual(model._filter_index, [3,4])
def test_add_item_without_select_without_filter(self): new_item = ['aaaa'] model = FilterCheckboxListModel() model.set_list(self.data) with mock.patch("tabularview_models.FilterCheckboxListModel.beginInsertRows") as bir, \ mock.patch("tabularview_models.FilterCheckboxListModel.endInsertRows") as eir, \ mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: model.add_item(new_item, selected=False) self.assertFalse(model._all_selected)
def test_click_select_empty_when_unselected(self): model = FilterCheckboxListModel() model.set_list(self.data) model._empty_selected = False with mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: index = model.index(1, 0) model.click_index(index) self.assertTrue(model._empty_selected) self.assertTrue(model._all_selected)
def test_click_unselected_item(self): model = FilterCheckboxListModel() model.set_list(self.data) model._selected.discard('a') with mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: index = model.index(2, 0) model.click_index(index) self.assertEqual(model._selected, set(self.data)) self.assertTrue(model._all_selected)
def test_add_item_with_select_without_filter(self): new_item = ['aaaa'] model = FilterCheckboxListModel() model.set_list(self.data) with mock.patch("tabularview_models.FilterCheckboxListModel.beginInsertRows") as bir, \ mock.patch("tabularview_models.FilterCheckboxListModel.endInsertRows") as eir, \ mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: model.add_item(new_item) self.assertEqual(model._data, sorted(self.data + new_item)) self.assertEqual(model._data_set, set(self.data + new_item))
def test_apply_filter_with_add(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') model._add_to_selection = True model._selected_filtered.discard('bbb') model.apply_filter() self.assertFalse(model._is_filtered) self.assertEqual(model._selected, set(self.data[:5])) self.assertEqual(model.rowCount(), 8) self.assertFalse(model._all_selected)
def test_add_item_with_select_with_filter_middle(self): new_item = ['b1'] model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') with mock.patch("tabularview_models.FilterCheckboxListModel.beginInsertRows") as bir, \ mock.patch("tabularview_models.FilterCheckboxListModel.endInsertRows") as eir, \ mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: model.add_item(new_item) self.assertEqual(model._filter_index, [3,4,5,6]) self.assertEqual(model.data(model.index(1 + 2, 0)), new_item[0])
def test_remove_items_filtered_data_not_selected(self): items = set('b') model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') model._selected_filtered.discard('a') model._all_selected = False with mock.patch("tabularview_models.FilterCheckboxListModel.beginResetModel") as br, \ mock.patch("tabularview_models.FilterCheckboxListModel.endResetModel") as er: model.remove_items(items) self.assertEqual(model._selected_filtered, set(self.data[4:])) self.assertTrue(model._all_selected)
def test_click_select_all_when_all_not_selected_and_filtered(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') with mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: index = model.index(2, 0) model.click_index(index) index = model.index(0, 0) model.click_index(index) self.assertTrue(model._all_selected) self.assertEqual(model._selected, set(self.data)) self.assertEqual(model._selected_filtered, set(self.data[3:]))
def test_add_item_with_select_with_filter_last(self): new_item = ['bbbb'] model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') with mock.patch("tabularview_models.FilterCheckboxListModel.beginInsertRows") as bir, \ mock.patch("tabularview_models.FilterCheckboxListModel.endInsertRows") as eir, \ mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: model.add_item(new_item) self.assertEqual(model._data, sorted(self.data + new_item)) self.assertEqual(model._data_set, set(self.data + new_item)) self.assertEqual(model._filter_index, [3,4,5,6]) self.assertEqual(model._selected_filtered, set(self.data[3:] + new_item)) self.assertEqual(model.data(model.index(3 + 2, 0)), new_item[0])
class FilterWidget(QWidget): okPressed = Signal() cancelPressed = Signal() def __init__(self, parent=None): super().__init__(parent) # parameters self._filter_state = set() self._filter_empty_state = False self._search_text = '' self.search_delay = 200 # create ui elements self._ui_vertical_layout = QVBoxLayout(self) self._ui_list = QListView() self._ui_edit = QLineEdit() self._ui_edit.setPlaceholderText('Search') self._ui_edit.setClearButtonEnabled(True) self._ui_buttons = QDialogButtonBox(QDialogButtonBox.Cancel | QDialogButtonBox.Ok) self._ui_vertical_layout.addWidget(self._ui_edit) self._ui_vertical_layout.addWidget(self._ui_list) self._ui_vertical_layout.addWidget(self._ui_buttons) # add models # used to limit search so it doesn't search when typing self._search_timer = QTimer() self._search_timer.setSingleShot(True) self._filter_model = FilterCheckboxListModel() self._filter_model.set_list(self._filter_state) self._ui_list.setModel(self._filter_model) # connect signals self._ui_list.clicked.connect(self._filter_model.click_index) self._search_timer.timeout.connect(self._filter_list) self._ui_edit.textChanged.connect(self._text_edited) self._ui_buttons.button(QDialogButtonBox.Ok).clicked.connect( self._apply_filter) self._ui_buttons.button(QDialogButtonBox.Cancel).clicked.connect( self._cancel_filter) def save_state(self): """Saves the state of the FilterCheckboxListModel""" self._filter_state = self._filter_model.get_selected() self._filter_empty_state = self._filter_model._empty_selected def reset_state(self): """Sets the state of the FilterCheckboxListModel to saved state""" self._filter_model.set_selected(self._filter_state, self._filter_empty_state) def clear_filter(self): """Selects all items in FilterCheckBoxListModel""" self._filter_model.reset_selection() self.save_state() def has_filter(self): """Returns true if any item is filtered in FilterCheckboxListModel false otherwise""" return not self._filter_model._all_selected def set_filter_list(self, data): """Sets the list of items to filter""" self._filter_state = set(data) self._filter_empty_state = True self._filter_model.set_list(self._filter_state) def _apply_filter(self): """apply current filter and save state""" self._filter_model.apply_filter() self.save_state() self._ui_edit.setText('') self.okPressed.emit() def _cancel_filter(self): """cancel current edit of filter and set the state to the stored state""" self._filter_model.remove_filter() self.reset_state() self._ui_edit.setText('') self.cancelPressed.emit() def _filter_list(self): """filter list with current text""" # filter model self._filter_model.set_filter(self._search_text) def _text_edited(self, new_text): """callback for edit text, starts/restarts timer""" # start timer after text is edited, restart timer if text # is edited before last time is out. self._search_text = new_text self._search_timer.start(self.search_delay)
def test_set_filter_index(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') self.assertEqual(model._filter_index, [3, 4, 5])
def test_rowCount_when_filter(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') self.assertEqual(model.rowCount(), 5)
def test_add_to_selection_when_filter(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') self.assertFalse(model._add_to_selection) self.assertFalse(model.data(model.index(1,0), Qt.CheckStateRole))
def test_selected_when_filtered(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') self.assertEqual(model._selected, set(self.data)) self.assertEqual(model._selected_filtered, set(self.data[3:]))
def test_is_all_selected_when_not_empty_selected(self): model = FilterCheckboxListModel() model.set_list(self.data) model._empty_selected = False self.assertFalse(model._is_all_selected())
def test_is_all_selected_when_not_all_selected(self): model = FilterCheckboxListModel() model.set_list(self.data) model._selected.discard('a') self.assertFalse(model._is_all_selected())
def test_is_all_selected_when_all_selected(self): model = FilterCheckboxListModel() model.set_list(self.data) self.assertTrue(model._is_all_selected())
def test_get_data_when_filtered(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') self.assertEqual(model.data(model.index(2,0)), 'b')