Skip to content

Commit 6b2f2a1

Browse files
committed
Add/remove rows of the Participant data table (GitHub issue #253)
1 parent c7ec40a commit 6b2f2a1

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

bidscoin/bidseditor.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,8 @@ def fill_participant_table(self, dataformat: str):
575575
edit_button.setToolTip('Data for participants json sidecar-file')
576576
edit_button.clicked.connect(self.edit_metadata)
577577
participant_table.setCellWidget(n, 2, edit_button)
578+
participant_table.setItem(n+1, 0, MyQTableItem())
579+
participant_table.setItem(n+1, 1, MyQTableItem())
578580

579581
participant_table.show()
580582
participant_table.blockSignals(False)
@@ -592,21 +594,37 @@ def participant_table2bidsmap(self, rowindex: int, colindex: int):
592594
# Only if cell was actually clicked, update
593595
if self.tabwidget.currentIndex() < 0:
594596
return
595-
dataformat = self.tabwidget.currentWidget().objectName()
596-
if colindex == 1 and dataformat in self.dataformats:
597-
key = self.participant_table[dataformat].item(rowindex, 0).text().strip()
598-
value = self.participant_table[dataformat].item(rowindex, 1).text().strip()
599-
oldvalue = self.output_bidsmap.dataformat(dataformat).participant[key]['value']
600-
if oldvalue is None:
601-
oldvalue = ''
597+
dataformat = self.tabwidget.currentWidget().objectName()
598+
participant_data = self.output_bidsmap.dataformat(dataformat).participant
599+
participant_table = self.participant_table[dataformat]
600+
key = participant_table.item(rowindex, 0).text().strip()
601+
value = participant_table.item(rowindex, 1).text().strip()
602+
603+
# Check if the participant keys are still present in the participant_table
604+
datachanged = False
605+
if colindex == 0:
606+
for key_ in participant_data.copy():
607+
if key_ not in [participant_table.item(row, 0).text().strip() for row in range(participant_table.rowCount()-1)]:
608+
participant_data.pop(key_, None)
609+
datachanged = True
610+
611+
# Add the key-value data to the participant data
612+
if key:
613+
if key not in participant_data:
614+
LOGGER.verbose(f"User adds {dataformat}['{key}']")
615+
participant_data[key] = {'value': value, 'meta': {}}
616+
datachanged = True
617+
else:
618+
if value != (oldvalue := participant_data[key]['value']):
619+
LOGGER.verbose(f"User sets {dataformat}['{key}'] from '{oldvalue}' to '{value}'")
620+
participant_data[key]['value'] = value
621+
datachanged = True
602622

603-
# Only if cell content was changed, update
604-
if key and value != oldvalue:
605-
LOGGER.verbose(f"User sets {dataformat}['{key}'] from '{oldvalue}' to '{value}'")
606-
self.output_bidsmap.dataformat(dataformat).participant[key]['value'] = value
607-
self.fill_participant_table(dataformat)
608-
self.fill_samples_table(dataformat)
609-
self.datachanged = True
623+
# Only if cell content was changed, update
624+
if datachanged:
625+
self.fill_participant_table(dataformat)
626+
self.fill_samples_table(dataformat)
627+
self.datachanged = True
610628

611629
def edit_metadata(self):
612630
"""Pop-up a text window to edit the sidecar metadata of participant item"""
@@ -618,7 +636,7 @@ def edit_metadata(self):
618636
key = participant_table.item(rowindex, 0).text().strip()
619637
meta = self.output_bidsmap.dataformat(dataformat).participant[key]['meta']
620638

621-
text, ok = QtWidgets.QInputDialog.getMultiLineText(self, f"Edit sidecar metadata for {key}", 'json data', text=json.dumps(meta, indent=2))
639+
text, ok = QtWidgets.QInputDialog.getMultiLineText(self, 'Edit sidecar metadata', f"json metadata for '{key}':", text=json.dumps(meta, indent=2))
622640
if ok:
623641
try:
624642
meta_ = json.loads(text)

0 commit comments

Comments
 (0)