From 76cb03fad884c1bc18f422ecb2f31f06344d79fc Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Sun, 26 May 2019 15:34:34 +0200 Subject: [PATCH] Some reorganisation of how stuff is opened and closed. --- nw/gui/mainmenu.py | 9 ++- nw/gui/winmain.py | 63 +++++++++---------- nw/project/project.py | 4 +- .../sampleNovel/data_6/36b6aa9b697b_main.nwd | 2 +- sample/sampleNovel/meta/sessionInfo.log | 7 +++ sample/sampleNovel/nwProject.nwx | 12 ++-- 6 files changed, 52 insertions(+), 45 deletions(-) diff --git a/nw/gui/mainmenu.py b/nw/gui/mainmenu.py index 8c731c5b..04233cca 100644 --- a/nw/gui/mainmenu.py +++ b/nw/gui/mainmenu.py @@ -157,7 +157,7 @@ class GuiMainMenu(QMenuBar): menuItem = QAction(QIcon.fromTheme("document-revert"), "Close Project", self) menuItem.setStatusTip("Close Project") menuItem.setShortcut("Ctrl+Shift+W") - menuItem.triggered.connect(self.theParent.closeProject) + menuItem.triggered.connect(lambda : self.theParent.closeProject(False)) self.projMenu.addAction(menuItem) # Project > Recent Projects @@ -253,6 +253,13 @@ class GuiMainMenu(QMenuBar): menuItem.triggered.connect(self.theParent.saveDocument) self.docuMenu.addAction(menuItem) + # Document > Close + menuItem = QAction(QIcon.fromTheme("document-revert"), "Close Document", self) + menuItem.setStatusTip("Close Current Document") + menuItem.setShortcut("Ctrl+W") + menuItem.triggered.connect(self.theParent.closeDocument) + self.docuMenu.addAction(menuItem) + # Document > Separator self.docuMenu.addSeparator() diff --git a/nw/gui/winmain.py b/nw/gui/winmain.py index 021024c7..585b9853 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/winmain.py @@ -216,16 +216,18 @@ class GuiMain(QMainWindow): return True - def closeProject(self): - + def closeProject(self, isYes=False): + """Closes the project if one is open. + isYes is passed on from the close application event so the user doesn't get prompted twice. + """ if not self.hasProject: + # There is no project loaded, everything OK return True - if self.mainConf.showGUI: + if self.mainConf.showGUI and not isYes: msgBox = QMessageBox() msgRes = msgBox.question( - self, "Close Project", - "Close current project?
Unsaved changes will be saved." + self, "Close Project", "Save changes and close current project?" ) if msgRes != QMessageBox.Yes: return False @@ -244,15 +246,19 @@ class GuiMain(QMainWindow): return saveOK def openProject(self, projFile=None): - + """Open a project. + projFile is passed from the open recent projects menu, so can be set. If not, we pop the dialog. + """ if projFile is None: projFile = self.openProjectDialog() if projFile is None: return False + # Make sure any open project is cleared out first before we load another one if not self.closeProject(): return False + # Do the stuff self.theProject.openProject(projFile) self._setWindowTitle(self.theProject.projName) self.rebuildTree() @@ -260,6 +266,7 @@ class GuiMain(QMainWindow): self.docEditor.setSpellCheck(self.theProject.spellCheck) self.mainMenu.updateMenu() + # Restore previously open documents, if any if self.theProject.lastEdited is not None: self.openDocument(self.theProject.lastEdited) if self.theProject.lastViewed is not None: @@ -270,7 +277,9 @@ class GuiMain(QMainWindow): return True def saveProject(self): - + """Save the current project. + """ + # If the project is new, it may not have a path, so we need one if self.theProject.projPath is None: projPath = self.saveProjectDialog() self.theProject.setProjectPath(projPath) @@ -288,13 +297,15 @@ class GuiMain(QMainWindow): ## def closeDocument(self): - self.saveDocument() + if self.docEditor.docChanged: + self.saveDocument() self.theDocument.clearDocument() + self.docEditor.clearEditor() + self.theProject.setLastEdited(None) return True def openDocument(self, tHandle): - if self.docEditor.docChanged: - self.saveDocument() + self.closeDocument() self.docEditor.setText(self.theDocument.openDocument(tHandle)) self.docEditor.setReadOnly(False) self.docEditor.setCursorPosition(self.theDocument.theItem.cursorPos) @@ -305,13 +316,13 @@ class GuiMain(QMainWindow): def saveDocument(self): if self.theDocument.theItem is not None: - docHtml = self.docEditor.getText() + docText = self.docEditor.getText() cursPos = self.docEditor.getCursorPosition() self.theDocument.theItem.setCharCount(self.docEditor.charCount) self.theDocument.theItem.setWordCount(self.docEditor.wordCount) self.theDocument.theItem.setParaCount(self.docEditor.paraCount) self.theDocument.theItem.setCursorPos(cursPos) - self.theDocument.saveDocument(docHtml) + self.theDocument.saveDocument(docText) self.docEditor.setDocumentChanged(False) return True @@ -440,18 +451,14 @@ class GuiMain(QMainWindow): if self.mainConf.showGUI: msgBox = QMessageBox() msgRes = msgBox.question( - self, "Exit", - "Do you want to exit %s?" % nw.__package__ + self, "Exit", "Do you want to save changes and exit?" ) if msgRes != QMessageBox.Yes: return False logger.info("Exiting %s" % nw.__package__) - if self._takeDocumentAction(): - self.saveDocument() - if self._takeProjectAction(): - self.saveProject() - self.theProject.closeProject() + self.closeProject(True) + self.mainConf.setWinSize(self.width(), self.height()) self.mainConf.setTreeColWidths(self.treeView.getColumnSizes()) self.mainConf.setMainPanePos(self.splitMain.sizes()) @@ -493,31 +500,17 @@ class GuiMain(QMainWindow): return True def _autoSaveProject(self): - if self._takeProjectAction(): + if self.hasProject and self.theProject.projChanged and self.theProject.projPath is not None: logger.debug("Autosaving project") self.saveProject() return def _autoSaveDocument(self): - if self._takeDocumentAction(): + if self.hasProject and self.docEditor.docChanged and self.theDocument.theItem is not None: logger.debug("Autosaving document") self.saveDocument() return - def _takeProjectAction(self): - if self.theProject.projPath is None: - return False - if not self.theProject.projChanged: - return False - return True - - def _takeDocumentAction(self): - if self.theDocument.theItem is None: - return False - if not self.docEditor.docChanged: - return False - return True - def _makeStatusIcons(self): self.statusIcons = {} for sLabel, sCol, _ in self.theProject.statusItems: diff --git a/nw/project/project.py b/nw/project/project.py index 6ddb992d..dce45ef3 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -123,14 +123,14 @@ class NWProject(): ## def newProject(self): - hNovel = self.newRoot("Novel", nwItemClass.NOVEL) hChars = self.newRoot("Characters", nwItemClass.CHARACTER) hWorld = self.newRoot("Plot", nwItemClass.PLOT) hWorld = self.newRoot("World", nwItemClass.WORLD) hChapt = self.newFolder("New Chapter", nwItemClass.NOVEL, hNovel) hScene = self.newFile("New Scene", nwItemClass.NOVEL, hChapt) - + self.projOpened = time() + self.setProjectChanged(True) return True def clearProject(self): diff --git a/sample/sampleNovel/data_6/36b6aa9b697b_main.nwd b/sample/sampleNovel/data_6/36b6aa9b697b_main.nwd index 52aacd59..99650f20 100644 --- a/sample/sampleNovel/data_6/36b6aa9b697b_main.nwd +++ b/sample/sampleNovel/data_6/36b6aa9b697b_main.nwd @@ -13,7 +13,7 @@ Some text here would look good as well, and maybe some "dialogue"? So, this is some __text__ that we’ve been adding to this document. It is utterly meaningless text, _but_ since this is just dummy text, that doesn’t really matter. The text is perfectly happy to live in this document regardless. -This paragraph is also meaningless. At least a bit. It’s also very short. But we could make it less meaningless if we wanted to … +This paragraph is also meaningless. At least a bit. It’s also very short. But we could make it less meaningless if we wanted to … but we won’t. This one is a bit longer. “It also has some dialogue in it” she said, before she moved on to check if the spellchecker worked. It did. “Cool,” she concluded. diff --git a/sample/sampleNovel/meta/sessionInfo.log b/sample/sampleNovel/meta/sessionInfo.log index c4442696..035ac8c3 100644 --- a/sample/sampleNovel/meta/sessionInfo.log +++ b/sample/sampleNovel/meta/sessionInfo.log @@ -1,3 +1,10 @@ Start: 2019-05-26 14:28:28 End: 2019-05-26 14:28:38 Words: -23 Start: 2019-05-26 14:31:12 End: 2019-05-26 14:31:34 Words: 12 Start: 2019-05-26 14:33:27 End: 2019-05-26 14:33:31 Words: 0 +Start: 2019-05-26 14:40:29 End: 2019-05-26 14:40:32 Words: 0 +Start: 2019-05-26 14:40:43 End: 2019-05-26 14:40:46 Words: 0 +Start: 2019-05-26 14:48:40 End: 2019-05-26 14:48:50 Words: 0 +Start: 2019-05-26 14:50:18 End: 2019-05-26 14:50:21 Words: 0 +Start: 2019-05-26 14:50:29 End: 2019-05-26 14:50:47 Words: 3 +Start: 2019-05-26 15:10:52 End: 2019-05-26 15:11:09 Words: 0 +Start: 2019-05-26 15:28:56 End: 2019-05-26 15:29:08 Words: 0 diff --git a/sample/sampleNovel/nwProject.nwx b/sample/sampleNovel/nwProject.nwx index a4d878b0..5b583ada 100644 --- a/sample/sampleNovel/nwProject.nwx +++ b/sample/sampleNovel/nwProject.nwx @@ -1,5 +1,5 @@ - + Sample Project Sample Project @@ -8,9 +8,9 @@ True - 636b6aa9b697b + None None - 535 + 538 New Notes @@ -73,10 +73,10 @@ Notes False SCENE - 633 - 116 + 647 + 119 5 - 551 + 565 New File