check for correct axes input

This commit is contained in:
Dominik Demuth 2024-04-29 18:24:19 +02:00
parent 7a851191d9
commit 2fbae4697c

View File

@ -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,11 +195,21 @@ 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:
@ -208,6 +219,11 @@ class QAsciiReader(QtWidgets.QDialog, Ui_ascii_reader):
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 = []
@ -231,9 +247,10 @@ class QAsciiReader(QtWidgets.QDialog, Ui_ascii_reader):
)
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
@ -288,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]
@ -299,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