示例#1
0
    def test_data_name_attr_true(self):
        fun = sys._getframe().f_code.co_name
        print "Run: %s.%s() " % (self.__class__.__name__, fun)

        doc = QDomDocument()
        nname = "definition"
        qdn = doc.createElement(nname)
        doc.appendChild(qdn)
        nkids = self.__rnd.randint(1, 10)
        kds = []
        tkds = []
        for n in range(nkids):
            kds.append(doc.createElement("kid%s" % n))
            kds[-1].setAttribute("name", "myname%s" % n)
            kds[-1].setAttribute("type", "mytype%s" % n)
            kds[-1].setAttribute("units", "myunits%s" % n)
            qdn.appendChild(kds[-1])
            tkds.append(doc.createTextNode("\nText\n %s\n" % n))
            kds[-1].appendChild(tkds[-1])

#        print doc.toString()

        allAttr = True
        cm = ComponentModel(doc, allAttr)
        self.assertTrue(isinstance(cm, QAbstractItemModel))
        self.assertTrue(isinstance(cm.rootIndex, QModelIndex))
        cd = cm.rootIndex.internalPointer()
        self.assertTrue(isinstance(cd, ComponentItem))
        self.assertEqual(cm.rootIndex.row(), 0)
        self.assertEqual(cm.rootIndex.column(), 0)
        self.assertEqual(cm.headerData(0, Qt.Vertical), None)

        ri = cm.rootIndex
        di = cm.index(0, 0, ri)
        ci = cd.child(0)
        for n in range(nkids):
            kd = ci.child(n)

            ki0 = cm.index(n, 0, di)
            dt = cm.data(ki0)
            self.assertTrue(isinstance(dt, QVariant))
            self.assertEqual(dt.toString(), 'kid%s: myname%s' % (n, n))

            ki1 = cm.index(n, 1, di)
            dt = cm.data(ki1)
            self.assertTrue(isinstance(dt, QVariant))
            self.assertEqual(
                str(dt.toString()).strip(),
                'units="myunits%s" type="mytype%s" name="myname%s"' %
                (n, n, n))

            ki2 = cm.index(n, 2, di)
            dt = cm.data(ki2)
            self.assertTrue(isinstance(dt, QVariant))
            self.assertEqual(str(dt.toString()).strip(), '')
示例#2
0
class TestView(object):
    def __init__(self):
        try:
            self.__seed = long(binascii.hexlify(os.urandom(16)), 16)
        except NotImplementedError:
            self.__seed = long(time.time() * 256)
#        self.__seed = 71366247078680776091931824685320965500
        self.__rnd = random.Random(self.__seed)
        print "VIEW SEED", self.__seed

        self.sindex = None
        self.eindex = None

        self.doc = QDomDocument()
        self.nname = "definition"
        self.qdn = self.doc.createElement(self.nname)
        self.doc.appendChild(self.qdn)
        self.nkids = self.__rnd.randint(1, 10)
        #        print "NKID", self.nkids
        self.kds = []
        self.tkds = []
        for n in range(self.nkids):
            self.kds.append(self.doc.createElement("kid%s" % n))
            self.kds[-1].setAttribute("name", "myname%s" % n)
            self.kds[-1].setAttribute("type", "mytype%s" % n)
            self.kds[-1].setAttribute("units", "myunits%s" % n)
            self.qdn.appendChild(self.kds[-1])
            self.tkds.append(self.doc.createTextNode("\nText\n %s\n" % n))
            self.kds[-1].appendChild(self.tkds[-1])

#        print doc.toString()

        self.allAttr = False
        self.testModel = ComponentModel(self.doc, self.allAttr)
        #        self.myindex = self.setIndex(0, 0, self.testModel.rootIndex)
        self.myindex = QModelIndex()

    def setIndex(self, row, column, parent):
        self.myindex = self.testModel.index(row, column, parent)

#       print self.myindex.column()

    def currentIndex(self):
        return self.myindex

    def model(self):
        return self.testModel

    def dataChanged(self, sindex, eindex):
        self.sindex = sindex
        self.eindex = eindex
示例#3
0
    def test_insertItem(self):
        fun = sys._getframe().f_code.co_name
        print "Run: %s.%s() " % (self.__class__.__name__, fun)

        doc = QDomDocument()
        nname = "definition"
        qdn = doc.createElement(nname)
        doc.appendChild(qdn)
        nkids = self.__rnd.randint(1, 10)
        kds = []
        tkds = []
        for n in range(nkids):
            kds.append(doc.createElement("kid%s" % n))
            kds[-1].setAttribute("name", "myname%s" % n)
            kds[-1].setAttribute("type", "mytype%s" % n)
            kds[-1].setAttribute("units", "myunits%s" % n)
            qdn.appendChild(kds[-1])
            tkds.append(doc.createTextNode("\nText\n %s\n" % n))
            kds[-1].appendChild(tkds[-1])

