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

RF: Add keyword arguments decorator to module: UI #902

Merged
merged 1 commit into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions fury/ui/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np

from fury.actor import grid
from fury.decorators import warn_on_args_to_kwargs
from fury.io import load_image
from fury.lib import (
CellArray,
Expand Down Expand Up @@ -32,9 +33,11 @@ class Panel2D(UI):

"""

@warn_on_args_to_kwargs()
def __init__(
self,
size,
*,
position=(0, 0),
color=(0.1, 0.1, 0.1),
opacity=0.7,
Expand Down Expand Up @@ -68,7 +71,7 @@ def __init__(
self.has_border = has_border
self._border_color = border_color
self._border_width = border_width
super(Panel2D, self).__init__(position)
super(Panel2D, self).__init__(position=position)
self.resize(size)
self.alignment = align
self.color = color
Expand Down Expand Up @@ -205,7 +208,8 @@ def opacity(self):
def opacity(self, opacity):
self.background.opacity = opacity

def add_element(self, element, coords, anchor="position"):
@warn_on_args_to_kwargs()
def add_element(self, element, coords, *, anchor="position"):
"""Add a UI component to the panel.

The coordinates represent an offset from the lower left corner of the
Expand Down Expand Up @@ -255,7 +259,8 @@ def remove_element(self, element):
del self._elements[idx]
del self.element_offsets[idx]

def update_element(self, element, coords, anchor="position"):
@warn_on_args_to_kwargs()
def update_element(self, element, coords, *, anchor="position"):
"""Update the position of a UI component in the panel.

Parameters
Expand All @@ -271,7 +276,7 @@ def update_element(self, element, coords, anchor="position"):

"""
self.remove_element(element)
self.add_element(element, coords, anchor)
self.add_element(element, coords, anchor=anchor)

def left_button_pressed(self, i_ren, _obj, panel2d_object):
click_pos = np.array(i_ren.event.position)
Expand Down Expand Up @@ -382,8 +387,10 @@ class TabPanel2D(UI):

"""

@warn_on_args_to_kwargs()
def __init__(
self,
*,
position=(0, 0),
size=(100, 100),
title="New Tab",
Expand Down Expand Up @@ -572,7 +579,8 @@ def title_italic(self, italic):
"""
self.text_block.italic = italic

def add_element(self, element, coords, anchor="position"):
@warn_on_args_to_kwargs()
def add_element(self, element, coords, *, anchor="position"):
"""Add a UI component to the content panel.

The coordinates represent an offset from the lower left corner of the
Expand All @@ -590,7 +598,7 @@ def add_element(self, element, coords, anchor="position"):

"""
element.set_visibility(False)
self.content_panel.add_element(element, coords, anchor)
self.content_panel.add_element(element, coords, anchor=anchor)

def remove_element(self, element):
"""Remove a UI component from the content panel.
Expand All @@ -603,7 +611,8 @@ def remove_element(self, element):
"""
self.content_panel.remove_element(element)

def update_element(self, element, coords, anchor="position"):
@warn_on_args_to_kwargs()
def update_element(self, element, coords, *, anchor="position"):
"""Update the position of a UI component in the content panel.

Parameters
Expand Down Expand Up @@ -631,8 +640,10 @@ class TabUI(UI):

"""

@warn_on_args_to_kwargs()
def __init__(
self,
*,
position=(0, 0),
size=(100, 100),
nb_tabs=1,
Expand Down Expand Up @@ -835,10 +846,11 @@ def collapse_tab_ui(self, iren, _obj, _tab_comp):
iren.force_render()
iren.event.abort()

def add_element(self, tab_idx, element, coords, anchor="position"):
@warn_on_args_to_kwargs()
def add_element(self, tab_idx, element, coords, *, anchor="position"):
"""Add element to content panel after checking its existence."""
if tab_idx < self.nb_tabs and tab_idx >= 0:
self.tabs[tab_idx].add_element(element, coords, anchor)
self.tabs[tab_idx].add_element(element, coords, anchor=anchor)
if tab_idx == self.active_tab_idx:
element.set_visibility(True)
else:
Expand All @@ -851,10 +863,11 @@ def remove_element(self, tab_idx, element):
else:
raise IndexError("Tab with index " "{} does not exist".format(tab_idx))

def update_element(self, tab_idx, element, coords, anchor="position"):
@warn_on_args_to_kwargs()
def update_element(self, tab_idx, element, coords, *, anchor="position"):
"""Update element on content panel after checking its existence."""
if tab_idx < self.nb_tabs and tab_idx >= 0:
self.tabs[tab_idx].update_element(element, coords, anchor)
self.tabs[tab_idx].update_element(element, coords, anchor=anchor)
else:
raise IndexError("Tab with index " "{} does not exist".format(tab_idx))

Expand Down Expand Up @@ -886,7 +899,8 @@ class ImageContainer2D(UI):

"""

def __init__(self, img_path, position=(0, 0), size=(100, 100)):
@warn_on_args_to_kwargs()
def __init__(self, img_path, *, position=(0, 0), size=(100, 100)):
"""Init class instance.

Parameters
Expand All @@ -899,7 +913,7 @@ def __init__(self, img_path, position=(0, 0), size=(100, 100)):
Width and height in pixels of the image.

"""
super(ImageContainer2D, self).__init__(position)
super(ImageContainer2D, self).__init__(position=position)
self.img = load_image(img_path, as_vtktype=True)
self.set_img(self.img)
self.resize(size)
Expand Down Expand Up @@ -1028,9 +1042,11 @@ def set_img(self, img):
class GridUI(UI):
"""Add actors in a grid and interact with them individually."""

@warn_on_args_to_kwargs()
def __init__(
self,
actors,
*,
captions=None,
caption_offset=(0, -100, 0),
cell_padding=0,
Expand Down
35 changes: 25 additions & 10 deletions fury/ui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np

from fury.decorators import warn_on_args_to_kwargs
from fury.interactor import CustomInteractorStyle
from fury.io import load_image
from fury.lib import (
Expand Down Expand Up @@ -79,7 +80,8 @@ class UI(object, metaclass=abc.ABCMeta):

"""

def __init__(self, position=(0, 0)):
@warn_on_args_to_kwargs()
def __init__(self, *, position=(0, 0)):
"""Init scene.

Parameters
Expand Down Expand Up @@ -179,7 +181,8 @@ def add_to_scene(self, scene):
if len(callback) > 3:
iren.add_callback(*callback[:3], priority=callback[3], args=[self])

def add_callback(self, prop, event_type, callback, priority=0):
@warn_on_args_to_kwargs()
def add_callback(self, prop, event_type, callback, *, priority=0):
"""Add a callback to a specific event for this UI component.

Parameters
Expand Down Expand Up @@ -353,7 +356,8 @@ def key_press_callback(i_ren, obj, self):
class Rectangle2D(UI):
"""A 2D rectangle sub-classed from UI."""

def __init__(self, size=(0, 0), position=(0, 0), color=(1, 1, 1), opacity=1.0):
@warn_on_args_to_kwargs()
def __init__(self, *, size=(0, 0), position=(0, 0), color=(1, 1, 1), opacity=1.0):
"""Initialize a rectangle.

Parameters
Expand All @@ -368,7 +372,7 @@ def __init__(self, size=(0, 0), position=(0, 0), color=(1, 1, 1), opacity=1.0):
Must take values in [0, 1].

"""
super(Rectangle2D, self).__init__(position)
super(Rectangle2D, self).__init__(position=position)
self.color = color
self.opacity = opacity
self.resize(size)
Expand Down Expand Up @@ -519,8 +523,15 @@ def opacity(self, opacity):
class Disk2D(UI):
"""A 2D disk UI component."""

@warn_on_args_to_kwargs()
def __init__(
self, outer_radius, inner_radius=0, center=(0, 0), color=(1, 1, 1), opacity=1.0
self,
outer_radius,
*,
inner_radius=0,
center=(0, 0),
color=(1, 1, 1),
opacity=1.0,
):
"""Initialize a 2D Disk.

Expand Down Expand Up @@ -693,8 +704,10 @@ class TextBlock2D(UI):

"""

@warn_on_args_to_kwargs()
def __init__(
self,
*,
text="Text Block",
font_size=18,
font_family="Arial",
Expand Down Expand Up @@ -781,7 +794,7 @@ def resize(self, size):
Text bounding box size(width, height) in pixels.

"""
self.update_bounding_box(size)
self.update_bounding_box(size=size)

def _get_actors(self):
"""Get the actors composing this UI component."""
Expand Down Expand Up @@ -1092,7 +1105,7 @@ def auto_font_scale(self, flag):
if flag:
self.actor.SetTextScaleModeToProp()
self._justification = "left"
self.update_bounding_box(self.size)
self.update_bounding_box(size=self.size)
else:
self.actor.SetTextScaleModeToNone()

Expand Down Expand Up @@ -1165,7 +1178,8 @@ def cal_size_from_message(self):
max_length = max(len(line) for line in lines)
return [max_length * self.font_size, len(lines) * self.font_size]

def update_bounding_box(self, size=None):
@warn_on_args_to_kwargs()
def update_bounding_box(self, *, size=None):
"""Update Text Bounding Box.

Parameters
Expand Down Expand Up @@ -1226,7 +1240,8 @@ class Button2D(UI):

"""

def __init__(self, icon_fnames, position=(0, 0), size=(30, 30)):
@warn_on_args_to_kwargs()
def __init__(self, icon_fnames, *, position=(0, 0), size=(30, 30)):
"""Init class instance.

Parameters
Expand All @@ -1239,7 +1254,7 @@ def __init__(self, icon_fnames, position=(0, 0), size=(30, 30)):
Width and height in pixels of the button.

"""
super(Button2D, self).__init__(position)
super(Button2D, self).__init__(position=position)

self.icon_extents = {}
self.icons = self._build_icons(icon_fnames)
Expand Down
Loading
Loading