|
10 | 10 | from numpy.testing import assert_array_equal |
11 | 11 | import pandas as pd |
12 | 12 |
|
13 | | -from AnyQt.QtCore import QItemSelectionModel, Qt, QItemSelection |
| 13 | +from AnyQt.QtCore import QItemSelectionModel, Qt, QItemSelection, QPoint |
| 14 | +from AnyQt.QtGui import QPalette, QColor, QHelpEvent |
14 | 15 | from AnyQt.QtWidgets import QAction, QComboBox, QLineEdit, \ |
15 | | - QStyleOptionViewItem, QDialog, QMenu |
| 16 | + QStyleOptionViewItem, QDialog, QMenu, QToolTip, QListView |
16 | 17 | from AnyQt.QtTest import QTest, QSignalSpy |
17 | 18 |
|
18 | | -from Orange.widgets.utils import colorpalettes |
19 | 19 | from orangewidget.tests.utils import simulate |
20 | | -from orangewidget.utils.itemmodels import PyListModel |
21 | 20 |
|
22 | 21 | from Orange.data import ( |
23 | 22 | ContinuousVariable, DiscreteVariable, StringVariable, TimeVariable, |
|
35 | 34 | VariableEditDelegate, TransformRole, |
36 | 35 | RealVector, TimeVector, StringVector, make_dict_mapper, DictMissingConst, |
37 | 36 | LookupMappingTransform, as_float_or_nan, column_str_repr, time_parse, |
38 | | - GroupItemsDialog) |
| 37 | + GroupItemsDialog, VariableListModel |
| 38 | +) |
39 | 39 | from Orange.widgets.data.owcolor import OWColor, ColorRole |
40 | 40 | from Orange.widgets.tests.base import WidgetTest, GuiTest |
41 | 41 | from Orange.widgets.tests.utils import contextMenu |
| 42 | +from Orange.widgets.utils import colorpalettes |
42 | 43 | from Orange.tests import test_filename, assert_array_nanequal |
43 | 44 |
|
44 | 45 | MArray = np.ma.MaskedArray |
@@ -665,32 +666,72 @@ def test_unlink(self): |
665 | 666 | self.assertFalse(cbox.isChecked()) |
666 | 667 |
|
667 | 668 |
|
| 669 | +class TestModels(GuiTest): |
| 670 | + def test_variable_model(self): |
| 671 | + model = VariableListModel() |
| 672 | + self.assertEqual(model.effective_name(model.index(-1, -1)), None) |
| 673 | + |
| 674 | + def data(row, role): |
| 675 | + return model.data(model.index(row,), role) |
| 676 | + |
| 677 | + def set_data(row, data, role): |
| 678 | + model.setData(model.index(row), data, role) |
| 679 | + |
| 680 | + model[:] = [ |
| 681 | + RealVector(Real("A", (3, "g"), (), False), lambda: MArray([])), |
| 682 | + RealVector(Real("B", (3, "g"), (), False), lambda: MArray([])), |
| 683 | + ] |
| 684 | + self.assertEqual(data(0, Qt.DisplayRole), "A") |
| 685 | + self.assertEqual(data(1, Qt.DisplayRole), "B") |
| 686 | + self.assertEqual(model.effective_name(model.index(1)), "B") |
| 687 | + set_data(1, [Rename("A")], TransformRole) |
| 688 | + self.assertEqual(model.effective_name(model.index(1)), "A") |
| 689 | + self.assertEqual(data(0, MultiplicityRole), 2) |
| 690 | + self.assertEqual(data(1, MultiplicityRole), 2) |
| 691 | + set_data(1, [], TransformRole) |
| 692 | + self.assertEqual(data(0, MultiplicityRole), 1) |
| 693 | + self.assertEqual(data(1, MultiplicityRole), 1) |
| 694 | + |
| 695 | + |
668 | 696 | class TestDelegates(GuiTest): |
669 | 697 | def test_delegate(self): |
670 | | - model = PyListModel([None]) |
| 698 | + model = VariableListModel([None, None]) |
671 | 699 |
|
672 | | - def set_item(v: dict): |
673 | | - model.setItemData(model.index(0), v) |
| 700 | + def set_item(row: int, v: dict): |
| 701 | + model.setItemData(model.index(row), v) |
674 | 702 |
|
675 | | - def get_style_option() -> QStyleOptionViewItem: |
| 703 | + def get_style_option(row: int) -> QStyleOptionViewItem: |
676 | 704 | opt = QStyleOptionViewItem() |
677 | | - delegate.initStyleOption(opt, model.index(0)) |
| 705 | + delegate.initStyleOption(opt, model.index(row)) |
678 | 706 | return opt |
679 | 707 |
|
680 | | - set_item({Qt.EditRole: Categorical("a", (), (), False)}) |
| 708 | + set_item(0, {Qt.EditRole: Categorical("a", (), (), False)}) |
681 | 709 | delegate = VariableEditDelegate() |
682 | | - opt = get_style_option() |
| 710 | + opt = get_style_option(0) |
683 | 711 | self.assertEqual(opt.text, "a") |
684 | 712 | self.assertFalse(opt.font.italic()) |
685 | | - set_item({TransformRole: [Rename("b")]}) |
686 | | - opt = get_style_option() |
| 713 | + set_item(0, {TransformRole: [Rename("b")]}) |
| 714 | + opt = get_style_option(0) |
687 | 715 | self.assertEqual(opt.text, "a \N{RIGHTWARDS ARROW} b") |
688 | 716 | self.assertTrue(opt.font.italic()) |
689 | 717 |
|
690 | | - set_item({TransformRole: [AsString()]}) |
691 | | - opt = get_style_option() |
| 718 | + set_item(0, {TransformRole: [AsString()]}) |
| 719 | + opt = get_style_option(0) |
692 | 720 | self.assertIn("reinterpreted", opt.text) |
693 | 721 | self.assertTrue(opt.font.italic()) |
| 722 | + set_item(1, { |
| 723 | + Qt.EditRole: String("b", (), False), |
| 724 | + TransformRole: [Rename("a")] |
| 725 | + }) |
| 726 | + opt = get_style_option(1) |
| 727 | + self.assertEqual(opt.palette.color(QPalette.Text), QColor(Qt.red)) |
| 728 | + view = QListView() |
| 729 | + with patch.object(QToolTip, "showText") as p: |
| 730 | + delegate.helpEvent( |
| 731 | + QHelpEvent(QHelpEvent.ToolTip, QPoint(0, 0), QPoint(0, 0)), |
| 732 | + view, opt, model.index(1), |
| 733 | + ) |
| 734 | + p.assert_called_once() |
694 | 735 |
|
695 | 736 |
|
696 | 737 | class TestTransforms(TestCase): |
|
0 commit comments