From 4baf48649c350c964f8e79d6b4e80f63bbbc5ea4 Mon Sep 17 00:00:00 2001 From: Marin Visscher <103424764+mrvisscher@users.noreply.github.com> Date: Wed, 20 Dec 2023 10:31:52 +0100 Subject: [PATCH] Impact Category Duplication Fixes (#1171) * Impact Category Duplication Fixes * Fix error checking --------- Co-authored-by: marc-vdm --- activity_browser/controllers/project.py | 24 +++++++++++--------- activity_browser/ui/widgets/dialog.py | 30 ++++++++++++++++--------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/activity_browser/controllers/project.py b/activity_browser/controllers/project.py index 6f1443794..92b1c870c 100644 --- a/activity_browser/controllers/project.py +++ b/activity_browser/controllers/project.py @@ -243,17 +243,19 @@ def copy_method(self, method: tuple, level: str = None) -> None: dialog = TupleNameDialog.get_combined_name( self.window, "Impact category name", "Combined name:", method, " - Copy" ) - if dialog.exec_() == TupleNameDialog.Accepted: - new_name = dialog.result_tuple - for mthd in methods: - new_method = new_name + mthd.name[len(new_name):] - if new_method in bw.methods: - warn = "Impact Category with name '{}' already exists!".format(new_method) - QtWidgets.QMessageBox.warning(self.window, "Copy failed", warn) - return - mthd.copy(new_method) - log.info("Copied method {} into {}".format(str(mthd.name), str(new_method))) - signals.new_method.emit() + if dialog.exec_() != TupleNameDialog.Accepted: return + + new_name = dialog.result_tuple + for mthd in methods: + new_method = new_name + mthd.name[len(new_name):] + print('+', mthd) + if new_method in bw.methods: + warn = f"Impact Category with name '{new_method}' already exists!" + QtWidgets.QMessageBox.warning(self.window, "Copy failed", warn) + return + mthd.copy(new_method) + log.info("Copied method {} into {}".format(str(mthd.name), str(new_method))) + signals.new_method.emit() @Slot(tuple, name="deleteMethod") def delete_method(self, method_: tuple, level:str = None) -> None: diff --git a/activity_browser/ui/widgets/dialog.py b/activity_browser/ui/widgets/dialog.py index af18813ac..195483533 100644 --- a/activity_browser/ui/widgets/dialog.py +++ b/activity_browser/ui/widgets/dialog.py @@ -63,7 +63,7 @@ def __init__(self, parent=None): super().__init__(parent) self.name_label = QtWidgets.QLabel("New name") self.view_name = QtWidgets.QLabel() - self.no_comma_validator = QtGui.QRegExpValidator(QRegExp("[^,]+")) + self.input_fields = [] self.buttons = QtWidgets.QDialogButtonBox( QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel, @@ -91,20 +91,27 @@ def combined_names(self) -> str: @property def result_tuple(self) -> tuple: - result = [f.text() for f in self.input_fields if f.text()] - if not self.input_fields[-1].text(): - result.append(self.input_fields[-1].placeholderText()) - return tuple(result) + return tuple([f.text() for f in self.input_fields if f.text()]) @Slot(name="inputChanged") def changed(self) -> None: - """Rebuild the view_name with text from all of the input fields.""" - self.view_name.setText("'({})'".format(self.combined_names)) + """ + Actions when the text within the TupleNameDialog is edited by the user + """ + # rebuild the combined name example + self.view_name.setText(f"'({self.combined_names})'") + + # disable the button (and its outline) when all fields are empty + if self.combined_names == "": + self.buttons.buttons()[0].setDefault(False) + self.buttons.buttons()[0].setDisabled(True) + # enable when that's not the case (anymore) + else: + self.buttons.buttons()[0].setDisabled(False) + self.buttons.buttons()[0].setDefault(True) - def add_input_field(self, text: str, placeholder: str = None) -> None: + def add_input_field(self, text: str) -> None: edit = QtWidgets.QLineEdit(text, self) - edit.setPlaceholderText(placeholder or "") - edit.setValidator(self.no_comma_validator) edit.textChanged.connect(self.changed) self.input_fields.append(edit) self.input_box.layout().addWidget(edit) @@ -126,7 +133,8 @@ def get_combined_name(cls, parent: QtWidgets.QWidget, title: str, label: str, field_content = str(field) # if it's the last element, add extra to the string - if i + 1 == len(fields): field_content += extra + if i + 1 == len(fields): + field_content += extra obj.add_input_field(field_content) obj.input_box.updateGeometry() obj.changed()