11"""Defines the model and view components for the structure setup stage."""
22
3- import ase
43import ipywidgets as ipw
5- from aiida .orm import SinglefileData , StructureData
6- from aiidalab_widgets_base import SmilesWidget , WizardAppWidgetStep
4+ from aiidalab_widgets_base import WizardAppWidgetStep
75
8- from aiidalab_chemshell .common .database import AiiDADatabaseWidget
9- from aiidalab_chemshell .common .file_handling import FileUploadWidget
10- from aiidalab_chemshell .common .structure_viewer import StructureViewWidget
6+ from aiidalab_chemshell .common .structure_uploader import StructureSelectionWidget
117from aiidalab_chemshell .models .structure import StructureInputModel
128
139
@@ -42,35 +38,15 @@ def __init__(self, model: StructureInputModel, **kwargs):
4238 """
4339 )
4440
45- self .tabs = ipw . Tab ()
41+ self .structure_uploader = StructureSelectionWidget ()
4642
47- # upload file
48- self .file_input_widget = ipw .VBox ()
49- self .file_uploader = FileUploadWidget (description = "Structure file: " )
50- self .file_input_widget .children = [
51- self .file_uploader ,
52- ]
53- ipw .dlink ((self .file_uploader , "file" ), (self .model , "structure_file" ))
54-
55- # AiiDA database
56- self .database_widget = AiiDADatabaseWidget (
57- title = "AiiDA Database" ,
58- query = [SinglefileData , StructureData ],
43+ ipw .dlink (
44+ (self .structure_uploader , "structure_file" ),
45+ (self .model , "structure_file" ),
46+ )
47+ ipw .dlink (
48+ (self .structure_uploader , "structure_data" ), (self .model , "structure" )
5949 )
60-
61- self .smiles_widget = SmilesWidget (title = "SMILES" )
62-
63- self .tabs .children = [
64- self .file_input_widget ,
65- self .database_widget ,
66- self .smiles_widget ,
67- ]
68- for i , title in enumerate (["Upload File" , "AiiDA Database" , "SMILES String" ]):
69- self .tabs .set_title (i , title )
70-
71- self .model .observe (self ._on_file_upload , "structure_file" )
72- self .database_widget .observe (self ._on_database_search , "data_object" )
73- self .smiles_widget .observe (self ._on_smiles_generation , "structure" )
7450
7551 def render (self ):
7652 """Render the wizard's contents if not already rendered."""
@@ -95,64 +71,17 @@ def render(self):
9571 def _update_children (self ) -> None :
9672 self .children = [
9773 self .info ,
98- self .tabs ,
99- ipw .HTML ("<h2>Viewer:</h2>" ),
100- self .viewer ,
74+ self .structure_uploader ,
10175 self .submit_btn ,
10276 ]
10377 return
10478
105- def _on_file_upload (self , change : dict ) -> None :
106- """When file upload button is pressed."""
107- if self .model .has_file :
108- self .viewer = StructureViewWidget ()
109- self .viewer .assign_structure_from_file (
110- self .model .structure_file .filename , self .model .structure_file .content
111- )
112- self ._update_children ()
113- return
114-
115- def _on_smiles_generation (self , change : dict ) -> None :
116- """When SMILES string is inputted."""
117- if change ["new" ] != change ["old" ]:
118- self ._create_viewer (change ["new" ])
119- self .model .structure = StructureData (ase = change ["new" ])
120- return
121-
122- def _on_database_search (self , change : dict ) -> None :
123- """When data is loaded from AiiDA database."""
124- if change ["new" ] == change ["old" ]:
125- return
126- if isinstance (change ["new" ], SinglefileData ):
127- self .model .structure_file = change ["new" ]
128- self ._on_file_upload (change )
129- elif isinstance (change ["new" ], StructureData ):
130- self .model .structure = change ["new" ]
131- self ._create_viewer (change ["new" ]._get_object_ase ())
132- else :
133- self ._create_viewer (None )
134- return
135-
136- def _create_viewer (self , structure : ase .Atoms | None ) -> None :
137- """Create a viewer widget with the loaded ase.Atoms structure object."""
138- # if structure:
139- # # self.viewer = awb.viewers.StructureDataViewer(structure=structure)
140- # self.viewer = WeasWidget()
141- # self.viewer.from_ase(structure)
142- # else:
143- # self.viewer = ipw.HTML("<p>Could not visualise structure ...</p>")
144- self .viewer = StructureViewWidget ()
145- self .viewer .assign_structure_from_ase (structure )
146- self ._update_children ()
147- return
148-
14979 def submit_structure (self , _ ):
15080 """Submit the structure step."""
15181 if self .model .has_file or self .model .has_structure :
152- self .file_uploader .disable (True )
153- self .database_widget .disable (True )
15482 self .submit_btn .disabled = True
15583 self .submit_btn .description = "Submitted"
84+ self .structure_uploader .disable (True )
15685 self .model .submitted = True
15786 else :
15887 self .model .submitted = False
0 commit comments