Skip to content

Commit

Permalink
🎉 Make shift+enter in editor run left (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisFederico authored Feb 15, 2022
2 parents 16baf87 + 714f260 commit 2768c1a
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions pyflow/blocks/pyeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
QKeyEvent,
QWheelEvent,
)

from PyQt5.QtWidgets import QApplication
from PyQt5.Qsci import QsciScintilla, QsciLexerPython

from pyflow.core.editor import Editor
Expand Down Expand Up @@ -42,8 +42,6 @@ def __init__(self, block: "CodeBlock"):
self.background_color = QColor("#212121")

self.history = EditorHistory(self)
self.pressingControl = False
# self.startOfSequencePos

self.update_theme()
theme_manager().themeChanged.connect(self.update_theme)
Expand Down Expand Up @@ -107,6 +105,7 @@ def focusOutEvent(self, event: QFocusEvent):
self.block.scene().history.checkpoint(
"A codeblock source was updated", set_modified=True
)
self.history.checkpoint()
return super().focusOutEvent(event)

def keyPressEvent(self, event: QKeyEvent) -> None:
Expand All @@ -115,18 +114,27 @@ def keyPressEvent(self, event: QKeyEvent) -> None:
# Disable QsciScintilla undo
self.SendScintilla(QsciScintilla.SCI_EMPTYUNDOBUFFER, 1)

# Check if Shift+Return is pressed
# If so, the cell should be (left) run
shift_is_pressed: bool = (
QApplication.keyboardModifiers() == Qt.KeyboardModifier.ShiftModifier
)
if shift_is_pressed and event.key() in {Qt.Key.Key_Return, Qt.Key.Key_Enter}:
self.block.run_left()
return

# Manualy check if Ctrl+Z or Ctrl+Y is pressed
if self.pressingControl and event.key() == Qt.Key.Key_Z:
control_is_pressed: bool = (
QApplication.keyboardModifiers() == Qt.KeyboardModifier.ControlModifier
)
if control_is_pressed and event.key() == Qt.Key.Key_Z:
# The sequence ends and a new one starts when pressing Ctrl+Z
self.history.end_sequence()
self.history.start_sequence()
self.history.undo()
elif self.pressingControl and event.key() == Qt.Key.Key_Y:
elif control_is_pressed and event.key() == Qt.Key.Key_Y:
self.history.redo()
elif event.key() == Qt.Key.Key_Control:
self.pressingControl = True
else:
self.pressingControl = False
elif not control_is_pressed:
self.history.start_sequence()

if event.key() in {Qt.Key.Key_Return, Qt.Key.Key_Enter}:
Expand Down

0 comments on commit 2768c1a

Please sign in to comment.