Commit 3cf15346 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[widgets] Port asset related widgets to use Asset

Targets are:
- AssetBrowser
- AssetWidget
- MainWindow

and also:

- Start script
parent 2965b338
......@@ -48,7 +48,8 @@ from beat.cmdline.decorators import verbosity_option
from ..utils import setup_logger
from ..widgets.mainwindow import MainWindow
from ..widgets.assetwidget import AssetWidget
from ..backend.assetmodel import AssetType
from ..backend.asset import AssetType
from ..backend.asset import Asset
from .. import version
......@@ -157,13 +158,9 @@ def edit(ctx, asset_type, asset_name):
asset_widget = AssetWidget()
asset_widget.set_context(ctx)
asset_path = os.path.join(
ctx.meta["config"].path,
AssetType[asset_type.upper()].path,
asset_name + ".json",
)
asset_widget.load_json(asset_path)
asset_type = AssetType[asset_type.upper()]
asset = Asset(ctx.meta["config"].path, asset_type, asset_name)
asset_widget.loadAsset(asset)
asset_widget.show()
return app.exec_()
......
......@@ -92,7 +92,7 @@ class TestAssetWidget:
(asset_name, editor_type),
) in asset_type_prefix_entry_map.items():
asset = Asset(test_prefix, asset_type, asset_name)
asset_widget.load_json(asset.declaration_path)
asset_widget.loadAsset(asset)
assert isinstance(asset_widget.current_editor, editor_type)
def test_dirty(
......@@ -112,7 +112,7 @@ class TestAssetWidget:
(asset_name, editor_type),
) in asset_type_prefix_entry_map.items():
asset = Asset(test_prefix, asset_type, asset_name)
asset_widget.load_json(asset.declaration_path)
asset_widget.loadAsset(asset)
assert not asset_widget.current_editor.isDirty()
with qtbot.waitSignal(asset_widget.json_widget.textChanged):
......@@ -144,7 +144,7 @@ class TestAssetWidget:
(asset_name, editor_type),
) in asset_type_prefix_entry_map.items():
asset = Asset(test_prefix, asset_type, asset_name)
asset_widget.load_json(asset.declaration_path)
asset_widget.loadAsset(asset)
with qtbot.waitSignal(asset_widget.json_widget.textChanged):
asset_widget.current_editor.description_lineedit.selectAll()
......@@ -302,7 +302,7 @@ class TestAssetWidget:
monkeypatch.setattr(QMessageBox, "question", lambda *args: QMessageBox.Yes)
asset = Asset(test_prefix, asset_type, asset_name)
asset_widget.load_json(asset.declaration_path)
asset_widget.loadAsset(asset)
assert asset_widget.current_editor.asset_type != AssetType.UNKNOWN
asset_widget.deleteAsset(asset.declaration_path)
assert asset_widget.current_editor.asset_type == AssetType.UNKNOWN
......
......@@ -36,7 +36,8 @@ from PyQt5.QtWidgets import QTreeView
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
from ..backend.assetmodel import AssetType
from ..backend.asset import AssetType
from ..backend.asset import Asset
class AssetFilterProxyModel(QSortFilterProxyModel):
......@@ -90,7 +91,7 @@ class AssetBrowser(QWidget):
Widget that will allow to browse the various assets
"""
jsonSelected = pyqtSignal(["QString"])
assetSelected = pyqtSignal([Asset])
deletionRequested = pyqtSignal(["QString"])
def __init__(self, parent=None):
......@@ -124,7 +125,12 @@ class AssetBrowser(QWidget):
source_index = self.proxy_model.mapToSource(index)
if self.filesystem_model.type(source_index).lower() == "json file":
self.jsonSelected.emit(self.filesystem_model.filePath(source_index))
self.assetSelected.emit(
Asset.from_path(
self.filesystem_model.rootPath(),
self.filesystem_model.filePath(source_index),
)
)
@pyqtSlot("QPoint")
def __openMenu(self, position):
......
......@@ -38,7 +38,8 @@ from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QStackedWidget
from PyQt5.QtWidgets import QMessageBox
from ..backend.assetmodel import AssetType
from ..backend.asset import AssetType
from ..backend.asset import Asset
from ..utils import frozen
from .editor import PlaceholderEditor
......@@ -125,7 +126,7 @@ class AssetWidget(QWidget):
super(AssetWidget, self).__init__(parent)
self.context = None
self.current_json = None
self.current_asset = None
self.watcher = QFileSystemWatcher()
self.update_timer = QTimer()
self.update_timer.setSingleShot(True)
......@@ -180,18 +181,6 @@ class AssetWidget(QWidget):
return self.context.meta["config"].path
def __asset_type_for_path(self, path):
"""Returns the AssetType matching the path given
:param path str: path to asset
:return: AssetType matching the path given
"""
asset_path = path[len(self.prefix_root_path) :] # noqa
asset_str = asset_path.split("/")[1]
return AssetType.from_path(asset_str)
def __update_content(self):
"""Update the content of this widget"""
......@@ -201,14 +190,13 @@ class AssetWidget(QWidget):
# Nothing was connected yet
pass
with open(self.current_json) as json_file:
with open(self.current_asset.declaration_path) as json_file:
json_data = json_file.read()
self.json_widget.setText(json_data)
asset_type = self.__asset_type_for_path(self.current_json)
editor = self.editors_type[asset_type]
editor = self.editors_type[self.current_asset.type]
editor.load_json(json.loads(json_data))
editor.dataChanged.connect(self.update_timer.start)
self.set_current_editor(asset_type)
self.set_current_editor(self.current_asset.type)
self.save_button.setEnabled(False)
......@@ -234,7 +222,7 @@ class AssetWidget(QWidget):
self.tr("File changed"),
self.tr(
"The file:\n{}\nhas changed on disk.\n"
"Do you want to reload it ?".format(self.current_json)
"Do you want to reload it ?".format(self.current_asset.declaration_path)
),
)
......@@ -289,7 +277,7 @@ class AssetWidget(QWidget):
:param file_path str: path to the json file of the asset to delete
"""
if self.current_json == file_path:
if self.current_asset.declaration_path == file_path:
"""Check before deletion"""
answer = QMessageBox.question(
self,
......@@ -303,28 +291,28 @@ class AssetWidget(QWidget):
self.set_current_editor(AssetType.UNKNOWN)
self.json_widget.clear()
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):
self.current_asset.delete()
self.current_asset = None
else:
asset = Asset.from_path(self.prefix_root_path, file_path)
asset.delete()
@pyqtSlot(Asset)
def loadAsset(self, asset):
""" Load the content of the file given in parameter
:param file_path str: path to the json file to load
:param asset Asset: asset to edit
"""
if self.current_json == file_path and not self.current_editor.isDirty():
if self.current_asset == asset and not self.current_editor.isDirty():
return
self.maybe_save()
self.current_json = file_path
self.current_asset = asset
self.__clear_watcher()
self.watcher.addPath(self.current_json)
self.watcher.addPath(self.current_asset.declaration_path)
self.__update_content()
......@@ -335,13 +323,14 @@ class AssetWidget(QWidget):
json_data = self.current_editor.dump_json()
# Currently merging until we have all editors implemented
with open(self.current_json, "rt") as json_file:
declaration_path = self.current_asset.declaration_path
with open(declaration_path, "rt") as json_file:
original = json.loads(json_file.read())
original.update(json_data)
with FileBlocker(self.watcher, self.current_json):
with open(self.current_json, "wt") as json_file:
with FileBlocker(self.watcher, declaration_path):
with open(declaration_path, "wt") as json_file:
json_file.write(json.dumps(original, sort_keys=True, indent=4))
self.current_editor.clearDirty()
......
......@@ -69,7 +69,7 @@ class MainWindow(QMainWindow):
centralWidget.addWidget(self.assetWidget)
self.setCentralWidget(centralWidget)
self.assetBrowser.jsonSelected.connect(self.assetWidget.load_json)
self.assetBrowser.assetSelected.connect(self.assetWidget.loadAsset)
self.assetBrowser.deletionRequested.connect(self.assetWidget.deleteAsset)
quitAction.triggered.connect(qApp.quit)
aboutAction.triggered.connect(self.showAbout)
......
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