From 79d0ab162802adfea86aaf9ccc18dd2db0f38f85 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Mon, 29 Apr 2024 16:26:09 +0000 Subject: [PATCH] read-fid (#269); closes #268 Co-authored-by: Dominik Demuth Reviewed-on: https://gitea.pkm.physik.tu-darmstadt.de/IPKM/nmreval/pulls/269 --- src/gui_qt/io/asciireader.py | 44 ++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/gui_qt/io/asciireader.py b/src/gui_qt/io/asciireader.py index ea4c980..ea559c2 100644 --- a/src/gui_qt/io/asciireader.py +++ b/src/gui_qt/io/asciireader.py @@ -186,6 +186,7 @@ class QAsciiReader(QtWidgets.QDialog, Ui_ascii_reader): def apply(self): # default row for x is the first row, it will be superseded if an integer number is given. x = self.x_lineedit.text() + is_valid = True if x: try: x = int(x)-1 @@ -194,16 +195,35 @@ class QAsciiReader(QtWidgets.QDialog, Ui_ascii_reader): else: x = None + if not self.check_column_numbers(x, max(self.reader.width)): + _ = QtWidgets.QMessageBox.information(self, 'Improper input', + f'Input for x axis is invalid') + return False + try: y = [int(t)-1 for t in self.y_lineedit.text().split(' ')] except ValueError: y = None + if not self.check_column_numbers(y, max(self.reader.width)): + _ = QtWidgets.QMessageBox.information(self, 'Improper input', + f'Input for y axis is invalid') + return False + try: y_err = [int(t)-1 for t in self.deltay_lineEdit.text().split(' ')] except ValueError: y_err = None + mode = self.buttonGroup.checkedButton().text() + if mode != 'Points': + y_err = None + + if not self.check_column_numbers(y, max(self.reader.width)): + _ = QtWidgets.QMessageBox.information(self, 'Improper input', + f'Input for y_err axis is invalid') + return False + col_header = None if self.column_checkBox.isChecked(): col_header = [] @@ -221,15 +241,16 @@ class QAsciiReader(QtWidgets.QDialog, Ui_ascii_reader): x=x, y=y, yerr=y_err, - mode=self.buttonGroup.checkedButton().text(), + mode=mode, col_names=col_header, num_value=self.get_numerical_value(), ) self.data_read.emit(ret_dic) - except ImportError as e: + except Exception as e: _ = QtWidgets.QMessageBox.information(self, 'Reading failed', - f'Import data failed with {e.args}') + f'Import data failed with\n {e.args[0]}') + return False return True @@ -284,7 +305,7 @@ class QAsciiReader(QtWidgets.QDialog, Ui_ascii_reader): else: self.label_8.setText(fname) - def get_numerical_value(self): + def get_numerical_value(self) -> float: val = 0 if self.re_button.isChecked() and self._matches: m = self._matches[self.re_match_index.value()-1] @@ -295,3 +316,18 @@ class QAsciiReader(QtWidgets.QDialog, Ui_ascii_reader): val = float(self.custom_input.text()) return val + + def check_column_numbers(self, values: int | list[int] | None, num_column: int) -> bool: + is_valid = False + if values is None: + is_valid = True + elif isinstance(values, int): + is_valid = values < num_column + elif isinstance(values, list): + try: + is_valid = all(v < num_column for v in values) + except TypeError: + is_valid = False + + return is_valid +