From 96b3376a7a8ac22445602fe2b0b89527ab6ebcf5 Mon Sep 17 00:00:00 2001 From: amalss18 Date: Sun, 1 Sep 2019 15:57:20 +0530 Subject: [PATCH 1/9] added basic functions for the slider widget --- mayavi/mlab.py | 2 + mayavi/tools/widgets.py | 92 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 mayavi/tools/widgets.py diff --git a/mayavi/mlab.py b/mayavi/mlab.py index 43e99b4d6..630e1183e 100644 --- a/mayavi/mlab.py +++ b/mayavi/mlab.py @@ -58,6 +58,8 @@ def show_engine(): from .tools.notebook import init as init_notebook +from .tools.widgets import slider_widget + if __name__ == "__main__": import numpy as np diff --git a/mayavi/tools/widgets.py b/mayavi/tools/widgets.py new file mode 100644 index 000000000..c80dbbaeb --- /dev/null +++ b/mayavi/tools/widgets.py @@ -0,0 +1,92 @@ +from tvtk.api import tvtk +from .tools import gcf + + +class SliderWidget(): + def __init__(self): + self.slider_rep = tvtk.SliderRepresentation2D() + self.slider_widget = tvtk.SliderWidget() + self.slider() + self.title() + self.set_values() + self.cap() + self.tube() + self.position() + + def edit_slider_rep(self): + self.slider_rep.edit_traits() + + def set_values(self, min_value=0, max_value=1, value=0.1): + self.slider_rep.set(minimum_value=min_value) + self.slider_rep.set(maximum_value=max_value) + self.slider_rep.set(value=value) + + def return_value(self): + return self.slider_widget._get_representation().value + + def edit_title(self): + self.slider_rep._get_title_property().edit_traits() + + def title(self, title_text="Slider", color=(1, 1, 1)): + self.slider_rep.set(title_text=title_text) + self.slider_rep._get_title_property().set(color=color) + + def edit_slider(self): + self.slider_rep._get_slider_property().edit_traits() + + def edit_slider_select(self): + self.slider_rep._get_selected_property().edit_traits() + + def slider(self, color=(0.3, 0.2, 0.9), selected_color=(1, 0, 0), opacity=1): + self.slider_rep._get_slider_property().set(color=color) + self.slider_rep._get_selected_property().set(color=selected_color) + self.slider_rep._get_selected_property().set(opacity=opacity) + + def edit_label(self): + self.slider_rep._get_label_property().edit_traits() + + def label(self, label=(1, 1, 1), opacity=1): + self.slider_rep._get_label_property().set(color=label) + self.slider_rep._get_label_property().opacity = opacity + + def edit_cap(self): + self.slider_rep._get_cap_property().edit_traits() + + def cap(self, color=(1, 1, 1), opacity=0): + self.slider_rep._get_cap_property().set(color=color) + self.slider_rep._get_cap_property().opacity = opacity + + def edit_tube(self): + self.slider_rep._get_tube_property().edit_traits() + + def tube(self, color=(0, 0, 0), opacity=1): + self.slider_rep._get_tube_property().set(color=color) + self.slider_rep._get_tube_property().opacity = opacity + + def position(self, point1=(0.8, 0.9, 0), point2=(1, 0.9, 0)): + self.slider_rep._get_point1_coordinate().coordinate_system = "normalized_display" + self.slider_rep._get_point1_coordinate().set(value=point1) + self.slider_rep._get_point2_coordinate().coordinate_system = "normalized_display" + self.slider_rep._get_point2_coordinate().set(value=point2) + + def _slider_setup(self, figure): + self.slider_widget.set(interactor=figure().scene.interactor) + self.slider_widget.set(representation=self.slider_rep) + self.slider_rep._get_title_property().shadow = 0 + self.slider_rep._get_label_property().shadow = 0 + self.slider_widget.animation_mode = "animate" + self.slider_widget.enabled = 1 + + def callback(self, callback): + self.slider_widget.add_observer("InteractionEvent", callback) + + +def empty_call(event, observer): + pass + + +def slider_widget(figure=gcf, callback=empty_call): + slider = SliderWidget() + slider._slider_setup(figure) + slider.callback(callback) + return slider From 8d0773809f38ebebdc2e779eef7a1c5604eb0846 Mon Sep 17 00:00:00 2001 From: amalss18 Date: Mon, 2 Sep 2019 18:52:14 +0530 Subject: [PATCH 2/9] major changes --- mayavi/tools/widgets.py | 184 +++++++++++++++++++++++++++------------- 1 file changed, 126 insertions(+), 58 deletions(-) diff --git a/mayavi/tools/widgets.py b/mayavi/tools/widgets.py index c80dbbaeb..8ce42fb17 100644 --- a/mayavi/tools/widgets.py +++ b/mayavi/tools/widgets.py @@ -2,89 +2,157 @@ from .tools import gcf -class SliderWidget(): +class SliderWidget: + + slider_rep = tvtk.SliderRepresentation2D() + slider_widget = tvtk.SliderWidget() + def __init__(self): - self.slider_rep = tvtk.SliderRepresentation2D() - self.slider_widget = tvtk.SliderWidget() - self.slider() - self.title() - self.set_values() - self.cap() - self.tube() - self.position() + self.value = Values() + self.title = Title() + self.slider = Slider() + self.label = Label() + self.cap = Cap() + self.tube = Tube() + self.position = Position() + self.slider_setup() def edit_slider_rep(self): self.slider_rep.edit_traits() - def set_values(self, min_value=0, max_value=1, value=0.1): - self.slider_rep.set(minimum_value=min_value) - self.slider_rep.set(maximum_value=max_value) - self.slider_rep.set(value=value) - - def return_value(self): + def get_value(self): return self.slider_widget._get_representation().value - def edit_title(self): - self.slider_rep._get_title_property().edit_traits() + def position(self, point1=(0.8, 0.9, 0), point2=(1, 0.9, 0)): + self.slider_rep._get_point1_coordinate().coordinate_system = "normalized_display" + self.slider_rep._get_point2_coordinate().coordinate_system = "normalized_display" + + def _slider_setup(self, figure): + self.slider_widget.set(interactor=figure().scene.interactor) + self.slider_widget.set(representation=self.slider_rep) + self.slider_widget.animation_mode = "animate" + self.slider_widget.enabled = 1 + + def callback(self, callback): + self.slider_widget.add_observer("InteractionEvent", callback) + - def title(self, title_text="Slider", color=(1, 1, 1)): - self.slider_rep.set(title_text=title_text) - self.slider_rep._get_title_property().set(color=color) +class Values(SliderWidget): + def __init__(self): + self.minimum_value(0) + self.maximum_value(1) + self.value(0.1) - def edit_slider(self): - self.slider_rep._get_slider_property().edit_traits() + def minimum_value(self, min_val): + SliderWidget.slider_rep.minimum_value = min_val - def edit_slider_select(self): - self.slider_rep._get_selected_property().edit_traits() + def maximum_value(self, max_val): + SliderWidget.slider_rep.maximum_value = max_val - def slider(self, color=(0.3, 0.2, 0.9), selected_color=(1, 0, 0), opacity=1): - self.slider_rep._get_slider_property().set(color=color) - self.slider_rep._get_selected_property().set(color=selected_color) - self.slider_rep._get_selected_property().set(opacity=opacity) + def value(self,value): + SliderWidget.slider_rep.value = value - def edit_label(self): - self.slider_rep._get_label_property().edit_traits() + def edit(self): + self.slider_rep.edit_traits()\ - def label(self, label=(1, 1, 1), opacity=1): - self.slider_rep._get_label_property().set(color=label) - self.slider_rep._get_label_property().opacity = opacity - def edit_cap(self): - self.slider_rep._get_cap_property().edit_traits() +class Title(SliderWidget): + def __init__(self): + self.color((1,1,1)) + self.title_text("Slider") + SliderWidget.slider_rep._get_title_property().shadow = 0 - def cap(self, color=(1, 1, 1), opacity=0): - self.slider_rep._get_cap_property().set(color=color) - self.slider_rep._get_cap_property().opacity = opacity + def color(self, color): + SliderWidget.slider_rep._get_title_property().color = color - def edit_tube(self): - self.slider_rep._get_tube_property().edit_traits() + def title_text(self, text): + SliderWidget.slider_rep.title_text = text - def tube(self, color=(0, 0, 0), opacity=1): - self.slider_rep._get_tube_property().set(color=color) - self.slider_rep._get_tube_property().opacity = opacity + def edit(self): + SliderWidget.slider_rep._get_title_property().edit_traits() - def position(self, point1=(0.8, 0.9, 0), point2=(1, 0.9, 0)): - self.slider_rep._get_point1_coordinate().coordinate_system = "normalized_display" - self.slider_rep._get_point1_coordinate().set(value=point1) - self.slider_rep._get_point2_coordinate().coordinate_system = "normalized_display" - self.slider_rep._get_point2_coordinate().set(value=point2) - def _slider_setup(self, figure): - self.slider_widget.set(interactor=figure().scene.interactor) - self.slider_widget.set(representation=self.slider_rep) - self.slider_rep._get_title_property().shadow = 0 - self.slider_rep._get_label_property().shadow = 0 - self.slider_widget.animation_mode = "animate" - self.slider_widget.enabled = 1 +class Slider(SliderWidget): + def __init__(self): + self.color((0.3,0.2,0.9)) + self.selected_color((1,0,0)) - def callback(self, callback): - self.slider_widget.add_observer("InteractionEvent", callback) + def color(self, color): + SliderWidget.slider_rep._get_slider_property().color = color + + def selected_color(self, color): + SliderWidget.slider_rep._get_selected_property().color = color + + def edit(self): + SliderWidget.slider_rep._get_slider_property().edit_traits() + + def selected_edit(self): + SliderWidget.slider_rep._get_selected_property().edit_traits() + + +class Label(SliderWidget): + def __init__(self): + self.color((1,1,1)) + self.opacity(1) + SliderWidget.slider_rep._get_label_property().shadow = 0 + + def color(self,color): + SliderWidget.slider_rep._get_label_property().color = color + + def opacity(self, opacity): + SliderWidget.slider_rep._get_label_property().opacity = opacity + + def edit(self): + SliderWidget.slider_rep._get_label_property().edit_traits() + + +class Cab(SliderWidget): + def __init__(self): + self.color((1,1,1)) + self.opacity(0) + + def color(self, color): + SliderWidget.slider_rep._get_cap_property().color = color + + def opacity(self, opacity); + SliderWidget.slider_rep._get_cap_property().opacity = opacity + + def edit(self): + SliderWidget.slider_rep._get_cap_property().edit_traits() + + +class Tube(SliderWidget): + def __init__(self): + self.color((0,0,0)) + self.opacity(1) + + def color(self, color): + SliderWidget.slider_rep._get_tube_property().color = color + + def opacity(self, color): + SliderWidget.slider_rep._get_tube_property().opacity = opacity + + def edit(self): + SliderWidget.slider_rep._get_tube_property().edit_traits() + + +class Position(SliderWidget): + def __init__(self): + self.position((0.8, 0.9, 0)) + self.position2((1, 0.9, 0)) + SliderWidget.slider_rep._get_point1_coordinate().coordinate_system = "normalized_display" + SliderWidget.slider_rep._get_point2_coordinate().coordinate_system = "normalized_display" + + def position(self, pos): + SliderWidget.slider_rep._get_point1_coordinate().value = pos + + def position2(self, pos): + SliderWidget.slider_rep._get_point2_coordinate().value = pos def empty_call(event, observer): pass - def slider_widget(figure=gcf, callback=empty_call): slider = SliderWidget() slider._slider_setup(figure) From e2a261acc1e278bdc8045c47f51254e62aa89956 Mon Sep 17 00:00:00 2001 From: amalss18 Date: Fri, 6 Sep 2019 22:34:49 +0530 Subject: [PATCH 3/9] major refactoring --- mayavi/tools/widgets.py | 181 +++++++++++----------------------------- 1 file changed, 49 insertions(+), 132 deletions(-) diff --git a/mayavi/tools/widgets.py b/mayavi/tools/widgets.py index 8ce42fb17..00313d30d 100644 --- a/mayavi/tools/widgets.py +++ b/mayavi/tools/widgets.py @@ -6,155 +6,72 @@ class SliderWidget: slider_rep = tvtk.SliderRepresentation2D() slider_widget = tvtk.SliderWidget() + title = slider_rep._get_title_property() + label = slider_rep._get_label_property() + slider = slider_rep._get_slider_property() + cap = slider_rep._get_cap_property() + tube = slider_rep._get_tube_property() + point1 = slider_rep._get_point1_coordinate() + point2 = slider_rep._get_point2_coordinate() def __init__(self): - self.value = Values() - self.title = Title() - self.slider = Slider() - self.label = Label() - self.cap = Cap() - self.tube = Tube() - self.position = Position() - self.slider_setup() - - def edit_slider_rep(self): - self.slider_rep.edit_traits() - - def get_value(self): + self.value_setup(0, 1) + self.title_setup("Slider", (0,0,0)) + self.slider_setup((0.3,0.2,0.9), (1,0,0)) + self.label_setup((1,1,1)) + self.cap_setup((0,0,0), 0) + self.tube_setup((0,0,0), 1) + self.position_setup((0.8,0.9,0), (1,0.9,0)) + + def return_value(self): return self.slider_widget._get_representation().value - def position(self, point1=(0.8, 0.9, 0), point2=(1, 0.9, 0)): - self.slider_rep._get_point1_coordinate().coordinate_system = "normalized_display" - self.slider_rep._get_point2_coordinate().coordinate_system = "normalized_display" + def position_setup(self, point1, point2): + self.point1.coordinate_system = "normalized_display" + self.point1.value = point1 + self.point2.coordinate_system = "normalized_display" + self.point2.value = point2 - def _slider_setup(self, figure): - self.slider_widget.set(interactor=figure().scene.interactor) - self.slider_widget.set(representation=self.slider_rep) - self.slider_widget.animation_mode = "animate" - self.slider_widget.enabled = 1 - - def callback(self, callback): - self.slider_widget.add_observer("InteractionEvent", callback) + def value_setup(self, min_val, max_val): + self.slider_rep.minimum_value = min_val + self.slider_rep.maximum_value = max_val + self.slider_rep.value = min_val + def title_setup(self, title_text, color): + self.title.title_text = title_text + self.title.color = color + self.title.shadow = 0 -class Values(SliderWidget): - def __init__(self): - self.minimum_value(0) - self.maximum_value(1) - self.value(0.1) + def slider_setup(self, color1, color2): + self.slider.color = color1 + self.slider_rep._get_selected_property().color = color2 - def minimum_value(self, min_val): - SliderWidget.slider_rep.minimum_value = min_val - - def maximum_value(self, max_val): - SliderWidget.slider_rep.maximum_value = max_val - - def value(self,value): - SliderWidget.slider_rep.value = value - - def edit(self): - self.slider_rep.edit_traits()\ - - -class Title(SliderWidget): - def __init__(self): - self.color((1,1,1)) - self.title_text("Slider") - SliderWidget.slider_rep._get_title_property().shadow = 0 + def cap_setup(self, color, opacity): + self.cap.color = color + self.cap.opacity = opacity - def color(self, color): - SliderWidget.slider_rep._get_title_property().color = color - - def title_text(self, text): - SliderWidget.slider_rep.title_text = text - - def edit(self): - SliderWidget.slider_rep._get_title_property().edit_traits() - - -class Slider(SliderWidget): - def __init__(self): - self.color((0.3,0.2,0.9)) - self.selected_color((1,0,0)) - - def color(self, color): - SliderWidget.slider_rep._get_slider_property().color = color - - def selected_color(self, color): - SliderWidget.slider_rep._get_selected_property().color = color - - def edit(self): - SliderWidget.slider_rep._get_slider_property().edit_traits() - - def selected_edit(self): - SliderWidget.slider_rep._get_selected_property().edit_traits() - - -class Label(SliderWidget): - def __init__(self): - self.color((1,1,1)) - self.opacity(1) - SliderWidget.slider_rep._get_label_property().shadow = 0 + def tube_setup(self, color, opacity): + self.tube.color = color + self.tube.opacity = opacity - def color(self,color): - SliderWidget.slider_rep._get_label_property().color = color + def label_setup(self, color): + self.label.color = color + self.label.shadow = 0 - def opacity(self, opacity): - SliderWidget.slider_rep._get_label_property().opacity = opacity - - def edit(self): - SliderWidget.slider_rep._get_label_property().edit_traits() - - -class Cab(SliderWidget): - def __init__(self): - self.color((1,1,1)) - self.opacity(0) - - def color(self, color): - SliderWidget.slider_rep._get_cap_property().color = color - - def opacity(self, opacity); - SliderWidget.slider_rep._get_cap_property().opacity = opacity - - def edit(self): - SliderWidget.slider_rep._get_cap_property().edit_traits() - - -class Tube(SliderWidget): - def __init__(self): - self.color((0,0,0)) - self.opacity(1) - - def color(self, color): - SliderWidget.slider_rep._get_tube_property().color = color - - def opacity(self, color): - SliderWidget.slider_rep._get_tube_property().opacity = opacity - - def edit(self): - SliderWidget.slider_rep._get_tube_property().edit_traits() - - -class Position(SliderWidget): - def __init__(self): - self.position((0.8, 0.9, 0)) - self.position2((1, 0.9, 0)) - SliderWidget.slider_rep._get_point1_coordinate().coordinate_system = "normalized_display" - SliderWidget.slider_rep._get_point2_coordinate().coordinate_system = "normalized_display" - - def position(self, pos): - SliderWidget.slider_rep._get_point1_coordinate().value = pos - - def position2(self, pos): - SliderWidget.slider_rep._get_point2_coordinate().value = pos + def _widget_setup(self, figure): + self.slider_widget.set(interactor=figure().scene.interactor) + self.slider_widget.set(representation=self.slider_rep) + self.slider_widget.animation_mode = "animate" + self.slider_widget.enabled = 1 + def callback(self, callback): + self.slider_widget.add_observer("InteractionEvent", callback) def empty_call(event, observer): pass def slider_widget(figure=gcf, callback=empty_call): slider = SliderWidget() - slider._slider_setup(figure) + slider._widget_setup(figure) slider.callback(callback) return slider From 30cfac2431132132618f30dbcb9e977223db03f4 Mon Sep 17 00:00:00 2001 From: amalss18 Date: Mon, 9 Sep 2019 23:01:43 +0530 Subject: [PATCH 4/9] added button widget --- mayavi/mlab.py | 2 +- mayavi/tools/widgets.py | 54 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/mayavi/mlab.py b/mayavi/mlab.py index 630e1183e..3d4d3755e 100644 --- a/mayavi/mlab.py +++ b/mayavi/mlab.py @@ -58,7 +58,7 @@ def show_engine(): from .tools.notebook import init as init_notebook -from .tools.widgets import slider_widget +from .tools.widgets import slider_widget, button_widget if __name__ == "__main__": import numpy as np diff --git a/mayavi/tools/widgets.py b/mayavi/tools/widgets.py index 00313d30d..54e799e6c 100644 --- a/mayavi/tools/widgets.py +++ b/mayavi/tools/widgets.py @@ -38,7 +38,7 @@ def value_setup(self, min_val, max_val): self.slider_rep.value = min_val def title_setup(self, title_text, color): - self.title.title_text = title_text + self.slider_rep.title_text = title_text self.title.color = color self.title.shadow = 0 @@ -67,11 +67,55 @@ def _widget_setup(self, figure): def callback(self, callback): self.slider_widget.add_observer("InteractionEvent", callback) -def empty_call(event, observer): - pass -def slider_widget(figure=gcf, callback=empty_call): +class ButtonWidget: + + button_rep = tvtk.TexturedButtonRepresentation2D() + button_widget = tvtk.ButtonWidget() + + def __init__(self): + self.button_rep.set_number_of_states(1) + self.add_text("Button", (1,1,0), 25) + self.place_widget((-70,70,-70,70,0,0)) + + def add_image(self, location): + reader = tvtk.JPEGReader() + if location[-3:] == "png": + reader = tvtk.PNGReader() + reader.set(file_name = location) + reader.update() + image = tvtk.ImageData() + image = reader._get_output() + self.button_rep.set_button_texture(0, image) + + + def add_text(self, text, color, font_size): + free_type = tvtk.FreeTypeStringToImage() + text_property = tvtk.TextProperty() + text_property.color = color + text_property.font_size = font_size + text_image = tvtk.ImageData() + free_type.render_string(text_property, text, 0, text_image) + self.button_rep.set_button_texture(0, text_image) + + def place_widget(self, a): + self.button_rep.place_widget(a) + + def _widget_setup(self, figure): + self.button_widget.set(interactor=figure().scene.interactor) + self.button_widget.set(representation=self.button_rep) + self.button_widget.enabled = 1 + + def callback(self, callback): + self.button_widget.add_observer("StateChangedEvent", callback) + + +def slider_widget(figure=gcf): slider = SliderWidget() slider._widget_setup(figure) - slider.callback(callback) return slider + +def button_widget(figure=gcf): + button = ButtonWidget() + button._widget_setup(figure) + return button From eb5dcbcde35a400ac54560ce4611c5ce90add391 Mon Sep 17 00:00:00 2001 From: amalss18 Date: Mon, 23 Sep 2019 18:26:35 +0530 Subject: [PATCH 5/9] minor chane --- mayavi/tools/widgets.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/mayavi/tools/widgets.py b/mayavi/tools/widgets.py index 54e799e6c..8dd1aa0a1 100644 --- a/mayavi/tools/widgets.py +++ b/mayavi/tools/widgets.py @@ -64,7 +64,7 @@ def _widget_setup(self, figure): self.slider_widget.animation_mode = "animate" self.slider_widget.enabled = 1 - def callback(self, callback): + def add_callback(self, callback): self.slider_widget.add_observer("InteractionEvent", callback) @@ -79,9 +79,18 @@ def __init__(self): self.place_widget((-70,70,-70,70,0,0)) def add_image(self, location): - reader = tvtk.JPEGReader() - if location[-3:] == "png": - reader = tvtk.PNGReader() + d = {'bmp':tvtk.BMPReader(), + 'jpg':tvtk.JPEGReader(), + 'png':tvtk.PNGReader(), + 'pnm':tvtk.PNMReader(), + 'dcm':tvtk.DICOMImageReader(), + 'tiff':tvtk.TIFFReader(), + 'ximg':tvtk.GESignaReader(), + 'dem':tvtk.DEMReader(), + 'mha':tvtk.MetaImageReader(), + 'mhd':tvtk.MetaImageReader(), + } + reader = d[location[-3:]] reader.set(file_name = location) reader.update() image = tvtk.ImageData() @@ -106,7 +115,7 @@ def _widget_setup(self, figure): self.button_widget.set(representation=self.button_rep) self.button_widget.enabled = 1 - def callback(self, callback): + def add_callback(self, callback): self.button_widget.add_observer("StateChangedEvent", callback) From 6295c7a0e484c52dcad9dbf88661c72b941ff57b Mon Sep 17 00:00:00 2001 From: amalss18 Date: Sun, 20 Oct 2019 10:46:40 +0530 Subject: [PATCH 6/9] added new widget_source --- mayavi/sources/widget_source.py | 51 ++++++++++++++++++++++++++ mayavi/tools/widgets.py | 64 +++++++++++++++++++++++++++++---- 2 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 mayavi/sources/widget_source.py diff --git a/mayavi/sources/widget_source.py b/mayavi/sources/widget_source.py new file mode 100644 index 000000000..8a3e564f9 --- /dev/null +++ b/mayavi/sources/widget_source.py @@ -0,0 +1,51 @@ +from mayavi import mlab +from mayavi.core.source import Source + +class WidgetSource(Source): + ############################################## + #'Pipeline Interface'# + ############################################## + def add_actors(self): + """Adds `self.widgets` to the scene. + """ + if not self._actors_added: + self._update() + self._actors_added = True + if not self.visible: + self._visible_changed(self.visible) + self.scene.render() + + def remove_actors(self): + """Removes `self.widgets` from the scene. + """ + if self._actors_added: + self.scene.remove_widgets(self.widgets) + self._actors_added = False + self.scene.render() + + ###################################################################### + # Non-public interface + ###################################################################### + + def _update(self): + if self.scene is None: + return + + def _scene_changed(self, old, new): + if self._actors_added: + old.remove_wdigets(self.widgets) + self._update() + + def _actors_changed(self, old, new): + if self._actors_added: + self.scene.remove_widgets(old) + # The widgets are added automatically when the importer + # does a read. + self.scene.render() + + def _actors_items_changed(self, list_event): + if self._actors_added: + self.scene.remove_widgets(list_event.removed) + # The widgets are added automatically when the importer + # does a read. + self.scene.render() diff --git a/mayavi/tools/widgets.py b/mayavi/tools/widgets.py index 8dd1aa0a1..024bc74f2 100644 --- a/mayavi/tools/widgets.py +++ b/mayavi/tools/widgets.py @@ -1,8 +1,12 @@ from tvtk.api import tvtk from .tools import gcf +from mayavi.sources.widget_source import WidgetSource +from mayavi import mlab +from traits.api import RGBColor, Str, on_trait_change +from traitsui.api import Group, View, Item +class SliderWidget(WidgetSource): -class SliderWidget: slider_rep = tvtk.SliderRepresentation2D() slider_widget = tvtk.SliderWidget() @@ -14,9 +18,18 @@ class SliderWidget: point1 = slider_rep._get_point1_coordinate() point2 = slider_rep._get_point2_coordinate() + title_color = RGBColor((0,0,0)) + title_text = Str("Slider") + + traits_view = View(Group(Item(name = "title_color"), + Item(name = "title_text"), + label = "Title", + )) + def __init__(self): + self.value_setup(0, 1) - self.title_setup("Slider", (0,0,0)) + self.title_config() self.slider_setup((0.3,0.2,0.9), (1,0,0)) self.label_setup((1,1,1)) self.cap_setup((0,0,0), 0) @@ -37,9 +50,12 @@ def value_setup(self, min_val, max_val): self.slider_rep.maximum_value = max_val self.slider_rep.value = min_val - def title_setup(self, title_text, color): - self.slider_rep.title_text = title_text - self.title.color = color + @on_trait_change("+title") + def title_config(self): + self.title.color = self.title_color + print(self.title_color) + self.slider_rep.title_text = self.title_text + print("shizz") self.title.shadow = 0 def slider_setup(self, color1, color2): @@ -67,11 +83,25 @@ def _widget_setup(self, figure): def add_callback(self, callback): self.slider_widget.add_observer("InteractionEvent", callback) + def _update(self): + if self.scene is None: + return + self.widgets = [self.slider_widget] + + def _visible_changed(self, value): + if value: + if not self._actors_added: + self.scene.add_widgets(self.widgets) + self._actors_added = True + super(SliderWidget, self)._visible_changed(value) + + -class ButtonWidget: +class ButtonWidget(WidgetSource): button_rep = tvtk.TexturedButtonRepresentation2D() button_widget = tvtk.ButtonWidget() + text_image = tvtk.ImageData() def __init__(self): self.button_rep.set_number_of_states(1) @@ -118,13 +148,35 @@ def _widget_setup(self, figure): def add_callback(self, callback): self.button_widget.add_observer("StateChangedEvent", callback) + def _update(self): + if self.scene is None: + return + self.widgets = [self.button_widget] + + def _visible_changed(self, value): + if value: + if not self._actors_added: + self.scene.add_widgets(self.widgets) + self._actors_added = True + super(ButtonWidget, self)._visible_changed(value) + + def remove_actors(self): + """Removes `self.widgets` from the scene. + """ + if self._actors_added: + self.scene.remove_widgets(self.widgets) + self._actors_added = False + self.button_rep.set_button_texture(0, self.text_image) + self.scene.render() def slider_widget(figure=gcf): slider = SliderWidget() slider._widget_setup(figure) + mlab.get_engine().add_source(slider) return slider def button_widget(figure=gcf): button = ButtonWidget() button._widget_setup(figure) + mlab.get_engine().add_source(button) return button From b8273f347fabcc48dc420f78dd23291589dd28ed Mon Sep 17 00:00:00 2001 From: amalss18 Date: Thu, 24 Oct 2019 23:44:14 +0530 Subject: [PATCH 7/9] added traits objects --- mayavi/sources/widget_source.py | 3 +- mayavi/tools/widgets.py | 230 ++++++++++++++++++++++---------- 2 files changed, 159 insertions(+), 74 deletions(-) diff --git a/mayavi/sources/widget_source.py b/mayavi/sources/widget_source.py index 8a3e564f9..55a5ca0e5 100644 --- a/mayavi/sources/widget_source.py +++ b/mayavi/sources/widget_source.py @@ -1,9 +1,10 @@ from mayavi import mlab from mayavi.core.source import Source + class WidgetSource(Source): ############################################## - #'Pipeline Interface'# + # 'Pipeline Interface'# ############################################## def add_actors(self): """Adds `self.widgets` to the scene. diff --git a/mayavi/tools/widgets.py b/mayavi/tools/widgets.py index 024bc74f2..0bd703f7d 100644 --- a/mayavi/tools/widgets.py +++ b/mayavi/tools/widgets.py @@ -2,11 +2,30 @@ from .tools import gcf from mayavi.sources.widget_source import WidgetSource from mayavi import mlab -from traits.api import RGBColor, Str, on_trait_change +from traits.api import RGBColor, Str, on_trait_change, Array, Float, Int, File, Bool +from traitsui.api import ArrayEditor from traitsui.api import Group, View, Item + class SliderWidget(WidgetSource): + title_color = RGBColor + title_text = Str + minimum_value = Float + maximum_value = Float + position_1 = Array(dtype="float64", shape=(3, ), editor=ArrayEditor()) + position_2 = Array(dtype="float64", shape=(3, ), editor=ArrayEditor()) + + slider_color = RGBColor + slider_color_2 = RGBColor(desc="color of the slider when it's picked") + + cap_color = RGBColor + cap_opacity = Float + + tube_color = RGBColor + tube_opacity = Float + + label_color = RGBColor slider_rep = tvtk.SliderRepresentation2D() slider_widget = tvtk.SliderWidget() @@ -14,71 +33,89 @@ class SliderWidget(WidgetSource): label = slider_rep._get_label_property() slider = slider_rep._get_slider_property() cap = slider_rep._get_cap_property() - tube = slider_rep._get_tube_property() + tube = slider_rep._get_tube_property() point1 = slider_rep._get_point1_coordinate() point2 = slider_rep._get_point2_coordinate() - title_color = RGBColor((0,0,0)) - title_text = Str("Slider") - - traits_view = View(Group(Item(name = "title_color"), - Item(name = "title_text"), - label = "Title", - )) - - def __init__(self): - - self.value_setup(0, 1) - self.title_config() - self.slider_setup((0.3,0.2,0.9), (1,0,0)) - self.label_setup((1,1,1)) - self.cap_setup((0,0,0), 0) - self.tube_setup((0,0,0), 1) - self.position_setup((0.8,0.9,0), (1,0.9,0)) - - def return_value(self): + traits_view = View(Group(Item(name="title_color"), + Item(name="title_text"), + Item(name="position_1"), + Item(name="position_2"), + Item(name="minimum_value"), + Item(name="maximum_value"), + Item(name="slider_color"), + Item(name="slider_color_2"), + Item(name="cap_color"), + Item(name="cap_opacity"), + Item(name="tube_color"), + Item(name="tube_opacity"), + Item(name="label_color"), + label="Properties") + ) + + def get_value(self): return self.slider_widget._get_representation().value - def position_setup(self, point1, point2): - self.point1.coordinate_system = "normalized_display" - self.point1.value = point1 - self.point2.coordinate_system = "normalized_display" - self.point2.value = point2 + @on_trait_change('position+') + def _position_config(self): + self.point1.value = self.position_1 + self.point2.value = self.position_2 - def value_setup(self, min_val, max_val): - self.slider_rep.minimum_value = min_val - self.slider_rep.maximum_value = max_val - self.slider_rep.value = min_val + @on_trait_change("minimum_value, maximum_value") + def _value_config(self): + self.slider_rep.minimum_value = self.minimum_value + self.slider_rep.maximum_value = self.maximum_value - @on_trait_change("+title") - def title_config(self): + @on_trait_change('title+') + def _title_config(self): self.title.color = self.title_color - print(self.title_color) self.slider_rep.title_text = self.title_text - print("shizz") self.title.shadow = 0 - def slider_setup(self, color1, color2): - self.slider.color = color1 - self.slider_rep._get_selected_property().color = color2 + @on_trait_change('slider+') + def _slider_config(self): + self.slider.color = self.slider_color + self.slider_rep._get_selected_property().color = self.slider_color_2 - def cap_setup(self, color, opacity): - self.cap.color = color - self.cap.opacity = opacity + @on_trait_change("cap+") + def cap_setup(self): + self.cap.color = self.cap_color + self.cap.opacity = self.cap_opacity - def tube_setup(self, color, opacity): - self.tube.color = color - self.tube.opacity = opacity + @on_trait_change("tube+") + def tube_setup(self): + self.tube.color = self.tube_color + self.tube.opacity = self.tube_opacity - def label_setup(self, color): - self.label.color = color - self.label.shadow = 0 + @on_trait_change("label+") + def label_setup(self): + self.label.color = self.label_color def _widget_setup(self, figure): self.slider_widget.set(interactor=figure().scene.interactor) self.slider_widget.set(representation=self.slider_rep) self.slider_widget.animation_mode = "animate" self.slider_widget.enabled = 1 + self.default_slider() + + def default_slider(self): + self.title_text = "Slider" + self.title_color = (0, 0, 0) + self.point1.coordinate_system = "normalized_display" + self.point2.coordinate_system = "normalized_display" + self.position_1 = (0.8, 0.9, 0) + self.position_2 = (1, 0.9, 0) + self.minimum_value = 0 + self.maximum_value = 1 + self.slider_rep.value = self.minimum_value + self.slider_color = (0.3, 0.2, 0.9) + self.slider_color_2 = (1, 0, 0) + self.cap_color = (0, 0, 0) + self.cap_opacity = 0 + self.tube_color = (0, 0, 0) + self.tube_opacity = 1 + self.label_color = (1, 1, 1) + self.label.shadow = 0 def add_callback(self, callback): self.slider_widget.add_observer("InteractionEvent", callback) @@ -96,54 +133,99 @@ def _visible_changed(self, value): super(SliderWidget, self)._visible_changed(value) - class ButtonWidget(WidgetSource): button_rep = tvtk.TexturedButtonRepresentation2D() button_widget = tvtk.ButtonWidget() text_image = tvtk.ImageData() - def __init__(self): - self.button_rep.set_number_of_states(1) - self.add_text("Button", (1,1,0), 25) - self.place_widget((-70,70,-70,70,0,0)) - - def add_image(self, location): - d = {'bmp':tvtk.BMPReader(), - 'jpg':tvtk.JPEGReader(), - 'png':tvtk.PNGReader(), - 'pnm':tvtk.PNMReader(), - 'dcm':tvtk.DICOMImageReader(), - 'tiff':tvtk.TIFFReader(), - 'ximg':tvtk.GESignaReader(), - 'dem':tvtk.DEMReader(), - 'mha':tvtk.MetaImageReader(), - 'mhd':tvtk.MetaImageReader(), - } - reader = d[location[-3:]] - reader.set(file_name = location) + file_location = File(exists=True) + position = Array(dtype="float64", shape=(6,), editor=ArrayEditor(), + desc="position as xmin, xmax, ymin, ymax, zmin, zmax") + + add_image = Bool + text_color = RGBColor + text = Str + text_font_size = Int + + box_group = Group( + Item(name="add_image"), + Item(name="position") + ) + + text_group = Group( + Item(name="text"), + Item(name="text_color"), + Item(name="text_font_size"), + enabled_when="add_image is False" + ) + + image_group = Group( + Item(name="file_location"), + enabled_when="add_image is True" + ) + + default_view = View( + Group( + box_group, + text_group, + image_group + ) + ) + + @on_trait_change("add_image") + def image_conifg(self): + if self.add_image == 1 and self.file_location is not "": + self.image() + if self.add_image == 0: + self.add_text() + + @on_trait_change("file_location") + def image(self): + d = {'bmp': tvtk.BMPReader(), + 'jpg': tvtk.JPEGReader(), + 'png': tvtk.PNGReader(), + 'pnm': tvtk.PNMReader(), + 'dcm': tvtk.DICOMImageReader(), + 'tiff': tvtk.TIFFReader(), + 'ximg': tvtk.GESignaReader(), + 'dem': tvtk.DEMReader(), + 'mha': tvtk.MetaImageReader(), + 'mhd': tvtk.MetaImageReader(), + } + reader = d[self.file_location[-3:]] + reader.set(file_name=self.file_location) reader.update() image = tvtk.ImageData() image = reader._get_output() self.button_rep.set_button_texture(0, image) - - def add_text(self, text, color, font_size): + @on_trait_change("text+") + def add_text(self): free_type = tvtk.FreeTypeStringToImage() text_property = tvtk.TextProperty() - text_property.color = color - text_property.font_size = font_size + text_property.color = self.text_color + text_property.font_size = self.text_font_size text_image = tvtk.ImageData() - free_type.render_string(text_property, text, 0, text_image) + free_type.render_string(text_property, self.text, 0, text_image) self.button_rep.set_button_texture(0, text_image) - def place_widget(self, a): - self.button_rep.place_widget(a) + @on_trait_change("position") + def place_widget(self): + self.button_rep.place_widget(self.position) def _widget_setup(self, figure): self.button_widget.set(interactor=figure().scene.interactor) self.button_widget.set(representation=self.button_rep) self.button_widget.enabled = 1 + self.default_button() + + def default_button(self): + self.button_rep.set_number_of_states(1) + self.text = "Button" + self.text_color = (1, 1, 0) + self.text_font_size = 25 + self.position = (-70, 70, -70, 70, 0, 0) def add_callback(self, callback): self.button_widget.add_observer("StateChangedEvent", callback) @@ -169,12 +251,14 @@ def remove_actors(self): self.button_rep.set_button_texture(0, self.text_image) self.scene.render() + def slider_widget(figure=gcf): slider = SliderWidget() slider._widget_setup(figure) mlab.get_engine().add_source(slider) return slider + def button_widget(figure=gcf): button = ButtonWidget() button._widget_setup(figure) From aeb67c2cf8fda45ab43e7954e3cd69095b5166bf Mon Sep 17 00:00:00 2001 From: amalss18 Date: Fri, 25 Oct 2019 09:44:09 +0530 Subject: [PATCH 8/9] minor fix --- mayavi/sources/widget_source.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mayavi/sources/widget_source.py b/mayavi/sources/widget_source.py index 55a5ca0e5..f5b71669d 100644 --- a/mayavi/sources/widget_source.py +++ b/mayavi/sources/widget_source.py @@ -1,4 +1,3 @@ -from mayavi import mlab from mayavi.core.source import Source From 0361f5b0d037a5e16fb535bf93c7556bfad381e5 Mon Sep 17 00:00:00 2001 From: amalss18 Date: Fri, 25 Oct 2019 17:10:54 +0530 Subject: [PATCH 9/9] removed error with color and other small mods --- mayavi/tools/widgets.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/mayavi/tools/widgets.py b/mayavi/tools/widgets.py index 0bd703f7d..9600d19d1 100644 --- a/mayavi/tools/widgets.py +++ b/mayavi/tools/widgets.py @@ -1,7 +1,6 @@ from tvtk.api import tvtk from .tools import gcf from mayavi.sources.widget_source import WidgetSource -from mayavi import mlab from traits.api import RGBColor, Str, on_trait_change, Array, Float, Int, File, Bool from traitsui.api import ArrayEditor from traitsui.api import Group, View, Item @@ -13,8 +12,10 @@ class SliderWidget(WidgetSource): title_text = Str minimum_value = Float maximum_value = Float - position_1 = Array(dtype="float64", shape=(3, ), editor=ArrayEditor()) - position_2 = Array(dtype="float64", shape=(3, ), editor=ArrayEditor()) + position_1 = Array(dtype="float64", shape=(3, ), editor=ArrayEditor(), + desc="x, y, z coordinates of the bottom left point") + position_2 = Array(dtype="float64", shape=(3, ), editor=ArrayEditor(), + desc="x, y, z coordinates of the top right point") slider_color = RGBColor slider_color_2 = RGBColor(desc="color of the slider when it's picked") @@ -68,14 +69,17 @@ def _value_config(self): @on_trait_change('title+') def _title_config(self): - self.title.color = self.title_color + color = self.title_color + self.title.color = (color if color is not "white" else (1, 1, 1)) self.slider_rep.title_text = self.title_text self.title.shadow = 0 @on_trait_change('slider+') def _slider_config(self): + color = self.slider_color_2 self.slider.color = self.slider_color - self.slider_rep._get_selected_property().color = self.slider_color_2 + self.slider_rep._get_selected_property().color = \ + (color if color is not "white" else (1, 1, 1)) @on_trait_change("cap+") def cap_setup(self): @@ -204,7 +208,8 @@ def image(self): def add_text(self): free_type = tvtk.FreeTypeStringToImage() text_property = tvtk.TextProperty() - text_property.color = self.text_color + color = self.text_color + text_property.color = (color if color is not "white" else (1, 1, 1)) text_property.font_size = self.text_font_size text_image = tvtk.ImageData() free_type.render_string(text_property, self.text, 0, text_image) @@ -252,15 +257,17 @@ def remove_actors(self): self.scene.render() -def slider_widget(figure=gcf): +def slider_widget(figure=None): + if figure is None: + figure = gcf slider = SliderWidget() slider._widget_setup(figure) - mlab.get_engine().add_source(slider) return slider -def button_widget(figure=gcf): +def button_widget(figure=None): + if figure is None: + figure = gcf button = ButtonWidget() button._widget_setup(figure) - mlab.get_engine().add_source(button) return button