#        print doc.toString()

        allAttr = True
        cm = ComponentModel(doc, allAttr)
        self.assertTrue(isinstance(cm, QAbstractItemModel))
        self.assertTrue(isinstance(cm.rootIndex, QModelIndex))
        cd = cm.rootIndex.internalPointer()
        self.assertTrue(isinstance(cd, ComponentItem))
        self.assertEqual(cm.rootIndex.row(), 0)
        self.assertEqual(cm.rootIndex.column(), 0)

        ri = cm.rootIndex
        self.assertEqual(cm.columnCount(ri), 3)

        # avoids showing #document
        di = cm.index(0, 0, ri)
        self.assertEqual(cm.columnCount(di), 3)

        iv = cm.index(0, 0)
        self.assertEqual(cm.columnCount(iv), 3)

        ci = di.internalPointer()
        self.assertEqual(ci.node, qdn)
        self.assertEqual(ci.childNumber(), 0)
        self.assertEqual(ci.node.nodeName(), nname)
        for k in range(nkids):
            ks = ci.child(k)
            self.assertTrue(isinstance(ks, ComponentItem))
            self.assertTrue(isinstance(ks.parent, ComponentItem))
            self.assertEqual(ks.childNumber(), k)
            self.assertEqual(ks.node, kds[k])
            self.assertEqual(ks.parent.node, qdn)
            self.assertEqual(ks.node.nodeName(), "kid%s" % k)
            self.assertEqual(ks.parent, ci)
            self.assertTrue(isinstance(ks.child(0), ComponentItem))
            self.assertTrue(isinstance(ks.child(0).parent, ComponentItem))
            self.assertEqual(ks.child(0).childNumber(), 0)
            self.assertEqual(ks.child(0).node, tkds[k])
            self.assertEqual(ks.child(0).parent.node, ks.node)
            self.assertEqual(ks.child(0).node.nodeName(), "#text")
            self.assertEqual(
                ks.child(0).node.toText().data(), '\nText\n %s\n' % k)
            self.assertEqual(ks.child(0).parent, ks)

        insd = self.__rnd.randint(0, nkids - 1)
        inkd = doc.createElement("insertedkid")
        self.assertTrue(not cm.insertItem(insd, inkd, QModelIndex()))

        self.assertTrue(cm.insertItem(insd, inkd, di))

        for k in range(nkids + 1):
            ks = ci.child(k)
            if k == insd:
                self.assertTrue(isinstance(ks, ComponentItem))
                self.assertTrue(isinstance(ks.parent, ComponentItem))
                self.assertEqual(ks.childNumber(), k)
                self.assertEqual(ks.node, inkd)
                self.assertEqual(ks.parent.node, qdn)
                self.assertEqual(ks.node.nodeName(), "insertedkid")
                self.assertEqual(ks.parent, ci)
                continue
            kk = k if k < insd else k - 1
            self.assertTrue(isinstance(ks, ComponentItem))
            self.assertTrue(isinstance(ks.parent, ComponentItem))
            self.assertEqual(ks.childNumber(), k)
            self.assertEqual(ks.node, kds[kk])
            self.assertEqual(ks.parent.node, qdn)
            self.assertEqual(ks.node.nodeName(), "kid%s" % kk)
            self.assertEqual(ks.parent, ci)
            self.assertTrue(isinstance(ks.child(0), ComponentItem))
            self.assertTrue(isinstance(ks.child(0).parent, ComponentItem))
            self.assertEqual(ks.child(0).childNumber(), 0)
            self.assertEqual(ks.child(0).node, tkds[kk])
            self.assertEqual(ks.child(0).parent.node, ks.node)
            self.assertEqual(ks.child(0).node.nodeName(), "#text")
            self.assertEqual(
                ks.child(0).node.toText().data(), '\nText\n %s\n' % kk)
            self.assertEqual(ks.child(0).parent, ks)
示例#4
0
    def test_columnCount(self):
        fun = sys._getframe().f_code.co_name
        print "Run: %s.%s() " % (self.__class__.__name__, fun)

        doc = QDomDocument()
        nname = "definition"
        qdn = doc.createElement(nname)
        doc.appendChild(qdn)
        nkids = self.__rnd.randint(1, 10)
        kds = []
        tkds = []
        for n in range(nkids):
            kds.append(doc.createElement("kid%s" % n))
            kds[-1].setAttribute("name", "myname%s" % n)
            kds[-1].setAttribute("type", "mytype%s" % n)
            kds[-1].setAttribute("units", "myunits%s" % n)
            qdn.appendChild(kds[-1])
            tkds.append(doc.createTextNode("\nText\n %s\n" % n))
            kds[-1].appendChild(tkds[-1])

#        print doc.toString()

        allAttr = True
        cm = ComponentModel(doc, allAttr)
        self.assertTrue(isinstance(cm, QAbstractItemModel))
        self.assertTrue(isinstance(cm.rootIndex, QModelIndex))
        cd = cm.rootIndex.internalPointer()
        self.assertTrue(isinstance(cd, ComponentItem))
        self.assertEqual(cm.rootIndex.row(), 0)
        self.assertEqual(cm.rootIndex.column(), 0)

        ri = cm.rootIndex
        self.assertEqual(cm.columnCount(ri), 3)

        # avoids showing #document
        di = cm.index(0, 0, ri)
        self.assertEqual(cm.columnCount(di), 3)

        iv = cm.index(0, 0)
        self.assertEqual(cm.columnCount(iv), 3)

        for n in range(nkids):
            allAttr = not allAttr
            cm.setAttributeView(allAttr)

            ki = cm.index(n, 0, di)
            self.assertEqual(cm.columnCount(ki), 3)

            ki = cm.index(n, 1, di)
            self.assertEqual(cm.columnCount(ki), 3)

            ki = cm.index(n, 2, di)
            self.assertEqual(cm.columnCount(ki), 3)

            # invalid index
            ki = cm.index(n, 3, di)
            self.assertEqual(cm.columnCount(ki), 3)

            ki = cm.index(n, 0, di)
            ti = cm.index(0, 0, ki)
            self.assertEqual(cm.columnCount(ti), 3)

            ti = cm.index(0, 1, ki)
            self.assertEqual(cm.columnCount(ti), 3)

            ti = cm.index(0, 2, ki)
            self.assertEqual(cm.columnCount(ti), 3)

            ti = cm.index(0, 3, ki)
            self.assertEqual(cm.columnCount(ti), 3)