Exemplo n.º 1
0
 def _has_children(self, parent: QtCore.QModelIndex = QtCore.QModelIndex()):
     """Test hasChildren."""
     model_types = (QtCore.QAbstractListModel, QtCore.QAbstractTableModel)
     if isinstance(self._model, model_types):
         print(parent == QtCore.QModelIndex(), self._model.rowCount())
         return parent == QtCore.QModelIndex(
         ) and self._model.rowCount() > 0
     else:
         return self._model.hasChildren(parent)
Exemplo n.º 2
0
    def _column_count(self, parent: QtCore.QModelIndex = QtCore.QModelIndex()):
        """Test columnCount.

        Workaround for the fact that ``columnCount`` is a private method in
        QAbstractListModel/QAbstractTableModel subclasses.
        """
        if isinstance(self._model, QtCore.QAbstractListModel):
            return 1 if parent == QtCore.QModelIndex() else 0
        else:
            return self._model.columnCount(parent)
Exemplo n.º 3
0
    def _test_basic(self):
        """Try to call a number of the basic functions (not all).

        Make sure the model doesn't outright segfault, testing the functions
        which make sense.
        """
        assert not self._model.buddy(QtCore.QModelIndex()).isValid()

        self._model.canFetchMore(QtCore.QModelIndex())
        assert self._column_count(QtCore.QModelIndex()) >= 0
        self._fetch_more(QtCore.QModelIndex())
        flags = self._model.flags(QtCore.QModelIndex())
        assert flags == constants.DROP_ENABLED or not flags
        self._has_children(QtCore.QModelIndex())

        has_row = self._model.hasIndex(0, 0)
        if has_row:
            cache = None
            self._model.match(self._model.index(0, 0), -1, cache)

        self._model.mimeTypes()
        assert not self._parent(QtCore.QModelIndex()).isValid()
        assert self._model.rowCount() >= 0
        self._model.span(QtCore.QModelIndex())

        self._model.supportedDropActions()
        self._model.roleNames()
Exemplo n.º 4
0
def test_abstracttablemodel():
    class Test(core.AbstractTableModel):
        def rowCount(self, parent=None):
            return 0

        def columnCount(self, parent=None):
            return 0

    model = Test()
    assert model.rowCount() == 0
    assert model.columnCount() == 0
    with model.change_layout():
        pass
    with model.reset_model():
        pass
    with model.remove_rows():
        pass
    with model.remove_columns():
        pass
    with model.insert_rows():
        pass
    with model.append_rows(1):
        pass
    with model.insert_columns():
        pass
    model.force_reset()
    model.force_layoutchange()
    model.check_index(
        QtCore.QModelIndex(),
        index_is_valid=True,
        do_not_use_parent=True,
        parent_is_invalid=True,
    )
Exemplo n.º 5
0
 def _parent(self, index: QtCore.QModelIndex):
     """Test parent."""
     model_types = (QtCore.QAbstractListModel, QtCore.QAbstractTableModel)
     if isinstance(self._model, model_types):
         return QtCore.QModelIndex()
     else:
         return self._model.parent(index)
Exemplo n.º 6
0
    def _test_row_count_and_column_count(self):
        """Test model's implementation of row/columnCount() and hasChildren().

        Models that are dynamically populated are not as fully tested here.

        The models rowCount() is tested more extensively in _check_children(),
        but this catches the big mistakes.
        """
        # check top row
        top_index = self._model.index(0, 0, QtCore.QModelIndex())

        rows = self._model.rowCount(top_index)
        assert rows >= 0

        columns = self._column_count(top_index)
        assert columns >= 0

        if rows == 0 or columns == 0:
            return

        assert self._has_children(top_index)

        second_level_index = self._model.index(0, 0, top_index)
        assert second_level_index.isValid()

        rows = self._model.rowCount(second_level_index)
        assert rows >= 0

        columns = self._column_count(second_level_index)
        assert columns >= 0

        if rows == 0 or columns == 0:
            return

        assert self._has_children(second_level_index)
Exemplo n.º 7
0
 def append_rows(self,
                 num_rows: int,
                 parent: QtCore.QModelIndex | None = None):
     parent = QtCore.QModelIndex() if parent is None else parent
     self.beginInsertRows(parent, self.rowCount(),
                          self.rowCount() + num_rows - 1)
     yield None
     self.endInsertRows()
Exemplo n.º 8
0
    def _test_parent(self):
        """Test model's implementation of QAbstractItemModel::parent()."""
        # Make sure the model won't crash and will return an invalid
        # QModelIndex when asked for the parent of an invalid index.
        assert not self._parent(QtCore.QModelIndex()).isValid()

        if not self._has_children():
            return

        # Column 0                | Column 1      |
        # QModelIndex()           |               |
        #    \- top_index         | top_index_1   |
        #         \- child_index  | child_index_1 |

        # Common error test #1, make sure that a top level index has a parent
        # that is a invalid QModelIndex.
        top_index = self._model.index(0, 0, QtCore.QModelIndex())
        assert not self._parent(top_index).isValid()

        # Common error test #2, make sure that a second level index has a
        # parent that is the first level index.
        if self._has_children(top_index):
            child_index = self._model.index(0, 0, top_index)
            assert self._parent(child_index) == top_index

        # Common error test #3, the second column should NOT have the same
        # children as the first column in a row.
        # Usually the second column shouldn't have children.
        if self._model.hasIndex(0, 1):
            top_index_1 = self._model.index(0, 1, QtCore.QModelIndex())
            if self._has_children(top_index) and self._has_children(
                    top_index_1):
                child_index = self._model.index(0, 0, top_index)
                assert child_index.isValid()
                child_index_1 = self._model.index(0, 0, top_index_1)
                assert child_index_1.isValid()
                assert child_index != child_index_1

        # Full test, walk n levels deep through the model making sure that all
        # parent's children correctly specify their parent.
        self._check_children(QtCore.QModelIndex())
Exemplo n.º 9
0
 def remove_rows(
     self,
     first: int | None = None,
     last: int | None = None,
     parent: QtCore.QModelIndex | None = None,
 ):
     parent = QtCore.QModelIndex() if parent is None else parent
     first = first if first is not None else 0
     last = last if last is not None else self.rowCount()
     self.beginRemoveRows(parent, first, last)
     yield None
     self.endRemoveRows()
Exemplo n.º 10
0
 def insert_columns(
     self,
     first: int | None = None,
     last: int | None = None,
     parent: QtCore.QModelIndex | None = None,
 ):
     parent = QtCore.QModelIndex() if parent is None else parent
     first = first if first is not None else 0
     last = last if last is not None else self.rowCount()
     self.beginInsertColumns(parent, first, last)
     yield None
     self.endInsertColumns()