Skip to content

Commit

Permalink
Fix traceback from add entities dialog (#2881)
Browse files Browse the repository at this point in the history
  • Loading branch information
PiispaH authored Jul 9, 2024
1 parent 4e25527 commit d56a0eb
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 7 deletions.
2 changes: 1 addition & 1 deletion spinetoolbox/mvcmodels/minimal_table_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def setData(self, index, value, role=Qt.ItemDataRole.EditRole):
"""Set data in model."""
if not index.isValid():
return False
if role not in (Qt.ItemDataRole.DisplayRole, Qt.ItemDataRole.EditRole):
if role not in (Qt.ItemDataRole.DisplayRole, Qt.ItemDataRole.EditRole, Qt.ItemDataRole.UserRole):
return False
return self.batch_set_data([index], [value])

Expand Down
2 changes: 1 addition & 1 deletion spinetoolbox/spine_db_editor/mvcmodels/empty_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def batch_set_data(self, indexes, data):
column = index.column()
if column == self.header.index(self._parent.dialog_item_name()) and not value:
self._entity_name_user_defined = False
self._main_data[row][column] = self._parent.construct_composite_class_name(index.row())
self._main_data[row][column] = self._parent.construct_composite_name(index.row())
else:
self._main_data[row][column] = value
rows.append(row)
Expand Down
24 changes: 20 additions & 4 deletions spinetoolbox/spine_db_editor/widgets/add_items_dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ def _handle_model_data_changed(self, top_left, bottom_right, roles):
top = top_left.row()
bottom = bottom_right.row()
for row in range(top, bottom + 1):
relationship_class_name = self.construct_composite_class_name(row)
entity_class_name = self.construct_composite_name(row)
self.model.setData(
self.model.index(row, self.number_of_dimensions), relationship_class_name, role=Qt.ItemDataRole.UserRole
self.model.index(row, self.number_of_dimensions), entity_class_name, role=Qt.ItemDataRole.UserRole
)

@Slot()
Expand Down Expand Up @@ -335,7 +335,7 @@ def accept(self):
self.db_mngr.add_entity_classes(db_map_data)
super().accept()

def construct_composite_class_name(self, row):
def construct_composite_name(self, row):
"""Returns a ND entity class name from all the currently selected dimension names.
Args:
Expand Down Expand Up @@ -430,7 +430,7 @@ def _handle_model_data_changed(self, top_left, bottom_right, roles):
for row in range(top, bottom + 1):
el_names = [n for n in (self.model.index(row, j).data() for j in range(dimension_count)) if n]
entity_name = name_from_elements(el_names)
self.model.setData(self.model.index(row, dimension_count), entity_name)
self.model.setData(self.model.index(row, dimension_count), entity_name, role=Qt.ItemDataRole.UserRole)


class AddEntitiesDialog(AddEntitiesOrManageElementsDialog):
Expand Down Expand Up @@ -481,6 +481,22 @@ def __init__(self, parent, item, db_mngr, *db_maps, force_default=False, commit_
self.ent_cls_combo_box.setCurrentIndex(current_index)
self.connect_signals()

def construct_composite_name(self, row):
"""Returns a ND entity name from the currently selected element names.
Args:
row (int): The index of the row.
Returns:
str: The name of the entity
"""
el_names = [
n
for n in (self.model.index(row, j).data() for j in range(len(self.entity_class["dimension_name_list"])))
if n
]
return name_from_elements(el_names)

def _class_key_to_str(self, key, *db_maps):
class_name = self.db_map_ent_cls_lookup[db_maps[0]][key]["name"]
if len(db_maps) == len(self.db_maps):
Expand Down
57 changes: 56 additions & 1 deletion tests/spine_db_editor/widgets/test_add_items_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
from PySide6.QtWidgets import QApplication
from spinetoolbox.spine_db_manager import SpineDBManager
from spinetoolbox.spine_db_editor.widgets.spine_db_editor import SpineDBEditor
from spinetoolbox.spine_db_editor.widgets.add_items_dialogs import AddEntityClassesDialog, ManageElementsDialog
from spinetoolbox.spine_db_editor.widgets.add_items_dialogs import (
AddEntityClassesDialog,
ManageElementsDialog,
AddEntitiesDialog,
)
from tests.spine_db_editor.helpers import TestBase


Expand Down Expand Up @@ -196,6 +200,57 @@ def test_composite_name_functionality(self):
result = [model.index(0, column).data() for column in range(model.columnCount())]
self.assertEqual(expected, result)

def test_add_entities_dialog_autofill(self):
"""Test that the autofill also works for the add entities dialog."""
self._db_mngr.add_entity_classes(
{self._db_map: [{"name": "first_class", "id": 1}, {"name": "second_class", "id": 2}]}
)
self._db_mngr.add_entity_classes(
{self._db_map: [{"name": "entity_class", "id": 3, "dimension_id_list": [1, 2]}]}
)
self._db_mngr.add_entities(
{
self._db_map: [
{"class_id": 1, "name": "entity_1", "id": 1},
{"class_id": 2, "name": "entity_2", "id": 2},
]
}
)
for item in self._db_editor.entity_tree_model.visit_all():
while item.can_fetch_more():
item.fetch_more()
qApp.processEvents()
entity_classes = self._db_editor.entity_tree_model.root_item.children
dialog = AddEntitiesDialog(self._db_editor, entity_classes[2], self._db_mngr, self._db_map)
model = dialog.model
header = model.header
model.fetchMore(QModelIndex())
self.assertEqual(
header,
("first_class", "second_class", "entity name", "alternative", "entity group", "databases"),
)
indexes = [model.index(0, header.index(field)) for field in ("first_class", "second_class", "entity name")]
values = ["entity_1"]
model.batch_set_data([indexes[0]], values)
expected = ["entity_1", None, "entity_1__", "Base", None, "mock_db"]
result = [model.index(0, column).data() for column in range(model.columnCount())]
self.assertEqual(expected, result)
value = "entity_name"
model.setData(indexes[2], value)
expected = ["entity_1", None, "entity_name", "Base", None, "mock_db"]
result = [model.index(0, column).data() for column in range(model.columnCount())]
self.assertEqual(expected, result)
value = "End"
model.setData(indexes[1], value)
expected = ["entity_1", "End", "entity_name", "Base", None, "mock_db"]
result = [model.index(0, column).data() for column in range(model.columnCount())]
self.assertEqual(expected, result)
values = [None, None]
model.batch_set_data(indexes[1:3], values)
expected = ["entity_1", None, "entity_1__", "Base", None, "mock_db"]
result = [model.index(0, column).data() for column in range(model.columnCount())]
self.assertEqual(expected, result)

@staticmethod
def _paste_to_table_view(text, dialog):
mock_clipboard = mock.MagicMock()
Expand Down

0 comments on commit d56a0eb

Please sign in to comment.