Clean up document viewer class

This commit is contained in:
Veronica Berglyd Olsen
2023-11-17 00:06:29 +01:00
parent 9d662a0d40
commit f81c9aa319
+46 -49
View File
@@ -225,14 +225,12 @@ class GuiDocViewer(QTextBrowser):
if updateHistory: if updateHistory:
self.docHistory.append(tHandle) self.docHistory.append(tHandle)
self.setHtml(aDoc.theResult.replace("\t", "!!tab!!"))
self.setDocumentTitle(tHandle) self.setDocumentTitle(tHandle)
# Loop through the text and put back in the tabs. Tabs are removed by # Replace tabs before setting the HTML, and then put them back in
# the setHtml function, so the ToHtml class puts in a placeholder. self.setHtml(aDoc.theResult.replace("\t", "!!tab!!"))
while self.find("!!tab!!"): while self.find("!!tab!!"):
theCursor = self.textCursor() self.textCursor().insertText("\t")
theCursor.insertText("\t")
if self._docHandle == tHandle: if self._docHandle == tHandle:
# This is a refresh, so we set the scrollbar back to where it was # This is a refresh, so we set the scrollbar back to where it was
@@ -263,22 +261,22 @@ class GuiDocViewer(QTextBrowser):
self.updateDocMargins() self.updateDocMargins()
return return
def docAction(self, theAction: nwDocAction) -> bool: def docAction(self, action: nwDocAction) -> bool:
"""Process document actions on the current document.""" """Process document actions on the current document."""
logger.debug("Requesting action: '%s'", theAction.name) logger.debug("Requesting action: '%s'", action.name)
if self._docHandle is None: if self._docHandle is None:
logger.error("No document open") logger.error("No document open")
return False return False
if theAction == nwDocAction.CUT: if action == nwDocAction.CUT:
self.copy() self.copy()
elif theAction == nwDocAction.COPY: elif action == nwDocAction.COPY:
self.copy() self.copy()
elif theAction == nwDocAction.SEL_ALL: elif action == nwDocAction.SEL_ALL:
self._makeSelection(QTextCursor.Document) self._makeSelection(QTextCursor.Document)
elif theAction == nwDocAction.SEL_PARA: elif action == nwDocAction.SEL_PARA:
self._makeSelection(QTextCursor.BlockUnderCursor) self._makeSelection(QTextCursor.BlockUnderCursor)
else: else:
logger.debug("Unknown or unsupported document action '%s'", str(theAction)) logger.debug("Unknown or unsupported document action '%s'", str(action))
return False return False
return True return True
@@ -366,11 +364,11 @@ class GuiDocViewer(QTextBrowser):
@pyqtSlot("QUrl") @pyqtSlot("QUrl")
def _linkClicked(self, url: QUrl) -> None: def _linkClicked(self, url: QUrl) -> None:
"""Process a clicked link internally in the document.""" """Process a clicked link in the document."""
theLink = url.url() link = url.url()
logger.debug("Clicked link: '%s'", theLink) logger.debug("Clicked link: '%s'", link)
if len(theLink) > 0: if len(link) > 0:
theBits = theLink.split("=") theBits = link.split("=")
if len(theBits) == 2: if len(theBits) == 2:
self.loadDocumentTagRequest.emit(theBits[1], nwDocMode.VIEW) self.loadDocumentTagRequest.emit(theBits[1], nwDocMode.VIEW)
return return
@@ -442,29 +440,28 @@ class GuiDocViewer(QTextBrowser):
## ##
def _makeSelection(self, selType: QTextCursor.SelectionType) -> None: def _makeSelection(self, selType: QTextCursor.SelectionType) -> None:
"""Handle select of text based on a selection mode.""" """Handle selection of text based on a selection mode."""
theCursor = self.textCursor() cursor = self.textCursor()
theCursor.clearSelection() cursor.clearSelection()
theCursor.select(selType) cursor.select(selType)
if selType == QTextCursor.BlockUnderCursor: if selType == QTextCursor.BlockUnderCursor:
# This selection mode also selects the preceding paragraph # This selection mode also selects the preceding paragraph
# separator, which we want to avoid. # separator, which we want to avoid.
posS = theCursor.selectionStart() posS = cursor.selectionStart()
posE = theCursor.selectionEnd() posE = cursor.selectionEnd()
selTxt = theCursor.selectedText() selTxt = cursor.selectedText()
if selTxt.startswith(nwUnicode.U_PSEP): if selTxt.startswith(nwUnicode.U_PSEP):
theCursor.setPosition(posS+1, QTextCursor.MoveAnchor) cursor.setPosition(posS+1, QTextCursor.MoveAnchor)
theCursor.setPosition(posE, QTextCursor.KeepAnchor) cursor.setPosition(posE, QTextCursor.KeepAnchor)
self.setTextCursor(theCursor) self.setTextCursor(cursor)
return return
def _makePosSelection(self, selType: QTextCursor.SelectionType, pos: QPoint) -> None: def _makePosSelection(self, selType: QTextCursor.SelectionType, pos: QPoint) -> None:
"""Handle text selection at a given location.""" """Handle text selection at a given location."""
theCursor = self.cursorForPosition(pos) self.setTextCursor(self.cursorForPosition(pos))
self.setTextCursor(theCursor)
self._makeSelection(selType) self._makeSelection(selType)
return return
@@ -668,18 +665,18 @@ class GuiDocViewHeader(QWidget):
self.setAutoFillBackground(True) self.setAutoFillBackground(True)
# Title Label # Title Label
self.theTitle = QLabel() self.docTitle = QLabel()
self.theTitle.setText("") self.docTitle.setText("")
self.theTitle.setIndent(0) self.docTitle.setIndent(0)
self.theTitle.setMargin(0) self.docTitle.setMargin(0)
self.theTitle.setContentsMargins(0, 0, 0, 0) self.docTitle.setContentsMargins(0, 0, 0, 0)
self.theTitle.setAutoFillBackground(True) self.docTitle.setAutoFillBackground(True)
self.theTitle.setAlignment(Qt.AlignHCenter | Qt.AlignTop) self.docTitle.setAlignment(Qt.AlignHCenter | Qt.AlignTop)
self.theTitle.setFixedHeight(fPx) self.docTitle.setFixedHeight(fPx)
lblFont = self.theTitle.font() lblFont = self.docTitle.font()
lblFont.setPointSizeF(0.9*SHARED.theme.fontPointSize) lblFont.setPointSizeF(0.9*SHARED.theme.fontPointSize)
self.theTitle.setFont(lblFont) self.docTitle.setFont(lblFont)
# Buttons # Buttons
self.backButton = QToolButton(self) self.backButton = QToolButton(self)
@@ -723,7 +720,7 @@ class GuiDocViewHeader(QWidget):
self.outerBox.setSpacing(hSp) self.outerBox.setSpacing(hSp)
self.outerBox.addWidget(self.backButton, 0) self.outerBox.addWidget(self.backButton, 0)
self.outerBox.addWidget(self.forwardButton, 0) self.outerBox.addWidget(self.forwardButton, 0)
self.outerBox.addWidget(self.theTitle, 1) self.outerBox.addWidget(self.docTitle, 1)
self.outerBox.addWidget(self.refreshButton, 0) self.outerBox.addWidget(self.refreshButton, 0)
self.outerBox.addWidget(self.closeButton, 0) self.outerBox.addWidget(self.closeButton, 0)
self.setLayout(self.outerBox) self.setLayout(self.outerBox)
@@ -771,12 +768,12 @@ class GuiDocViewHeader(QWidget):
"""Update the colours of the widget to match those of the syntax """Update the colours of the widget to match those of the syntax
theme rather than the main GUI. theme rather than the main GUI.
""" """
thePalette = QPalette() palette = QPalette()
thePalette.setColor(QPalette.Window, QColor(*SHARED.theme.colBack)) palette.setColor(QPalette.Window, QColor(*SHARED.theme.colBack))
thePalette.setColor(QPalette.WindowText, QColor(*SHARED.theme.colText)) palette.setColor(QPalette.WindowText, QColor(*SHARED.theme.colText))
thePalette.setColor(QPalette.Text, QColor(*SHARED.theme.colText)) palette.setColor(QPalette.Text, QColor(*SHARED.theme.colText))
self.setPalette(thePalette) self.setPalette(palette)
self.theTitle.setPalette(thePalette) self.docTitle.setPalette(palette)
return return
def setTitleFromHandle(self, tHandle: str | None) -> bool: def setTitleFromHandle(self, tHandle: str | None) -> bool:
@@ -785,7 +782,7 @@ class GuiDocViewHeader(QWidget):
""" """
self._docHandle = tHandle self._docHandle = tHandle
if tHandle is None: if tHandle is None:
self.theTitle.setText("") self.docTitle.setText("")
self.backButton.setVisible(False) self.backButton.setVisible(False)
self.forwardButton.setVisible(False) self.forwardButton.setVisible(False)
self.closeButton.setVisible(False) self.closeButton.setVisible(False)
@@ -801,12 +798,12 @@ class GuiDocViewHeader(QWidget):
if nwItem is not None: if nwItem is not None:
tTitle.append(nwItem.itemName) tTitle.append(nwItem.itemName)
sSep = " %s " % nwUnicode.U_RSAQUO sSep = " %s " % nwUnicode.U_RSAQUO
self.theTitle.setText(sSep.join(tTitle)) self.docTitle.setText(sSep.join(tTitle))
else: else:
nwItem = pTree[tHandle] nwItem = pTree[tHandle]
if nwItem is None: if nwItem is None:
return False return False
self.theTitle.setText(nwItem.itemName) self.docTitle.setText(nwItem.itemName)
self.backButton.setVisible(True) self.backButton.setVisible(True)
self.forwardButton.setVisible(True) self.forwardButton.setVisible(True)