Skip to content

Commit

Permalink
Merge pull request #1650 from bnmajor/instrument-templates
Browse files Browse the repository at this point in the history
Add an option to load pre-configured instrument files
  • Loading branch information
psavery authored Jan 22, 2024
2 parents 351f24d + ae5b141 commit df12cf6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
39 changes: 36 additions & 3 deletions hexrdgui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
)
from PySide6.QtGui import QDesktopServices
from PySide6.QtWidgets import (
QApplication, QDockWidget, QFileDialog, QMainWindow, QMessageBox
QApplication, QDockWidget, QFileDialog,
QInputDialog, QMainWindow, QMessageBox
)

from hexrdgui.about_dialog import AboutDialog
Expand Down Expand Up @@ -76,7 +77,8 @@
from hexrdgui.utils.dialog import add_help_url
from hexrdgui.zoom_canvas_dialog import ZoomCanvasDialog
from hexrdgui.rerun_clustering_dialog import RerunClusteringDialog
from hexrdgui import state
from hexrdgui import resource_loader, state
from hexrd.resources import instrument_templates


class MainWindow(QObject):
Expand Down Expand Up @@ -177,7 +179,7 @@ def __init__(self, parent=None, image_files=None):
def setup_connections(self):
"""This is to setup connections for non-gui objects"""
self.ui.installEventFilter(self)
self.ui.action_open_config_file.triggered.connect(
self.ui.action_open_instrument_file.triggered.connect(
self.on_action_open_config_file_triggered)
self.ui.action_open_grain_fitting_results.triggered.connect(
self.open_grain_fitting_results)
Expand Down Expand Up @@ -286,6 +288,8 @@ def setup_connections(self):
self.active_canvas_changed)
self.ui.action_edit_apply_threshold.triggered.connect(
self.on_action_edit_apply_threshold_triggered)
self.ui.action_open_preconfigured_instrument_file.triggered.connect(
self.on_action_open_preconfigured_instrument_file_triggered)

self.image_mode_widget.polar_show_snip1d.connect(
self.ui.image_tab_widget.polar_show_snip1d)
Expand Down Expand Up @@ -1575,3 +1579,32 @@ def load_recent_state_file(self, path):
return

self.load_state_file(path)

def on_action_open_preconfigured_instrument_file_triggered(self):
# Should we put this in HEXRD?
aliases = {
'dual_dexelas.yml': 'Dual Dexelas',
}

# Create a dict of options for loading an instrument, mapping file
# name to instrument config
options = {}
for f in resource_loader.module_contents(instrument_templates):
if f.endswith(('.yml', '.yaml', '.hexrd', '.h5', '.hdf5')):
name = Path(f).name
if name in aliases:
name = aliases[name]

options[name] = f

# Provide simple dialog for selecting instrument to import
msg = 'Select pre-configured instrument to load'
instr_name, ok = QInputDialog.getItem(
self.ui, 'Load Instrument', msg, list(options), 0, False)

if not ok:
return

fname = options[instr_name]
with resource_loader.resource_path(instrument_templates, fname) as f:
HexrdConfig().load_instrument_config(Path(f))
26 changes: 19 additions & 7 deletions hexrdgui/resources/ui/main_window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,16 @@
<property name="toolTipsVisible">
<bool>true</bool>
</property>
<widget class="QMenu" name="menu_instrument">
<property name="title">
<string>Instrument</string>
</property>
<addaction name="action_open_instrument_file"/>
<addaction name="action_open_preconfigured_instrument_file"/>
</widget>
<addaction name="action_open_images"/>
<addaction name="action_open_state"/>
<addaction name="action_open_config_file"/>
<addaction name="menu_instrument"/>
<addaction name="action_open_materials"/>
<addaction name="separator"/>
<addaction name="action_open_grain_fitting_results"/>
Expand All @@ -74,7 +81,7 @@
</property>
<widget class="QMenu" name="menu_save_config">
<property name="title">
<string>&amp;Configuration</string>
<string>&amp;Instrument</string>
</property>
<property name="toolTipsVisible">
<bool>true</bool>
Expand Down Expand Up @@ -629,11 +636,6 @@
<string>Laue</string>
</property>
</action>
<action name="action_open_config_file">
<property name="text">
<string>Configuration</string>
</property>
</action>
<action name="action_save_config_yaml">
<property name="text">
<string>YAML</string>
Expand Down Expand Up @@ -851,6 +853,16 @@
<string>Configuration</string>
</property>
</action>
<action name="action_open_instrument_file">
<property name="text">
<string>File</string>
</property>
</action>
<action name="action_open_preconfigured_instrument_file">
<property name="text">
<string>Preconfigured</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down
2 changes: 1 addition & 1 deletion tests/test_hedm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_load_data(qtbot, main_window, default_config_path, default_data_path):

# Load config file
with select_files_when_asked(default_config_path):
main_window.ui.action_open_config_file.triggered.emit()
main_window.ui.action_open_instrument_file.triggered.emit()

# Should have loaded the instrument config
detectors = HexrdConfig().detectors
Expand Down

0 comments on commit df12cf6

Please sign in to comment.