From 6ac6c455c548ab1ebe76ca3c18a4e86c4a07b70e Mon Sep 17 00:00:00 2001 From: krzywon Date: Tue, 29 Oct 2024 10:36:49 -0400 Subject: [PATCH] Abstract the empty row removal tool and apply to the PD and regular params in the model editor window --- src/sas/qtgui/Utilities/PluginDefinition.py | 61 ++++++++------------- 1 file changed, 24 insertions(+), 37 deletions(-) diff --git a/src/sas/qtgui/Utilities/PluginDefinition.py b/src/sas/qtgui/Utilities/PluginDefinition.py index 4803ce9a1c..dada924d69 100644 --- a/src/sas/qtgui/Utilities/PluginDefinition.py +++ b/src/sas/qtgui/Utilities/PluginDefinition.py @@ -5,12 +5,25 @@ from sas.qtgui.Utilities.UI.PluginDefinitionUI import Ui_PluginDefinition from sas.qtgui.Utilities import GuiUtils -# txtName -# txtDescription -# chkOverwrite -# tblParams -# tblParamsPD -# txtFunction + +def remove_empty_table_rows(tbl: QtWidgets.QTableWidget): + """A helper function to remove empty rows in a PySide Table, if there are more than two empty rows at the end. + This function ensures there is always an empty row in the table. + + :param tbl: A QTableWidget on i rows and j columns. + """ + for i in range(0, tbl.rowCount()): + for j in range(0, tbl.columnCount()): + cell_contents = tbl.item(i, j) + if cell_contents is None or cell_contents.text() == "": + # cell_contents may be None: Ensure this check remains, otherwise the .text() call may throw an error + pass + else: + break + else: + tbl.removeRow(i) + tbl.insertRow(tbl.rowCount()) + class PluginDefinition(QtWidgets.QDialog, Ui_PluginDefinition): """ @@ -132,27 +145,14 @@ def onParamsChanged(self, row, column): self.parameter_dict[row] = (param, value) self.model['parameters'] = self.parameter_dict - # Check if the update was Value for last row. If so, add a new row - if row == self.tblParams.rowCount() - 1: - # Add a row - self.tblParams.insertRow(self.tblParams.rowCount()) - - # Check to see if the last two rows are empty. If so, remove the last row - remove_last_row = 0 - for i in range(2): - for j in range(2): - cell_contents = self.tblParams.item(self.tblParams.rowCount() - (2 - i), j) - if cell_contents == None or cell_contents.text() == "": - remove_last_row += 1 - if remove_last_row == 4: - # If all four last cells are empty, remove the last row - self.tblParams.removeRow(self.tblParams.rowCount() - 1) + # Check if there are empty rows. + remove_empty_table_rows(self.tblParams) self.modelModified.emit() def onParamsPDChanged(self, row, column): """ - Respond to changes in non-polydisperse parameter table + Respond to changes in polydisperse parameter table """ param = value = None if self.tblParamsPD.item(row, 0): @@ -164,21 +164,8 @@ def onParamsPDChanged(self, row, column): self.pd_parameter_dict[row] = (param, value) self.model['pd_parameters'] = self.pd_parameter_dict - # Check if the update was Value for last row. If so, add a new row - if row == self.tblParamsPD.rowCount() - 1: - # Add a row - self.tblParamsPD.insertRow(self.tblParamsPD.rowCount()) - - # Check to see if the last two rows are empty. If so, remove the last row - remove_last_row = 0 - for i in range(2): - for j in range(2): - cell_contents = self.tblParamsPD.item(self.tblParamsPD.rowCount() - (2 - i), j) - if cell_contents == None or cell_contents.text() == "": - remove_last_row += 1 - if remove_last_row == 4: - # If all four last cells are empty, remove the last row - self.tblParamsPD.removeRow(self.tblParamsPD.rowCount() - 1) + # Check if there are empty rows. + remove_empty_table_rows(self.tblParamsPD) self.modelModified.emit()