Commit e4087575 authored by Samuel GAIST's avatar Samuel GAIST Committed by Flavio TARSETTI

[widgets][toolchaineditor] Add edition enabling to ToolchainWidget

This allows to re-use the widget in the ExperimentEditor
for just visualizing the toolchain.
parent c5f86eb1
...@@ -27,6 +27,7 @@ import pytest ...@@ -27,6 +27,7 @@ import pytest
import simplejson as json import simplejson as json
from PyQt5.QtCore import QFile from PyQt5.QtCore import QFile
from PyQt5.QtWidgets import QGraphicsItem
from PyQt5.QtWidgets import QGraphicsScene from PyQt5.QtWidgets import QGraphicsScene
from PyQt5.QtWidgets import QGraphicsView from PyQt5.QtWidgets import QGraphicsView
...@@ -37,6 +38,7 @@ from ..widgets.toolchaineditor import BlockType ...@@ -37,6 +38,7 @@ from ..widgets.toolchaineditor import BlockType
from ..widgets.toolchaineditor import LoopWidget from ..widgets.toolchaineditor import LoopWidget
from ..widgets.toolchaineditor import ToolchainEditor from ..widgets.toolchaineditor import ToolchainEditor
from ..widgets.toolchaineditor import ToolchainView from ..widgets.toolchaineditor import ToolchainView
from ..widgets.toolchaineditor import ToolchainWidget
from ..widgets.toolchainscene import ToolchainScene from ..widgets.toolchainscene import ToolchainScene
from .conftest import prefix from .conftest import prefix
from .conftest import sync_prefix from .conftest import sync_prefix
...@@ -171,8 +173,33 @@ class TestLoopWidget: ...@@ -171,8 +173,33 @@ class TestLoopWidget:
assert listwidget.item(0).text() == "No valid algorithm found" assert listwidget.item(0).text() == "No valid algorithm found"
class TestToolchainWidget:
"""Test the toolchain widget"""
@pytest.mark.parametrize("toolchain", get_valid_toolchains(prefix))
@pytest.mark.parametrize("editable", [True, False])
def test_editable(self, qtbot, test_prefix, toolchain, editable):
reference_json = get_toolchain_declaration(test_prefix, toolchain)
widget = ToolchainWidget()
widget.show()
qtbot.addWidget(widget)
qtbot.waitForWindowShown(widget)
widget.setEditionEnabled(editable)
assert widget.isEditionEnabled() == editable
assert widget.toolbar.isVisible() == editable
widget.load(reference_json)
reference_flags = QGraphicsItem.ItemIsSelectable | QGraphicsItem.ItemIsMovable
for item in widget.scene.items():
if editable:
assert item.flags() and reference_flags
else:
assert not item.flags() and reference_flags
class TestToolchainEditor: class TestToolchainEditor:
"""Test that the mock editor works correctly""" """Test that the toolchain editor works correctly"""
@pytest.mark.parametrize("toolchain", get_valid_toolchains(prefix)) @pytest.mark.parametrize("toolchain", get_valid_toolchains(prefix))
def test_load_and_dump(self, qtbot, test_prefix, toolchain): def test_load_and_dump(self, qtbot, test_prefix, toolchain):
......
...@@ -28,7 +28,6 @@ from functools import partial ...@@ -28,7 +28,6 @@ from functools import partial
import simplejson as json import simplejson as json
from beat.backend.python.algorithm import Algorithm
from PyQt5.QtCore import QFile from PyQt5.QtCore import QFile
from PyQt5.QtCore import QPointF from PyQt5.QtCore import QPointF
from PyQt5.QtCore import QRect from PyQt5.QtCore import QRect
...@@ -64,6 +63,8 @@ from PyQt5.QtWidgets import QToolBar ...@@ -64,6 +63,8 @@ from PyQt5.QtWidgets import QToolBar
from PyQt5.QtWidgets import QVBoxLayout from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget from PyQt5.QtWidgets import QWidget
from beat.backend.python.algorithm import Algorithm
from ..backend.asset import Asset from ..backend.asset import Asset
from ..backend.asset import AssetType from ..backend.asset import AssetType
from ..backend.assetmodel import AssetModel from ..backend.assetmodel import AssetModel
...@@ -904,7 +905,7 @@ class Block(QGraphicsObject): ...@@ -904,7 +905,7 @@ class Block(QGraphicsObject):
self.setAcceptHoverEvents(True) self.setAcceptHoverEvents(True)
self.setFlag(QGraphicsItem.ItemIsSelectable, True) self.setFlag(QGraphicsItem.ItemIsSelectable, True)
self.setFlag(QGraphicsItem.ItemIsMovable) self.setFlag(QGraphicsItem.ItemIsMovable, True)
# Geometry settings # Geometry settings
self.width = config["width"] self.width = config["width"]
...@@ -1107,6 +1108,10 @@ class Block(QGraphicsObject): ...@@ -1107,6 +1108,10 @@ class Block(QGraphicsObject):
def mouseDoubleClickEvent(self, event): def mouseDoubleClickEvent(self, event):
"""Update block information""" """Update block information"""
if not self.flags() and QGraphicsItem.ItemIsSelectable:
return
value = None value = None
ok = False ok = False
block_updated = False block_updated = False
...@@ -1421,6 +1426,7 @@ class ToolchainWidget(QWidget): ...@@ -1421,6 +1426,7 @@ class ToolchainWidget(QWidget):
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent=parent) super().__init__(parent=parent)
self.__edition_enabled = True
self.json_object = {} self.json_object = {}
self.sequential_loop_processor_list = [] self.sequential_loop_processor_list = []
...@@ -1499,6 +1505,26 @@ class ToolchainWidget(QWidget): ...@@ -1499,6 +1505,26 @@ class ToolchainWidget(QWidget):
) )
action.setEnabled(False) action.setEnabled(False)
def __update_items(self):
for item in self.scene.items():
item.setFlag(QGraphicsItem.ItemIsSelectable, self.__edition_enabled)
item.setFlag(QGraphicsItem.ItemIsMovable, self.__edition_enabled)
def isEditionEnabled(self):
"""Returns whether this widget allows edition"""
return self.__edition_enabled
@pyqtSlot(bool)
def setEditionEnabled(self, enabled):
"""Sets whether this widget allows edition"""
if self.__edition_enabled == enabled:
return
self.__edition_enabled = enabled
self.toolbar.setVisible(enabled)
self.__update_items()
def add_loop_block(self): def add_loop_block(self):
loops, ok = LoopDialog.getLoops( loops, ok = LoopDialog.getLoops(
self, self,
...@@ -1598,6 +1624,7 @@ class ToolchainWidget(QWidget): ...@@ -1598,6 +1624,7 @@ class ToolchainWidget(QWidget):
block.dataChanged.emit() block.dataChanged.emit()
self.blocks.append(block) self.blocks.append(block)
self.scene.addItem(block) self.scene.addItem(block)
self.__update_items()
def update_channel_path(self, block, old_channel, new_channel): def update_channel_path(self, block, old_channel, new_channel):
# check if current block is synchronized on old_channel # check if current block is synchronized on old_channel
...@@ -1687,7 +1714,6 @@ class ToolchainWidget(QWidget): ...@@ -1687,7 +1714,6 @@ class ToolchainWidget(QWidget):
block.dataChanged.connect(self.dataChanged) block.dataChanged.connect(self.dataChanged)
block.dataChanged.emit() block.dataChanged.emit()
self.blocks.append(block) self.blocks.append(block)
self.scene.addItem(block)
def set_prefix_databases_algorithms_lists( def set_prefix_databases_algorithms_lists(
self, self,
...@@ -1764,6 +1790,8 @@ class ToolchainWidget(QWidget): ...@@ -1764,6 +1790,8 @@ class ToolchainWidget(QWidget):
self.connections.append(connection) self.connections.append(connection)
self.scene.addItem(connection) self.scene.addItem(connection)
self.__update_items()
def dump(self): def dump(self):
"""Returns the json used to load the widget""" """Returns the json used to load the widget"""
......
Markdown is supported
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