From 9fea45b7dfb363eae63578f0cd6409c55cee279c Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Thu, 31 Oct 2019 13:37:27 +0100 Subject: [PATCH] The editable flag for files is now respected by the editor, and trash files are not editable --- nw/gui/doceditor.py | 5 ++- nw/gui/doctree.py | 22 +++++++------ nw/project/document.py | 25 +++++++++++---- nw/project/project.py | 13 ++++++++ .../sampleNovel/data_4/cd0bd12b087d_main.nwd | 4 ++- sample/sampleNovel/nwProject.nwx | 32 +++++++++---------- 6 files changed, 67 insertions(+), 34 deletions(-) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index e8e9bb4e..15688c1f 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -164,6 +164,7 @@ class GuiDocEditor(QTextEdit): # If we have a document open, we should reload it in case the font changed, otherwise # we just clear the editor entirely, which makes it read only. if self.theHandle is not None: + # We must save the current handle as clearEditor() sets it to None tHandle = self.theHandle self.clearEditor() self.loadText(tHandle) @@ -194,9 +195,11 @@ class GuiDocEditor(QTextEdit): self._runCounter() self.wcTimer.start() self.setDocumentChanged(False) - self.setReadOnly(False) self.theHandle = tHandle + if self.nwDocument.docEditable: + self.setReadOnly(False) + return True def saveText(self): diff --git a/nw/gui/doctree.py b/nw/gui/doctree.py index 1ef4f5fa..75c745a6 100644 --- a/nw/gui/doctree.py +++ b/nw/gui/doctree.py @@ -41,8 +41,8 @@ class GuiDocTree(QTreeWidget): self.theProject = theProject # Tree Settings - self.theMap = None - self.orphRoot = None + self.theMap = None + self.orphRoot = None self.clearTree() @@ -97,15 +97,23 @@ class GuiDocTree(QTreeWidget): return False if itemClass is None and pHandle is not None: - itemClass = self.theProject.getItem(pHandle).itemClass + pItem = self.theProject.getItem(pHandle) + if pItem is not None: + itemClass = pItem.itemClass if itemClass is None: if itemType is not None: if itemType == nwItemType.FILE: - self.makeAlert("Please select a location in the tree to add a document.", nwAlert.ERROR) + self.makeAlert( + "Please select a valid location in the tree to add a document.", + nwAlert.ERROR + ) return False elif itemType == nwItemType.FOLDER: - self.makeAlert("Please select a location in the tree to add a folder.", nwAlert.ERROR) + self.makeAlert( + "Please select a valid location in the tree to add a folder.", + nwAlert.ERROR + ) return False self.makeAlert("Failed to add new item.", nwAlert.BUG) return False @@ -141,10 +149,6 @@ class GuiDocTree(QTreeWidget): self.makeAlert("Cannot add new files or folders to the trash folder.", nwAlert.ERROR) return False - if pHandle == self.orphRoot: - self.makeAlert("Cannot add new files or folders to the orphaned folder.", nwAlert.ERROR) - return False - # If we're still here, add the file or folder if itemType == nwItemType.FILE: tHandle = self.theProject.newFile("New File", itemClass, pHandle) diff --git a/nw/project/document.py b/nw/project/document.py index 923f5c9e..24e203ea 100644 --- a/nw/project/document.py +++ b/nw/project/document.py @@ -25,11 +25,12 @@ class NWDoc(): def __init__(self, theProject, theParent): - self.mainConf = nw.CONFIG - self.theProject = theProject - self.theParent = theParent - self.theItem = None - self.docHandle = None + self.mainConf = nw.CONFIG + self.theProject = theProject + self.theParent = theParent + self.theItem = None + self.docHandle = None + self.docEditable = False # Internal Mapping self.makeAlert = self.theParent.makeAlert @@ -37,8 +38,9 @@ class NWDoc(): return def clearDocument(self): - self.theItem = None - self.docHandle = None + self.theItem = None + self.docHandle = None + self.docEditable = False return def openDocument(self, tHandle, showStatus=True): @@ -46,6 +48,15 @@ class NWDoc(): self.docHandle = tHandle self.theItem = self.theProject.getItem(tHandle) + if self.theItem is None: + self.clearDocument() + return None + + # By default, the document is editable. Except for files in the trash folder. + self.docEditable = True + if self.theItem.parHandle == self.theProject.trashRoot: + self.docEditable = False + docDir, docFile = self._assemblePath(self.FILE_MN) logger.debug("Opening document %s" % path.join(docDir,docFile)) dataDir = path.join(self.theProject.projPath, docDir) diff --git a/nw/project/project.py b/nw/project/project.py index df298dc8..06c3d593 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -478,6 +478,19 @@ class NWProject(): logger.error("No tree item with handle %s" % str(tHandle)) return None + def getRootItem(self, tHandle): + """Iterate upwards in the tree until we find the item with parent None, the root item. + We do this with a for loop with a maximum depth of 200 to make infinite loops impossible. + """ + tItem = self.getItem(tHandle) + if tItem is not None: + for i in range(200): + tHandle = tItem.parHandle + tItem = self.getItem(tHandle) + if tItem is None: + return tHandle + return None + 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 the project file, but first it checks that it has diff --git a/sample/sampleNovel/data_4/cd0bd12b087d_main.nwd b/sample/sampleNovel/data_4/cd0bd12b087d_main.nwd index 21e28fe8..47a92622 100644 --- a/sample/sampleNovel/data_4/cd0bd12b087d_main.nwd +++ b/sample/sampleNovel/data_4/cd0bd12b087d_main.nwd @@ -1 +1,3 @@ -# Very Deep File \ No newline at end of file +# Very Deep File + +This file is still editable. diff --git a/sample/sampleNovel/nwProject.nwx b/sample/sampleNovel/nwProject.nwx index 11629296..fe8d3b52 100644 --- a/sample/sampleNovel/nwProject.nwx +++ b/sample/sampleNovel/nwProject.nwx @@ -1,5 +1,5 @@ - + Sample Project Sample Project @@ -11,7 +11,7 @@ True b8136a5a774a0 ba8a28a246524 - 879 + 884 B E @@ -142,7 +142,7 @@ 567 112 6 - 634 + 326 Characters @@ -220,19 +220,7 @@ None True - - Orphaned File 2 - FILE - NO_CLASS - None - False - NO_LAYOUT - 14 - 3 - 0 - 1 - - + Delete Me! FILE NOVEL @@ -244,5 +232,17 @@ 1 36 + + Orphaned File 1 + FILE + NO_CLASS + None + False + NO_LAYOUT + 42 + 8 + 1 + 47 +