Ejemplo n.º 1
0
    def test_sorting(self):
        assert issubclass(PyTableModel, AbstractSortTableModel)
        model = PyTableModel([[1, 4],
                              [2, 2],
                              [3, 3]])
        model.sort(1, Qt.AscendingOrder)
        # mapToSourceRows
        self.assertSequenceEqual(model.mapToSourceRows(...).tolist(), [1, 2, 0])
        self.assertEqual(model.mapToSourceRows(1).tolist(), 2)
        self.assertSequenceEqual(model.mapToSourceRows([1, 2]).tolist(), [2, 0])
        self.assertSequenceEqual(model.mapToSourceRows([]), [])
        self.assertSequenceEqual(model.mapToSourceRows(np.array([], dtype=int)).tolist(), [])
        self.assertRaises(IndexError, model.mapToSourceRows, np.r_[0.])

        # mapFromSourceRows
        self.assertSequenceEqual(model.mapFromSourceRows(...).tolist(), [2, 0, 1])
        self.assertEqual(model.mapFromSourceRows(1).tolist(), 0)
        self.assertSequenceEqual(model.mapFromSourceRows([1, 2]).tolist(), [0, 1])
        self.assertSequenceEqual(model.mapFromSourceRows([]), [])
        self.assertSequenceEqual(model.mapFromSourceRows(np.array([], dtype=int)).tolist(), [])
        self.assertRaises(IndexError, model.mapFromSourceRows, np.r_[0.])

        model.sort(1, Qt.DescendingOrder)
        self.assertSequenceEqual(model.mapToSourceRows(...).tolist(), [0, 2, 1])
        self.assertSequenceEqual(model.mapFromSourceRows(...).tolist(), [0, 2, 1])
Ejemplo n.º 2
0
 def test_init_wrap_empty(self):
     # pylint: disable=protected-access
     t = []
     model = PyTableModel(t)
     self.assertIs(model._table, t)
     t.append([1, 2, 3])
     self.assertEqual(list(model), [[1, 2, 3]])
 def test_editable(self):
     editable_model = PyTableModel([[0]], editable=True)
     self.assertFalse(
         int(self.model.flags(self.model.index(0, 0)) & Qt.ItemIsEditable))
     self.assertTrue(
         int(
             editable_model.flags(editable_model.index(0, 0))
             & Qt.ItemIsEditable))
Ejemplo n.º 4
0
    def test_report_table_with_images(self):
        def basic_icon():
            pixmap = QPixmap(15, 15)
            pixmap.fill(QColor("red"))
            return QIcon(pixmap)

        def basic_scene():
            scene = QGraphicsScene()
            scene.addRect(QRectF(0, 0, 100, 100))
            return scene

        rep = OWReport()
        model = PyTableModel([['x', 1, 2]])
        model.setHorizontalHeaderLabels(['a', 'b', 'c'])

        model.setData(model.index(0, 1), basic_icon(), Qt.DecorationRole)
        model.setData(model.index(0, 2), basic_scene(), Qt.DisplayRole)

        view = gui.TableView()
        view.show()
        view.setModel(model)
        rep.report_table('Name', view)
        self.maxDiff = None
        pattern = re.compile(
            re.escape('<h2>Name</h2><table>\n'
                      '<tr>'
                      '<th style="color:black;border:0;background:transparent;'
                      'text-align:left;vertical-align:middle;">a</th>'
                      '<th style="color:black;border:0;background:transparent;'
                      'text-align:left;vertical-align:middle;">b</th>'
                      '<th style="color:black;border:0;background:transparent;'
                      'text-align:left;vertical-align:middle;">c</th>'
                      '</tr>'
                      '<tr>'
                      '<td style="color:black;border:0;background:transparent;'
                      'text-align:left;vertical-align:middle;">x</td>'
                      '<td style="color:black;border:0;background:transparent;'
                      'text-align:right;vertical-align:middle;">'
                      '<img src="data:image/png;base64,') + '(.+)' +
            re.escape(  # any string for the icon
                '"/>1</td>'
                '<td style="color:black;border:0;background:transparent;'
                'text-align:right;vertical-align:middle;">') + '(.+)' +
            re.escape('</td></tr></table>')  # str for the scene
        )
        self.assertTrue(bool(pattern.match(rep.report_html)))
Ejemplo n.º 5
0
    def test_report_table(self):
        rep = OWReport()
        model = PyTableModel([['x', 1, 2], ['y', 2, 2]])
        model.setHorizontalHeaderLabels(['a', 'b', 'c'])

        model.setData(model.index(0, 0), Qt.AlignHCenter | Qt.AlignTop,
                      Qt.TextAlignmentRole)
        model.setData(model.index(1, 0), QFont('', -1, QFont.Bold),
                      Qt.FontRole)
        model.setData(model.index(1, 2), QBrush(Qt.red), Qt.BackgroundRole)

        view = gui.TableView()
        view.show()
        view.setModel(model)
        rep.report_table('Name', view)
        self.maxDiff = None
        self.assertEqual(
            rep.report_html, '<h2>Name</h2><table>\n'
            '<tr>'
            '<th style="color:black;border:0;background:transparent;'
            'text-align:left;vertical-align:middle;">a</th>'
            '<th style="color:black;border:0;background:transparent;'
            'text-align:left;vertical-align:middle;">b</th>'
            '<th style="color:black;border:0;background:transparent;'
            'text-align:left;vertical-align:middle;">c</th>'
            '</tr>'
            '<tr>'
            '<td style="color:black;border:0;background:transparent;'
            'text-align:center;vertical-align:top;">x</td>'
            '<td style="color:black;border:0;background:transparent;'
            'text-align:right;vertical-align:middle;">1</td>'
            '<td style="color:black;border:0;background:transparent;'
            'text-align:right;vertical-align:middle;">2</td>'
            '</tr>'
            '<tr>'
            '<td style="color:black;border:0;background:transparent;'
            'font-weight: bold;text-align:left;vertical-align:middle;">y</td>'
            '<td style="color:black;border:0;background:transparent;'
            'text-align:right;vertical-align:middle;">2</td>'
            '<td style="color:black;border:0;background:#ff0000;'
            'text-align:right;vertical-align:middle;">2</td>'
            '</tr></table>')
Ejemplo n.º 6
0
 def test_init(self):
     self.model = PyTableModel()
     self.assertEqual(self.model.rowCount(), 0)
Ejemplo n.º 7
0
 def setUp(self):
     self.model = PyTableModel([[1, 4],
                                [2, 3]])