It should now be possible to navigate to positions within the documents when clicking links in various places

This commit is contained in:
Veronica K. B. Olsen
2020-05-25 23:31:41 +02:00
parent b3e4af1394
commit 4340b6b8b5
4 changed files with 40 additions and 52 deletions
+23 -29
View File
@@ -28,7 +28,7 @@
import logging
import nw
from PyQt5.QtCore import Qt
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtWidgets import QTextBrowser
from PyQt5.QtGui import QTextOption, QFont, QPalette, QColor, QTextCursor
@@ -133,6 +133,7 @@ class GuiDocViewer(QTextBrowser):
sPos = self.verticalScrollBar().value()
aDoc = ToHtml(self.theProject, self.theParent)
aDoc.setPreview(True, self.mainConf.viewComments)
aDoc.setLinkHeaders(True)
aDoc.setText(tHandle)
aDoc.doAutoReplace()
aDoc.tokenizeText()
@@ -162,19 +163,17 @@ class GuiDocViewer(QTextBrowser):
index being up to date.
"""
logger.debug("Loading document from tag '%s'" % theTag)
if theTag in self.theParent.theIndex.tagIndex.keys():
theTarget = self.theParent.theIndex.tagIndex[theTag]
tHandle, onLine, sTitle = self.theParent.theIndex.getTagSource(theTag)
if tHandle is None:
self.theParent.makeAlert((
"Could not find the reference for tag '%s'. It either doesn't "
"exist, or the index is out of date. The index can be updated "
"from the Tools menu, or by pressing F9."
) % theTag, nwAlert.ERROR)
return
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])
self.loadText(tHandle)
self.navigateTo("#head_%s:%s" % (tHandle, sTitle))
return True
def docAction(self, theAction):
@@ -198,6 +197,15 @@ class GuiDocViewer(QTextBrowser):
return False
return True
def navigateTo(self, navLink):
"""Go to a specific #link in the document.
"""
if not isinstance(navLink, str):
return False
if navLink.startswith("#"):
self.setSource(QUrl(navLink))
return True
def updateDocTitle(self, tHandle):
"""Called when an item label is changed to check if the document
title bar needs updating,
@@ -271,25 +279,11 @@ class GuiDocViewer(QTextBrowser):
"""Slot for a link in the document being clicked.
"""
theLink = theURL.url()
tHandle = None
onLine = 0
theTag = ""
logger.verbose("Clicked link: '%s'" % theLink)
if len(theLink) > 0:
theBits = theLink.split("=")
if len(theBits) == 2:
theTag = theBits[1]
tHandle, onLine = self.theParent.theIndex.getTagSource(theBits[1])
if tHandle is None:
self.theParent.makeAlert((
"Could not find the reference for tag '%s'. It either doesn't exist, or the index "
"is out of date. The index can be updated from the Tools menu.") % theTag,
nwAlert.ERROR
)
return
else:
self.loadText(tHandle)
self.loadFromTag(theBits[1])
return
def _makeStyleSheet(self):
+4 -11
View File
@@ -107,12 +107,10 @@ class GuiDocViewDetails(QWidget):
for tHandle in theRefs:
tItem = self.theProject.projTree[tHandle]
if tItem is not None:
theList.append("<a href='#tag=%s:%s'>%s</a>" % (
theList.append("<a href='#head_%s:%s'>%s</a>" % (
tHandle, theRefs[tHandle], tItem.itemName
))
# print(theList)
self.refList.setText(", ".join(theList))
self.refList.adjustSize()
@@ -127,14 +125,9 @@ class GuiDocViewDetails(QWidget):
class for handling.
"""
logger.verbose("Clicked link: '%s'" % theLink)
if len(theLink) == 26:
tHandle = theLink[5:18]
tLine = theLink[19:26]
if tLine[1:].isdigit():
nLine = int(tLine[1:])
else:
nLine = 1
self.theParent.viewDocument(tHandle, nLine)
if len(theLink) == 27:
tHandle = theLink[6:19]
self.theParent.viewDocument(tHandle, theLink)
return
def _doShowHide(self, chState):