-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for {enabled/visible}_when for Tabbed and VFold #1705
Open
aaronayres35
wants to merge
21
commits into
main
Choose a base branch
from
tabbed-enabled-visible-when
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f956869
add support for disabling tabs (previously content would be disabled …
aaronayres35 4ff2f31
get visible_when working as well
aaronayres35 b15347c
clean up
aaronayres35 2465e94
remove unneeded imports
aaronayres35 ac39d86
raise a TypeError with message
aaronayres35 82c9a63
update comment
aaronayres35 291aaa1
add a regression test for Tabbed visible_when
aaronayres35 98f22b7
fix newly introduced bug with unpacking in for loop
aaronayres35 eec5f97
add Tabbed enabled_when regression test
aaronayres35 3acd7a1
add same tests for VFold
aaronayres35 e77e12b
comments / naming
aaronayres35 3bfe512
refactor tests a bit
aaronayres35 0e70767
remove unneeded HGroups
aaronayres35 146b241
rename test case as TestVFold
aaronayres35 3637bcd
add comments to regression tests refering to the issue
aaronayres35 8c405bb
tests are qt specific
aaronayres35 c73854a
flake8
aaronayres35 a908d3b
add news fragment
aaronayres35 c050ebf
remove irrelevant changes
aaronayres35 4fe708a
Merge branch 'main' into tabbed-enabled-visible-when
aaronayres35 74dfc68
flake8
aaronayres35 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add support for {enabled/visible}_when on Tabbed and VFold groups (#1705) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
# (C) Copyright 2004-2021 Enthought, Inc., Austin, TX | ||
# All rights reserved. | ||
# | ||
# This software is provided without warranty under the terms of the BSD | ||
# license included in LICENSE.txt and may be redistributed only under | ||
# the conditions described in the aforementioned license. The license | ||
# is also available online at http://www.enthought.com/licenses/BSD.txt | ||
# | ||
# Thanks for using Enthought open source! | ||
|
||
import unittest | ||
|
||
from traits.api import Float, HasTraits, Int | ||
|
||
from traitsui.api import Item, Tabbed, VFold, VGroup, View | ||
from traitsui.testing.api import KeyClick, UITester | ||
from traitsui.tests._tools import requires_toolkit, ToolkitName | ||
|
||
|
||
class Foo(HasTraits): | ||
a = Int() | ||
b = Float() | ||
|
||
|
||
def get_view(group_type, enabled_visible): | ||
if enabled_visible == "enabled": | ||
return View( | ||
group_type( | ||
VGroup( | ||
Item('a'), | ||
label='Fold #1', | ||
enabled_when='object.b != 0.0', | ||
id="first_fold" | ||
), | ||
VGroup( | ||
Item('b'), | ||
label='Fold #2', | ||
id="second_fold" | ||
), | ||
id="interesting_group" | ||
) | ||
) | ||
else: | ||
return View( | ||
group_type( | ||
VGroup( | ||
Item('a'), | ||
label='Fold #1', | ||
visible_when='object.b != 0.0', | ||
id="first_fold" | ||
), | ||
VGroup( | ||
Item('b'), | ||
label='Fold #2', | ||
id="second_fold" | ||
), | ||
id="interesting_group" | ||
) | ||
) | ||
|
||
|
||
class TestTabbed(unittest.TestCase): | ||
|
||
# regression test for enthought/tratsui#758 | ||
@requires_toolkit([ToolkitName.qt]) | ||
def test_visible_when(self): | ||
tabbed_visible = Foo() | ||
view = get_view(Tabbed, "visible") | ||
tester = UITester() | ||
|
||
with tester.create_ui(tabbed_visible, dict(view=view)) as ui: | ||
tabbed_fold_group_editor = tester.find_by_id( | ||
ui, "interesting_group" | ||
)._target | ||
q_tab_widget = tabbed_fold_group_editor.container | ||
# only Tab#2 is available at first | ||
self.assertEqual(q_tab_widget.count(), 1) | ||
|
||
# change b to != 0.0 so Tab #1 is visible | ||
b_field = tester.find_by_name(ui, 'b') | ||
b_field.perform(KeyClick("1")) | ||
b_field.perform(KeyClick("Enter")) | ||
|
||
self.assertEqual(q_tab_widget.count(), 2) | ||
|
||
# regression test for enthought/tratsui#758 | ||
@requires_toolkit([ToolkitName.qt]) | ||
def test_enabled_when(self): | ||
tabbed_enabled = Foo() | ||
view = get_view(Tabbed, "enabled") | ||
tester = UITester() | ||
|
||
with tester.create_ui(tabbed_enabled, dict(view=view)) as ui: | ||
tabbed_fold_group_editor = tester.find_by_id( | ||
ui, "interesting_group" | ||
)._target | ||
q_tab_widget = tabbed_fold_group_editor.container | ||
# both tabs exist | ||
self.assertEqual(q_tab_widget.count(), 2) | ||
# but first is disabled | ||
self.assertFalse(q_tab_widget.isTabEnabled(0)) | ||
|
||
# change b to != 0.0 so Tab #1 is enabled | ||
b_field = tester.find_by_name(ui, 'b') | ||
b_field.perform(KeyClick("1")) | ||
b_field.perform(KeyClick("Enter")) | ||
|
||
self.assertEqual(q_tab_widget.count(), 2) | ||
self.assertTrue(q_tab_widget.isTabEnabled(0)) | ||
|
||
|
||
class TestVFold(unittest.TestCase): | ||
|
||
# regression test for enthought/tratsui#758 | ||
@requires_toolkit([ToolkitName.qt]) | ||
def test_visible_when(self): | ||
fold_visible = Foo() | ||
view = get_view(VFold, "visible") | ||
tester = UITester() | ||
|
||
with tester.create_ui(fold_visible, dict(view=view)) as ui: | ||
tabbed_fold_group_editor = tester.find_by_id( | ||
ui, "interesting_group" | ||
)._target | ||
q_tool_box = tabbed_fold_group_editor.container | ||
# only Fold #2 is available at first | ||
self.assertEqual(q_tool_box.count(), 1) | ||
|
||
# change b to != 0.0 so Fold #1 is visible | ||
b_field = tester.find_by_name(ui, 'b') | ||
b_field.perform(KeyClick("1")) | ||
b_field.perform(KeyClick("Enter")) | ||
|
||
self.assertEqual(q_tool_box.count(), 2) | ||
|
||
# regression test for enthought/tratsui#758 | ||
@requires_toolkit([ToolkitName.qt]) | ||
def test_enabled_when(self): | ||
fold_enabled = Foo() | ||
view = get_view(VFold, "enabled") | ||
tester = UITester() | ||
|
||
with tester.create_ui(fold_enabled, dict(view=view)) as ui: | ||
tabbed_fold_group_editor = tester.find_by_id( | ||
ui, "interesting_group" | ||
)._target | ||
q_tool_box = tabbed_fold_group_editor.container | ||
# both folds exist | ||
self.assertEqual(q_tool_box.count(), 2) | ||
# but first is disabled | ||
self.assertFalse(q_tool_box.isItemEnabled(0)) | ||
|
||
# change b to != 0.0 so Fold #1 is enabled | ||
b_field = tester.find_by_name(ui, 'b') | ||
b_field.perform(KeyClick("1")) | ||
b_field.perform(KeyClick("Enter")) | ||
|
||
self.assertEqual(q_tool_box.count(), 2) | ||
self.assertTrue(q_tool_box.isItemEnabled(0)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
theoretically, if the widget is visible,
idx == self.container.indexOf(widget)
. However, perhaps the tab could change its index during the lifetime of the tab widget, in which case we should callmethod_to_call(self.container.indexOf(widget))
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this comment raises a significant issue that needs to be considered: the index of the widget in the
Tabbed
group doesn't necessarily match the index of the Qt tab, because visibility is controlled by adding/removing the widget from the container.I think things will go wrong in situations like the following:
I think there are situations where you can get things showing up in pretty much arbitrary order (eg. I think hide T2, hide T1, show T2 ends up with T0, T3, T2 as the layout).
I could be missing something about indices are being computed, but it looks like you can't assume that TraitsUI and Qt-level indices match, and computing the right Qt level index for re-insertion needs some thought (I think we need to work out how many TraitsUI tabs before the tab of interest are visible).