From 7d91a15841571573b74107728d14aa16ec9eda95 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Fri, 1 May 2020 17:36:02 +0200 Subject: [PATCH] The document title bar is no populated on document open --- nw/constants/constants.py | 12 ++++++++++ nw/gui/elements/doctitlebar.py | 43 ++++++++++++++++++++++++++++++---- nw/guimain.py | 11 +++++++-- nw/project/project.py | 25 ++++++++++++++++++++ 4 files changed, 84 insertions(+), 7 deletions(-) diff --git a/nw/constants/constants.py b/nw/constants/constants.py index 643c3a37..3f6192d4 100644 --- a/nw/constants/constants.py +++ b/nw/constants/constants.py @@ -234,6 +234,12 @@ class nwUnicode: U_NBSP = "\u00a0" # Non-breaking space U_PARA = "\u2029" # Paragraph separator + ## Arrows + U_UTRI = "\u2bc5" # Up-pointing triangle + U_DTRI = "\u2bc6" # Down-pointing triangle + U_LTRI = "\u2bc7" # Left-pointing triangle + U_RTRI = "\u2bc8" # Right-pointing triangle + # HTML Equivalents ## Quotes @@ -265,4 +271,10 @@ class nwUnicode: ## Other H_NBSP = " " + ## Arrows + H_UTRI = "⯅" + H_DTRI = "⯆" + H_LTRI = "⯇" + H_RTRI = "⯈" + # END Class nwUnicode diff --git a/nw/gui/elements/doctitlebar.py b/nw/gui/elements/doctitlebar.py index 7addfcdf..1d018536 100644 --- a/nw/gui/elements/doctitlebar.py +++ b/nw/gui/elements/doctitlebar.py @@ -32,23 +32,28 @@ from PyQt5.QtCore import Qt from PyQt5.QtGui import QPalette, QColor from PyQt5.QtWidgets import QLabel +from nw.constants import nwUnicode + logger = logging.getLogger(__name__) class GuiDocTitleBar(QLabel): - def __init__(self, theParent): + def __init__(self, theParent, theProject): QLabel.__init__(self, theParent) logger.debug("Initialising DocTitleBar ...") - self.mainConf = nw.CONFIG - self.theParent = theParent - self.theTheme = theParent.theTheme + self.mainConf = nw.CONFIG + self.theParent = theParent + self.theProject = theProject + self.theTheme = theParent.theTheme + self.theHandle = None - self.setText("A > B > C") + self.setText("") self.setContentsMargins(8,2,8,2) self.setAutoFillBackground(True) self.setAlignment(Qt.AlignCenter) + self.setWordWrap(True) docPalette = self.palette() docPalette.setColor(QPalette.Window, QColor(*self.theTheme.colBack)) docPalette.setColor(QPalette.Text, QColor(*self.theTheme.colText)) @@ -58,4 +63,32 @@ class GuiDocTitleBar(QLabel): return + def setTitleFromHandle(self, tHandle): + """Sets the document title from the handle, or alternatively, + set the whole document path. + """ + + self.setText("") + self.theHandle = tHandle + if tHandle is None: + return False + + if True: + tTitle = [] + tTree = self.theProject.getItemPath(tHandle) + for aHandle in reversed(tTree): + nwItem = self.theProject.getItem(aHandle) + if nwItem is not None: + tTitle.append(nwItem.itemName) + sSep = " %s " % nwUnicode.U_RTRI + self.setText(sSep.join(tTitle)) + else: + nwItem = self.theProject.getItem(tHandle) + if nwItem is None: + return False + + self.setText(nwItem.itemName) + + return True + # END Class GuiDocTitleBar diff --git a/nw/guimain.py b/nw/guimain.py index 8e561fc7..c9558787 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -93,8 +93,8 @@ class GuiMain(QMainWindow): self.treeView = GuiDocTree(self, self.theProject) self.projView = GuiProjectOutline(self, self.theProject) self.mainMenu = GuiMainMenu(self, self.theProject) - self.viewTitle = GuiDocTitleBar(self) - self.editTitle = GuiDocTitleBar(self) + self.viewTitle = GuiDocTitleBar(self, self.theProject) + self.editTitle = GuiDocTitleBar(self, self.theProject) # Minor Gui Elements self.statusIcons = [] @@ -452,10 +452,13 @@ class GuiMain(QMainWindow): ## def closeDocument(self): + """Close the document and clear the editor and title field. + """ if self.hasProject: if self.docEditor.docChanged: self.saveDocument() self.docEditor.clearEditor() + self.editTitle.setTitleFromHandle(None) return True def openDocument(self, tHandle, tLine=None): @@ -467,6 +470,7 @@ class GuiMain(QMainWindow): if self.docEditor.loadText(tHandle, tLine): self.docEditor.setFocus() self.theProject.setLastEdited(tHandle) + self.editTitle.setTitleFromHandle(tHandle) else: return False return True @@ -477,6 +481,8 @@ class GuiMain(QMainWindow): return True def viewDocument(self, tHandle=None): + """Load a document for viewing in the view panel. + """ if tHandle is None: tHandle = self.treeView.getSelectedHandle() @@ -494,6 +500,7 @@ class GuiMain(QMainWindow): self.tabWidget.setCurrentWidget(self.splitView) if self.docViewer.loadText(tHandle) and not self.viewPane.isVisible(): + self.viewTitle.setTitleFromHandle(tHandle) bPos = self.splitMain.sizes() self.viewPane.setVisible(True) vPos = [0,0] diff --git a/nw/project/project.py b/nw/project/project.py index aa63649a..ef09f671 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -601,6 +601,9 @@ class NWProject(): ## def getItem(self, tHandle): + """Return a project item based on its handle. Returns None if + the handle doesn't exist in the project. + """ if tHandle in self.projTree: return self.projTree[tHandle] logger.error("No tree item with handle %s" % str(tHandle)) @@ -626,6 +629,28 @@ class NWProject(): return tHandle return None + def getItemPath(self, tHandle): + """Iterate upwards in the tree until we find the item with + parent None, the root item, and return the list of handles. + We do this with a for loop with a maximum depth of 200 to make + infinite loops impossible. + """ + tTree = [] + tItem = self.getItem(tHandle) + if tItem is not None: + tTree.append(tHandle) + for i in range(200): + if tItem.parHandle is None: + return tTree + else: + tHandle = tItem.parHandle + tItem = self.getItem(tHandle) + if tItem is None: + return tTree + else: + tTree.append(tHandle) + return tTree + def getProjectItems(self): """This function is called from the tree view when building the tree. Each item in the project is returned in the order saved in