Skip to content

Commit

Permalink
🎉 Make shift+alt+arrow select the newly crated block
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisFederico committed Feb 23, 2022
1 parent 827059a commit 11bfc3e
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions pyflow/graphics/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ def moveViewOnArrow(self, event: QKeyEvent) -> bool:
selected_item.x() + selected_item.width / 2,
selected_item.y() + selected_item.height / 2,
)
self.scene().clearSelection()
self.currentSelectedBlock = selected_item

dist_array = []
Expand Down Expand Up @@ -290,29 +289,36 @@ def oriented_distance(x, y, key):
dist_array.sort(key=lambda pos: oriented_distance(pos[2], pos[3], key_id))
block_center_x, block_center_y, _, _ = dist_array[0]

item_to_navigate = self.scene().itemAt(
block_to_navigate = self.scene().itemAt(
block_center_x, block_center_y, self.transform()
)
self.scene().clearSelection()
self.scene().clearFocus()
parent = item_to_navigate.parentItem()
if isinstance(parent, Block):
parent.setSelected(True)
parent.setFocus(True)
if alt_is_pressed and hasattr(parent, "source_editor"):
parent.source_editor.setFocus(True)
self.mode = View.MODE_EDITING

self.centerView(block_center_x, block_center_y)
block_to_navigate = block_to_navigate.parentItem()

if isinstance(block_to_navigate, Block):
self.moveToBlock(block_to_navigate)

return True

def moveToBlock(self, block: Block):
"""Move view to a given block and selecting it.
Args:
block (Block): Block to move the view to.
"""
self.centerView(
block.pos().x() + block.width / 2,
block.pos().y() + block.height / 2,
)
self.currentSelectedBlock = block

def addBlock(self, block: CodeBlock, direction=("down", "mid")):
"""Add a block linked with the current block."""

empty_code_block_path: str = os.path.join(BLOCKFILES_PATH, "empty.pfb")
new_block = self.scene().create_block_from_file(empty_code_block_path, 0, 0)

block.link_and_place(new_block, direction)
self.moveToBlock(new_block)

self.scene().history.checkpoint("Created a new linked block", set_modified=True)

Expand Down Expand Up @@ -366,7 +372,10 @@ def keyPressEvent(self, event: QKeyEvent):
self.scene().clearSelection()
self.scene().clearFocus()

if key_id in (Qt.Key.Key_Return, Qt.Key.Key_Enter) and self.mode != View.MODE_EDITING:
if (
key_id in (Qt.Key.Key_Return, Qt.Key.Key_Enter)
and self.mode != View.MODE_EDITING
):
selected_items = self.scene().selectedItems()
if len(selected_items) == 1:
item = selected_items[0]
Expand Down Expand Up @@ -476,9 +485,20 @@ def currentSelectedBlock(self, block: Block):
current = self.currentSelectedBlock
if current is not None:
current.setZValue(0)
current.setSelected(False)
block.setZValue(1)

self.scene().clearSelection()
self.scene().clearFocus()
block.setSelected(True)
block.setFocus(True)
block.setZValue(1)

alt_is_pressed: bool = (
QApplication.keyboardModifiers() == Qt.KeyboardModifier.AltModifier
)
if alt_is_pressed and isinstance(block, CodeBlock):
block.source_editor.setFocus(True)
self.mode = View.MODE_EDITING

self._currentSelectedBlock = block

def drag_scene(self, event: QMouseEvent, action="press"):
Expand Down

0 comments on commit 11bfc3e

Please sign in to comment.