From 7d11917d1ca42e8fa4d4fc07b2b0eab679cfd9fb Mon Sep 17 00:00:00 2001 From: Teymour Aldridge <42674621+teymour-aldridge@users.noreply.github.com> Date: Wed, 9 Jun 2021 18:42:16 +0100 Subject: [PATCH 1/7] Remove redundant assignment. --- nw/core/index.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nw/core/index.py b/nw/core/index.py index d22f3000..fdf342af 100644 --- a/nw/core/index.py +++ b/nw/core/index.py @@ -297,7 +297,6 @@ class NWIndex(): nTitle = 0 theLines = theText.splitlines() for aLine in theLines: - aLine = aLine nLine += 1 nChar = len(aLine.strip()) if nChar == 0: From 6e1e40f13e2d8040e5da6e63a4af3188162ee77d Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 10 Jun 2021 21:42:40 +0200 Subject: [PATCH 2/7] Fix translations and refactor makeAlert calls --- nw/core/project.py | 200 +++++++++++++++++-------------------- nw/dialogs/docmerge.py | 30 +++--- nw/dialogs/docsplit.py | 48 +++++---- nw/dialogs/preferences.py | 7 +- nw/dialogs/projsettings.py | 6 +- nw/dialogs/wordlist.py | 11 +- nw/gui/doceditor.py | 60 +++++------ nw/gui/docviewer.py | 15 ++- nw/gui/noveltree.py | 3 - nw/gui/projtree.py | 64 ++++++------ nw/gui/theme.py | 18 ++-- nw/guimain.py | 37 +++---- nw/tools/build.py | 25 ++--- nw/tools/writingstats.py | 24 ++--- tests/mock.py | 1 + 15 files changed, 252 insertions(+), 297 deletions(-) diff --git a/nw/core/project.py b/nw/core/project.py index c3dba7e6..cae1e60f 100644 --- a/nw/core/project.py +++ b/nw/core/project.py @@ -120,7 +120,7 @@ class NWProject(): CUSTOM, and always have parent handle set to None. """ if not self.projTree.checkRootUnique(rootClass): - self.makeAlert("Duplicate root item detected.", nwAlert.ERROR) + self.makeAlert(self.tr("Duplicate root item detected."), nwAlert.ERROR) return None newItem = NWItem(self) newItem.setName(rootName) @@ -362,7 +362,9 @@ class NWProject(): if not os.path.isfile(fileName): fileName = os.path.join(fileName, nwFiles.PROJ_FILE) if not os.path.isfile(fileName): - self.makeAlert(self.tr("File not found: {0}").format(fileName), nwAlert.ERROR) + self.makeAlert(self.tr( + "File not found: {0}" + ).format(fileName), nwAlert.ERROR) return False self.clearProject() @@ -411,20 +413,22 @@ class NWProject(): try: nwXML = etree.parse(fileName) except Exception as e: - self.makeAlert([self.tr("Failed to parse project xml."), str(e)], nwAlert.ERROR) + self.makeAlert([ + self.tr("Failed to parse project xml."), str(e) + ], nwAlert.ERROR) # Trying to open backup file instead backFile = fileName[:-3]+"bak" if os.path.isfile(backFile): - self.makeAlert( - self.tr("Attempting to open backup project file instead."), nwAlert.INFO - ) + self.makeAlert(self.tr( + "Attempting to open backup project file instead." + ), nwAlert.INFO) try: nwXML = etree.parse(backFile) except Exception as e: - self.makeAlert( - [self.tr("Failed to parse project xml."), str(e)], nwAlert.ERROR - ) + self.makeAlert([ + self.tr("Failed to parse project xml."), str(e) + ], nwAlert.ERROR) self.clearProject() return False else: @@ -445,10 +449,9 @@ class NWProject(): # =============== if not nwxRoot == "novelWriterXML": - self.makeAlert( - self.tr("Project file does not appear to be a novelWriterXML file."), - nwAlert.ERROR - ) + self.makeAlert(self.tr( + "Project file does not appear to be a novelWriterXML file." + ), nwAlert.ERROR) self.clearProject() return False @@ -465,13 +468,11 @@ class NWProject(): # read the file. Introduced in version 0.10. if fileVersion not in ("1.0", "1.1", "1.2"): - self.makeAlert(( - self.tr( - "Unknown or unsupported novelWriter project file format. " - "The project cannot be opened by this version of novelWriter. " - "The file was saved with novelWriter version {0}." - ).format(appVersion) - ), nwAlert.ERROR) + self.makeAlert(self.tr( + "Unknown or unsupported novelWriter project file format. " + "The project cannot be opened by this version of novelWriter. " + "The file was saved with novelWriter version {0}." + ).format(appVersion), nwAlert.ERROR) self.clearProject() return False @@ -600,9 +601,9 @@ class NWProject(): file. """ if self.projPath is None: - self.makeAlert( - self.tr("Project path not set, cannot save project."), nwAlert.ERROR - ) + self.makeAlert(self.tr( + "Project path not set, cannot save project." + ), nwAlert.ERROR) return False saveTime = time() @@ -681,7 +682,9 @@ class NWProject(): xml_declaration = True )) except Exception as e: - self.makeAlert([self.tr("Failed to save project."), str(e)], nwAlert.ERROR) + self.makeAlert([ + self.tr("Failed to save project."), str(e) + ], nwAlert.ERROR) return False # If we're here, the file was successfully saved, @@ -755,30 +758,24 @@ class NWProject(): self.theParent.setStatus(self.tr("Backing up project ...")) if self.mainConf.backupPath is None or self.mainConf.backupPath == "": - self.theParent.makeAlert( - self.tr( - "Cannot backup project because no backup path is set. " - "Please set a valid backup location in Tools > Preferences." - ), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "Cannot backup project because no backup path is set. " + "Please set a valid backup location in Tools > Preferences." + ), nwAlert.ERROR) return False if self.projName is None or self.projName == "": - self.theParent.makeAlert( - self.tr( - "Cannot backup project because no project name is set. " - "Please set a Working Title in Project > Project Settings." - ), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "Cannot backup project because no project name is set. " + "Please set a Working Title in Project > Project Settings." + ), nwAlert.ERROR) return False if not os.path.isdir(self.mainConf.backupPath): - self.theParent.makeAlert( - self.tr( - "Cannot backup project because the backup path does not exist. " - "Please set a valid backup location in Tools > Preferences." - ), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "Cannot backup project because the backup path does not exist. " + "Please set a valid backup location in Tools > Preferences." + ), nwAlert.ERROR) return False cleanName = makeFileNameSafe(self.projName) @@ -788,20 +785,17 @@ class NWProject(): os.mkdir(baseDir) logger.debug("Created folder %s" % baseDir) except Exception as e: - self.theParent.makeAlert( - [self.tr("Could not create backup folder."), str(e)], - nwAlert.ERROR - ) + self.theParent.makeAlert([ + self.tr("Could not create backup folder."), str(e) + ], nwAlert.ERROR) return False if os.path.commonpath([self.projPath, baseDir]) == self.projPath: - self.theParent.makeAlert( - self.tr( - "Cannot backup project because the backup path is within the " - "project folder to be backed up. Please choose a different " - "backup path in Tools > Preferences." - ), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "Cannot backup project because the backup path is within the " + "project folder to be backed up. Please choose a different " + "backup path in Tools > Preferences." + ), nwAlert.ERROR) return False archName = self.tr("Backup from {0}").format(formatTimeStamp(time(), fileSafe=True)) @@ -813,22 +807,19 @@ class NWProject(): self._writeLockFile() logger.info("Backup written to: %s" % archName) if doNotify: - self.theParent.makeAlert( - self.tr( - "Backup archive file written to: {0}" - ).format( - f"{os.path.join(cleanName, archName)}.zip" - ), nwAlert.INFO - ) + self.theParent.makeAlert(self.tr( + "Backup archive file written to: {0}" + ).format(f"{os.path.join(cleanName, archName)}.zip"), nwAlert.INFO) except Exception as e: - self.theParent.makeAlert( - [self.tr("Could not write backup archive."), str(e)], - nwAlert.ERROR - ) + self.theParent.makeAlert([ + self.tr("Could not write backup archive."), str(e) + ], nwAlert.ERROR) return False - self.theParent.setStatus(self.tr("Project backed up to '{0}'").format(f"{baseName}.zip")) + self.theParent.setStatus(self.tr( + "Project backed up to '{0}'" + ).format(f"{baseName}.zip")) return True @@ -854,9 +845,9 @@ class NWProject(): shutil.unpack_archive(pkgSample, projPath) isSuccess = True except Exception as e: - self.makeAlert( - [self.tr("Failed to create a new example project."), str(e)], nwAlert.ERROR - ) + self.makeAlert([ + self.tr("Failed to create a new example project."), str(e) + ], nwAlert.ERROR) elif os.path.isdir(srcSample): @@ -876,17 +867,15 @@ class NWProject(): isSuccess = True except Exception as e: - self.makeAlert( - [self.tr("Failed to create a new example project."), str(e)], nwAlert.ERROR - ) + self.makeAlert([ + self.tr("Failed to create a new example project."), str(e) + ], nwAlert.ERROR) else: - self.makeAlert( - self.tr( - "Failed to create a new example project. Could not find the " - "necessary files. They seem to be missing from this installation." - ), nwAlert.ERROR - ) + self.makeAlert(self.tr( + "Failed to create a new example project. Could not find the " + "necessary files. They seem to be missing from this installation." + ), nwAlert.ERROR) if isSuccess: self.clearProject() @@ -916,19 +905,17 @@ class NWProject(): os.mkdir(projPath) logger.debug("Created folder %s" % projPath) except Exception as e: - self.theParent.makeAlert(( - [self.tr("Could not create new project folder."), str(e)] - ), nwAlert.ERROR) + self.theParent.makeAlert([ + self.tr("Could not create new project folder."), str(e) + ], nwAlert.ERROR) return False if os.path.isdir(projPath): if os.listdir(self.projPath): - self.theParent.makeAlert( - self.tr( - "New project folder is not empty. " - "Each project requires a dedicated project folder." - ), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "New project folder is not empty. " + "Each project requires a dedicated project folder." + ), nwAlert.ERROR) return False self.ensureFolderStructure() @@ -975,21 +962,17 @@ class NWProject(): self.doBackup = doBackup if doBackup: if not os.path.isdir(self.mainConf.backupPath): - self.theParent.makeAlert( - self.tr( - "You must set a valid backup path in Preferences to use " - "the automatic project backup feature." - ), nwAlert.WARN - ) + self.theParent.makeAlert(self.tr( + "You must set a valid backup path in Preferences to use " + "the automatic project backup feature." + ), nwAlert.WARN) return False if self.projName == "": - self.theParent.makeAlert( - self.tr( - "You must set a valid project name in Project Settings to " - "use the automatic project backup feature." - ), nwAlert.WARN - ) + self.theParent.makeAlert(self.tr( + "You must set a valid project name in Project Settings to " + "use the automatic project backup feature." + ), nwAlert.WARN) return False return True @@ -1317,7 +1300,9 @@ class NWProject(): os.mkdir(thePath) logger.debug("Created folder %s" % thePath) except Exception as e: - self.makeAlert(["Could not create folder.", str(e)], nwAlert.ERROR) + self.makeAlert([ + self.tr("Could not create folder."), str(e) + ], nwAlert.ERROR) return False return True @@ -1374,10 +1359,9 @@ class NWProject(): # Report status if len(orphanFiles) > 0: - self.makeAlert( - self.tr("Found {0} orphaned file(s) in project folder.").format(len(orphanFiles)), - nwAlert.WARN - ) + self.makeAlert(self.tr( + "Found {0} orphaned file(s) in project folder." + ).format(len(orphanFiles)), nwAlert.WARN) else: logger.debug("File check OK") return @@ -1431,12 +1415,10 @@ class NWProject(): self.projTree.append(oHandle, oParent, orphItem) if noWhere: - self.makeAlert( - self.tr( - "One or more orphaned files could not be added back into the " - "project. Make sure at least a Novel root folder exists." - ), nwAlert.WARN - ) + self.makeAlert(self.tr( + "One or more orphaned files could not be added back into the " + "project. Make sure at least a Novel root folder exists." + ), nwAlert.WARN) return True diff --git a/nw/dialogs/docmerge.py b/nw/dialogs/docmerge.py index fe5b5511..89b8233f 100644 --- a/nw/dialogs/docmerge.py +++ b/nw/dialogs/docmerge.py @@ -102,9 +102,9 @@ class GuiDocMerge(QDialog): finalOrder.append(self.listBox.item(i).data(Qt.UserRole)) if len(finalOrder) == 0: - self.theParent.makeAlert( - self.tr("No source documents found. Nothing to do."), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "No source documents found. Nothing to do." + ), nwAlert.ERROR) return False theText = "" @@ -113,16 +113,16 @@ class GuiDocMerge(QDialog): docText = inDoc.readDocument() docErr = inDoc.getError() if docText is None and docErr: - self.theParent.makeAlert( - [self.tr("Failed to open document file."), docErr], nwAlert.ERROR - ) + self.theParent.makeAlert([ + self.tr("Failed to open document file."), docErr + ], nwAlert.ERROR) if docText: theText += docText.rstrip("\n")+"\n\n" if self.sourceItem is None: - self.theParent.makeAlert( - self.tr("No source folder selected. Nothing to do."), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "No source folder selected. Nothing to do." + ), nwAlert.ERROR) return False srcItem = self.theProject.projTree[self.sourceItem] @@ -136,9 +136,9 @@ class GuiDocMerge(QDialog): outDoc = NWDoc(self.theProject, nHandle) if not outDoc.writeDocument(theText): - self.theParent.makeAlert( - [self.tr("Could not save document."), outDoc.getError()], nwAlert.ERROR - ) + self.theParent.makeAlert([ + self.tr("Could not save document."), outDoc.getError() + ], nwAlert.ERROR) return False self.theParent.treeView.revealNewTreeItem(nHandle) @@ -174,9 +174,9 @@ class GuiDocMerge(QDialog): return False if nwItem.itemType is not nwItemType.FOLDER: - self.theParent.makeAlert( - self.tr("Element selected in the project tree must be a folder."), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "Element selected in the project tree must be a folder." + ), nwAlert.ERROR) return False for sHandle in self.theParent.treeView.getTreeFromHandle(tHandle): diff --git a/nw/dialogs/docsplit.py b/nw/dialogs/docsplit.py index 39b91bcb..02dcd83a 100644 --- a/nw/dialogs/docsplit.py +++ b/nw/dialogs/docsplit.py @@ -117,16 +117,16 @@ class GuiDocSplit(QDialog): logger.verbose("GuiDocSplit split button clicked") if self.sourceItem is None: - self.theParent.makeAlert( - self.tr("No source document selected. Nothing to do."), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "No source document selected. Nothing to do." + ), nwAlert.ERROR) return False srcItem = self.theProject.projTree[self.sourceItem] if srcItem is None: - self.theParent.makeAlert( - self.tr("Could not parse source document."), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "Could not parse source document." + ), nwAlert.ERROR) return False inDoc = NWDoc(self.theProject, self.sourceItem) @@ -134,9 +134,9 @@ class GuiDocSplit(QDialog): docErr = inDoc.getError() if theText is None and docErr: - self.theParent.makeAlert( - [self.tr("Failed to open document file."), docErr], nwAlert.ERROR - ) + self.theParent.makeAlert([ + self.tr("Failed to open document file."), docErr + ], nwAlert.ERROR) if theText is None: theText = "" @@ -157,21 +157,19 @@ class GuiDocSplit(QDialog): nFiles = len(finalOrder) if nFiles == 0: - self.theParent.makeAlert( - self.tr("No headers found. Nothing to do."), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "No headers found. Nothing to do." + ), nwAlert.ERROR) return False # Check that another folder can be created parTree = self.theProject.projTree.getItemPath(srcItem.itemParent) if len(parTree) >= nwConst.MAX_DEPTH - 1: - self.theParent.makeAlert( - self.tr( - "Cannot add new folder for the document split. " - "Maximum folder depth has been reached. " - "Please move the file to another level in the project tree." - ), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "Cannot add new folder for the document split. " + "Maximum folder depth has been reached. " + "Please move the file to another level in the project tree." + ), nwAlert.ERROR) return False msgYes = self.theParent.askQuestion( @@ -227,9 +225,9 @@ class GuiDocSplit(QDialog): outDoc = NWDoc(self.theProject, nHandle) if not outDoc.writeDocument(theText): - self.theParent.makeAlert( - [self.tr("Could not save document."), outDoc.getError()], nwAlert.ERROR - ) + self.theParent.makeAlert([ + self.tr("Could not save document."), outDoc.getError() + ], nwAlert.ERROR) return False self.theParent.treeView.revealNewTreeItem(nHandle) @@ -267,9 +265,9 @@ class GuiDocSplit(QDialog): return False if nwItem.itemType is not nwItemType.FILE: - self.theParent.makeAlert( - self.tr("Element selected in the project tree must be a file."), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "Element selected in the project tree must be a file." + ), nwAlert.ERROR) return False inDoc = NWDoc(self.theProject, self.sourceItem) diff --git a/nw/dialogs/preferences.py b/nw/dialogs/preferences.py index 2680dc64..88022e81 100644 --- a/nw/dialogs/preferences.py +++ b/nw/dialogs/preferences.py @@ -104,10 +104,9 @@ class GuiPreferences(PagedDialog): self.tabQuote.saveValues() if needsRestart: - self.theParent.makeAlert( - self.tr("Some changes will not be applied until novelWriter has been restarted."), - nwAlert.INFO - ) + self.theParent.makeAlert(self.tr( + "Some changes will not be applied until novelWriter has been restarted." + ), nwAlert.INFO) self._saveWindowSize() self.accept() diff --git a/nw/dialogs/projsettings.py b/nw/dialogs/projsettings.py index 014fd32b..86bbdbe5 100644 --- a/nw/dialogs/projsettings.py +++ b/nw/dialogs/projsettings.py @@ -397,9 +397,9 @@ class GuiProjectEditStatus(QWidget): self.listBox.takeTopLevelItem(iRow) self.colChanged = True else: - self.theParent.makeAlert( - self.tr("Cannot delete a status item that is in use."), nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "Cannot delete a status item that is in use." + ), nwAlert.ERROR) return def _saveItem(self): diff --git a/nw/dialogs/wordlist.py b/nw/dialogs/wordlist.py index 6870c271..71442593 100644 --- a/nw/dialogs/wordlist.py +++ b/nw/dialogs/wordlist.py @@ -122,14 +122,15 @@ class GuiWordList(QDialog): """ newWord = self.newEntry.text().strip() if newWord == "": - self.theParent.makeAlert(self.tr("Cannot add a blank word."), nwAlert.ERROR) + self.theParent.makeAlert(self.tr( + "Cannot add a blank word." + ), nwAlert.ERROR) return False if self.listBox.findItems(newWord, Qt.MatchExactly): - self.theParent.makeAlert( - self.tr("The word '{0}' is already in the word list.").format(newWord), - nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "The word '{0}' is already in the word list." + ).format(newWord), nwAlert.ERROR) return False self.listBox.addItem(newWord) diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index a6cc404e..9209ab4c 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -315,17 +315,14 @@ class GuiDocEditor(QTextEdit): docSize = len(theDoc) if docSize > nwConst.MAX_DOCSIZE: - self.theParent.makeAlert( - self.tr( - "The document you are trying to open is too big. " - "The document size is {0} MB. " - "The maximum size allowed is {1} MB." - ).format( - f"{docSize/1.0e6:.2f}", - f"{nwConst.MAX_DOCSIZE/1.0e6:.2f}" - ), - nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "The document you are trying to open is too big. " + "The document size is {0} MB. " + "The maximum size allowed is {1} MB." + ).format( + f"{docSize/1.0e6:.2f}", + f"{nwConst.MAX_DOCSIZE/1.0e6:.2f}" + ), nwAlert.ERROR) self.clearEditor() return False @@ -413,17 +410,14 @@ class GuiDocEditor(QTextEdit): """ docSize = len(theText) if docSize > nwConst.MAX_DOCSIZE: - self.theParent.makeAlert( - self.tr( - "The text you are trying to add is too big. " - "The text size is {0} MB. " - "The maximum size allowed is {1} MB." - ).format( - f"{docSize/1.0e6:.2f}", - f"{nwConst.MAX_DOCSIZE/1.0e6:.2f}" - ), - nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "The text you are trying to add is too big. " + "The text size is {0} MB. " + "The maximum size allowed is {1} MB." + ).format( + f"{docSize/1.0e6:.2f}", + f"{nwConst.MAX_DOCSIZE/1.0e6:.2f}" + ), nwAlert.ERROR) return False qApp.setOverrideCursor(QCursor(Qt.WaitCursor)) @@ -1009,15 +1003,12 @@ class GuiDocEditor(QTextEdit): self._lastFind = None if self._qDocument.characterCount() > nwConst.MAX_DOCSIZE: - self.theParent.makeAlert( - self.tr( - "The document has grown too big and you cannot add more text to it. " - "The maximum size of a single novelWriter document is {0} MB." - ).format( - f"{nwConst.MAX_DOCSIZE/1.0e6:.2f}" - ), - nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "The document has grown too big and you cannot add more text to it. " + "The maximum size of a single novelWriter document is {0} MB." + ).format( + f"{nwConst.MAX_DOCSIZE/1.0e6:.2f}" + ), nwAlert.ERROR) self.undo() return @@ -1552,10 +1543,9 @@ class GuiDocEditor(QTextEdit): self._allowAutoReplace(True) else: - self.theParent.makeAlert( - self.tr("Please select some text before calling replace quotes."), - nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "Please select some text before calling replace quotes." + ), nwAlert.ERROR) return diff --git a/nw/gui/docviewer.py b/nw/gui/docviewer.py index 52e44b5d..00cb9ddb 100644 --- a/nw/gui/docviewer.py +++ b/nw/gui/docviewer.py @@ -246,14 +246,13 @@ class GuiDocViewer(QTextBrowser): logger.debug("Loading document from tag '%s'" % theTag) tHandle, _, sTitle = self.theParent.theIndex.getTagSource(theTag) if tHandle is None: - self.theParent.makeAlert( - self.tr( - "Could not find the reference for tag '{0}'. It either doesn't " - "exist, or the index is out of date. The index can be updated " - "from the Tools menu, or by pressing {1}." - ).format(theTag, "F9"), - nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "Could not find the reference for tag '{0}'. It either doesn't " + "exist, or the index is out of date. The index can be updated " + "from the Tools menu, or by pressing {1}." + ).format( + theTag, "F9" + ), nwAlert.ERROR) return False else: # Let the parent handle the opening as it also ensures that diff --git a/nw/gui/noveltree.py b/nw/gui/noveltree.py index c559f414..fa65de23 100644 --- a/nw/gui/noveltree.py +++ b/nw/gui/noveltree.py @@ -99,9 +99,6 @@ class GuiNovelTree(QTreeWidget): logger.debug("GuiNovelTree initialisation complete") - # Internal Mapping - self.makeAlert = self.theParent.makeAlert - return def initTree(self): diff --git a/nw/gui/projtree.py b/nw/gui/projtree.py index c9b82cbe..a2d9a1e1 100644 --- a/nw/gui/projtree.py +++ b/nw/gui/projtree.py @@ -197,15 +197,13 @@ class GuiProjectTree(QTreeWidget): # If class is still not set, alert the user and exit if itemClass is None: if itemType == nwItemType.FILE: - self.makeAlert( - self.tr("Please select a valid location in the tree to add the document."), - nwAlert.ERROR - ) + self.makeAlert(self.tr( + "Please select a valid location in the tree to add the document." + ), nwAlert.ERROR) else: - self.makeAlert( - self.tr("Please select a valid location in the tree to add the folder."), - nwAlert.ERROR - ) + self.makeAlert(self.tr( + "Please select a valid location in the tree to add the folder." + ), nwAlert.ERROR) return False # Everything is fine, we have what we need, so we proceed @@ -227,9 +225,9 @@ class GuiProjectTree(QTreeWidget): # If still nothing, give up if pHandle is None: - self.makeAlert( - self.tr("Did not find anywhere to add the file or folder!"), nwAlert.ERROR - ) + self.makeAlert(self.tr( + "Did not find anywhere to add the file or folder!" + ), nwAlert.ERROR) return False # Now check if the selected item is a file, in which case @@ -241,15 +239,15 @@ class GuiProjectTree(QTreeWidget): # If we again have no home, give up if pHandle is None: - self.makeAlert( - self.tr("Did not find anywhere to add the file or folder!"), nwAlert.ERROR - ) + self.makeAlert(self.tr( + "Did not find anywhere to add the file or folder!" + ), nwAlert.ERROR) return False if self.theProject.projTree.isTrashRoot(pHandle): - self.makeAlert( - self.tr("Cannot add new files or folders to the Trash folder."), nwAlert.ERROR - ) + self.makeAlert(self.tr( + "Cannot add new files or folders to the Trash folder." + ), nwAlert.ERROR) return False parTree = self.theProject.projTree.getItemPath(pHandle) @@ -262,9 +260,9 @@ class GuiProjectTree(QTreeWidget): if len(parTree) >= nwConst.MAX_DEPTH - 1: # Folders cannot be deeper than MAX_DEPTH - 1, leaving room # for one more level of files. - self.makeAlert(( - self.tr("Cannot add new folder to this item."), - self.tr("Maximum folder depth has been reached.") + self.makeAlert(self.tr( + "Cannot add new folder to this item. " + "Maximum folder depth has been reached." ), nwAlert.ERROR) return False tHandle = self.theProject.newFolder(self.tr("New Folder"), itemClass, pHandle) @@ -433,9 +431,9 @@ class GuiProjectTree(QTreeWidget): logger.debug("Emptying Trash folder") if trashHandle is None: - self.makeAlert( - self.tr("There is currently no Trash folder in this project."), nwAlert.INFO - ) + self.makeAlert(self.tr( + "There is currently no Trash folder in this project." + ), nwAlert.INFO) return False theTrash = self.getTreeFromHandle(trashHandle) @@ -444,7 +442,9 @@ class GuiProjectTree(QTreeWidget): nTrash = len(theTrash) if nTrash == 0: - self.makeAlert(self.tr("The Trash folder is already empty."), nwAlert.INFO) + self.makeAlert(self.tr( + "The Trash folder is already empty." + ), nwAlert.INFO) return False msgYes = self.askQuestion( @@ -857,7 +857,9 @@ class GuiProjectTree(QTreeWidget): snItem = self.theProject.projTree[sHandle] dnItem = self.theProject.projTree[dHandle] if dnItem is None: - self.makeAlert(self.tr("The item cannot be moved to that location."), nwAlert.ERROR) + self.makeAlert(self.tr( + "The item cannot be moved to that location." + ), nwAlert.ERROR) return pItem = sItem.parent() @@ -888,7 +890,9 @@ class GuiProjectTree(QTreeWidget): else: theEvent.ignore() logger.debug("Drag'n'drop of item %s not accepted" % sHandle) - self.makeAlert(self.tr("The item cannot be moved to that location."), nwAlert.ERROR) + self.makeAlert(self.tr( + "The item cannot be moved to that location." + ), nwAlert.ERROR) return @@ -985,11 +989,9 @@ class GuiProjectTree(QTreeWidget): elif nwItem.itemType == nwItemType.TRASH: self.addTopLevelItem(newItem) else: - self.makeAlert( - self.tr( - "There is nowhere to add item with name '{0}'." - ).format(nwItem.itemName), nwAlert.ERROR - ) + self.makeAlert(self.tr( + "There is nowhere to add item with name '{0}'." + ).format(nwItem.itemName), nwAlert.ERROR) del self._treeMap[tHandle] return None diff --git a/nw/gui/theme.py b/nw/gui/theme.py index 32d5f0dc..8f43141d 100644 --- a/nw/gui/theme.py +++ b/nw/gui/theme.py @@ -397,9 +397,9 @@ class GuiTheme: with open(themeConf, mode="r", encoding="utf8") as inFile: confParser.read_file(inFile) except Exception as e: - self.makeAlert( - [self.tr("Could not load theme config file."), str(e)], nwAlert.ERROR - ) + self.makeAlert([ + self.tr("Could not load theme config file."), str(e) + ], nwAlert.ERROR) continue themeName = "" if confParser.has_section("Main"): @@ -430,9 +430,9 @@ class GuiTheme: with open(syntaxPath, mode="r", encoding="utf8") as inFile: confParser.read_file(inFile) except Exception as e: - self.makeAlert( - [self.tr("Could not load syntax file."), str(e)], nwAlert.ERROR - ) + self.makeAlert([ + self.tr("Could not load syntax file."), str(e) + ], nwAlert.ERROR) return [] syntaxName = "" if confParser.has_section("Main"): @@ -745,9 +745,9 @@ class GuiIcons: with open(themeConf, mode="r", encoding="utf8") as inFile: confParser.read_file(inFile) except Exception as e: - self.makeAlert( - [self.tr("Could not load theme config file."), str(e)], nwAlert.ERROR - ) + self.makeAlert([ + self.tr("Could not load theme config file."), str(e) + ], nwAlert.ERROR) continue themeName = "" if confParser.has_section("Main"): diff --git a/nw/guimain.py b/nw/guimain.py index 2db0add2..700f670e 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -350,10 +350,9 @@ class GuiMain(QMainWindow): """ if self.hasProject: if not self.closeProject(): - self.makeAlert( - self.tr("Cannot create new project when another project is open."), - nwAlert.ERROR - ) + self.makeAlert(self.tr( + "Cannot create new project when another project is open." + ), nwAlert.ERROR) return False if projData is None: @@ -368,12 +367,10 @@ class GuiMain(QMainWindow): return False if os.path.isfile(os.path.join(projPath, self.theProject.projFile)): - self.makeAlert( - self.tr( - "A project already exists in that location. " - "Please choose another folder." - ), nwAlert.ERROR - ) + self.makeAlert(self.tr( + "A project already exists in that location. " + "Please choose another folder." + ), nwAlert.ERROR) return False logger.info("Creating new project") @@ -538,10 +535,9 @@ class GuiMain(QMainWindow): # Check if we need to rebuild the index if self.theIndex.indexBroken: - self.makeAlert( - self.tr("The project index is outdated or broken. Rebuilding index."), - nwAlert.WARN - ) + self.makeAlert(self.tr( + "The project index is outdated or broken. Rebuilding index." + ), nwAlert.WARN) self.rebuildIndex() # Make sure the changed status is set to false on all that was @@ -735,10 +731,9 @@ class GuiMain(QMainWindow): return False if self.docEditor.docHandle() is None: - self.makeAlert( - self.tr("Please open a document to import the text file into."), - nwAlert.ERROR - ) + self.makeAlert(self.tr( + "Please open a document to import the text file into." + ), nwAlert.ERROR) return False if not self.docEditor.isEmpty(): @@ -912,9 +907,9 @@ class GuiMain(QMainWindow): qApp.restoreOverrideCursor() if not beQuiet: - self.makeAlert( - self.tr("The project index has been successfully rebuilt."), nwAlert.INFO - ) + self.makeAlert(self.tr( + "The project index has been successfully rebuilt." + ), nwAlert.INFO) return True diff --git a/nw/tools/build.py b/nw/tools/build.py index ac9ea364..55597605 100644 --- a/nw/tools/build.py +++ b/nw/tools/build.py @@ -747,11 +747,10 @@ class GuiBuildNovel(QDialog): tEnd = int(time()) logger.debug("Built project in %.3f ms" % (1000*(tEnd - tStart))) - if bldObj.errData: - self.theParent.makeAlert("%s:
- %s" % ( - self.tr("There were problems when building the project"), - "
- ".join(bldObj.errData)), nwAlert.ERROR - ) + if bldObj.errData and isinstance(bldObj.errData, list): + self.theParent.makeAlert([ + self.tr("There were problems when building the project:") + ] + bldObj.errData, nwAlert.ERROR) return @@ -998,17 +997,13 @@ class GuiBuildNovel(QDialog): # ============== if wSuccess: - self.theParent.makeAlert( - "%s
%s" % ( - self.tr("{0} file successfully written to:").format(textFmt), savePath - ), - nwAlert.INFO - ) + self.theParent.makeAlert([ + self.tr("{0} file successfully written to:").format(textFmt), savePath + ], nwAlert.INFO) else: - self.theParent.makeAlert( - self.tr("Failed to write {0} file. {1}").format(textFmt, errMsg), - nwAlert.ERROR - ) + self.theParent.makeAlert(self.tr( + "Failed to write {0} file. {1}" + ).format(textFmt, errMsg), nwAlert.ERROR) return wSuccess diff --git a/nw/tools/writingstats.py b/nw/tools/writingstats.py index 55dfc8ea..771870d7 100644 --- a/nw/tools/writingstats.py +++ b/nw/tools/writingstats.py @@ -412,17 +412,13 @@ class GuiWritingStats(QDialog): # Report to user if wSuccess: - self.theParent.makeAlert( - "%s file successfully written to:
%s" % ( - textFmt, savePath - ), nwAlert.INFO - ) + self.theParent.makeAlert([ + self.tr("{0} file successfully written to:").format(textFmt), savePath + ], nwAlert.INFO) else: - self.theParent.makeAlert( - "Failed to write %s file.
%s" % ( - textFmt, errMsg - ), nwAlert.ERROR - ) + self.theParent.makeAlert([ + self.tr("Failed to write {0} file.").format(textFmt), errMsg + ], nwAlert.ERROR) return wSuccess @@ -487,9 +483,9 @@ class GuiWritingStats(QDialog): self.logData.append((dStart, sDiff, wcNovel, wcNotes, sIdle)) except Exception as e: - self.theParent.makeAlert( - [self.tr("Failed to read session log file."), str(e)], nwAlert.ERROR - ) + self.theParent.makeAlert([ + self.tr("Failed to read session log file."), str(e) + ], nwAlert.ERROR) return False ttWords = ttNovel + ttNotes @@ -505,7 +501,7 @@ class GuiWritingStats(QDialog): # Slots ## - def _updateListBox(self, dummyVar=None): + def _updateListBox(self): """Load/reload the content of the list box. The dummyVar variable captures the variable sent from the widgets connecting to it and discards it. diff --git a/tests/mock.py b/tests/mock.py index 05298ad3..eb76e5a6 100644 --- a/tests/mock.py +++ b/tests/mock.py @@ -43,6 +43,7 @@ class MockGuiMain(): return def makeAlert(self, theMessage, theLevel): + assert isinstance(theMessage, str) or isinstance(theMessage, list) print("%s: %s" % (str(theLevel), theMessage)) self.lastAlert = str(theMessage) return From d511c69ee7ee2c4d76643bd5cd21f0908bfdd525 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 10 Jun 2021 21:51:52 +0200 Subject: [PATCH 3/7] Rewrap some alert messages --- nw/core/project.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/nw/core/project.py b/nw/core/project.py index cae1e60f..dcf92822 100644 --- a/nw/core/project.py +++ b/nw/core/project.py @@ -873,8 +873,9 @@ class NWProject(): else: self.makeAlert(self.tr( - "Failed to create a new example project. Could not find the " - "necessary files. They seem to be missing from this installation." + "Failed to create a new example project. " + "Could not find the necessary files. " + "They seem to be missing from this installation." ), nwAlert.ERROR) if isSuccess: @@ -1416,8 +1417,8 @@ class NWProject(): if noWhere: self.makeAlert(self.tr( - "One or more orphaned files could not be added back into the " - "project. Make sure at least a Novel root folder exists." + "One or more orphaned files could not be added back into the project. " + "Make sure at least a Novel root folder exists." ), nwAlert.WARN) return True From b0225515d21b2337faf6ecf12f485a17d4997ef6 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 10 Jun 2021 21:55:40 +0200 Subject: [PATCH 4/7] Remove isinstance check in error reporting --- nw/tools/build.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nw/tools/build.py b/nw/tools/build.py index 55597605..b64fe0bd 100644 --- a/nw/tools/build.py +++ b/nw/tools/build.py @@ -747,7 +747,7 @@ class GuiBuildNovel(QDialog): tEnd = int(time()) logger.debug("Built project in %.3f ms" % (1000*(tEnd - tStart))) - if bldObj.errData and isinstance(bldObj.errData, list): + if bldObj.errData: self.theParent.makeAlert([ self.tr("There were problems when building the project:") ] + bldObj.errData, nwAlert.ERROR) From 530d92cc3ffa9b6b03ee6373f10e7adff64d69cc Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 10 Jun 2021 22:08:26 +0200 Subject: [PATCH 5/7] Make the extractVersion function in setup a bit more robust --- setup.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index 7f222796..4bb3801f 100755 --- a/setup.py +++ b/setup.py @@ -50,12 +50,19 @@ def extractVersion(): numVers = "Unknown" hexVers = "Unknown" initFile = os.path.join("nw", "__init__.py") - with open(initFile, mode="r") as inFile: - for aLine in inFile: - if aLine.startswith("__version__"): - numVers = getValue((aLine)) - if aLine.startswith("__hexversion__"): - hexVers = getValue((aLine)) + try: + with open(initFile, mode="r", encoding="utf-8") as inFile: + for aLine in inFile: + if aLine.startswith("__version__"): + numVers = getValue((aLine)) + if aLine.startswith("__hexversion__"): + hexVers = getValue((aLine)) + except Exception as e: + print("Could not read file: %s" % initFile) + print(str(e)) + + print("novelWriter version is: %s (%s)" % (numVers, hexVers)) + print("") return numVers, hexVers From efe759b94ed7ebafe28dd6020756947866d45b12 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 10 Jun 2021 22:16:06 +0200 Subject: [PATCH 6/7] Change focus shortcuts on Window as they conflict with alt codes --- nw/gui/mainmenu.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/nw/gui/mainmenu.py b/nw/gui/mainmenu.py index 402e40a9..3619f07b 100644 --- a/nw/gui/mainmenu.py +++ b/nw/gui/mainmenu.py @@ -468,28 +468,40 @@ class GuiMainMenu(QMenuBar): # View > TreeView self.aFocusTree = QAction(self.tr("Focus Project Tree"), self) self.aFocusTree.setStatusTip(self.tr("Move focus to project tree")) - self.aFocusTree.setShortcut("Alt+1") + if self.mainConf.osWindows: + self.aFocusTree.setShortcut("Ctrl+Alt+1") + else: + self.aFocusTree.setShortcut("Alt+1") self.aFocusTree.triggered.connect(lambda: self.theParent.switchFocus(nwWidget.TREE)) self.viewMenu.addAction(self.aFocusTree) # View > Document Pane 1 self.aFocusEditor = QAction(self.tr("Focus Document Editor"), self) self.aFocusEditor.setStatusTip(self.tr("Move focus to left document pane")) - self.aFocusEditor.setShortcut("Alt+2") + if self.mainConf.osWindows: + self.aFocusEditor.setShortcut("Ctrl+Alt+2") + else: + self.aFocusEditor.setShortcut("Alt+2") self.aFocusEditor.triggered.connect(lambda: self.theParent.switchFocus(nwWidget.EDITOR)) self.viewMenu.addAction(self.aFocusEditor) # View > Document Pane 2 self.aFocusView = QAction(self.tr("Focus Document Viewer"), self) self.aFocusView.setStatusTip(self.tr("Move focus to right document pane")) - self.aFocusView.setShortcut("Alt+3") + if self.mainConf.osWindows: + self.aFocusView.setShortcut("Ctrl+Alt+3") + else: + self.aFocusView.setShortcut("Alt+3") self.aFocusView.triggered.connect(lambda: self.theParent.switchFocus(nwWidget.VIEWER)) self.viewMenu.addAction(self.aFocusView) # View > Outline self.aFocusOutline = QAction(self.tr("Focus Outline"), self) self.aFocusOutline.setStatusTip(self.tr("Move focus to outline")) - self.aFocusOutline.setShortcut("Alt+4") + if self.mainConf.osWindows: + self.aFocusOutline.setShortcut("Ctrl+Alt+4") + else: + self.aFocusOutline.setShortcut("Alt+4") self.aFocusOutline.triggered.connect(lambda: self.theParent.switchFocus(nwWidget.OUTLINE)) self.viewMenu.addAction(self.aFocusOutline) From 0bfb9d070089dae97aec9f81baa26e09363b81df Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Thu, 10 Jun 2021 22:18:51 +0200 Subject: [PATCH 7/7] Update docs --- docs/source/usage_interface.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/source/usage_interface.rst b/docs/source/usage_interface.rst index 47038ae9..6296fffe 100644 --- a/docs/source/usage_interface.rst +++ b/docs/source/usage_interface.rst @@ -413,10 +413,10 @@ Most features are available as keyboard shortcuts. These are as follows: :widths: 30, 70 :class: "tight-table" - ":kbd:`Alt`:kbd:`1`", "Switch focus to the project tree." - ":kbd:`Alt`:kbd:`2`", "Switch focus to document editor." - ":kbd:`Alt`:kbd:`3`", "Switch focus to document viewer." - ":kbd:`Alt`:kbd:`4`", "Switch focus to outline view." + ":kbd:`Alt`:kbd:`1`", "Switch focus to the project tree. On Windows, use :kbd:`Ctrl`:kbd:`Alt`:kbd:`1`." + ":kbd:`Alt`:kbd:`2`", "Switch focus to document editor. On Windows, use :kbd:`Ctrl`:kbd:`Alt`:kbd:`2`." + ":kbd:`Alt`:kbd:`3`", "Switch focus to document viewer. On Windows, use :kbd:`Ctrl`:kbd:`Alt`:kbd:`3`." + ":kbd:`Alt`:kbd:`4`", "Switch focus to outline view. On Windows, use :kbd:`Ctrl`:kbd:`Alt`:kbd:`4`." ":kbd:`Alt`:kbd:`Left`", "Move backward in the view history of the document viewer." ":kbd:`Alt`:kbd:`Right`", "Move forward in the view history of the document viewer." ":kbd:`Ctrl`:kbd:`.`", "Open menu to correct word under cursor."