Commit 1f017060 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[widgets][assetwidget] Implement support for deletion

parent 7a50ac0a
......@@ -281,3 +281,26 @@ class TestAssetWidget:
assert target_name in asset_model.stringList()
assert os.path.exists(asset_model.json_path(target_name))
def test_delete(
self,
qtbot,
monkeypatch,
test_prefix,
beat_context,
asset_type,
asset_type_prefix_entry_map,
):
asset_widget = AssetWidget()
qtbot.addWidget(asset_widget)
asset_widget.set_context(beat_context)
asset_name = asset_type_prefix_entry_map[asset_type][0]
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.Yes)
asset_path = self.get_asset_path(test_prefix, asset_type, asset_name)
asset_widget.load_json(asset_path)
assert asset_widget.current_editor.asset_type != AssetType.UNKNOWN
asset_widget.delete_asset(asset_path)
assert asset_widget.current_editor.asset_type == AssetType.UNKNOWN
assert not os.path.exists(asset_path)
......@@ -169,6 +169,11 @@ class AssetWidget(QWidget):
return self.editors.currentWidget()
def set_current_editor(self, asset_type):
"""Set the current editor"""
self.editors.setCurrentWidget(self.editors_type[asset_type])
@property
def prefix_root_path(self):
"""Returns the prefix root path"""
......@@ -203,10 +208,17 @@ class AssetWidget(QWidget):
editor = self.editors_type[asset_type]
editor.load_json(json.loads(json_data))
editor.dataChanged.connect(self.update_timer.start)
self.editors.setCurrentWidget(editor)
self.set_current_editor(asset_type)
self.save_button.setEnabled(False)
def __clear_watcher(self):
"""Clears the content of the file system watcher"""
files = self.watcher.files()
if files:
self.watcher.removePaths(files)
@pyqtSlot()
def __enable_save(self):
"""Enable the save button"""
......@@ -270,10 +282,37 @@ class AssetWidget(QWidget):
action_list = [editor.create_action for editor in self.editors_type.values()]
return filter(None, action_list)
@pyqtSlot("QString")
def delete_asset(self, file_path):
"""Delete the requested asset
:param file_path str: path to the json file of the asset to delete
"""
if self.current_json == file_path:
"""Check before deletion"""
answer = QMessageBox.question(
self,
self.tr("Deletion requested"),
self.tr(
"You are about to delete the asset you are currently editing\n\nAre you sure you want to do that ?"
),
)
if answer == QMessageBox.No:
return
self.set_current_editor(AssetType.UNKNOWN)
self.__clear_watcher()
path = file_path[len(self.prefix_root_path) + 1 :]
items = path.split("/")
asset_type = AssetType.from_path(items[0])
asset_name = "/".join(items[1:])[:-5]
asset_type.delete(self.prefix_root_path, asset_name)
def load_json(self, file_path):
""" Load the content of the file given in parameter
:param file_path: path to the json file to load
:param file_path str: path to the json file to load
"""
if self.current_json == file_path and not self.current_editor.is_dirty():
......@@ -283,9 +322,7 @@ class AssetWidget(QWidget):
self.current_json = file_path
files = self.watcher.files()
if files:
self.watcher.removePaths(files)
self.__clear_watcher()
self.watcher.addPath(self.current_json)
self.__update_content()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment