Commit bec41118 authored by payno's avatar payno

[gui] add interface to select energy unit

parent 91be990b
Pipeline #23855 failed with stage
in 6 minutes and 16 seconds
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""Tools to select energy unit"""
__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "31/03/2020"
from silx.gui import qt
from est.units import ur
class EnergyUnitSelector(qt.QComboBox):
"""Simple class to define a unit for energy"""
def __init__(self, parent=None):
qt.QComboBox.__init__(self, parent)
for unit in ('eV', 'keV', 'J', 'kJ'):
self.addItem(unit)
def getUnit(self):
current_unit = self.currentText()
if current_unit == 'eV':
return ur.eV
elif current_unit == 'keV':
return ur.keV
elif current_unit == 'J':
return ur.J
elif current_unit == 'kJ':
return ur.kJ
else:
raise ValueError('current unit is not supported')
def setUnit(self, unit: str) -> None:
if unit in ('eV', 'keV', 'J', 'kJ'):
txt = unit
elif unit == ur.eV:
txt = 'eV'
elif unit == ur.keV:
txt = 'keV'
elif unit == ur.J:
txt = 'J'
elif unit == ur.kJ:
txt = 'kJ'
else:
raise ValueError('Given unit is not managed: {}'.format())
index = self.findText(txt)
if index >= 0:
self.setCurrentIndex(index)
......@@ -31,10 +31,11 @@ __date__ = "03/07/2019"
from silx.gui.dialog.DataFileDialog import DataFileDialog
from silx.io.url import DataUrl
from silx.gui import qt
from est.core.io import read as read_xas, read_frm_file, Dim
from est.core.io import read as read_xas, read_frm_file
from est.io import InputType
import logging
from est.core.types import Dim
from est.gui.unit.energy import EnergyUnitSelector
_logger = logging.getLogger(__name__)
......@@ -116,10 +117,12 @@ class XASObjectDialog(qt.QWidget):
def buildXASObject(self):
if self.getCurrentType() in (InputType.dat_spectrum, InputType.xmu_spectrum, InputType.csv_spectrum):
return read_frm_file(file_path=self._singleSpectrumDialog.getFileSelected())
return read_frm_file(file_path=self._singleSpectrumDialog.getFileSelected(),
energy_unit=self._singleSpectrumDialog.getEnergyUnit())
elif self.getCurrentType() == InputType.hdf5_spectra:
spectra_url = self._h5Dialog.getSpectraUrl()
energy_url = self._h5Dialog.getEnergyUrl()
energy_unit = self._h5Dialog.getEnergyUnit()
def check_url(url_path, name):
if url_path in (None, ''):
......@@ -135,6 +138,7 @@ class XASObjectDialog(qt.QWidget):
check_url(energy_url, 'energy / channel')
return read_xas(spectra_url=self._h5Dialog.getSpectraUrl(),
channel_url=self._h5Dialog.getEnergyUrl(),
energy_unit=energy_unit,
config_url=self._h5Dialog.getConfigurationUrl(),
dimensions=self._h5Dialog.getDimensions())
else:
......@@ -152,12 +156,18 @@ class _XASObjectFrmSingleSpectrum(qt.QWidget):
qt.QWidget.__init__(self, parent)
self.setLayout(qt.QGridLayout())
self.layout().addWidget(qt.QLabel('file', parent=self), 0, 0)
self._inputLe = qt.QLineEdit('', parent=self)
# select file
self.layout().addWidget(qt.QLabel('file', self), 0, 0)
self._inputLe = qt.QLineEdit('', self)
self.layout().addWidget(self._inputLe, 0, 1)
self._selectPB = qt.QPushButton('select', parent=self)
self._selectPB = qt.QPushButton('select', self)
self.layout().addWidget(self._selectPB, 0, 2)
# select energy
self.layout().addWidget(qt.QLabel('energy unit', self), 1, 0)
self._energyUnitSelector = EnergyUnitSelector(parent=self)
self.layout().addWidget(self._energyUnitSelector, 1, 1)
spacer = qt.QWidget(parent=self)
spacer.setSizePolicy(qt.QSizePolicy.Minimum, qt.QSizePolicy.Expanding)
self.layout().addWidget(spacer, 2, 0)
......@@ -190,6 +200,12 @@ class _XASObjectFrmSingleSpectrum(qt.QWidget):
def setFileSelected(self, file_path):
self._inputLe.setText(file_path)
def getEnergyUnit(self):
return self._energyUnitSelector.getUnit()
def setEnergyUnit(self, unit):
return self._energyUnitSelector.setUnit(unit=unit)
class _URLSelector(qt.QWidget):
def __init__(self, parent, name, layout=None, position=None):
......@@ -247,6 +263,8 @@ class _XASObjFrmH5(qt.QWidget):
# energy / channel url
self._energySelector = _URLSelector(parent=self, name='energy /channel url',
layout=self.layout(), position=(2, 0))
self._energyUnit = EnergyUnitSelector(parent=self)
self.layout().addWidget(self._energyUnit, 2, 3, 1, 1)
# configuration url
self._configSelector = _URLSelector(parent=self, name='configuration url',
layout=self.layout(),
......@@ -256,6 +274,7 @@ class _XASObjFrmH5(qt.QWidget):
self._spectraSelector._qLineEdit.textChanged.connect(self._editingIsFinished)
self._energySelector._qLineEdit.textChanged.connect(self._editingIsFinished)
self._configSelector._qLineEdit.textChanged.connect(self._editingIsFinished)
self._energyUnit.currentIndexChanged.connect(self._editingIsFinished)
# expose APi
self.setDimensions = self._dimensionSelection.setDimensions
......@@ -306,6 +325,12 @@ class _XASObjFrmH5(qt.QWidget):
"""
return self._dimensionSelection.getDimensions()
def getEnergyUnit(self):
return self._energyUnit.getUnit()
def setEnergyUnit(self, unit):
self._energyUnit.setUnit(unit=unit)
class _QDimComboBox(qt.QComboBox):
def __init__(self, parent):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment