Commit e14583a7 authored by Flavio TARSETTI's avatar Flavio TARSETTI Committed by Samuel GAIST
Browse files

[widgets/test][plottereditor] Implemented PlotterEditor

Fixes #181
parent d9f346e8
......@@ -23,17 +23,89 @@
# #
###############################################################################
import pytest
import simplejson as json
from ..backend.asset import AssetType
from ..backend.asset import Asset
from ..backend.assetmodel import AssetModel
from ..widgets.plottereditor import ParameterViewer
from ..widgets.plottereditor import PlotterEditor
@pytest.fixture()
def dataformat_model(test_prefix):
asset_model = AssetModel()
asset_model.asset_type = AssetType.DATAFORMAT
asset_model.prefix_path = test_prefix
return asset_model
@pytest.fixture()
def reference_parameter_json():
return {
"axis-fontsize": {
"default": 10,
"description": "Controls the axis font size (labels and values)",
"type": "uint16",
}
}
class TestParameterViewer:
"""Test that the viewer dedicated to the parameters work correctly"""
def test_load_and_dump(self, qtbot, reference_parameter_json):
parameter_viewer = ParameterViewer()
key = next(iter(reference_parameter_json))
value = reference_parameter_json.get(key, {})
parameter_viewer.load(key, value)
assert parameter_viewer.dump() == reference_parameter_json
def test_get_name(self, qtbot, reference_parameter_json):
parameter_viewer = ParameterViewer()
key = next(iter(reference_parameter_json))
value = reference_parameter_json.get(key, {})
parameter_viewer.load(key, value)
assert parameter_viewer.dump() == reference_parameter_json
assert parameter_viewer.name() == key
class TestPlotterEditor:
"""Test that the mock editor works correctly"""
"""Test that the plotter editor works correctly"""
def test_load_and_dump(self, qtbot, dataformat_model, test_prefix):
asset_name = "user/scatter/1"
asset = Asset(test_prefix, AssetType.PLOTTER, asset_name)
with open(asset.declaration_path, "rt") as json_file:
json_data = json.load(json_file)
editor = PlotterEditor()
editor.set_dataformat_model_to_combobox(dataformat_model)
editor.load_json(json_data)
qtbot.addWidget(editor)
assert editor.dump_json() == json_data
def test_change_dataformat(self, qtbot, dataformat_model, test_prefix):
asset_name = "user/scatter/1"
asset = Asset(test_prefix, AssetType.PLOTTER, asset_name)
with open(asset.declaration_path, "rt") as json_file:
json_data = json.load(json_file)
editor = PlotterEditor()
editor.set_dataformat_model_to_combobox(dataformat_model)
editor.load_json(json_data)
qtbot.addWidget(editor)
def test_load_and_dump(self, qtbot):
reference_json = {"description": "test"}
editor = PlotterEditor()
assert editor.dump_json() == json_data
editor.load_json(reference_json)
asset_list = dataformat_model.stringList()
qtbot.keyClicks(editor.dataformat_combobox, asset_list[1])
assert editor.dump_json() == reference_json
assert editor.dump_json()["dataformat"] == asset_list[1]
......@@ -23,10 +23,83 @@
# #
###############################################################################
from ..backend.asset import AssetType
from PyQt5.QtCore import Qt
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QComboBox
from PyQt5.QtWidgets import QFormLayout
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QInputDialog
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QWidget
from ..utils import frozen
from ..backend.asset import AssetType
from ..backend.assetmodel import AssetModel
from .scrollwidget import ScrollWidget
from .parameterwidget import ParameterWidget
from .editor import AbstractAssetEditor
from .libraries import LibrariesWidget
class ParameterViewer(QWidget):
dataChanged = pyqtSignal()
deletionRequested = pyqtSignal()
def __init__(self, parent=None):
super(ParameterViewer, self).__init__(parent)
self.delete_button = QPushButton(self.tr("-"))
self.delete_button.setFixedSize(30, 30)
delete_layout = QHBoxLayout()
delete_layout.addStretch(1)
delete_layout.addWidget(self.delete_button)
self.name_lineedit = QLineEdit()
self.parameter_widget = ParameterWidget()
form_layout = QFormLayout()
form_layout.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow)
form_layout.addRow(self.tr("Name"), self.name_lineedit)
layout = QVBoxLayout(self)
layout.addLayout(delete_layout)
layout.addLayout(form_layout)
layout.addWidget(self.parameter_widget)
self.name_lineedit.textChanged.connect(self.dataChanged)
self.delete_button.clicked.connect(self.deletionRequested)
self.parameter_widget.dataChanged.connect(self.dataChanged)
def name(self):
"""Name of the parameter"""
return self.name_lineedit.text()
def load(self, name, data):
"""Load this widget with the content of json_data"""
self.name_lineedit.setText(name)
self.parameter_widget.load(data)
def dump(self):
"""Returns the json representation of this set"""
parameter_name = self.name_lineedit.text()
parameter_data = self.parameter_widget.dump()
return {parameter_name: parameter_data}
@frozen
......@@ -36,10 +109,155 @@ class PlotterEditor(AbstractAssetEditor):
self.setObjectName(self.__class__.__name__)
self.set_title(self.tr("Plotter"))
self.plotter_model = AssetModel()
self.plotter_model.asset_type = AssetType.PLOTTER
self.dataformat_model = AssetModel()
self.dataformat_model.asset_type = AssetType.DATAFORMAT
self.library_model = AssetModel()
self.library_model.asset_type = AssetType.LIBRARY
self.libraries_widget = LibrariesWidget()
self.scroll_widget = ScrollWidget()
self.add_parameter_button = QPushButton(self.tr("+"))
self.add_parameter_button.setFixedSize(30, 30)
self.dataformat_combobox = QComboBox(parent)
self.dataformat_combobox.setModel(self.dataformat_model)
self.language = ""
dataformat_label = QLabel(self.tr("Dataformat"))
self.layout().addWidget(dataformat_label)
self.layout().addWidget(self.dataformat_combobox)
self.layout().addWidget(self.scroll_widget, 1)
self.layout().addWidget(self.libraries_widget)
self.layout().addWidget(self.add_parameter_button, 1)
self.libraries_widget.dataChanged.connect(self.dataChanged)
self.scroll_widget.dataChanged.connect(self.dataChanged)
self.libraries_widget.dataChanged.connect(self.dataChanged)
self.add_parameter_button.clicked.connect(self.__addParameter)
self.contextChanged.connect(
lambda: self.plotter_model.setPrefixPath(self.prefix_path)
)
self.contextChanged.connect(
lambda: self.dataformat_model.setPrefixPath(self.prefix_path)
)
self.contextChanged.connect(
lambda: self.library_model.setPrefixPath(self.prefix_path)
)
def set_dataformat_model_to_combobox(self, dataformat_model):
self.dataformat_combobox.setModel(dataformat_model)
@property
def parameter_viewers(self):
return self.scroll_widget.widget_list
@pyqtSlot()
def __onRemoveRequested(self):
"""Remove the widget clicked"""
self.__remove_widget(self.sender())
def __remove_widget(self, widget):
"""Removes the widget that which signal triggered this slot"""
self.scroll_widget.removeWidget(widget)
@pyqtSlot()
def __addParameter(self):
"""Add a new parameter"""
parameter_names = [widget.name() for widget in self.parameter_viewers]
while True:
name, ok_pressed = QInputDialog.getText(
self, self.tr("Parameter name"), self.tr("Name")
)
if not ok_pressed:
break
if ok_pressed and name not in parameter_names:
new_parameter = {name: ParameterWidget().dump()}
self.__load_parameter({"parameters": new_parameter})
self.dataChanged.emit()
break
@pyqtSlot()
def __showLatestParameter(self):
"""Ensure that the latest set is visible"""
if self.parameter_viewers:
self.scroll_widget.ensureWidgetVisible(self.parameter_viewers[-1], 10, 10)
def __load_parameter(self, json_object):
parameters = json_object.get("parameters", {})
for name, data in parameters.items():
parameter_viewer = ParameterViewer()
parameter_viewer.load(name, data)
self.scroll_widget.addWidget(parameter_viewer)
parameter_viewer.deletionRequested.connect(self.__onRemoveRequested)
def __load_json(self, json_object):
"""Load the json object passed as parameter"""
self.blockSignals(True)
self.language = json_object.get("language", "")
selected_dataformat = json_object.get("dataformat", {})
index = self.dataformat_combobox.findText(
selected_dataformat, Qt.MatchFixedString
)
if index >= 0:
self.dataformat_combobox.setCurrentIndex(index)
else:
raise RuntimeError("Invalid default value")
self.libraries_widget.set_available_libraries(self.library_model.stringList())
self.libraries_widget.set_used_libraries(json_object.get("uses", {}))
self.__load_parameter(json_object)
self.blockSignals(False)
if self.parameter_viewers:
QTimer.singleShot(100, self.__showLatestParameter)
def _load_json(self, json_object):
"""Load the json object passed as parameter"""
pass
self.scroll_widget.clear()
while self.parameter_viewers:
widget = self.parameter_viewers[0]
self.__remove_widget(widget)
self.__load_json(json_object)
def _dump_json(self):
"""Returns the json representation of the asset"""
return {}
parameters = {}
for widget in self.parameter_viewers:
parameters.update(widget.dump())
used_libraries = self.libraries_widget.get_used_libraries()
output = {
"dataformat": self.dataformat_combobox.currentText(),
"parameters": parameters,
"language": self.language,
}
if len(used_libraries) != 0:
output["uses"] = used_libraries
return output
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