From a0917231904609ae72f3df7383750ffd784306ea Mon Sep 17 00:00:00 2001 From: WassCodeur Date: Sat, 22 Jun 2024 13:59:12 +0000 Subject: [PATCH] RF: Add keyword arguments to module: UI --- fury/ui/containers.py | 42 ++++++++++++++++++-------- fury/ui/core.py | 29 ++++++++++++------ fury/ui/elements.py | 62 +++++++++++++++++++++++++++++--------- fury/ui/tests/test_core.py | 18 +++++------ 4 files changed, 105 insertions(+), 46 deletions(-) diff --git a/fury/ui/containers.py b/fury/ui/containers.py index ca1606108..3f1d4f0fe 100644 --- a/fury/ui/containers.py +++ b/fury/ui/containers.py @@ -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, @@ -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, @@ -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 @@ -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 @@ -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 @@ -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) @@ -382,8 +387,10 @@ class TabPanel2D(UI): """ + @warn_on_args_to_kwargs() def __init__( self, + *, position=(0, 0), size=(100, 100), title="New Tab", @@ -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 @@ -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. @@ -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 @@ -631,8 +640,10 @@ class TabUI(UI): """ + @warn_on_args_to_kwargs() def __init__( self, + *, position=(0, 0), size=(100, 100), nb_tabs=1, @@ -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: @@ -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)) @@ -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 @@ -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) @@ -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, diff --git a/fury/ui/core.py b/fury/ui/core.py index 6ced09f59..b70755192 100644 --- a/fury/ui/core.py +++ b/fury/ui/core.py @@ -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 ( @@ -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 @@ -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 @@ -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 @@ -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) @@ -519,8 +523,9 @@ 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. @@ -693,8 +698,10 @@ class TextBlock2D(UI): """ + @warn_on_args_to_kwargs() def __init__( self, + *, text="Text Block", font_size=18, font_family="Arial", @@ -781,7 +788,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.""" @@ -1092,7 +1099,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() @@ -1165,7 +1172,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 @@ -1226,7 +1234,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 @@ -1239,7 +1248,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) diff --git a/fury/ui/elements.py b/fury/ui/elements.py index 7bd17d997..2023696e5 100644 --- a/fury/ui/elements.py +++ b/fury/ui/elements.py @@ -30,6 +30,7 @@ import numpy as np from fury.data import read_viz_icons +from fury.decorators import warn_on_args_to_kwargs from fury.lib import Command from fury.ui.containers import ImageContainer2D, Panel2D from fury.ui.core import UI, Button2D, Disk2D, Rectangle2D, TextBlock2D @@ -73,10 +74,12 @@ class TextBox2D(UI): """ + @warn_on_args_to_kwargs() def __init__( self, width, height, + *, text="Enter Text", position=(100, 10), color=(0, 0, 0), @@ -226,7 +229,7 @@ def handle_character(self, key, key_char): """ if key.lower() == "return": - self.render_text(False) + self.render_text(show_caret=False) self.off_focus(self) return True elif key_char != "" and key_char in printable: @@ -338,7 +341,8 @@ def showable_text(self, show_caret): ret_text = ret_text[self.window_left : self.window_right + 1] return ret_text - def render_text(self, show_caret=True): + @warn_on_args_to_kwargs() + def render_text(self, *, show_caret=True): """Render text after processing. Parameters @@ -422,8 +426,10 @@ class LineSlider2D(UI): """ + @warn_on_args_to_kwargs() def __init__( self, + *, center=(0, 0), initial_value=50, min_value=0, @@ -799,8 +805,10 @@ class LineDoubleSlider2D(UI): """ + @warn_on_args_to_kwargs() def __init__( self, + *, line_width=5, inner_radius=0, outer_radius=10, @@ -1388,8 +1396,10 @@ class RingSlider2D(UI): """ + @warn_on_args_to_kwargs() def __init__( self, + *, center=(0, 0), initial_value=180, min_value=0, @@ -1664,8 +1674,10 @@ class RangeSlider(UI): """ + @warn_on_args_to_kwargs() def __init__( self, + *, line_width=5, inner_radius=0, outer_radius=10, @@ -1837,7 +1849,8 @@ class Option(UI): """ - def __init__(self, label, position=(0, 0), font_size=18, checked=False): + @warn_on_args_to_kwargs() + def __init__(self, label, *, position=(0, 0), font_size=18, checked=False): """Init this class instance. Parameters @@ -1858,7 +1871,7 @@ def __init__(self, label, position=(0, 0), font_size=18, checked=False): self.checked = checked self.button_size = (font_size * 1.2, font_size * 1.2) self.button_label_gap = 10 - super(Option, self).__init__(position) + super(Option, self).__init__(position=position) # Offer some standard hooks to the user. self.on_change = lambda obj: None @@ -1948,9 +1961,11 @@ class Checkbox(UI): """ + @warn_on_args_to_kwargs() def __init__( self, labels, + *, checked_labels=(), padding=1, font_size=18, @@ -1981,7 +1996,7 @@ def __init__( self._font_size = font_size self.font_family = font_family self.checked_labels = list(checked_labels) - super(Checkbox, self).__init__(position) + super(Checkbox, self).__init__(position=position) self.on_change = lambda checkbox: None def _setup(self): @@ -2092,10 +2107,12 @@ class RadioButton(Checkbox): """ + @warn_on_args_to_kwargs() def __init__( self, labels, checked_labels, + *, padding=1, font_size=18, font_family="Arial", @@ -2156,8 +2173,10 @@ class ComboBox2D(UI): """ + @warn_on_args_to_kwargs() def __init__( self, + *, items=None, position=(0, 0), size=(300, 200), @@ -2495,9 +2514,11 @@ class ListBox2D(UI): """ + @warn_on_args_to_kwargs() def __init__( self, values, + *, position=(0, 0), size=(100, 300), multiselection=True, @@ -2863,7 +2884,8 @@ def update_scrollbar(self): def clear_selection(self): del self.selected[:] - def select(self, item, multiselect=False, range_select=False): + @warn_on_args_to_kwargs() + def select(self, item, *, multiselect=False, range_select=False): """Select the item. Parameters @@ -2908,10 +2930,12 @@ def select(self, item, multiselect=False, range_select=False): class ListBoxItem2D(UI): """The text displayed in a listbox.""" + @warn_on_args_to_kwargs() def __init__( self, list_box, size, + *, text_color=(1.0, 0.0, 0.0), selected_color=(0.4, 0.4, 0.4), unselected_color=(0.9, 0.9, 0.9), @@ -3030,7 +3054,7 @@ def left_button_clicked(self, i_ren, _obj, _list_box_item): """ multiselect = i_ren.event.ctrl_key range_select = i_ren.event.shift_key - self.list_box.select(self, multiselect, range_select) + self.list_box.select(item=self, multiselect=multiselect, range_select=range_select) i_ren.force_render() def resize(self, size): @@ -3052,9 +3076,11 @@ class FileMenu2D(UI): """ + @warn_on_args_to_kwargs() def __init__( self, directory_path, + *, extensions=None, position=(0, 0), size=(100, 300), @@ -3306,6 +3332,7 @@ def directory_click_callback(self, i_ren, _obj, listboxitem): class DrawShape(UI): """Create and Manage 2D Shapes.""" + @warn_on_args_to_kwargs() def __init__(self, shape_type, drawpanel=None, position=(0, 0)): """Init this UI element. @@ -3324,7 +3351,7 @@ def __init__(self, shape_type, drawpanel=None, position=(0, 0)): self.drawpanel = drawpanel self.max_size = None self.rotation = 0 - super(DrawShape, self).__init__(position) + super(DrawShape, self).__init__(position=position) self.shape.color = np.random.random(3) def _setup(self): @@ -3387,7 +3414,7 @@ def update_shape_position(self, center_position): """ new_center = self.clamp_position(center=center_position) - self.drawpanel.canvas.update_element(self, new_center, "center") + self.drawpanel.canvas.update_element(self, new_center, anchor="center") self.cal_bounding_box() @property @@ -3456,7 +3483,8 @@ def cal_bounding_box(self): self._bounding_box_offset = self.position - self._bounding_box_min - def clamp_position(self, center=None): + @warn_on_args_to_kwargs() + def clamp_position(self, *, center=None): """Clamp the given center according to the DrawPanel canvas. Parameters @@ -3538,6 +3566,7 @@ def left_button_released(self, i_ren, _obj, shape): class DrawPanel(UI): """The main Canvas(Panel2D) on which everything would be drawn.""" + @warn_on_args_to_kwargs() def __init__(self, size=(400, 400), position=(0, 0), is_draggable=False): """Init this UI element. @@ -3552,7 +3581,7 @@ def __init__(self, size=(400, 400), position=(0, 0), is_draggable=False): """ self.panel_size = size - super(DrawPanel, self).__init__(position) + super(DrawPanel, self).__init__(position=position) self.is_draggable = is_draggable self.current_mode = None @@ -3816,11 +3845,12 @@ class PlaybackPanel(UI): such as play, pause, stop, and seek. """ - def __init__(self, loop=False, position=(0, 0), width=None): + @warn_on_args_to_kwargs() + def __init__(self, *, loop=False, position=(0, 0), width=None): self._width = width if width is not None else 900 self._auto_width = width is None self._position = position - super(PlaybackPanel, self).__init__(position) + super(PlaybackPanel, self).__init__(position=position) self._playing = False self._loop = None self.loop() if loop else self.play_once() @@ -4185,9 +4215,11 @@ class Card2D(UI): """ + @warn_on_args_to_kwargs() def __init__( self, image_path, + *, body_text="", draggable=True, title_text="", @@ -4452,8 +4484,10 @@ def left_button_dragged(self, i_ren, _obj, _sub_component): class SpinBox(UI): """SpinBox UI.""" + @warn_on_args_to_kwargs() def __init__( self, + *, position=(350, 400), size=(300, 100), padding=10, @@ -4501,7 +4535,7 @@ def __init__( self.max_column = max_column self.max_line = max_line - super(SpinBox, self).__init__(position) + super(SpinBox, self).__init__(position=position) self.value = initial_val self.resize(size) diff --git a/fury/ui/tests/test_core.py b/fury/ui/tests/test_core.py index 5e76a1cfe..0377d3385 100644 --- a/fury/ui/tests/test_core.py +++ b/fury/ui/tests/test_core.py @@ -276,7 +276,7 @@ def test_text_block_2d_justification(): texts = [] texts += [ ui.TextBlock2D( - "HH", + text="HH", position=(left, top), font_size=font_size, color=(1, 0, 0), @@ -287,7 +287,7 @@ def test_text_block_2d_justification(): ] texts += [ ui.TextBlock2D( - "HH", + text="HH", position=(center, top), font_size=font_size, color=(0, 1, 0), @@ -298,7 +298,7 @@ def test_text_block_2d_justification(): ] texts += [ ui.TextBlock2D( - "HH", + text="HH", position=(right, top), font_size=font_size, color=(0, 0, 1), @@ -310,7 +310,7 @@ def test_text_block_2d_justification(): texts += [ ui.TextBlock2D( - "HH", + text="HH", position=(left, middle), font_size=font_size, color=(1, 1, 0), @@ -321,7 +321,7 @@ def test_text_block_2d_justification(): ] texts += [ ui.TextBlock2D( - "HH", + text="HH", position=(center, middle), font_size=font_size, color=(0, 1, 1), @@ -332,7 +332,7 @@ def test_text_block_2d_justification(): ] texts += [ ui.TextBlock2D( - "HH", + text="HH", position=(right, middle), font_size=font_size, color=(1, 0, 1), @@ -344,7 +344,7 @@ def test_text_block_2d_justification(): texts += [ ui.TextBlock2D( - "HH", + text="HH", position=(left, bottom), font_size=font_size, color=(0.5, 0, 1), @@ -355,7 +355,7 @@ def test_text_block_2d_justification(): ] texts += [ ui.TextBlock2D( - "HH", + text="HH", position=(center, bottom), font_size=font_size, color=(1, 0.5, 0), @@ -366,7 +366,7 @@ def test_text_block_2d_justification(): ] texts += [ ui.TextBlock2D( - "HH", + text="HH", position=(right, bottom), font_size=font_size, color=(0, 1, 0.5),