Commit 95cf4bf4 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[widgets] Implement v1 database viewer

Part of #183
parent 0dc00cf0
......@@ -23,16 +23,25 @@
# #
###############################################################################
import simplejson as json
from ..backend.assetmodel import AssetModel
from ..backend.assetmodel import AssetType
from ..widgets.databaseeditor import DatabaseEditor
class TestDatabaseEditor:
"""Test that the mock editor works correctly"""
def test_load_and_dump(self, qtbot):
reference_json = {"description": "test"}
editor = DatabaseEditor()
def test_load_and_dump(self, qtbot, test_prefix):
asset_model = AssetModel()
asset_model.asset_type = AssetType.DATABASE
asset_model.prefix_path = test_prefix
asset_name = "simple/1"
editor.load_json(reference_json)
with open(asset_model.json_path(asset_name), "rt") as json_file:
json_data = json.load(json_file)
editor = DatabaseEditor()
editor.load_json(json_data)
assert editor.dump_json() == reference_json
assert editor.dump_json() == json_data
......@@ -23,12 +23,134 @@
# #
###############################################################################
from PyQt5.QtWidgets import QFormLayout
from PyQt5.QtWidgets import QHeaderView
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QScrollArea
from PyQt5.QtWidgets import QStackedWidget
from PyQt5.QtWidgets import QTableWidget
from PyQt5.QtWidgets import QTableWidgetItem
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
from ..backend.assetmodel import AssetModel
from ..backend.assetmodel import AssetType
from ..utils import frozen
from .editor import AbstractAssetEditor
class SetViewer(QWidget):
def __init__(self, parent=None):
super(SetViewer, self).__init__(parent)
self.name_label = QLabel()
self.template_label = QLabel()
self.view_label = QLabel()
self.outputs_tablewidget = QTableWidget()
self.outputs_tablewidget.verticalHeader().setVisible(False)
self.outputs_tablewidget.setColumnCount(2)
self.outputs_tablewidget.horizontalHeader().setSectionResizeMode(
QHeaderView.Stretch
)
self.outputs_tablewidget.setHorizontalHeaderLabels(
[self.tr("Name"), self.tr("Type")]
)
self.outputs_tablewidget.setEditTriggers(QTableWidget.NoEditTriggers)
self.outputs_tablewidget.setMinimumHeight(200)
self.form_layout = QFormLayout(self)
self.form_layout.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow)
self.form_layout.addRow(self.tr("Name"), self.name_label)
self.form_layout.addRow(self.tr("Template"), self.template_label)
self.form_layout.addRow(self.tr("View"), self.view_label)
self.form_layout.addRow(self.tr("Outputs"), self.outputs_tablewidget)
def load(self, json_data):
self.name_label.setText(json_data["name"])
self.template_label.setText(json_data["template"])
self.view_label.setText(json_data["view"])
outputs = json_data["outputs"]
self.outputs_tablewidget.setRowCount(len(outputs))
row = 0
for name, type_ in outputs.items():
name_item = QTableWidgetItem(name)
type_item = QTableWidgetItem(type_)
self.outputs_tablewidget.setItem(row, 0, name_item)
self.outputs_tablewidget.setItem(row, 1, type_item)
row += 1
class ProtocolViewer(QWidget):
def __init__(self, parent=None):
super(ProtocolViewer, self).__init__(parent)
self.set_viewers = []
self.name_label = QLabel()
self.template_label = QLabel()
self.sets_layout = QVBoxLayout()
self.sets_layout.addStretch(1)
self.form_layout = QFormLayout(self)
self.form_layout.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow)
self.form_layout.addRow(self.tr("Name"), self.name_label)
self.form_layout.addRow(self.tr("Template"), self.template_label)
self.form_layout.addRow(self.tr("Sets"), self.sets_layout)
def load(self, json_data):
self.name_label.setText(json_data["name"])
self.template_label.setText(json_data["template"])
for set in json_data["sets"]:
set_viewer = SetViewer()
set_viewer.load(set)
index = self.sets_layout.count() - 1
self.sets_layout.insertWidget(index, set_viewer, 2)
self.set_viewers.append(set_viewer)
class DatabaseV1Viewer(QWidget):
def __init__(self, parent=None):
super(DatabaseV1Viewer, self).__init__(parent)
self.protocol_viewers = []
container_widget = QWidget()
self.container_widget_layout = QVBoxLayout(container_widget)
self.container_widget_layout.addStretch(1)
scroll_area = QScrollArea()
scroll_area.setWidget(container_widget)
scroll_area.setWidgetResizable(True)
layout = QVBoxLayout(self)
layout.addWidget(scroll_area)
self.json_object = None
def load(self, json_object):
while self.protocol_viewers:
widget = self.protocol_viewers[0]
self.container_widget_layout.removeWidget(widget)
self.protocol_viewers.pop(self.protocol_viewers.index(widget))
widget.setParent(None)
for protocol in json_object["protocols"]:
protocol_viewer = ProtocolViewer()
protocol_viewer.load(protocol)
self.protocol_viewers.append(protocol_viewer)
index = self.container_widget_layout.count() - 1
self.container_widget_layout.insertWidget(index, protocol_viewer, 2)
self.json_object = json_object
def dump(self):
return self.json_object
@frozen
class DatabaseEditor(AbstractAssetEditor):
def __init__(self, parent=None):
......@@ -36,10 +158,36 @@ class DatabaseEditor(AbstractAssetEditor):
self.setObjectName(self.__class__.__name__)
self.set_title(self.tr("Database"))
self.protocoltemplate_model = AssetModel()
self.protocoltemplate_model.asset_type = AssetType.PROTOCOLTEMPLATE
self.database_v1_viewer = DatabaseV1Viewer()
self.stacked_widget = QStackedWidget()
self.stacked_widget.addWidget(self.database_v1_viewer)
self.layout().addWidget(self.stacked_widget, 1)
self.database_widget.dataChanged.connect(self.dataChanged)
def set_prefix_root(self, prefix):
"""Re-impl"""
self.protocoltemplate_model.prefix_path = prefix
def _load_json(self, json_object):
"""Load the json object passed as parameter"""
pass
self.description_lineedit.setReadOnly(self.schema_version in [1, None])
if self.schema_version in [1, None]:
self.set_title(self.tr("Database V1 Read Only"))
self.database_v1_viewer.load(json_object)
self.stacked_widget.setCurrentWidget(self.database_v1_viewer)
else:
self.set_title(self.tr("Database"))
def _dump_json(self):
"""Returns the json representation of the asset"""
return {}
return self.stacked_widget.currentWidget().dump()
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