Merge pull request #98 from vkbo/follow_tags

Follow Tags
This commit is contained in:
Veronica K. Berglyd Olsen
2019-10-30 21:54:18 +01:00
committed by GitHub
3 changed files with 60 additions and 6 deletions
-3
View File
@@ -20,6 +20,3 @@ parsers:
macro: no macro: no
comment: false comment: false
# layout: "reach,diff,flags,tree"
# behavior: default
# require_changes: no
+42 -3
View File
@@ -16,7 +16,7 @@ import nw
from time import time from time import time
from PyQt5.QtCore import Qt, QTimer, QSizeF 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 PyQt5.QtGui import QTextCursor, QTextOption, QIcon, QKeySequence, QFont, QColor, QPalette, QTextDocument
from nw.project.document import NWDoc from nw.project.document import NWDoc
@@ -81,8 +81,8 @@ class GuiDocEditor(QTextEdit):
# Custom Shortcuts # Custom Shortcuts
QShortcut(QKeySequence("Ctrl+."), self, context=Qt.WidgetShortcut, activated=self._openSpellContext) 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_Return | Qt.ControlModifier, self, context=Qt.WidgetShortcut, activated=self._followTag)
# QShortcut(Qt.Key_Enter | Qt.ControlModifier, self, context=Qt.WidgetShortcut, activated=self._insertHardBreak) QShortcut(Qt.Key_Enter | Qt.ControlModifier, self, context=Qt.WidgetShortcut, activated=self._followTag)
# Set Up Word Count Thread and Timer # Set Up Word Count Thread and Timer
self.wcInterval = self.mainConf.wordCountTimer self.wcInterval = self.mainConf.wordCountTimer
@@ -352,10 +352,49 @@ class GuiDocEditor(QTextEdit):
return 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 # Internal Functions
## ##
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.
"""
if theCursor is None:
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): def _insertHardBreak(self):
theCursor = self.textCursor() theCursor = self.textCursor()
theCursor.beginEditBlock() theCursor.beginEditBlock()
+18
View File
@@ -115,6 +115,24 @@ class GuiDocViewer(QTextBrowser):
return True 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 # Internal Functions
## ##