3737from plaid .dialogs import H5Dialog , ExportSettingsDialog , ColorCycleDialog
3838from plaid .reference import Reference
3939from plaid .plot_widgets import HeatmapWidget , PatternWidget , AuxiliaryPlotWidget , CorrelationMapWidget , DiffractionMapWidget
40- from plaid .misc import q_to_tth , tth_to_q , get_divisors
40+ from plaid .misc import q_to_tth , tth_to_q , d_to_q , d_to_tth , get_divisors
4141from plaid .data_containers import AzintData , AuxData
4242from plaid import __version__ as CURRENT_VERSION
4343import plaid .resources
@@ -168,7 +168,7 @@ def save_recent_refs_settings(recent_refs):
168168 recent_refs = [r for r in recent_refs if r ] # Remove empty entries
169169 # Limit to the last 10 references
170170 if len (recent_refs ) > 10 :
171- recent_refs = recent_refs [- 10 : ]
171+ recent_refs = recent_refs [: 10 ]
172172 # Save the recent references
173173 settings .setValue ("recent-references" , recent_refs )
174174 settings .endGroup ()
@@ -231,7 +231,7 @@ def __init__(self):
231231 self .E = None # Energy in keV
232232 self .is_Q = False # flag to indicate if the data is in Q space (True) or 2theta space (False)
233233
234- self .azint_data = AzintData ()
234+ self .azint_data = AzintData (self )
235235 self .aux_data = {}
236236
237237 self .locked_patterns = [] # list of (is_Q, E) tuples for locked patterns
@@ -398,10 +398,11 @@ def _init_connections(self):
398398 self .file_tree .sigI0DataRequested .connect (self .load_I0_data )
399399 self .file_tree .sigAuxiliaryDataRequested .connect (self .load_auxiliary_data )
400400 # Connect the CIF tree signals to the appropriate slots
401- self .cif_tree .sigItemAdded .connect (self .add_reference )
402- self .cif_tree .sigItemChecked .connect (self .toggle_reference )
403- self .cif_tree .sigItemDoubleClicked .connect (self .rescale_reference )
404- self .cif_tree .sigItemRemoved .connect (self .remove_reference )
401+ self .cif_tree .sigItemAdded .connect (self .add_reference ) # --> str
402+ self .cif_tree .sigItemChecked .connect (self .toggle_reference ) # --> int, bool
403+ self .cif_tree .sigItemDoubleClicked .connect (self .rescale_reference ) # --> int, str
404+ self .cif_tree .sigItemRemoved .connect (self .remove_reference ) # --> int
405+ self .cif_tree .sigItemReloadRequested .connect (self .reload_reference ) # --> int
405406 # Connect the heatmap signals to the appropriate slots
406407 self .heatmap .sigHLineMoved .connect (self .hline_moved )
407408 self .heatmap .sigXRangeChanged .connect (self .pattern .set_xrange )
@@ -1008,24 +1009,49 @@ def add_reference(self, cif_file, Qmax=None):
10081009 self .plot_reference (color = color )
10091010 tooltip = f"{ self .ref .get_spacegroup_info ()} \n { self .ref .get_cell_parameter_info ()} "
10101011 self .cif_tree .set_latest_item_tooltip (tooltip )
1011-
1012- def plot_reference (self , Qmax = None , dmin = None , color = None ):
1013- """Plot the reference pattern in the pattern plot."""
1012+
1013+ def get_reference_reflections (self , Qmax = None , dmin = None ):
1014+ """
1015+ Get the reference reflections from the current reference pattern,
1016+ converted to the current x-axis units.
1017+ """
10141018 if Qmax is None :
10151019 Qmax = self .getQmax ()
10161020 hkl , d , I = self .ref .get_reflections (Qmax = Qmax , dmin = dmin )
10171021 if len (hkl ) == 0 :
10181022 QMessageBox .warning (self , "No Reflections" , "No reflections found in the reference pattern." )
10191023 return
1020-
10211024 if self .is_Q :
10221025 # Convert d to Q
1023- x = 4 * np . pi / d
1026+ x = d_to_q ( d )
10241027 else :
10251028 # Convert d to 2theta
1026- x = np .degrees (2 * np .arcsin ((12.398 / self .E ) / (2 * d )))
1029+ x = d_to_tth (d , self .E )
1030+ return hkl , x , I
1031+
1032+ def plot_reference (self , Qmax = None , dmin = None , color = None ):
1033+ """Plot the reference pattern in the pattern plot."""
1034+ hkl , x , I = self .get_reference_reflections (Qmax = Qmax , dmin = dmin )
10271035 self .pattern .add_reference (hkl , x , I ,color = color )
10281036
1037+ def reload_reference (self ,index , Qmax = None ):
1038+ """Reload the reference pattern at the given index from the cif tree."""
1039+ cif_file = self .cif_tree .files [index ]
1040+ # check that the cif file still exists
1041+ if not os .path .exists (cif_file ):
1042+ QMessageBox .critical (self , "Error" , f"CIF file not found: { cif_file } " )
1043+ return
1044+ item = self .cif_tree .file_tree .topLevelItem (index )
1045+ if Qmax is None :
1046+ Qmax = self .getQmax ()
1047+ self .ref = Reference (cif_file ,E = self .E , Qmax = Qmax )
1048+ hkl , x , I = self .get_reference_reflections (Qmax = Qmax )
1049+ self .pattern .update_reference (index , hkl , x , I )
1050+ # update the tooltip
1051+ tooltip = f"{ self .ref .get_spacegroup_info ()} \n { self .ref .get_cell_parameter_info ()} "
1052+ item .setToolTip (0 , tooltip )
1053+ self .statusBar ().showMessage (f"Reloaded reference from { cif_file } " )
1054+
10291055 def toggle_reference (self , index , is_checked ):
10301056 """
10311057 Toggle the visibility of the reference pattern.
@@ -1039,6 +1065,7 @@ def rescale_reference(self,index,name):
10391065 This method is called when a reference item is double-clicked in the CIF tree.
10401066 """
10411067 self .pattern .rescale_reference (index )
1068+ self .statusBar ().showMessage (f"Rescaled { name } " )
10421069
10431070 def load_I0_data (self , aname = None , fname = None ):
10441071 """Load auxillary data as I0. Called when the user requests I0 data from the file tree."""
0 commit comments