-
Notifications
You must be signed in to change notification settings - Fork 55
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
DataView checkboxes #694
Merged
Merged
DataView checkboxes #694
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9fe7eb8
Add support for checkboxes in the data view; add a bool value type.
corranwebster c432828
Apply suggestions from code review
corranwebster ca73b88
Fix comments and add a test based on PR review comments.
corranwebster 918c616
Add a reference to Pyface #695
corranwebster 01fcf13
Add missing argument to set_check_state call.
corranwebster 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
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
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,108 @@ | ||
# (C) Copyright 2005-2020 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! | ||
|
||
from traits.api import Str | ||
|
||
from pyface.data_view.abstract_value_type import AbstractValueType, CheckState | ||
|
||
|
||
class BoolValue(AbstractValueType): | ||
""" Value that presents a boolean value via checked state. | ||
""" | ||
|
||
#: The text to display next to a True value. | ||
true_text = Str() | ||
|
||
#: The text to display next to a False value. | ||
false_text = Str() | ||
|
||
def has_editor_value(self, model, row, column): | ||
""" BoolValues don't use editors, but have always-on checkbox. | ||
|
||
Parameters | ||
---------- | ||
model : AbstractDataModel | ||
The data model holding the data. | ||
row : sequence of int | ||
The row in the data model being queried. | ||
column : sequence of int | ||
The column in the data model being queried. | ||
|
||
Returns | ||
------- | ||
has_editor_value : bool | ||
Whether or not the value is editable. | ||
""" | ||
return False | ||
|
||
def get_text(self, model, row, column): | ||
""" The textual representation of the underlying value. | ||
|
||
|
||
Parameters | ||
---------- | ||
model : AbstractDataModel | ||
The data model holding the data. | ||
row : sequence of int | ||
The row in the data model being queried. | ||
column : sequence of int | ||
The column in the data model being queried. | ||
|
||
Returns | ||
------- | ||
text : str | ||
The textual representation of the underlying value. | ||
""" | ||
return ( | ||
self.true_text if model.get_value(row, column) else self.false_text | ||
) | ||
|
||
def has_check_state(self, model, row, column): | ||
""" Whether or not the value has checked state. | ||
|
||
The default implementation returns True. | ||
|
||
Parameters | ||
---------- | ||
model : AbstractDataModel | ||
The data model holding the data. | ||
row : sequence of int | ||
The row in the data model being queried. | ||
column : sequence of int | ||
The column in the data model being queried. | ||
|
||
Returns | ||
------- | ||
has_check_state : bool | ||
Whether or not the value has a checked state. | ||
""" | ||
return True | ||
|
||
def set_check_state(self, model, row, column, check_state): | ||
""" Set the boolean value from the check state. | ||
|
||
Parameters | ||
---------- | ||
model : AbstractDataModel | ||
The data model holding the data. | ||
row : sequence of int | ||
The row in the data model being set. | ||
column : sequence of int | ||
The column in the data model being set. | ||
check_state : "checked" or "unchecked" | ||
The check state being set. | ||
|
||
Raises | ||
------- | ||
DataViewSetError | ||
If the value cannot be set. | ||
""" | ||
value = (check_state == CheckState.CHECKED) | ||
model.set_value(row, column, value) |
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,85 @@ | ||
# (C) Copyright 2005-2020 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! | ||
|
||
from unittest import TestCase | ||
from unittest.mock import Mock | ||
|
||
from pyface.data_view.abstract_value_type import CheckState | ||
from pyface.data_view.data_view_errors import DataViewSetError | ||
from pyface.data_view.value_types.bool_value import BoolValue | ||
|
||
|
||
class TestBoolValue(TestCase): | ||
|
||
def setUp(self): | ||
self.model = Mock() | ||
self.model.get_value = Mock(return_value=True) | ||
self.model.can_set_value = Mock(return_value=True) | ||
self.model.set_value = Mock() | ||
|
||
def test_defaults(self): | ||
value = BoolValue() | ||
self.assertEqual(value.true_text, "") | ||
self.assertEqual(value.false_text, "") | ||
|
||
def test_has_text_default(self): | ||
value = BoolValue() | ||
has_text = value.has_text(self.model, [0], [0]) | ||
self.assertFalse(has_text) | ||
|
||
def test_has_text(self): | ||
value = BoolValue(true_text="Yes", false_text="No") | ||
has_text = value.has_text(self.model, [0], [0]) | ||
self.assertTrue(has_text) | ||
|
||
def test_get_text_default(self): | ||
value = BoolValue() | ||
text = value.get_text(self.model, [0], [0]) | ||
self.assertEqual(text, "") | ||
|
||
self.model.get_value = Mock(return_value=False) | ||
text = value.get_text(self.model, [0], [0]) | ||
self.assertEqual(text, "") | ||
|
||
def test_get_text(self): | ||
value = BoolValue(true_text="Yes", false_text="No") | ||
text = value.get_text(self.model, [0], [0]) | ||
self.assertEqual(text, "Yes") | ||
|
||
self.model.get_value = Mock(return_value=False) | ||
text = value.get_text(self.model, [0], [0]) | ||
self.assertEqual(text, "No") | ||
|
||
def test_get_check_state(self): | ||
value = BoolValue() | ||
check_state = value.get_check_state(self.model, [0], [0]) | ||
self.assertEqual(check_state, CheckState.CHECKED) | ||
|
||
def test_get_check_state_false(self): | ||
value = BoolValue() | ||
self.model.get_value = Mock(return_value=False) | ||
check_state = value.get_check_state(self.model, [0], [0]) | ||
self.assertEqual(check_state, CheckState.UNCHECKED) | ||
|
||
def test_set_check_state(self): | ||
value = BoolValue() | ||
value.set_check_state(self.model, [0], [0], CheckState.CHECKED) | ||
self.model.set_value.assert_called_once_with([0], [0], True) | ||
|
||
def test_set_check_state_unchecked(self): | ||
value = BoolValue() | ||
value.set_check_state(self.model, [0], [0], CheckState.UNCHECKED) | ||
self.model.set_value.assert_called_once_with([0], [0], False) | ||
|
||
def test_set_check_state_no_set_value(self): | ||
self.model.can_set_value = Mock(return_value=False) | ||
value = BoolValue() | ||
with self.assertRaises(DataViewSetError): | ||
value.set_text(self.model, [0], [0], CheckState.CHECKED) |
Oops, something went wrong.
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.
I'm not sure why the default
get_check_state
doesn't return aDataViewGetError
likeset_check_state
does.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.
This is a default implementation that is almost always what you want: if the value has a check state, then it should be checked if the underlying value is truthy and unchecked if it is falsey.
By implementing it on the ABC we save repetition elsewhere (we do similar things for get text and get image, try to have a sane default for the getter).