1""""Panels for HIST files."""
2import param
3import panel as pn
4import bokeh.models.widgets as bkw
5
6from abipy.panels.core import sizing_mode_select, AbipyParameterized
7
8
9_what_list = ["pressure", "forces", "energy", "abc", "angles", "volume"]
10
11
12class HistFilePanel(AbipyParameterized):
13    """
14    Panel with widgets to interact with a |HistFile|.
15    """
16    what_list = pn.widgets.CheckBoxGroup(name="Select", value=_what_list, options=_what_list, inline=False)
17    plot_relax_btn = pn.widgets.Button(name="Show relaxation", button_type="primary")
18
19    sizing_mode = sizing_mode_select(value="stretch_width")
20
21    appname = pn.widgets.Select(name="Viewer", value="ovito", options=["ovito", "mayavi", "vtk"])
22    to_unit_cell = pn.widgets.Checkbox(name="To unit cell")
23    view_relax_btn = pn.widgets.Button(name="View relaxation", button_type="primary")
24
25    def __init__(self, hist, **params):
26        super().__init__(**params)
27        self.hist = hist
28
29    def get_plot_relax_widgets(self):
30        """Widgets to visualize the structure relaxation."""
31        return pn.Column(self.what_list, self.sizing_mode, self.plot_relax_btn)
32
33    @param.depends('plot_relax_btn.clicks')
34    def on_plot_relax_btn(self):
35        """
36        Plot the evolution of structural parameters (lattice lengths, angles and volume)
37        as well as pressure, info on forces and total energy.
38        """
39        if self.plot_relax_btn.clicks == 0: return
40
41        num_plots, nrows, ncols = len(self.what_list.value), 1, 1
42        if num_plots > 1:
43            ncols = 2
44            nrows = (num_plots // ncols) + (num_plots % ncols)
45
46        box = pn.GridBox(nrows=nrows, ncols=ncols, sizing_mode=self.sizing_mode.value) #'scale_width')
47        for i, what in enumerate(self.what_list.value):
48            irow, icol = divmod(i, ncols)
49            box.append(self._mp(self.hist.plot(what, title=what, **self.fig_kwargs)))
50
51        return box
52        #return pn.Column(box, box.controls(jslink=True))
53
54    @param.depends('view_relax_btn.clicks')
55    def on_view_relax_btn(self):
56        if self.view_relax_btn.clicks == 0: return
57        return self.hist.visualize(appname=self.appname.value, to_unit_cell=self.to_unit_cell.value)
58
59    def get_panel(self):
60        """Return tabs with widgets to interact with the DDB file."""
61        tabs = pn.Tabs()
62        tabs.append(("Summary", pn.Row(bkw.PreText(text=self.hist.to_string(verbose=self.verbose),
63                     sizing_mode="scale_both"))))
64        tabs.append(("Relaxation", pn.Row(self.get_plot_relax_widgets(), self.on_plot_relax_btn)))
65        tabs.append(("Visualize", pn.Row(pn.Column(self.appname, self.to_unit_cell, self.view_relax_btn),
66                                         self.on_view_relax_btn)))
67
68        return tabs
69