Skip to content

Commit

Permalink
StructureDataViewer: selectable config tabs (#285).
Browse files Browse the repository at this point in the history
- Deprecate `configure_view`, as its functionality is taken over by `configuration_tabs`.
- Constructor argument `configuration_tabs` takes a list of tabs to display.
- Add `default_camera` construction argument with a default value.
- The camera selection widget moved to the Appearance tab.
  • Loading branch information
danielhollas committed Feb 9, 2022
1 parent 98caa06 commit 4badc55
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 31 deletions.
2 changes: 1 addition & 1 deletion aiidalab_widgets_base/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def __init__(
if viewer:
self.viewer = viewer
else:
self.viewer = StructureDataViewer(downloadable=False)
self.viewer = StructureDataViewer(**kwargs)
dlink((self, "structure_node"), (self.viewer, "structure"))

# Store button.
Expand Down
84 changes: 56 additions & 28 deletions aiidalab_widgets_base/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,14 @@ def __init__(self, parameter, downloadable=True, **kwargs):
class _StructureDataBaseViewer(ipw.VBox):
"""Base viewer class for AiiDA structure or trajectory objects.
:param configure_view: If True, add configuration tabs
:type configure_view: bool"""
:param configure_view: If True, add configuration tabs (deprecated)
:type configure_view: bool
:param configuration_tabs: List of configuration tabs (default: ["Selection", "Appearance", "Cell", "Download"])
:type configure_view: list
:param default_camera: default camera (orthographic|perspective), can be changed in the Appearance tab
:type default_camera: string
"""

selection = List(Int)
selection_adv = Unicode()
Expand All @@ -168,44 +174,48 @@ class _StructureDataBaseViewer(ipw.VBox):
DEFAULT_SELECTION_RADIUS = 6
DEFAULT_SELECTION_COLOR = "green"

def __init__(self, configure_view=True, **kwargs):
def __init__(
self,
configure_view=True,
configuration_tabs=["Selection", "Appearance", "Cell", "Download"],
default_camera="orthographic",
**kwargs,
):
# Defining viewer box.

# 1. Nglviwer
# Nglviwer
self._viewer = nglview.NGLWidget()
self._viewer.camera = "orthographic"
self._viewer.camera = default_camera
self._viewer.observe(self._on_atom_click, names="picked")
self._viewer.stage.set_parameters(mouse_preset="pymol")

# 2. Camera type.
camera_type = ipw.ToggleButtons(
options={"Orthographic": "orthographic", "Perspective": "perspective"},
description="Camera type:",
value="orthographic",
layout={"align_self": "flex-end"},
style={"button_width": "115.5px"},
orientation="vertical",
)

def change_camera(change):
view_box = ipw.VBox([self._viewer])

self._viewer.camera = change["new"]
configuration_tabs_map = {
"Selection": self._selection_tab(),
"Appearance": self._appearance_tab(),
"Cell": self._cell_tab(),
"Download": self._download_tab(),
}

camera_type.observe(change_camera, names="value")
view_box = ipw.VBox([self._viewer, camera_type])
if configure_view is not True:
warnings.warn(
"`configure_view` is deprecated, please use `configuration_tabs` instead.",
DeprecationWarning,
)
if not configure_view:
configuration_tabs.clear()

# Constructing configuration box
if configure_view:
if len(configuration_tabs) != 0:
configuration_box = ipw.Tab(
layout=ipw.Layout(flex="1 1 auto", width="auto")
)
configuration_box.children = [
self._selection_tab(),
self._appearance_tab(),
self._cell_tab(),
self._download_tab(),
configuration_tabs_map[tab_title] for tab_title in configuration_tabs
]
for i, title in enumerate(["Selection", "Appearance", "Cell", "Download"]):

for i, title in enumerate(configuration_tabs):
configuration_box.set_title(i, title)
children = [ipw.HBox([view_box, configuration_box])]
view_box.layout = {"width": "60%"}
Expand Down Expand Up @@ -299,11 +309,29 @@ def change_supercell(_=None):
link((background_color, "value"), (self._viewer, "background"))
background_color.value = "white"

# 3. Center button.
center_button = ipw.Button(description="Center")
# 3. Camera switcher
camera_type = ipw.ToggleButtons(
options={"Orthographic": "orthographic", "Perspective": "perspective"},
description="Camera type:",
value=self._viewer.camera,
layout={"align_self": "flex-start"},
style={"button_width": "115.5px"},
orientation="vertical",
)

def change_camera(change):

self._viewer.camera = change["new"]

camera_type.observe(change_camera, names="value")

# 4. Center button.
center_button = ipw.Button(description="Center molecule")
center_button.on_click(lambda c: self._viewer.center())

return ipw.VBox([supercell_selector, background_color, center_button])
return ipw.VBox(
[supercell_selector, background_color, camera_type, center_button]
)

@observe("cell")
def _observe_cell(self, _=None):
Expand Down
4 changes: 2 additions & 2 deletions notebooks/aiida_datatypes_viewers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"source": [
"CifData = DataFactory('cif')\n",
"s = CifData(ase=m)\n",
"vwr = viewer(s.store(), downloadable=True)\n",
"vwr = viewer(s.store(), configuration_tabs=['Selection', 'Appearance', 'Cell', 'Download'])\n",
"display(vwr)"
]
},
Expand All @@ -88,7 +88,7 @@
"source": [
"StructureData = DataFactory('structure')\n",
"s = StructureData(ase=m)\n",
"vwr = viewer(s.store(), downloadable=True)\n",
"vwr = viewer(s.store())\n",
"display(vwr)"
]
},
Expand Down

0 comments on commit 4badc55

Please sign in to comment.