Skip to content
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

Removed set_roi_alpha method and replaced it with set_roi_visibility #2345

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ashmeigh
Copy link
Collaborator

…flags to consolidate visibility and alpha management.

Issue

Closes #2342

Description

Removed set_roi_alpha and replaced it with set_roi_visibility_flags to handle both visibility and alpha transparency. Updated references in presenter.py, view.py, and spectrum_widget.py.

Testing

Refactored existing tests to validate the new set_roi_visibility_flags method.
Verified that ROIs are correctly shown/hidden and their transparency is adjusted as expected.

Acceptance Criteria

The reviewer should confirm that ROI visibility and alpha transparency are handled correctly.
Verify that set_roi_alpha references are fully replaced by set_roi_visibility_flags.
Run all tests to ensure they pass without errors.

Documentation

No changes needed in the documentation since this refactor does not affect the external API or user-facing functionality. Internal functionality has been improved for better maintainability.

…flags to consolidate visibility and alpha management.
@coveralls
Copy link

coveralls commented Oct 16, 2024

Coverage Status

coverage: 74.29% (+0.01%) from 74.279%
when pulling 6fc65f3 on 2342_Remove-set_alpha-Method
into b2ee792 on main.

Comment on lines 93 to 109
@parameterized.expand([("Visible", "visible_roi", 255), ("Invisible", "invisible_roi", 0)])
def test_WHEN_set_roi_alpha_called_THEN_roi_alpha_updated(self, _, name, alpha):
def test_WHEN_set_roi_visibility_flags_called_THEN_roi_alpha_updated(self, _, name, alpha):
spectrum_roi = SpectrumROI(name, self.sensible_roi, rotatable=False, scaleSnap=True, translateSnap=True)
self.spectrum_widget.roi_dict[name] = spectrum_roi
self.spectrum_widget.spectrum_data_dict[name] = np.array([0, 0, 0, 0])
self.spectrum_widget.set_roi_alpha(name, alpha)
self.spectrum_widget.set_roi_visibility_flags(name, visible=bool(alpha), alpha=alpha)
self.assertEqual(self.spectrum_widget.roi_dict[name].pen.color().getRgb()[-1], alpha)
self.assertEqual(self.spectrum_widget.roi_dict[name].hoverPen.color().getRgb()[-1], alpha)

@parameterized.expand([("Visible", "visible_roi", 1), ("Invisible", "invisible_roi", 0)])
def test_WHEN_set_roi_alpha_called_THEN_set_roi_visibility_flags_called(self, _, name, alpha):
def test_WHEN_set_roi_visibility_flags_called_THEN_visibility_and_alpha_handled(self, _, name, alpha):
spectrum_roi = SpectrumROI(name, self.sensible_roi, rotatable=False, scaleSnap=True, translateSnap=True)
self.spectrum_widget.roi_dict[name] = spectrum_roi
self.spectrum_widget.spectrum_data_dict[name] = np.array([0, 0, 0, 0])
with mock.patch.object(SpectrumWidget, "set_roi_visibility_flags") as mock_set_roi_visibility_flags:
self.spectrum_widget.set_roi_alpha(name, alpha)
mock_set_roi_visibility_flags.assert_called_once_with(name, alpha)
with mock.patch.object(spectrum_roi, "set_visibility") as mock_set_visibility:
self.spectrum_widget.set_roi_visibility_flags(name, visible=bool(alpha), alpha=alpha)
mock_set_visibility.assert_called_once_with(bool(alpha))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These can be removed, as there is no need to change alpha

Comment on lines 181 to 193
def set_roi_visibility_flags(self, name: str, visible: bool, alpha: float = 1.0) -> None:
"""
Change the visibility of an existing ROI including handles and update
the ROI dictionary, sending a signal to the main window.
the ROI dictionary, setting the alpha (transparency) value at the same time.

@param name: The name of the ROI.
@param visible: The new visibility of the ROI.
@param alpha: The new alpha (transparency) value of the ROI.
"""
self.roi_dict[name].set_visibility(visible)

def set_roi_alpha(self, name: str, alpha: float) -> None:
"""
Change the alpha value of an existing ROI
@param name: The name of the ROI.
@param alpha: The new alpha value of the ROI.
"""
self.roi_dict[name].colour = self.roi_dict[name].colour[:3] + (alpha, )
self.roi_dict[name].setPen(self.roi_dict[name].colour)
self.roi_dict[name].hoverPen = mkPen(self.roi_dict[name].colour, width=3)
self.set_roi_visibility_flags(name, bool(alpha))
if alpha is not None:
self.roi_dict[name].colour = self.roi_dict[name].colour[:3] + (alpha, )
self.roi_dict[name].set_visibility(visible)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is a need to have the alpha value. If the visibility is set to false then the element will be hidden.

Comment on lines 251 to 254
if roi_visible is False:
self.set_roi_alpha(0, roi_name)
self.set_roi_visibility_flags(roi_name, visible=False, alpha=0)
else:
self.set_roi_alpha(255, roi_name)
self.set_roi_visibility_flags(roi_name, visible=True, alpha=255)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If statement can be removed, and this can be:

self.set_roi_visibility_flags(roi_name, roi_visible)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor to Remove set_alpha Method and Consolidate Visibility Management Under set_roi_visibility_flags
3 participants