Skip to content

Commit

Permalink
Impact Category Duplication Fixes (#1171)
Browse files Browse the repository at this point in the history
* Impact Category Duplication Fixes

* Fix error checking

---------

Co-authored-by: marc-vdm <[email protected]>
  • Loading branch information
mrvisscher and marc-vdm authored Dec 20, 2023
1 parent 379c76f commit 4baf486
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
24 changes: 13 additions & 11 deletions activity_browser/controllers/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 19 additions & 11 deletions activity_browser/ui/widgets/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit 4baf486

Please sign in to comment.