From 5a76d4982dc071e3ad0be295d804cb1ee1dbac5f Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Wed, 30 Oct 2019 21:05:28 +0100 Subject: [PATCH 1/3] Ctrl+Enter on a tag name now opens the file in the view pane --- nw/gui/doceditor.py | 31 +++++++++++++++++++++++++++++-- nw/gui/docviewer.py | 18 ++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 44238fc5..ed85cb3a 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -81,8 +81,8 @@ class GuiDocEditor(QTextEdit): # Custom Shortcuts QShortcut(QKeySequence("Ctrl+."), self, context=Qt.WidgetShortcut, activated=self._openSpellContext) - # QShortcut(Qt.Key_Return | Qt.ControlModifier, self, context=Qt.WidgetShortcut, activated=self._insertHardBreak) - # QShortcut(Qt.Key_Enter | Qt.ControlModifier, self, context=Qt.WidgetShortcut, activated=self._insertHardBreak) + QShortcut(Qt.Key_Return | Qt.ControlModifier, self, context=Qt.WidgetShortcut, activated=self._followTag) + QShortcut(Qt.Key_Enter | Qt.ControlModifier, self, context=Qt.WidgetShortcut, activated=self._followTag) # Set Up Word Count Thread and Timer self.wcInterval = self.mainConf.wordCountTimer @@ -356,6 +356,33 @@ class GuiDocEditor(QTextEdit): # Internal Functions ## + def _followTag(self): + """Activated by Ctrl+Enter. Checks that we're in a block starting with '@'. We then find the + word under the cursor and check that it is after the ':'. If all this is fine, we have a tag + and can tell the document viewer to try and find and load the file where the tag is defined. + """ + + theCursor = self.textCursor() + theBlock = theCursor.block() + theText = theBlock.text() + + if len(theText) == 0: + return False + + if theText.startswith("@"): + + theCursor.select(QTextCursor.WordUnderCursor) + theWord = theCursor.selectedText() + cPos = theText.find(":") + wPos = theCursor.selectionStart() - theBlock.position() + if wPos <= cPos: + return False + + logger.verbose("Attempting to follow tag '%s'" % theWord) + self.theParent.docViewer.loadFromTag(theWord) + + return True + def _insertHardBreak(self): theCursor = self.textCursor() theCursor.beginEditBlock() diff --git a/nw/gui/docviewer.py b/nw/gui/docviewer.py index 76c57e67..39342036 100644 --- a/nw/gui/docviewer.py +++ b/nw/gui/docviewer.py @@ -115,6 +115,24 @@ class GuiDocViewer(QTextBrowser): return True + def loadFromTag(self, theTag): + + logger.debug("Loading document from tag '%s'" % theTag) + + if theTag in self.theParent.theIndex.tagIndex.keys(): + theTarget = self.theParent.theIndex.tagIndex[theTag] + else: + logger.debug("The tag was not found in the index") + return False + + if len(theTarget) != 3: + # Just to make sure the index is not messed up + return False + + self.loadText(theTarget[1]) + + return True + ## # Internal Functions ## From b7f2d649084891418e6c5b68afc5b8f0e9ab51cd Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Wed, 30 Oct 2019 21:28:46 +0100 Subject: [PATCH 2/3] Deleted comments in codecov file --- .codecov.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.codecov.yml b/.codecov.yml index caa2118b..8a775495 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -20,6 +20,3 @@ parsers: macro: no comment: false - # layout: "reach,diff,flags,tree" - # behavior: default - # require_changes: no From 9342f90a416eaa7eaa1af14e11839d29e26b4667 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Wed, 30 Oct 2019 21:49:48 +0100 Subject: [PATCH 3/3] Added ctrl + mouse click to follow tags as well. --- nw/gui/doceditor.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index ed85cb3a..d7b64290 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -16,7 +16,7 @@ import nw from time import time from PyQt5.QtCore import Qt, QTimer, QSizeF -from PyQt5.QtWidgets import QTextEdit, QAction, QMenu, QShortcut +from PyQt5.QtWidgets import qApp, QTextEdit, QAction, QMenu, QShortcut from PyQt5.QtGui import QTextCursor, QTextOption, QIcon, QKeySequence, QFont, QColor, QPalette, QTextDocument from nw.project.document import NWDoc @@ -352,19 +352,31 @@ class GuiDocEditor(QTextEdit): return + 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 follow tag function. + """ + if qApp.keyboardModifiers() == Qt.ControlModifier: + theCursor = self.cursorForPosition(mEvent.pos()) + self._followTag(theCursor) + QTextEdit.mouseReleaseEvent(self, mEvent) + return + ## # Internal Functions ## - def _followTag(self): + def _followTag(self, theCursor=None): """Activated by Ctrl+Enter. Checks that we're in a block starting with '@'. We then find the word under the cursor and check that it is after the ':'. If all this is fine, we have a tag and can tell the document viewer to try and find and load the file where the tag is defined. """ - theCursor = self.textCursor() - theBlock = theCursor.block() - theText = theBlock.text() + if theCursor is None: + theCursor = self.textCursor() + + theBlock = theCursor.block() + theText = theBlock.text() if len(theText) == 0: return False