Skip to content

Commit

Permalink
Update view/controller observables
Browse files Browse the repository at this point in the history
  • Loading branch information
edan-bainglass committed Oct 7, 2024
1 parent e3532b2 commit 2561827
Show file tree
Hide file tree
Showing 11 changed files with 206 additions and 236 deletions.
46 changes: 20 additions & 26 deletions src/aiidalab_qe/app/configuration/hubbard.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import ipywidgets as ipw
import traitlets as tl

from aiida import orm
from aiida_quantumespresso.data.hubbard_structure import HubbardStructureData

from .model import ConfigurationModel
Expand All @@ -10,14 +8,6 @@
class HubbardSettings(ipw.VBox):
"""Widget for setting up Hubbard parameters."""

input_structure = tl.Union(
[
tl.Instance(orm.StructureData),
tl.Instance(orm.KpointsData),
],
allow_none=True,
)

def __init__(self, model: ConfigurationModel, **kwargs):
from aiidalab_qe.common.widgets import LoadingWidget

Expand All @@ -27,6 +17,10 @@ def __init__(self, model: ConfigurationModel, **kwargs):
)

self._model = model
self._model.observe(
self._on_input_structure_change,
"input_structure",
)

self.links = []
self.eigenvalues_widget_links = []
Expand Down Expand Up @@ -78,6 +72,7 @@ def render(self):

self.hubbard_widget = ipw.VBox()
self.eigenvalues_widget = ipw.VBox()

self.container = ipw.VBox()

self.children = [
Expand All @@ -90,31 +85,27 @@ def render(self):
self.container,
]

ipw.dlink(
(self._model, "input_structure"),
(self, "input_structure"),
)

self.rendered = True

self._build_hubbard_widget()

def reset(self):
"""Reset the widget."""
self._unsubscribe()
self._model.advanced.hubbard.reset()

@tl.observe("input_structure")
def _on_input_structure_change(self, change):
def _on_input_structure_change(self, _):
self._unsubscribe()
self._model.advanced.hubbard.update()
self._build_hubbard_widget()
if isinstance(change["new"], HubbardStructureData):
if isinstance(self._model.input_structure, HubbardStructureData):
self._model.advanced.hubbard.set_parameters_from_hubbard_structure()

def _on_hubbard_check(self, change):
self._toggle_hubbard_widget(change)
def _on_hubbard_check(self, _):
self._toggle_hubbard_widget()

def _on_eigenvalues_check(self, change):
self._toggle_eigenvalues_widget(change)
def _on_eigenvalues_check(self, _):
self._toggle_eigenvalues_widget()

def _build_hubbard_widget(self):
"""Build the widget for defining Hubbard U values
Expand All @@ -124,6 +115,8 @@ def _build_hubbard_widget(self):
hubbard_widget (ipywidgets.VBox):
The widget containing the input fields for defining Hubbard U values.
"""
if not self.rendered:
return

children = []

Expand Down Expand Up @@ -241,16 +234,17 @@ def update(index, spin, state, symbol, value):

self.eigenvalues_widget.children = children

def _toggle_hubbard_widget(self, change):
self.container.children = [self.hubbard_widget] if change["new"] else []
def _toggle_hubbard_widget(self):
widget = [self.hubbard_widget] if self._model.advanced.hubbard.activate else []
self.container.children = widget

def _toggle_eigenvalues_widget(self, change):
def _toggle_eigenvalues_widget(self):
self.hubbard_widget.children = (
[
*self.hubbard_widget.children,
self.eigenvalues_widget,
]
if change["new"]
if self._model.advanced.hubbard.eigenvalues_label
else [*self.hubbard_widget.children][:-1]
)

Expand Down
87 changes: 40 additions & 47 deletions src/aiidalab_qe/app/configuration/magnetization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import ipywidgets as ipw
import traitlets as tl

from aiida import orm

from .model import ConfigurationModel

Expand All @@ -20,16 +17,6 @@ class MagnetizationSettings(ipw.VBox):
input_structure(StructureData): trait that contains the input_structure (confirmed structure from previous step)
"""

input_structure = tl.Union(
[
tl.Instance(orm.StructureData),
tl.Instance(orm.KpointsData),
],
allow_none=True,
)
electronic_type = tl.Unicode()
magnetization_type = tl.Unicode()

def __init__(self, model: ConfigurationModel, **kwargs):
from aiidalab_qe.common.widgets import LoadingWidget

Expand All @@ -40,8 +27,20 @@ def __init__(self, model: ConfigurationModel, **kwargs):
)

self._model = model
self._model.observe(
self._on_input_structure_change,
"input_structure",
)
self._model.workchain.observe(
self._on_electronic_type_change,
"electronic_type",
)
self._model.advanced.magnetization.observe(
self._on_magnetization_type_change,
"type",
)

self.kind_widget_links = []
self.links = []

self.rendered = False

Expand Down Expand Up @@ -96,48 +95,38 @@ def render(self):

self.children = [self.description]

with self.hold_trait_notifications():
ipw.dlink(
(self._model, "input_structure"),
(self, "input_structure"),
)
ipw.dlink(
(self._model.workchain, "electronic_type"),
(self, "electronic_type"),
)
ipw.dlink(
(self._model.advanced.magnetization, "type"),
(self, "magnetization_type"),
)

self.rendered = True

self._build_kinds_widget()
self._switch_widgets()
self._toggle_widgets()

def reset(self):
self._model.advanced.magnetization.reset()

@tl.observe("input_structure")
def _on_input_structure_change(self, change):
def _on_input_structure_change(self, _):
self._model.advanced.magnetization.update()
self._build_kinds_widget(change)
self._build_kinds_widget()

@tl.observe("electronic_type")
def _on_electronic_type_change(self, change):
self._switch_widgets(change)
def _on_electronic_type_change(self, _):
self._switch_widgets()

@tl.observe("magnetization_type")
def _on_magnetization_type_change(self, change):
self._toggle_widgets(change)
def _on_magnetization_type_change(self, _):
self._toggle_widgets()

def _build_kinds_widget(self):
if not self.rendered:
return

def _build_kinds_widget(self, change):
children = []

if (input_structure := change["new"]) is None:
if self._model.input_structure is None:
labels = []
for link in self.kind_widget_links:
for link in self.links:
link.unlink()
self.kind_widget_links.clear()
self.links.clear()
else:
labels = input_structure.get_kind_names()
labels = self._model.input_structure.get_kind_names()

for label in labels:
kind_widget = ipw.BoundedFloatText(
Expand All @@ -158,7 +147,7 @@ def _build_kinds_widget(self, change):
},
],
)
self.kind_widget_links.append(link)
self.links.append(link)
ipw.dlink(
(self._model.advanced, "override"),
(kind_widget, "disabled"),
Expand All @@ -168,16 +157,20 @@ def _build_kinds_widget(self, change):

self.kinds.children = children

def _switch_widgets(self, change):
def _switch_widgets(self):
if not self.rendered:
return
children = [self.description]
if change["new"] == "metal":
if self._model.workchain.electronic_type == "metal":
children.extend([self.magnetization_type_toggle, self.container])
else:
children.append(self.tot_magnetization)
self.children = children

def _toggle_widgets(self, change):
if change["new"] == "tot_magnetization":
def _toggle_widgets(self):
if not self.rendered:
return
if self._model.advanced.magnetization.type == "tot_magnetization":
self.container.children = [self.tot_magnetization]
else:
self.container.children = [self.kinds]
Loading

0 comments on commit 2561827

Please sign in to comment.