From 2a3df92b202c07ebf6bef41ab3fc79505c49dc0b Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Tue, 8 Sep 2020 18:44:08 +0200 Subject: [PATCH] Added starte view history class and connected mouse, menu and keyboard --- nw/gui/docviewer.py | 73 +++++++++++++++++++++++++++++++++++++++++---- nw/gui/mainmenu.py | 17 +++++++++++ 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/nw/gui/docviewer.py b/nw/gui/docviewer.py index 17b668c8..e8333a00 100644 --- a/nw/gui/docviewer.py +++ b/nw/gui/docviewer.py @@ -63,20 +63,22 @@ class GuiDocViewer(QTextBrowser): self.setMinimumWidth(self.mainConf.pxInt(300)) self.setAutoFillBackground(True) self.setOpenExternalLinks(False) + self.setFocusPolicy(Qt.StrongFocus) self.initViewer() # Document Header and Footer - self.docHeader = GuiDocViewHeader(self) - self.docFooter = GuiDocViewFooter(self) - self.stickyRef = False + self.docHeader = GuiDocViewHeader(self) + self.docFooter = GuiDocViewFooter(self) + self.docHistory = GuiDocViewHistory(self) + self.stickyRef = False theOpt = QTextOption() if self.mainConf.doJustify: theOpt.setAlignment(Qt.AlignJustify) self.qDocument.setDefaultTextOption(theOpt) + # Signals self.anchorClicked.connect(self._linkClicked) - self.setFocusPolicy(Qt.StrongFocus) # Context Menu self.setContextMenuPolicy(Qt.CustomContextMenu) @@ -169,6 +171,7 @@ class GuiDocViewer(QTextBrowser): self.setHtml(aDoc.theResult.replace("\t", "!!tab!!")) self.setDocumentTitle(tHandle) + self.docHistory.append(tHandle) # Loop through the text and put back in the tabs. Tabs are removed by # the setHtml function, so the ToHtml class puts in a placeholder. @@ -217,8 +220,7 @@ class GuiDocViewer(QTextBrowser): # the doc view panel is visible in case this request comes # from outside this class. logger.verbose("Tag points to %s#%s" % (tHandle, sTitle)) - self.theParent.viewDocument(tHandle) - self.navigateTo("#%s" % sTitle) + self.theParent.viewDocument(tHandle, "#%s" % sTitle) return True def docAction(self, theAction): @@ -252,6 +254,18 @@ class GuiDocViewer(QTextBrowser): self.setSource(QUrl(tAnchor)) return True + def navBackward(self): + """Navigate backwards in the document view history. + """ + self.docHistory.backward() + return + + def navForward(self): + """Navigate forwards in the document view history. + """ + self.docHistory.forward() + return + def updateDocMargins(self): """Automatically adjust the margins so the text is centred if Config.textFixedW is enabled or we're in Focus Mode. Otherwise, @@ -382,6 +396,15 @@ class GuiDocViewer(QTextBrowser): self.updateDocMargins() return + def mouseReleaseEvent(self, theEvent): + """Capture mouse click events on the document. + """ + if theEvent.button() == Qt.BackButton: + self.navBackward() + elif theEvent.button() == Qt.ForwardButton: + self.navForward() + return + ## # Internal Functions ## @@ -481,6 +504,44 @@ class GuiDocViewer(QTextBrowser): # END Class GuiDocViewer +class GuiDocViewHistory(): + + def __init__(self, docViewer): + + self.docViewer = docViewer + + self._navHistory = [] + self._navPosition = -1 + + return + + def clear(self): + logger.verbose("Clear view history") + self._navHistory = [] + self._navPosition = -1 + return + + def append(self, tHandle): + logger.verbose("Added %s to view history" % tHandle) + return + + def forward(self): + logger.verbose("Move forwards in view history") + return True + + def backward(self): + logger.verbose("Move backwards in view history") + return True + + ## + # Internal Functions + ## + + def _truncateHistory(self, atPos): + return + +# END Class GuiDocViewHistory + # =============================================================================================== # # The Embedded Document Header # Only used by DocViewer, and is at a fixed position in the QTextBrowser's viewport diff --git a/nw/gui/mainmenu.py b/nw/gui/mainmenu.py index b904ffd9..ed96a4ee 100644 --- a/nw/gui/mainmenu.py +++ b/nw/gui/mainmenu.py @@ -475,6 +475,23 @@ class GuiMainMenu(QMenuBar): # View > Separator self.viewMenu.addSeparator() + # View > Navigate Viewer Backward + self.aViewPrev = QAction("Navigate Viewer Backward", self) + self.aViewPrev.setStatusTip("Show the previous document in the right pane") + self.aViewPrev.setShortcut("Alt+Left") + self.aViewPrev.triggered.connect(self.theParent.docViewer.navBackward) + self.viewMenu.addAction(self.aViewPrev) + + # View > Navigate Viewer Forward + self.aViewNext = QAction("Navigate Viewer Forward", self) + self.aViewNext.setStatusTip("Show the next document in the right pane") + self.aViewNext.setShortcut("Alt+Right") + self.aViewNext.triggered.connect(self.theParent.docViewer.navForward) + self.viewMenu.addAction(self.aViewNext) + + # View > Separator + self.viewMenu.addSeparator() + # View > Toggle Distraction Free Mode self.aFocusMode = QAction("Distraction Free Mode", self) self.aFocusMode.setStatusTip("Toggles distraction free mode, only showing text editor")