Example #1
0
def testSimpleTable():
    t = buildTable()
    t.info()
    t._print()
    recorder = RecordingObject()
    model = TableModel(t, recorder)
    model.addNonEditable("rtmax")

    assert model.checkForAny("mzmin")
    assert not model.checkForAny("xxx")

    def idx(r, c):
        return model.createIndex(r, c)

    row = model.getRow(0)
    assert row.mz == 1.0
    assert row.mzmin == 0.975
    assert row.mzmax == 1.025

    assert row.rt == 10.0
    assert row.rtmin == 9.0
    assert row.rtmax == 15.0

    assert model.rowCount() == 3
    assert model.columnCount() == 7

    for i in range(3):
        for j in range(6):
            val = t.rows[i][j]
            is_ = model.data(idx(i, j))
            tobe = t.colFormatters[j](val)
            assert is_ == tobe, (is_, tobe)

    for i in range(3):
        for j in range(6):
            val = t.rows[i][j]
            is_ = model.data(idx(i, j), Qt.EditRole)
            tobe = t.colFormatters[j](val)
            assert is_ == tobe, (is_, tobe)

    for j in range(6):
        is_ = model.headerData(j, Qt.Horizontal)
        tobe = t.getColNames()[j]
        assert is_ == tobe, (is_, tobe)

    for j in range(6):
        flag = model.flags(idx(0, j))
        if j == 5:
            assert flag & Qt.ItemIsEditable == Qt.NoItemFlags
        else:
            assert flag & Qt.ItemIsEditable == Qt.ItemIsEditable

    for i in range(3):
        for j in range(3):
            val = i + j + 1.5
            assert model.setData(idx(i, j), QVariant(str(val)))
            is_ = model.data(idx(i, j))
            tobe = t.colFormatters[j](val)
            assert is_ == tobe, (is_, tobe)
        for j in range(3, 6):
            val = j * 1.0 + i
            sv = "%.1fm" % val
            assert model.setData(idx(i, j), QVariant(sv))
            is_ = model.data(idx(i, j))
            tobe = t.colFormatters[j](val * 60.0)
            assert is_ == tobe, (is_, tobe)

    assert len(model.actions) == 3 * 6
    assert recorder.accesses == 3 * 6 * ["updateMenubar"]
    model.emptyActionStack()

    before = model.data(idx(0, 0))
    model.setData(idx(0, 0), QVariant("-"))
    assert model.data(idx(0, 0)) == "-"
    assert model.table.rows[0][0] == None

    model.undoLastAction()
    assert before == model.data(idx(0, 0))
    assert len(model.actions) == 0

    model.redoLastAction()
    assert model.data(idx(0, 0)) == "-"
    assert model.table.rows[0][0] == None

    # here no undo !

    assert model.table.rows[0] != model.table.rows[1]
    assert model.rowCount() == 3
    model.cloneRow(0)
    assert model.table.rows[0] == model.table.rows[1]
    assert model.rowCount() == 4
    model.undoLastAction()
    assert model.table.rows[0] != model.table.rows[1]
    assert model.rowCount() == 3

    model.cloneRow(0)
    model.removeRows([0])
    assert model.table.rows[0] != model.table.rows[1]
    assert model.rowCount() == 3

    model.undoLastAction()
    model.undoLastAction()
    assert model.table.rows[0] != model.table.rows[1]
    assert model.rowCount() == 3

    assert len(model.actions) == 1  # 1 undo missing

    # we have to call sort three times, the first two are ingnored
    # for details look at the comment of the TableModel.sort method !
    model.sort(0, Qt.DescendingOrder)
    model.sort(0, Qt.DescendingOrder)
    model.sort(0, Qt.DescendingOrder)

    assert model.table.mz.values == (3.5, 2.5, None)
    model.undoLastAction()
    assert model.table.mz.values == (None, 2.5, 3.5)

    #assert model.postfixes == [""]

    assert model.hasFeatures()
    assert not model.isIntegrated()
def testSimpleTable():
    t =buildTable()
    t.info()
    t._print()
    recorder = RecordingObject()
    model = TableModel(t, recorder)
    model.addNonEditable("rtmax")


    assert model.checkForAny("mzmin")
    assert not model.checkForAny("xxx")

    def idx(r,c):
        return model.createIndex(r,c)

    row = model.getRow(0)
    assert row.mz == 1.0
    assert row.mzmin == 0.975
    assert row.mzmax == 1.025

    assert row.rt == 10.0
    assert row.rtmin == 9.0
    assert row.rtmax == 15.0

    assert model.rowCount() == 3
    assert model.columnCount() == 7

    for i in range(3):
        for j in range(6):
            val = t.rows[i][j]
            is_ = model.data(idx(i,j))
            tobe = t.colFormatters[j](val)
            assert is_ == tobe, (is_, tobe)

    for i in range(3):
        for j in range(6):
            val = t.rows[i][j]
            is_ = model.data(idx(i,j), Qt.EditRole)
            tobe = t.colFormatters[j](val)
            assert is_ == tobe, (is_, tobe)

    for j in range(6):
        is_ = model.headerData(j, Qt.Horizontal)
        tobe = t.getColNames()[j]
        assert is_ == tobe, (is_, tobe)

    for j in range(6):
        flag = model.flags(idx(0,j))
        if j==5:
            assert flag & Qt.ItemIsEditable == Qt.NoItemFlags
        else:
            assert flag & Qt.ItemIsEditable == Qt.ItemIsEditable

    for i in range(3):
        for j in range(3):
            val = i+j+1.5
            assert model.setData(idx(i,j), QVariant(str(val)))
            is_ = model.data(idx(i,j))
            tobe = t.colFormatters[j](val)
            assert is_ == tobe, (is_, tobe)
        for j in range(3,6):
            val = j*1.0+i
            sv = "%.1fm" % val
            assert model.setData(idx(i,j), QVariant(sv))
            is_ = model.data(idx(i,j))
            tobe = t.colFormatters[j](val*60.0)
            assert is_ == tobe, (is_, tobe)

    assert len(model.actions) == 3*6
    assert recorder.accesses == 3*6 * ["updateMenubar"]
    model.emptyActionStack()

    before = model.data(idx(0,0))
    model.setData(idx(0,0), QVariant("-"))
    assert model.data(idx(0,0)) == "-"
    assert model.table.rows[0][0] == None

    model.undoLastAction()
    assert before == model.data(idx(0,0))
    assert len(model.actions) == 0

    model.redoLastAction()
    assert model.data(idx(0,0)) == "-"
    assert model.table.rows[0][0] == None

    # here no undo !

    assert model.table.rows[0] != model.table.rows[1]
    assert model.rowCount() == 3
    model.cloneRow(0)
    assert model.table.rows[0] == model.table.rows[1]
    assert model.rowCount() == 4
    model.undoLastAction()
    assert model.table.rows[0] != model.table.rows[1]
    assert model.rowCount() == 3

    model.cloneRow(0)
    model.removeRow(0)
    assert model.table.rows[0] != model.table.rows[1]
    assert model.rowCount() == 3

    model.undoLastAction()
    model.undoLastAction()
    assert model.table.rows[0] != model.table.rows[1]
    assert model.rowCount() == 3

    assert len(model.actions) == 1 # 1 undo missing

    model.sort(0, Qt.DescendingOrder)

    assert model.table.mz.values == ( 3.5, 2.5, None)
    model.undoLastAction()
    assert model.table.mz.values == ( None, 2.5, 3.5)

    #assert model.postfixes == [""]

    assert model.hasFeatures()
    assert not model.isIntegrated()