示例#1
0
    def test_data_name_text(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):
            allAttr = not allAttr
            cm.setAttributeView(allAttr)
            ki = cm.index(n, 0, di)

            ti = cm.index(0, 0, ki)
            dt = cm.data(ti)
            self.assertTrue(isinstance(dt, QVariant))
            self.assertEqual(dt.toString(), '#text')

            ti = cm.index(0, 1, ki)
            dt = cm.data(ti)
            self.assertTrue(isinstance(dt, QVariant))
            self.assertEqual(str(dt.toString()).strip(), '')

            ti = cm.index(0, 2, ki)
            dt = cm.data(ti)
            self.assertTrue(isinstance(dt, QVariant))
            self.assertEqual(str(dt.toString()).strip(), 'Text  %s' % n)
示例#2
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)
示例#3
0
    def test_headerData(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 = []
        for n in range(nkids):
            kds.append(doc.createElement("kid%s" % n))
            qdn.appendChild(kds[-1])

        allAttr = False
        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)

        hd = cm.headerData(0, Qt.Horizontal)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), 'Name')
        hd = cm.headerData(0, Qt.Horizontal, Qt.DisplayRole)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), 'Name')

        hd = cm.headerData(1, Qt.Horizontal)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), 'Type')
        hd = cm.headerData(1, Qt.Horizontal, Qt.DisplayRole)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), 'Type')

        hd = cm.headerData(2, Qt.Horizontal)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), 'Value')
        hd = cm.headerData(2, Qt.Horizontal, Qt.DisplayRole)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), 'Value')

        hd = cm.headerData(3, Qt.Horizontal)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), '')
        hd = cm.headerData(3, Qt.Horizontal, Qt.DisplayRole)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), '')

        hd = cm.headerData(-1, Qt.Horizontal)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), '')
        hd = cm.headerData(-1, Qt.Horizontal, Qt.DisplayRole)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), '')

        cm.setAttributeView(True)

        hd = cm.headerData(1, Qt.Horizontal)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), 'Attributes')
        hd = cm.headerData(1, Qt.Horizontal, Qt.DisplayRole)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), 'Attributes')

        cm.setAttributeView(False)

        hd = cm.headerData(1, Qt.Horizontal)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), 'Type')
        hd = cm.headerData(1, Qt.Horizontal, Qt.DisplayRole)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), 'Type')

        allAttr = True
        cm = ComponentModel(doc, allAttr)
        hd = cm.headerData(1, Qt.Horizontal)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), 'Attributes')
        hd = cm.headerData(1, Qt.Horizontal, Qt.DisplayRole)
        self.assertTrue(isinstance(hd, QVariant))
        self.assertEqual(hd.toString(), 'Attributes')