Nav history in doc viewer now works

This commit is contained in:
Veronica K. B. Olsen
2020-09-08 21:20:38 +02:00
parent 2a3df92b20
commit e30064ba0b
2 changed files with 118 additions and 11 deletions
+117 -11
View File
@@ -10,6 +10,7 @@
Created: 2019-10-31 [0.3.2] GuiDocViewDetails Created: 2019-10-31 [0.3.2] GuiDocViewDetails
Created: 2020-04-25 [0.4.5] GuiDocViewHeader Created: 2020-04-25 [0.4.5] GuiDocViewHeader
Created: 2020-06-09 [0.8.0] GuiDocViewFooter Created: 2020-06-09 [0.8.0] GuiDocViewFooter
Created: 2020-09-08 [1.0b1] GuiDocViewHistory
This file is a part of novelWriter This file is a part of novelWriter
Copyright 2020, Veronica Berglyd Olsen Copyright 2020, Veronica Berglyd Olsen
@@ -131,7 +132,7 @@ class GuiDocViewer(QTextBrowser):
return True return True
def loadText(self, tHandle): def loadText(self, tHandle, updateHistory=True):
"""Load text into the viewer from an item handle. """Load text into the viewer from an item handle.
""" """
tItem = self.theProject.projTree[tHandle] tItem = self.theProject.projTree[tHandle]
@@ -169,9 +170,12 @@ class GuiDocViewer(QTextBrowser):
else: else:
self.setTabStopWidth(self.mainConf.getTabWidth()) self.setTabStopWidth(self.mainConf.getTabWidth())
# Must be before setHtml
if updateHistory:
self.docHistory.append(tHandle)
self.setHtml(aDoc.theResult.replace("\t", "!!tab!!")) self.setHtml(aDoc.theResult.replace("\t", "!!tab!!"))
self.setDocumentTitle(tHandle) self.setDocumentTitle(tHandle)
self.docHistory.append(tHandle)
# Loop through the text and put back in the tabs. Tabs are removed by # 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. # the setHtml function, so the ToHtml class puts in a placeholder.
@@ -266,6 +270,12 @@ class GuiDocViewer(QTextBrowser):
self.docHistory.forward() self.docHistory.forward()
return return
def clearNavHistory(self):
"""Clear the navigation history.
"""
self.docHistory.clear()
return
def updateDocMargins(self): def updateDocMargins(self):
"""Automatically adjust the margins so the text is centred if """Automatically adjust the margins so the text is centred if
Config.textFixedW is enabled or we're in Focus Mode. Otherwise, Config.textFixedW is enabled or we're in Focus Mode. Otherwise,
@@ -326,6 +336,26 @@ class GuiDocViewer(QTextBrowser):
logger.verbose("Cursor moved to line %d" % theLine) logger.verbose("Cursor moved to line %d" % theLine)
return True return True
def setScrollPosition(self, thePos):
"""Set the scrollbar position.
"""
vBar = self.verticalScrollBar()
if vBar.isVisible():
vBar.setValue(thePos)
return
##
# Getters
##
def getScrollPosition(self):
"""Get the scrollbar position. Returns 0 if no scrollbar.
"""
vBar = self.verticalScrollBar()
if vBar.isVisible():
return vBar.value()
return 0
## ##
# Slots # Slots
## ##
@@ -403,6 +433,8 @@ class GuiDocViewer(QTextBrowser):
self.navBackward() self.navBackward()
elif theEvent.button() == Qt.ForwardButton: elif theEvent.button() == Qt.ForwardButton:
self.navForward() self.navForward()
else:
QTextBrowser.mouseReleaseEvent(self, theEvent)
return return
## ##
@@ -511,35 +543,109 @@ class GuiDocViewHistory():
self.docViewer = docViewer self.docViewer = docViewer
self._navHistory = [] self._navHistory = []
self._navPosition = -1 self._posHistory = []
self._currPos = -1
self._prevPos = -1
return return
def clear(self): def clear(self):
logger.verbose("Clear view history") """Clear the view history.
"""
logger.verbose("View history cleared")
self._navHistory = [] self._navHistory = []
self._navPosition = -1 self._posHistory = []
self._currPos = -1
self._prevPos = -1
return return
def append(self, tHandle): def append(self, tHandle):
"""Append a document handle and its scroll bar position to the
history, but only if the document is different than the current
active entry. Any further entries are truncated.
"""
if self._currPos >= 0 and self._currPos < len(self._navHistory):
if tHandle == self._navHistory[self._currPos]:
logger.verbose("Not updating view hsitory")
return False
self._truncateHistory(self._currPos)
self._navHistory.append(tHandle)
self._posHistory.append(0)
self._prevPos = self._currPos
self._currPos = len(self._navHistory) - 1
self._updateScrollBar()
self._dumpHistory()
logger.verbose("Added %s to view history" % tHandle) logger.verbose("Added %s to view history" % tHandle)
return
return True
def forward(self): def forward(self):
logger.verbose("Move forwards in view history") """Navigate to the next entry in the view history.
return True """
newPos = self._currPos + 1
if newPos < len(self._navHistory):
logger.verbose("Move forward in view history")
self._prevPos = self._currPos
self._updateScrollBar()
self.docViewer.loadText(self._navHistory[newPos], updateHistory=False)
self.docViewer.setScrollPosition(self._posHistory[newPos])
self._currPos = newPos
self._dumpHistory()
return
def backward(self): def backward(self):
logger.verbose("Move backwards in view history") """Navigate to the previous entry in the view history.
return True """
newPos = self._currPos - 1
if newPos >= 0:
logger.verbose("Move backward in view history")
self._prevPos = self._currPos
self._updateScrollBar()
self.docViewer.loadText(self._navHistory[newPos], updateHistory=False)
self.docViewer.setScrollPosition(self._posHistory[newPos])
self._currPos = newPos
self._dumpHistory()
return
## ##
# Internal Functions # Internal Functions
## ##
def _truncateHistory(self, atPos): def _updateScrollBar(self):
"""Update the scrollbar position of the previous entry.
"""
if self._prevPos >= 0 and self._prevPos < len(self._posHistory):
self._posHistory[self._prevPos] = self.docViewer.getScrollPosition()
return return
def _truncateHistory(self, atPos):
"""Truncate the navigation history to the given position.
"""
nSkip = 1 if atPos > 49 else 0
self._navHistory = self._navHistory[nSkip:atPos + 1]
self._posHistory = self._posHistory[nSkip:atPos + 1]
self._currPos -= nSkip
self._prevPos -= nSkip
return
def _dumpHistory(self):
for i, (h, p) in enumerate(zip(self._navHistory, self._posHistory)):
print("%s %3d %13s %6d" % (">" if i == self._currPos else " ", i, h, p))
# END Class GuiDocViewHistory # END Class GuiDocViewHistory
# =============================================================================================== # # =============================================================================================== #
+1
View File
@@ -346,6 +346,7 @@ class GuiMain(QMainWindow):
if saveOK: if saveOK:
self.closeDocument() self.closeDocument()
self.docViewer.clearNavHistory()
self.projView.closeOutline() self.projView.closeOutline()
self.theProject.closeProject() self.theProject.closeProject()
self.theIndex.clearIndex() self.theIndex.clearIndex()