From c90929750441c21ef598d3f53ad3f299ce97d3da Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sun, 11 May 2025 13:32:32 +0200 Subject: [PATCH] Remember last used author name --- novelwriter/config.py | 35 ++++++++++++++------- novelwriter/core/coretools.py | 32 ++++++++++--------- novelwriter/tools/welcome.py | 1 + tests/reference/baseConfig_novelwriter.conf | 3 +- tests/test_base/test_base_config.py | 5 +++ 5 files changed, 49 insertions(+), 27 deletions(-) diff --git a/novelwriter/config.py b/novelwriter/config.py index d820c66a..1d2e47d8 100644 --- a/novelwriter/config.py +++ b/novelwriter/config.py @@ -42,7 +42,7 @@ from PyQt6.QtWidgets import QApplication from novelwriter.common import ( NWConfigParser, checkInt, checkPath, describeFont, fontMatcher, - formatTimeStamp, processDialogSymbols + formatTimeStamp, processDialogSymbols, simplified ) from novelwriter.constants import nwFiles, nwHtmlUnicode, nwQuotes, nwUnicode from novelwriter.error import formatException, logException @@ -65,16 +65,16 @@ class Config: __slots__ = ( "_appPath", "_appRoot", "_backPath", "_backupPath", "_confPath", "_dLocale", "_dShortDate", - "_dShortDateTime", "_dataPath", "_errData", "_hasError", "_homePath", "_manuals", - "_nwLangPath", "_qLocale", "_qtLangPath", "_qtTrans", "_recentPaths", "_recentProjects", - "_splash", "allowOpenDial", "altDialogClose", "altDialogOpen", "appHandle", "appName", - "askBeforeBackup", "askBeforeExit", "autoSaveDoc", "autoSaveProj", "autoScroll", - "autoScrollPos", "autoSelect", "backupOnClose", "cursorWidth", "dialogLine", "dialogStyle", - "doJustify", "doReplace", "doReplaceDQuote", "doReplaceDash", "doReplaceDots", - "doReplaceSQuote", "emphLabels", "fmtApostrophe", "fmtDQuoteClose", "fmtDQuoteOpen", - "fmtPadAfter", "fmtPadBefore", "fmtPadThin", "fmtSQuoteClose", "fmtSQuoteOpen", - "focusWidth", "guiFont", "guiLocale", "guiSyntax", "guiTheme", "hasEnchant", - "hideFocusFooter", "hideHScroll", "hideVScroll", "highlightEmph", "hostName", + "_dShortDateTime", "_dataPath", "_errData", "_hasError", "_homePath", "_lastAuthor", + "_manuals", "_nwLangPath", "_qLocale", "_qtLangPath", "_qtTrans", "_recentPaths", + "_recentProjects", "_splash", "allowOpenDial", "altDialogClose", "altDialogOpen", + "appHandle", "appName", "askBeforeBackup", "askBeforeExit", "autoSaveDoc", "autoSaveProj", + "autoScroll", "autoScrollPos", "autoSelect", "backupOnClose", "cursorWidth", "dialogLine", + "dialogStyle", "doJustify", "doReplace", "doReplaceDQuote", "doReplaceDash", + "doReplaceDots", "doReplaceSQuote", "emphLabels", "fmtApostrophe", "fmtDQuoteClose", + "fmtDQuoteOpen", "fmtPadAfter", "fmtPadBefore", "fmtPadThin", "fmtSQuoteClose", + "fmtSQuoteOpen", "focusWidth", "guiFont", "guiLocale", "guiSyntax", "guiTheme", + "hasEnchant", "hideFocusFooter", "hideHScroll", "hideVScroll", "highlightEmph", "hostName", "iconColDocs", "iconColTree", "iconTheme", "incNotesWCount", "isDebug", "kernelVer", "lastNotes", "mainPanePos", "mainWinSize", "memInfo", "narratorBreak", "narratorDialog", "nativeFont", "osDarwin", "osLinux", "osType", "osUnknown", "osWindows", "outlinePanePos", @@ -149,6 +149,7 @@ class Config: self._recentProjects = RecentProjects(self) self._recentPaths = RecentPaths(self) + self._lastAuthor = "" # General GUI Settings self.guiLocale = self._qLocale.name() @@ -321,6 +322,11 @@ class Config: def recentProjects(self) -> RecentProjects: return self._recentProjects + @property + def lastAuthor(self) -> str: + """Return the last author name used.""" + return simplified(self._lastAuthor) + ## # Getters ## @@ -336,6 +342,11 @@ class Config: # Setters ## + def setLastAuthor(self, value: str) -> None: + """Set tle last used author name.""" + self._lastAuthor = simplified(value) + return + def setMainWinSize(self, width: int, height: int) -> None: """Set the size of the main window, but only if the change is larger than 5 pixels. The OS window manager will sometimes @@ -645,6 +656,7 @@ class Config: self.backupOnClose = conf.rdBool(sec, "backuponclose", self.backupOnClose) self.askBeforeBackup = conf.rdBool(sec, "askbeforebackup", self.askBeforeBackup) self.askBeforeExit = conf.rdBool(sec, "askbeforeexit", self.askBeforeExit) + self._lastAuthor = conf.rdStr(sec, "lastauthor", self._lastAuthor) # Editor sec = "Editor" @@ -769,6 +781,7 @@ class Config: "backuponclose": str(self.backupOnClose), "askbeforebackup": str(self.askBeforeBackup), "askbeforeexit": str(self.askBeforeExit), + "lastauthor": str(self._lastAuthor), } conf["Editor"] = { diff --git a/novelwriter/core/coretools.py b/novelwriter/core/coretools.py index 8c7b7195..4c8d6004 100644 --- a/novelwriter/core/coretools.py +++ b/novelwriter/core/coretools.py @@ -383,6 +383,8 @@ class ProjectBuilder: """Build or copy a project from a data dictionary.""" if isinstance(data, dict): path = data.get("path", None) or None + if author := data.get("author"): + CONFIG.setLastAuthor(author) if isinstance(path, str | Path): self._path = Path(path).resolve() if data.get("sample"): @@ -416,21 +418,21 @@ class ProjectBuilder: self._path = project.storage.storagePath - lblName = self.tr("New Project") - lblAuthor = self.tr("Author") - lblTitlePage = self.tr("Title Page") + trName = self.tr("New Project") + trAuthor = self.tr("Author Name") + trTitlePage = self.tr("Title Page") # Settings project.data.setUuid(None) - project.data.setName(data.get("name", lblName)) - project.data.setAuthor(data.get("author", lblAuthor)) + project.data.setName(data.get("name", trName)) + project.data.setAuthor(data.get("author", trAuthor)) project.data.setLanguage(CONFIG.guiLocale) project.setDefaultStatusImport() project.session.startSession() # Add Root Folders hNovelRoot = project.newRoot(nwItemClass.NOVEL) - hTitlePage = project.newFile(lblTitlePage, hNovelRoot) + hTitlePage = project.newFile(trTitlePage, hNovelRoot) # Generate Title Page aDoc = project.storage.getDocument(hTitlePage) @@ -447,9 +449,9 @@ class ProjectBuilder: "\n" ">> {count}: [field:{field}] <<\n" ).format( - author=project.data.author or lblAuthor, + author=project.data.author or trAuthor, address=self.tr("Address Line"), - title=project.data.name or lblName, + title=project.data.name or trName, by=self.tr("By"), count=self.tr("Word Count"), field=nwStats.WORDS_TEXT, @@ -459,9 +461,9 @@ class ProjectBuilder: numChapters = data.get("chapters", 0) numScenes = data.get("scenes", 0) - chSynop = self.tr("Summary of the chapter.") - scSynop = self.tr("Summary of the scene.") - bfNote = self.tr("A short description.") + trChSynop = self.tr("Summary of the chapter.") + trScSynop = self.tr("Summary of the scene.") + trNoteDesc = self.tr("A short description.") # Create chapters if numChapters > 0: @@ -469,7 +471,7 @@ class ProjectBuilder: chTitle = self.tr("Chapter {0}").format(f"{ch+1:d}") cHandle = project.newFile(chTitle, hNovelRoot) aDoc = project.storage.getDocument(cHandle) - aDoc.writeDocument(f"## {chTitle}\n\n%Synopsis: {chSynop}\n\n") + aDoc.writeDocument(f"## {chTitle}\n\n%Synopsis: {trChSynop}\n\n") # Create chapter scenes if numScenes > 0 and cHandle: @@ -477,7 +479,7 @@ class ProjectBuilder: scTitle = self.tr("Scene {0}").format(f"{ch+1:d}.{sc+1:d}") sHandle = project.newFile(scTitle, cHandle) aDoc = project.storage.getDocument(sHandle) - aDoc.writeDocument(f"### {scTitle}\n\n%Synopsis: {scSynop}\n\n") + aDoc.writeDocument(f"### {scTitle}\n\n%Synopsis: {trScSynop}\n\n") # Create scenes (no chapters) elif numScenes > 0: @@ -485,7 +487,7 @@ class ProjectBuilder: scTitle = self.tr("Scene {0}").format(f"{sc+1:d}") sHandle = project.newFile(scTitle, hNovelRoot) aDoc = project.storage.getDocument(sHandle) - aDoc.writeDocument(f"### {scTitle}\n\n%Synopsis: {scSynop}\n\n") + aDoc.writeDocument(f"### {scTitle}\n\n%Synopsis: {trScSynop}\n\n") # Create notes folders noteTitles = { @@ -505,7 +507,7 @@ class ProjectBuilder: aDoc.writeDocument( f"# {noteTitles[newRoot]}\n\n" f"@tag: {ntTag}\n\n" - f"%Short: {bfNote}\n\n" + f"%Short: {trNoteDesc}\n\n" ) # Also add the archive and trash folders diff --git a/novelwriter/tools/welcome.py b/novelwriter/tools/welcome.py index 3638c18d..9b0abe33 100644 --- a/novelwriter/tools/welcome.py +++ b/novelwriter/tools/welcome.py @@ -565,6 +565,7 @@ class _NewProjectForm(QWidget): self.projAuthor = QLineEdit(self) self.projAuthor.setMaxLength(200) self.projAuthor.setPlaceholderText(self.tr("Optional")) + self.projAuthor.setText(CONFIG.lastAuthor) # Project Path self.projPath = QLineEdit(self) diff --git a/tests/reference/baseConfig_novelwriter.conf b/tests/reference/baseConfig_novelwriter.conf index fb701405..78441636 100644 --- a/tests/reference/baseConfig_novelwriter.conf +++ b/tests/reference/baseConfig_novelwriter.conf @@ -1,5 +1,5 @@ [Meta] -timestamp = 2025-02-22 19:57:47 +timestamp = 2025-05-11 13:28:11 [Main] font = @@ -31,6 +31,7 @@ backuppath = backuponclose = False askbeforebackup = True askbeforeexit = True +lastauthor = [Editor] textfont = diff --git a/tests/test_base/test_base_config.py b/tests/test_base/test_base_config.py index ee43b86a..81b03831 100644 --- a/tests/test_base/test_base_config.py +++ b/tests/test_base/test_base_config.py @@ -242,6 +242,11 @@ def testBaseConfig_Methods(fncPath): # Recent Projects assert isinstance(tstConf.recentProjects, RecentProjects) + # Last Author + assert CONFIG.lastAuthor == "" + CONFIG.setLastAuthor(" Jane Doe ") + assert CONFIG.lastAuthor == "Jane Doe" + @pytest.mark.base def testBaseConfig_SettersGetters(fncPath):