Skip to content

Commit

Permalink
Move exec_selected handling into reusable workbox method
Browse files Browse the repository at this point in the history
This calls `__exec_selected__` detects if the user pressed `Shift + Return` or number pad enter.
  • Loading branch information
MHendricks committed Nov 3, 2023
1 parent 327c586 commit 26416f8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
31 changes: 31 additions & 0 deletions preditor/gui/workbox_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import tempfile
import textwrap

from Qt.QtCore import Qt
from Qt.QtWidgets import QStackedWidget

from ..prefs import prefs_path
Expand Down Expand Up @@ -324,3 +325,33 @@ def __show__(self):
elif self._tempfile:
txt = self.__open_file__(self.__tempfile__())
self.__set_text__(txt)

def process_shortcut(self, event, run=True):
"""Check for workbox shortcuts and optionally call them.
Args:
event (QEvent): The keyPressEvent to process.
run (bool, optional): Run the expected action if possible.
Returns:
str or False: Returns False if the key press was not handled, indicating
that the subclass needs to handle it(or call super). If a known
shortcut was detected, a string indicating the action is returned
after running the action if enabled and supported.
Known actions:
__exec_selected__: If the user pressed Shift + Return or pressed the
number pad enter key calling `__exec_selected__`.
"""
if event.key() == Qt.Key_Enter or (
event.key() == Qt.Key_Return and event.modifiers() == Qt.ShiftModifier
):
# Number pad enter, or Shift + Return pressed, execute selected
if run:
self.__exec_selected__()

if self.window().uiAutoPromptACT.isChecked():
self.__console__().startInputLine()
return '__exec_selected__'

return False
11 changes: 3 additions & 8 deletions preditor/gui/workbox_text_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import logging

from Qt.QtCore import Qt
from Qt.QtGui import QFont, QFontMetrics, QTextCursor
from Qt.QtWidgets import QTextEdit

Expand Down Expand Up @@ -87,7 +86,7 @@ def __tab_width__(self):
# TODO: Implement custom tab widths
return 4

def __text__(self):
def __text__(self, line=None, start=None, end=None):
return self.toPlainText()

def __set_text__(self, text):
Expand All @@ -112,11 +111,7 @@ def __selected_text__(self, start_of_line=False):
return self.textCursor().selection().toPlainText()

def keyPressEvent(self, event):
# If number pad enter key or Shift and keyboard return key
# are used run selected text.
if event.key() == Qt.Key_Enter or (
event.key() == Qt.Key_Return and event.modifiers() == Qt.ShiftModifier
):
self.__exec_selected__()
if self.process_shortcut(event):
return
else:
super(WorkboxTextEdit, self).keyPressEvent(event)
9 changes: 2 additions & 7 deletions preditor/gui/workboxwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,8 @@ def keyPressEvent(self, event):
if self._software == 'softimage':
DocumentEditor.keyPressEvent(self, event)
else:
if event.key() == Qt.Key_Enter or (
event.key() == Qt.Key_Return and event.modifiers() == Qt.ShiftModifier
):
self.__exec_selected__()

if self.window().uiAutoPromptACT.isChecked():
self.__console__().startInputLine()
if self.process_shortcut(event):
return
else:
DocumentEditor.keyPressEvent(self, event)

Expand Down

0 comments on commit 26416f8

Please sign in to comment.