def test_view_layout(self): view_c = base_components.View(layout="column") view_c._initialize() self.assertEqual(view_c.underlying_layout.__class__, QtWidgets.QVBoxLayout) view_r = base_components.View(layout="row") view_r._initialize() self.assertEqual(view_r.underlying_layout.__class__, QtWidgets.QHBoxLayout) view_n = base_components.View(layout="none") view_n._initialize() self.assertEqual(view_n.underlying_layout, None)
def render(self): if self.use_keys: if self.use_state_as_key: return base_components.View()( *[_TestComponentInner(text).set_key(text) for text in self.state] ) else: return base_components.View()( *[_TestComponentInner(text).set_key(str(i)) for i, text in enumerate(self.state)] ) return base_components.View()( *[_TestComponentInner(text) for text in self.state] )
def render(self): if not hasattr(self, "render_called_count"): self.render_called_count = 0 self.render_called_count += 1 return base_components.View()( TestComp1(), TestComp2(), )
def test_components(self): context_menu = { "play": lambda: None, "sep": None, "options": { "faster": lambda: None, "sloer": lambda: None, } } completer1 = base_components.Completer(["option1", "option2"]) completer2 = base_components.Completer(["option1", "option2"], "inline") self._test_comp(base_components.Window( title="title", menu={"Playback": context_menu}, )(base_components.View())) self._test_comp(base_components.IconButton("play", on_click=lambda e: None)) self._test_comp(base_components.View(context_menu=context_menu)) self._test_comp(base_components.Label(text="Hello", selectable=True)) self._test_comp(base_components.Image(src="tests/example.png", scale_to_fit=True)) self._test_comp(base_components.Image(src=np.zeros((100, 100, 3)), scale_to_fit=False)) self._test_comp(base_components.Button("play", on_click=lambda e: None)) self._test_comp(base_components.TextInput("initial_text", on_change=lambda text: None)) self._test_comp(base_components.Button("play", on_click=lambda e: None)) self._test_comp(base_components.Dropdown(selection="Option1", options=["Option1, Option2"], on_select=lambda text: None)) self._test_comp(base_components.Dropdown(editable=True, selection="Option1", options=["Option1, Option2"], on_change=lambda text: None)) self._test_comp(base_components.Dropdown( editable=True, selection="Option1", options=["Option1, Option2"], completer=completer2, on_change=lambda text: None)) self._test_comp(base_components.TextInput("initial_text", on_change=lambda text: None)) self._test_comp(base_components.TextInput("initial_text", completer=completer1, on_change=lambda text: None)) self._test_comp(base_components.CheckBox(checked=True, text="Test", on_change=lambda checked: None)) self._test_comp(base_components.RadioButton(checked=True, text="Test", on_change=lambda checked: None)) self._test_comp(base_components.Slider(value=1, min_value=0, max_value=3, on_change=lambda value: None)) self._test_comp(base_components.ScrollView(layout="row")) self._test_comp(base_components.GridView(layout="")) self._test_comp(base_components.List()) self._test_comp(base_components.GroupBox(title="Group")(base_components.View())) self._test_comp(base_components.TabView(labels=["Tab 1", "Tab 2"])(base_components.Label(), base_components.Label()))
def test_view_change(self): label1 = base_components.Label(text="A") label2 = base_components.Label(text="B") view = base_components.View()(label1) def label_tree(label): tree = engine._WidgetTree(label, []) with engine._storage_manager() as manager: return tree, tree.gen_qt_commands(MockRenderContext(manager)) label1_tree, label1_commands = label_tree(label1) label2_tree, label2_commands = label_tree(label2) view_tree = engine._WidgetTree(view, [label1_tree]) with engine._storage_manager() as manager: commands = view_tree.gen_qt_commands(MockRenderContext(manager)) font_size = label1.underlying.font().pointSize() self.assertCountEqual(commands, label1_commands + [ (view.underlying.setStyleSheet, "QWidget#%s{}" % id(view)), (view.underlying.setProperty, "css_class", []), (view.underlying.style().unpolish, view.underlying), (view.underlying.style().polish, view.underlying), (view.underlying.setContextMenuPolicy, QtCore.Qt.DefaultContextMenu), (view.underlying.setCursor, QtCore.Qt.ArrowCursor), (view._set_on_key_down, view.underlying, None), (view._set_on_key_up, view.underlying, None), (view._set_on_mouse_enter, view.underlying, None), (view._set_on_mouse_leave, view.underlying, None), (view._set_on_mouse_down, view.underlying, None), (view._set_on_mouse_up, view.underlying, None), (view._set_on_mouse_move, view.underlying, None), (view._set_on_click, view.underlying, None), (view._add_child, 0, label1.underlying)]) view_tree = engine._WidgetTree(view, [label1_tree, label2_tree]) with engine._storage_manager() as manager: commands = view_tree.gen_qt_commands(MockRenderContext(manager)) self.assertCountEqual(commands, label1_commands + label2_commands + [ (view.underlying.setStyleSheet, "QWidget#%s{}" % id(view)), (view.underlying.setProperty, "css_class", []), (view.underlying.style().unpolish, view.underlying), (view.underlying.style().polish, view.underlying), (view.underlying.setContextMenuPolicy, QtCore.Qt.DefaultContextMenu), (view.underlying.setCursor, QtCore.Qt.ArrowCursor), (view._set_on_key_down, view.underlying, None), (view._set_on_key_up, view.underlying, None), (view._set_on_mouse_enter, view.underlying, None), (view._set_on_mouse_leave, view.underlying, None), (view._set_on_mouse_down, view.underlying, None), (view._set_on_mouse_up, view.underlying, None), (view._set_on_mouse_move, view.underlying, None), (view._set_on_click, view.underlying, None), (view._add_child, 1, label2.underlying)]) inner_view = base_components.View() old_child = view_tree.children[0].component view_tree = engine._WidgetTree(view, [label2_tree, engine._WidgetTree(inner_view, [])]) with engine._storage_manager() as manager: commands = view_tree.gen_qt_commands(MockRenderContext(manager)) self.assertCountEqual( commands, label2_commands + [ (view.underlying.setStyleSheet, "QWidget#%s{}" % id(view)), (view.underlying.setProperty, "css_class", []), (view.underlying.style().unpolish, view.underlying), (view.underlying.style().polish, view.underlying), (view.underlying.setContextMenuPolicy, QtCore.Qt.DefaultContextMenu), (view.underlying.setCursor, QtCore.Qt.ArrowCursor), (view._set_on_key_down, view.underlying, None), (view._set_on_key_up, view.underlying, None), (view._set_on_mouse_enter, view.underlying, None), (view._set_on_mouse_leave, view.underlying, None), (view._set_on_mouse_down, view.underlying, None), (view._set_on_mouse_up, view.underlying, None), (view._set_on_mouse_move, view.underlying, None), (view._set_on_click, view.underlying, None), (inner_view.underlying.setStyleSheet, "QWidget#%s{}" % id(inner_view)), (inner_view.underlying.setProperty, "css_class", []), (inner_view.underlying.style().unpolish, inner_view.underlying), (inner_view.underlying.style().polish, inner_view.underlying), (inner_view.underlying.setContextMenuPolicy, QtCore.Qt.DefaultContextMenu), (inner_view.underlying.setCursor, QtCore.Qt.ArrowCursor), (inner_view._set_on_key_down, inner_view.underlying, None), (inner_view._set_on_key_up, inner_view.underlying, None), (inner_view._set_on_mouse_enter, inner_view.underlying, None), (inner_view._set_on_mouse_leave, inner_view.underlying, None), (inner_view._set_on_mouse_down, inner_view.underlying, None), (inner_view._set_on_mouse_up, inner_view.underlying, None), (inner_view._set_on_mouse_move, inner_view.underlying, None), (inner_view._set_on_click, inner_view.underlying, None), (view._delete_child, 0, old_child), (view._add_child, 1, inner_view.underlying) ])
def render(self): if self.props.recursion_level == 1: return base_components.Label("Test") else: return base_components.View()( MockComponent(self.props.recursion_level + 1))
def render(self): return base_components.View()( base_components.Label(self.props.prop_a), base_components.Label(self.state_a), )
def render(self): self.count += 1 return base_components.View()( OldInnerClass(5) )
def render(self): return base_components.View()( TestCompInner1(self.value * 2), TestCompInner2(self.value * 4), )
def render(self): return base_components.View()(TestCompInner(self.value))
def render(self): return base_components.View()( _TestComponentInner(self.state_a), _TestComponentInner(self.state_b), base_components.Label(self.state_c), )