From 3b7927f0ff306d6192498aea54ddd76bcf805a76 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Mon, 25 May 2020 21:14:50 +0200 Subject: [PATCH] Back-references not consideres all tags in a document, not just the first one --- nw/core/index.py | 17 ++++++++--------- nw/gui/elements/doceditor.py | 5 ++++- nw/gui/elements/docviewer.py | 27 +++++++++++++++++++++++++++ nw/gui/elements/viewdetails.py | 18 ++++++++++++++---- nw/guimain.py | 18 ++++++++++-------- sample/data_a/e7339df26ded_main.nwd | 2 +- sample/data_f/1471bef9f2ae_main.nwd | 6 ++++++ sample/nwProject.nwx | 22 +++++++++++----------- 8 files changed, 81 insertions(+), 34 deletions(-) diff --git a/nw/core/index.py b/nw/core/index.py index e585a03a..79e6ddba 100644 --- a/nw/core/index.py +++ b/nw/core/index.py @@ -260,9 +260,9 @@ class NWIndex(): logger.debug("Indexing item with handle %s" % tHandle) # Check file type, and reset its old index - # Also add a dummy entry for T0 in case the file has no title + # Also add a dummy entry T000000 in case the file has no title self.refIndex[tHandle] = {} - self.refIndex[tHandle]["T0"] = { + self.refIndex[tHandle]["T000000"] = { "tags" : [], "updated" : time(), } @@ -606,18 +606,17 @@ class NWIndex(): if tHandle is None: return theRefs - theTag = None + theTags = [] for tTag in self.tagIndex: if tHandle == self.tagIndex[tTag][1]: - theTag = tTag - break + theTags.append(tTag) - if theTag is not None: + if theTags: for tHandle in self.refIndex: for sTitle in self.refIndex[tHandle]: - for nLine, tKey, tTag in self.refIndex[tHandle][sTitle]["tags"]: - if tTag == theTag: - theRefs[tHandle] = nLine + for _, _, tTag in self.refIndex[tHandle][sTitle]["tags"]: + if tTag in theTags and tHandle not in theRefs: + theRefs[tHandle] = sTitle return theRefs diff --git a/nw/gui/elements/doceditor.py b/nw/gui/elements/doceditor.py index 906d4c65..d220a8bd 100644 --- a/nw/gui/elements/doceditor.py +++ b/nw/gui/elements/doceditor.py @@ -395,6 +395,8 @@ class GuiDocEditor(QTextEdit): def setCursorPosition(self, thePosition): """Move the cursor to a given position in the document. """ + if not isinstance(thePosition, int): + return False if thePosition >= 0: theCursor = self.textCursor() theCursor.setPosition(thePosition) @@ -410,12 +412,13 @@ class GuiDocEditor(QTextEdit): def setCursorLine(self, theLine): """Move the cursor to a given line in the document. """ - if theLine is None: + if not isinstance(theLine, int): return False if theLine >= 0: theBlock = self.qDocument.findBlockByLineNumber(theLine) if theBlock: self.setCursorPosition(theBlock.position()) + logger.verbose("Cursor moved to line %d" % theLine) return True ## diff --git a/nw/gui/elements/docviewer.py b/nw/gui/elements/docviewer.py index 7fdf22e4..6d90c2f5 100644 --- a/nw/gui/elements/docviewer.py +++ b/nw/gui/elements/docviewer.py @@ -206,6 +206,33 @@ class GuiDocViewer(QTextBrowser): self.docTitle.setTitleFromHandle(self.theHandle) return + ## + # Setters + ## + + def setCursorPosition(self, thePosition): + """Move the cursor to a given position in the document. + """ + if not isinstance(thePosition, int): + return False + if thePosition >= 0: + theCursor = self.textCursor() + theCursor.setPosition(thePosition) + self.setTextCursor(theCursor) + return True + + def setCursorLine(self, theLine): + """Move the cursor to a given line in the document. + """ + if not isinstance(theLine, int): + return False + if theLine >= 0: + theBlock = self.qDocument.findBlockByLineNumber(theLine) + if theBlock: + self.setCursorPosition(theBlock.position()) + logger.verbose("Cursor moved to line %d" % theLine) + return True + ## # Events ## diff --git a/nw/gui/elements/viewdetails.py b/nw/gui/elements/viewdetails.py index 589ffee5..d5ab7ee8 100644 --- a/nw/gui/elements/viewdetails.py +++ b/nw/gui/elements/viewdetails.py @@ -107,7 +107,11 @@ class GuiDocViewDetails(QWidget): for tHandle in theRefs: tItem = self.theProject.projTree[tHandle] if tItem is not None: - theList.append("%s" % (tHandle,tItem.itemName)) + theList.append("%s" % ( + tHandle, theRefs[tHandle], tItem.itemName + )) + + # print(theList) self.refList.setText(", ".join(theList)) self.refList.adjustSize() @@ -122,9 +126,15 @@ class GuiDocViewDetails(QWidget): """Capture the link-click and forward it to the document viewer class for handling. """ - if len(theLink) == 18: - tHandle = theLink[-13:] - self.theParent.viewDocument(tHandle) + 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) return def _doShowHide(self, chState): diff --git a/nw/guimain.py b/nw/guimain.py index 9c4d5d3a..8aeb7143 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -485,7 +485,7 @@ class GuiMain(QMainWindow): self.docEditor.saveText() return True - def viewDocument(self, tHandle=None): + def viewDocument(self, tHandle=None, nLine=0): """Load a document for viewing in the view panel. """ if tHandle is None: @@ -503,13 +503,15 @@ class GuiMain(QMainWindow): # Make sure main tab is in Editor view self.tabWidget.setCurrentWidget(self.splitView) - if self.docViewer.loadText(tHandle) and not self.viewPane.isVisible(): - bPos = self.splitMain.sizes() - self.viewPane.setVisible(True) - vPos = [0,0] - vPos[0] = int(bPos[1]/2) - vPos[1] = bPos[1]-vPos[0] - self.splitView.setSizes(vPos) + if self.docViewer.loadText(tHandle): + if not self.viewPane.isVisible(): + bPos = self.splitMain.sizes() + self.viewPane.setVisible(True) + vPos = [0,0] + vPos[0] = int(bPos[1]/2) + vPos[1] = bPos[1]-vPos[0] + self.splitView.setSizes(vPos) + self.docViewer.setCursorLine(nLine) return True diff --git a/sample/data_a/e7339df26ded_main.nwd b/sample/data_a/e7339df26ded_main.nwd index 05ed60d5..8b92a69c 100644 --- a/sample/data_a/e7339df26ded_main.nwd +++ b/sample/data_a/e7339df26ded_main.nwd @@ -2,6 +2,6 @@ ### We Found John! @pov: John -@location: Mars +@location: Mars, OuterSpace Jane has been searching for a while, and she finally found John on Mars. He was indeed in space! What was he doing on Mars anyway? Well, it turns out, he was farming potatoes. diff --git a/sample/data_f/1471bef9f2ae_main.nwd b/sample/data_f/1471bef9f2ae_main.nwd index 9b2d9368..3bd03e9d 100644 --- a/sample/data_f/1471bef9f2ae_main.nwd +++ b/sample/data_f/1471bef9f2ae_main.nwd @@ -5,4 +5,10 @@ Space … it’s an awful lot of nothing, with bits in it here and there. Some of which, people like to call home. +## Outer Space +@tag: OuterSpace + +Now even further into space! + +You can have more than one tag in a file, as long as there is only one tag per heading. diff --git a/sample/nwProject.nwx b/sample/nwProject.nwx index ad832035..c2d74892 100644 --- a/sample/nwProject.nwx +++ b/sample/nwProject.nwx @@ -1,5 +1,5 @@ - + Sample Project Sample Project @@ -10,9 +10,9 @@ True True - 636b6aa9b697b - ba8a28a246524 - 914 + ae7339df26ded + ae7339df26ded + 941 B E @@ -73,7 +73,7 @@ False True PAGE - 208 + 210 40 2 213 @@ -174,7 +174,7 @@ 139 28 1 - 343 + 237 We Found John! @@ -187,7 +187,7 @@ 189 37 1 - 224 + 236 Characters @@ -257,10 +257,10 @@ False True NOTE - 115 - 24 - 1 - 133 + 241 + 51 + 3 + 286 Mars