From 6b2f2a115add8b985846de3f627b8f30e84cd706 Mon Sep 17 00:00:00 2001 From: Marcel Zwiers Date: Thu, 23 Jan 2025 01:36:19 +0100 Subject: [PATCH] Add/remove rows of the `Participant data` table (GitHub issue #253) --- bidscoin/bidseditor.py | 48 +++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/bidscoin/bidseditor.py b/bidscoin/bidseditor.py index 1ddb06e1..035e71a7 100755 --- a/bidscoin/bidseditor.py +++ b/bidscoin/bidseditor.py @@ -575,6 +575,8 @@ def fill_participant_table(self, dataformat: str): edit_button.setToolTip('Data for participants json sidecar-file') edit_button.clicked.connect(self.edit_metadata) participant_table.setCellWidget(n, 2, edit_button) + participant_table.setItem(n+1, 0, MyQTableItem()) + participant_table.setItem(n+1, 1, MyQTableItem()) participant_table.show() participant_table.blockSignals(False) @@ -592,21 +594,37 @@ def participant_table2bidsmap(self, rowindex: int, colindex: int): # Only if cell was actually clicked, update if self.tabwidget.currentIndex() < 0: return - dataformat = self.tabwidget.currentWidget().objectName() - if colindex == 1 and dataformat in self.dataformats: - key = self.participant_table[dataformat].item(rowindex, 0).text().strip() - value = self.participant_table[dataformat].item(rowindex, 1).text().strip() - oldvalue = self.output_bidsmap.dataformat(dataformat).participant[key]['value'] - if oldvalue is None: - oldvalue = '' + dataformat = self.tabwidget.currentWidget().objectName() + participant_data = self.output_bidsmap.dataformat(dataformat).participant + participant_table = self.participant_table[dataformat] + key = participant_table.item(rowindex, 0).text().strip() + value = participant_table.item(rowindex, 1).text().strip() + + # Check if the participant keys are still present in the participant_table + datachanged = False + if colindex == 0: + for key_ in participant_data.copy(): + if key_ not in [participant_table.item(row, 0).text().strip() for row in range(participant_table.rowCount()-1)]: + participant_data.pop(key_, None) + datachanged = True + + # Add the key-value data to the participant data + if key: + if key not in participant_data: + LOGGER.verbose(f"User adds {dataformat}['{key}']") + participant_data[key] = {'value': value, 'meta': {}} + datachanged = True + else: + if value != (oldvalue := participant_data[key]['value']): + LOGGER.verbose(f"User sets {dataformat}['{key}'] from '{oldvalue}' to '{value}'") + participant_data[key]['value'] = value + datachanged = True - # Only if cell content was changed, update - if key and value != oldvalue: - LOGGER.verbose(f"User sets {dataformat}['{key}'] from '{oldvalue}' to '{value}'") - self.output_bidsmap.dataformat(dataformat).participant[key]['value'] = value - self.fill_participant_table(dataformat) - self.fill_samples_table(dataformat) - self.datachanged = True + # Only if cell content was changed, update + if datachanged: + self.fill_participant_table(dataformat) + self.fill_samples_table(dataformat) + self.datachanged = True def edit_metadata(self): """Pop-up a text window to edit the sidecar metadata of participant item""" @@ -618,7 +636,7 @@ def edit_metadata(self): key = participant_table.item(rowindex, 0).text().strip() meta = self.output_bidsmap.dataformat(dataformat).participant[key]['meta'] - text, ok = QtWidgets.QInputDialog.getMultiLineText(self, f"Edit sidecar metadata for {key}", 'json data', text=json.dumps(meta, indent=2)) + text, ok = QtWidgets.QInputDialog.getMultiLineText(self, 'Edit sidecar metadata', f"json metadata for '{key}':", text=json.dumps(meta, indent=2)) if ok: try: meta_ = json.loads(text)