Skip to content

Commit

Permalink
Merge pull request #412 from vkbo/editor_focus
Browse files Browse the repository at this point in the history
Editor Focus and Enter Key Behaviour
  • Loading branch information
vkbo committed Aug 16, 2020
2 parents 1a9e54d + fa1c79a commit 5427441
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
1 change: 1 addition & 0 deletions docs/source/interface.rst
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ Most features are available as keyboard shortcuts. These are as follows:
":kbd:`Ctrl`:kbd:`Shift`:kbd:`R`", "Close the document viewer."
":kbd:`Ctrl`:kbd:`Shift`:kbd:`S`", "Save the current project."
":kbd:`Ctrl`:kbd:`Shift`:kbd:`W`", "Close the current project."
":kbd:`Ctrl`:kbd:`Shift`:kbd:`Z`", "Alternative sequence for redo last undo."
":kbd:`Ctrl`:kbd:`Shift`:kbd:`Up`", "Move item one step up in the project tree."
":kbd:`Ctrl`:kbd:`Shift`:kbd:`Down`", "Move item one step down in the project tree."
":kbd:`F1`", "Open the documentation. This will either open the Qt Assistant, if available, or send you to the documentation website."
Expand Down
55 changes: 43 additions & 12 deletions nw/gui/doceditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,24 +641,36 @@ def closeSearch(self):
def keyPressEvent(self, keyEvent):
"""Intercept key press events.
We need to intercept a few key sequences:
* The return key redirects here even if the search box has
focus. Since we need the return key to continue search, we
block any further interaction here while it's in focus.
* The undo sequence bypasses the doAction pathway from the
menu, so we redirect it back from here.
* The default redo sequence is Ctrl+Shift+Z, which we don't
use, so we block it.
"""
if self.docSearch.searchBox.hasFocus():
* The return and enter key redirects here even if the search
box has focus. Since we need these keys to continue search,
we block any further interaction here while it's in focus.
* The undo/redo sequences bypasses the doAction pathway from
the menu, so we redirect them back from here.
"""
isReturn = keyEvent.key() == Qt.Key_Return
isReturn |= keyEvent.key() == Qt.Key_Enter
if isReturn and self.docSearch.searchBox.hasFocus():
return
elif keyEvent == QKeySequence.Redo:
return
self.docAction(nwDocAction.REDO)
elif keyEvent == QKeySequence.Undo:
self.docAction(nwDocAction.UNDO)
else:
QTextEdit.keyPressEvent(self, keyEvent)
return

def focusNextPrevChild(self, toNext):
"""Capture the focus request from the tab key on the text
editor. If the editor has focus, we do not change focus and
allow the editor to insert a tab. If the search bar has focus,
we forward the call to the search object.
"""
if self.hasFocus():
return False
elif self.docSearch.isVisible():
return self.docSearch.cycleFocus(toNext)
return True

def mouseReleaseEvent(self, mEvent):
"""If the mouse button is released and the control key is
pressed, check if we're clicking on a tag, and trigger the
Expand Down Expand Up @@ -891,6 +903,10 @@ def _docAutoReplace(self, theBlock):
else:
theCursor.insertText(self.typSQClose)

elif self.mainConf.doReplaceDash and theThree == "---":
theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 3)
theCursor.insertText(nwUnicode.U_EMDASH)

elif self.mainConf.doReplaceDash and theTwo == "--":
theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 2)
theCursor.insertText(nwUnicode.U_ENDASH)
Expand Down Expand Up @@ -1200,6 +1216,8 @@ def _beginReplace(self):
theCursor = self.textCursor()
if theCursor.hasSelection():
self.docSearch.setSearchText(theCursor.selectedText())
else:
self.docSearch.setSearchText(None)
self.docSearch.setReplaceText("")
self.updateDocMargins()
return
Expand Down Expand Up @@ -1399,12 +1417,12 @@ def __init__(self, docEditor):

# Text Boxes
# ==========
self.searchBox = QLineEdit()
self.searchBox = QLineEdit(self)
self.searchBox.setFont(boxFont)
self.searchBox.setPlaceholderText("Search")
self.searchBox.returnPressed.connect(self._doSearch)

self.replaceBox = QLineEdit()
self.replaceBox = QLineEdit(self)
self.replaceBox.setFont(boxFont)
self.replaceBox.setPlaceholderText("Replace")
self.replaceBox.returnPressed.connect(self._doSearch)
Expand Down Expand Up @@ -1560,6 +1578,19 @@ def closeSearch(self):

return

def cycleFocus(self, toNext):
"""The tab key just alternates focus between the two input
boxes, if the replace box is visible.
"""
if self.replaceBox.isVisible():
if self.searchBox.hasFocus():
self.replaceBox.setFocus(True)
return True
elif self.replaceBox.hasFocus():
self.searchBox.setFocus(True)
return True
return False

##
# Get and Set Functions
##
Expand Down

0 comments on commit 5427441

Please sign in to comment.