Commit eb83d495 authored by Flavio TARSETTI's avatar Flavio TARSETTI
Browse files

[widgets][toolchaineditor] adding draft for editable connections

parent 50dfa104
......@@ -14,17 +14,17 @@
"pin_height": 30,
"pin_font": "Arial",
"pin_font_size": 10,
"pin_color": [255, 155, 0, 255],
"background_color": [80, 80, 80, 255],
"background_color_datasets": [255, 254, 200, 255],
"background_color_blocks": [204, 204, 204, 255],
"background_color_analyzers": [136, 150, 216, 255],
"border_color": [50, 50, 50, 255],
"selection_border_color": [170, 80, 80, 255],
"text_color": [0, 0, 0, 255]
"pin_color": [255, 155, 0, 255],
"background_color": [80, 80, 80, 255],
"background_color_datasets": [255, 254, 200, 255],
"background_color_blocks": [204, 204, 204, 255],
"background_color_analyzers": [136, 150, 216, 255],
"border_color": [50, 50, 50, 255],
"selection_border_color": [170, 80, 80, 255],
"text_color": [0, 0, 0, 255]
},
"connection_config":{
"width": 2,
"color": [255, 155, 0, 255]
"color": [255, 180, 0, 255]
}
}
......@@ -39,6 +39,7 @@ from PyQt5.QtGui import QPen
from PyQt5.QtGui import QFont
from PyQt5.QtGui import QFontMetrics
from PyQt5.QtGui import QPainterPath
from PyQt5.QtGui import QTransform
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QWidget
......@@ -88,6 +89,63 @@ class BasePin(QGraphicsItem):
painter.drawEllipse(self.boundingRect())
def mousePressEvent(self, event):
"""Painting connection process"""
self.new_connection = Connection(self.block_object.connection_style)
self.block_object.scene().addItem(self.new_connection)
def mouseMoveEvent(self, event):
"""Painting connection process"""
mouse_position = self.mapToScene(event.pos())
self.new_connection.set_new_connection_pins_coordinates(self, mouse_position)
def mouseReleaseEvent(self, event):
"""Painting connection process"""
self.block_object.scene().removeItem(self.new_connection)
target = self.block_object.scene().itemAt(
event.scenePos().toPoint(), QTransform()
)
if isinstance(target, BasePin) and target.is_valid(self):
# This needs to be refactored and worked on!
connection_settings = {}
connection_settings["channel"] = "train"
if isinstance(target, InputPin) and isinstance(self, OutputPin):
connection_settings["from"] = self.block + "." + self.pin
connection_settings["to"] = target.block + "." + target.pin
else:
connection_settings["from"] = target.block + "." + target.pin
connection_settings["to"] = self.block + "." + self.pin
channel_colors = self.block_object.toolchain.json_object["representation"][
"channel_colors"
]
connection = Connection(self.block_object.connection_style)
connection.load(
self.block_object.toolchain, connection_settings, channel_colors
)
self.block_object.toolchain.connections.append(connection)
self.block_object.toolchain.scene.addItem(connection)
self.block_object.scene().addItem(connection)
else:
self.block_object.scene().removeItem(self.new_connection)
def is_valid(self, source):
# remove input-input and output-output connection
if type(source) == type(self):
return False
# remove connection to same block object
if source.block_object == self.block_object:
return False
return True
def get_center_point(self):
"""Get the center coordinates of the Pin(x,y)"""
......@@ -143,51 +201,29 @@ class OutputPin(BasePin):
class Connection(QGraphicsPathItem):
def __init__(self, toolchain, connection_details, style, channel_colors):
def __init__(self, style):
super().__init__()
self.start_block_name = connection_details["from"].split(".")[0]
self.start_pin_name = connection_details["from"].split(".")[1]
self.end_block_name = connection_details["to"].split(".")[0]
self.end_pin_name = connection_details["to"].split(".")[1]
self.channel = connection_details["channel"]
self.start_block_name = None
self.start_pin_name = None
self.start_pin_center_point = None
self.end_block_name = None
self.end_pin_name = None
self.end_pin_center_point = None
self.channel = None
hexadecimal = channel_colors[self.channel].lstrip("#")
hlen = len(hexadecimal)
self.connection_color = list(
int(hexadecimal[i : i + hlen // 3], 16) for i in range(0, hlen, hlen // 3)
)
self.connection_color.append(255)
self.blocks = toolchain.blocks
for block in self.blocks:
if block.name == self.start_block_name:
self.start_block = block
elif block.name == self.end_block_name:
self.end_block = block
self.start_pin = self.start_block.pins["outputs"][self.start_pin_name]
self.end_pin = self.end_block.pins["inputs"][self.end_pin_name]
self.pointer = None
self.connection_color = []
self.set_style(style)
self.drawCubicBezierCurve()
self.start_block.blockMoved.connect(self.drawCubicBezierCurve)
self.end_block.blockMoved.connect(self.drawCubicBezierCurve)
def set_style(self, config):
# Highlight
self.setAcceptHoverEvents(True)
# Geometry settings
self.width = config["width"]
self.color = config["color"]
# Geometry and color settings
self.connection_color = config["color"]
self.connection_pen = QPen()
self.connection_pen.setColor(QColor(*self.connection_color))
......@@ -196,26 +232,86 @@ class Connection(QGraphicsPathItem):
def drawCubicBezierCurve(self):
self.setPen(self.connection_pen)
start_pin_center_point = self.start_pin.get_center_point()
end_pin_center_point = self.end_pin.get_center_point()
path = QPainterPath()
middle_point_x = (end_pin_center_point.x() - start_pin_center_point.x()) / 2.0
middle_point_y = (end_pin_center_point.y() - start_pin_center_point.y()) / 2.0
middle_point_x = (
self.end_pin_center_point.x() - self.start_pin_center_point.x()
) / 2.0
middle_point_y = (
self.end_pin_center_point.y() - self.start_pin_center_point.y()
) / 2.0
second_middle_point_y = (
end_pin_center_point.y() - start_pin_center_point.y()
self.end_pin_center_point.y() - self.start_pin_center_point.y()
) / 4.0
control_point = QPointF(middle_point_x, middle_point_y)
second_control_point = QPointF(middle_point_x, second_middle_point_y)
path.moveTo(start_pin_center_point)
path.moveTo(self.start_pin_center_point)
path.cubicTo(
start_pin_center_point + control_point,
end_pin_center_point - second_control_point,
end_pin_center_point,
self.start_pin_center_point + control_point,
self.end_pin_center_point - second_control_point,
self.end_pin_center_point,
)
self.setPath(path)
def set_moved_block_pins_coordinates(self):
self.start_pin_center_point = self.start_pin.get_center_point()
self.end_pin_center_point = self.end_pin.get_center_point()
self.drawCubicBezierCurve()
def set_new_connection_pins_coordinates(self, selected_pin, mouse_position):
if isinstance(selected_pin, OutputPin):
self.start_block_name = selected_pin.block
self.start_pin_name = selected_pin.pin
self.start_pin = selected_pin
self.start_pin_center_point = self.start_pin.get_center_point()
self.end_pin_center_point = mouse_position
if isinstance(selected_pin, InputPin):
self.end_block_name = selected_pin.block
self.end_pin_name = selected_pin.pin
self.end_pin = selected_pin
self.end_pin_center_point = self.end_pin.get_center_point()
self.start_pin_center_point = mouse_position
self.drawCubicBezierCurve()
def load(self, toolchain, connection_details, channel_colors):
self.start_block_name = connection_details["from"].split(".")[0]
self.start_pin_name = connection_details["from"].split(".")[1]
self.end_block_name = connection_details["to"].split(".")[0]
self.end_pin_name = connection_details["to"].split(".")[1]
self.channel = connection_details["channel"]
hexadecimal = channel_colors[self.channel].lstrip("#")
hlen = len(hexadecimal)
self.connection_color = list(
int(hexadecimal[i : i + hlen // 3], 16) for i in range(0, hlen, hlen // 3)
)
self.connection_color.append(255)
self.connection_pen.setColor(QColor(*self.connection_color))
self.blocks = toolchain.blocks
for block in self.blocks:
if block.name == self.start_block_name:
self.start_block = block
elif block.name == self.end_block_name:
self.end_block = block
self.start_pin = self.start_block.pins["outputs"][self.start_pin_name]
self.end_pin = self.end_block.pins["inputs"][self.end_pin_name]
self.start_pin_center_point = self.start_pin.get_center_point()
self.end_pin_center_point = self.end_pin.get_center_point()
self.drawCubicBezierCurve()
self.start_block.blockMoved.connect(self.set_moved_block_pins_coordinates)
self.end_block.blockMoved.connect(self.set_moved_block_pins_coordinates)
class BlockType(Enum):
"""All possible block types"""
......@@ -238,10 +334,12 @@ class Block(QGraphicsObject):
dataChanged = pyqtSignal()
blockMoved = pyqtSignal()
def __init__(self, block_details, block_type, style):
def __init__(self, toolchain, block_details, block_type, style, connection_style):
super().__init__()
# Block information
self.toolchain = toolchain
self.name = block_details["name"]
if block_type == BlockType.DATASETS.name:
self.inputs = None
......@@ -258,6 +356,7 @@ class Block(QGraphicsObject):
self.synchronized_channel = None
self.type = block_type
self.style = style
self.connection_style = connection_style
self.position = QPointF(0, 0)
self.pins = dict()
......@@ -580,7 +679,13 @@ class Toolchain(QWidget):
# Get datasets, blocks, analyzers
for block_type in BlockType:
for block_item in self.json_object[block_type.value]:
block = Block(block_item, block_type.name, self.block_config)
block = Block(
self,
block_item,
block_type.name,
self.block_config,
self.connection_config,
)
# Place blocks (x,y) if information is given
if self.editor_gui is not None:
if block.name in self.editor_gui:
......@@ -598,9 +703,8 @@ class Toolchain(QWidget):
channel_colors = self.json_object["representation"]["channel_colors"]
for connection_item in connections:
connection = Connection(
self, connection_item, self.connection_config, channel_colors
)
connection = Connection(self.connection_config)
connection.load(self, connection_item, channel_colors)
self.connections.append(connection)
self.scene.addItem(connection)
......
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