Skip to content

Commit

Permalink
🎉 🎨 Add persistent colors (#276)
Browse files Browse the repository at this point in the history
🔧 Refactor custom_bfs and right_traversal
  • Loading branch information
MathisFederico authored Feb 19, 2022
2 parents 9634b5d + a2824b4 commit af0f810
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 258 deletions.
126 changes: 35 additions & 91 deletions examples/mnist.ipyg

Large diffs are not rendered by default.

25 changes: 21 additions & 4 deletions pyflow/blocks/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ def __init__(
self.sockets_in: List[Socket] = []
self.sockets_out: List[Socket] = []

self._pen_outline = QPen(QColor("#7F000000"))
self.pen_width = 3
self._pen_outline = QPen(QColor("#00000000"))
self._pen_outline.setWidth(self.pen_width)
self._pen_outline_selected = QPen(QColor("#FFFFA637"))
self._pen_outline_selected.setWidth(self.pen_width)
self._brush_background = QBrush(BACKGROUND_COLOR)

self.setFlag(QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)
Expand Down Expand Up @@ -138,12 +141,26 @@ def paint(
path_outline.addRoundedRect(
0, 0, self.width, self.height, self.edge_size, self.edge_size
)
painter.setPen(
self._pen_outline_selected if self.isSelected() else self.pen_outline
)
painter.setPen(self.pen_outline)
painter.setBrush(Qt.BrushStyle.NoBrush)
painter.drawPath(path_outline.simplified())

# selection inner outline
if self.isSelected():
path_in_outline = QPainterPath()
outline_width = self.pen_outline.widthF()
path_in_outline.addRoundedRect(
-2 * outline_width,
-2 * outline_width,
self.width + 4 * outline_width,
self.height + 4 * outline_width,
self.edge_size + 2 * outline_width,
self.edge_size + 2 * outline_width,
)
painter.setPen(self._pen_outline_selected)
painter.setBrush(Qt.BrushStyle.NoBrush)
painter.drawPath(path_in_outline.simplified())

def add_socket(self, socket: Socket):
"""Add a socket to the block."""
if socket.socket_type == "input":
Expand Down
33 changes: 21 additions & 12 deletions pyflow/blocks/codeblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from pyflow.blocks.block import Block
from pyflow.core.edge import Edge
from pyflow.blocks.executableblock import ExecutableBlock
from pyflow.blocks.executableblock import ExecutableBlock, ExecutableState
from pyflow.blocks.pyeditor import PythonEditor
from pyflow.core.add_button import AddEdgeButton, AddNewBlockButton

Expand Down Expand Up @@ -62,14 +62,17 @@ def __init__(self, source: str = "", **kwargs):
self.output_closed = True
self._splitter_size = [1, 1]
self._cached_stdout = ""
self.has_been_run = False
self.blocks_to_run = []

self._pen_outlines = [
QPen(QColor("#7F000000")), # Idle
QPen(QColor("#FF0000")), # Running
QPen(QColor("#00ff00")), # Transmitting
]
self._pen_outlines = {
ExecutableState.IDLE: QPen(QColor("#00000000")), # No outline
ExecutableState.RUNNING: QPen(QColor("#fffc6107")), # Dark orange
ExecutableState.PENDING: QPen(QColor("#90fc6107")), # Transparent orange
ExecutableState.DONE: QPen(QColor("#158000")), # Dark green
ExecutableState.CRASHED: QPen(QColor("#ff0000")), # Red: Crashed
}
for pen in self._pen_outlines.values():
pen.setWidth(self.pen_width)

self.output_panel_background_color = "#1E1E1E"

Expand Down Expand Up @@ -133,14 +136,14 @@ def init_add_newblock_button(self):

def handle_run_right(self):
"""Called when the button for "Run All" was pressed."""
if self.run_state != 0:
if self.run_state in (ExecutableState.PENDING, ExecutableState.RUNNING):
self._interrupt_execution()
else:
self.run_right()

def handle_run_left(self):
"""Called when the button for "Run Left" was pressed."""
if self.run_state != 0:
if self.run_state in (ExecutableState.PENDING, ExecutableState.RUNNING):
self._interrupt_execution()
else:
self.run_left()
Expand Down Expand Up @@ -170,10 +173,17 @@ def run_code(self):
super().run_code() # actually run the code

def execution_finished(self):
"""Reset the text of the run buttons after it was executed."""
super().execution_finished()
self.run_button.setText(">")
self.run_all_button.setText(">>")

def execution_canceled(self):
"""Reset the text of the run buttons after it was canceled."""
super().execution_canceled()
self.run_button.setText(">")
self.run_all_button.setText(">>")

def link(self, block: "ExecutableBlock"):
"""Link a block to the current one."""
# Add sockets to the new block and the current one
Expand Down Expand Up @@ -259,9 +269,8 @@ def source(self, value: str):
if value != self._source:
# If text has changed, set self and all output blocks to not run
output_blocks, _ = self.custom_bfs(self, reverse=True)
for block in output_blocks:
block.has_been_run = False
self.has_been_run = False
for block in output_blocks + [self]:
block.run_state = ExecutableState.IDLE
self.source_editor.setText(value)
self._source = value

Expand Down
Loading

0 comments on commit af0f810

Please sign in to comment.