From e2895ce4f77494a5daf6e3f9f34cd2dd10be40fa Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Sun, 12 May 2019 20:34:52 +0200 Subject: [PATCH] Improved the alert to user interface, and added an enum class for severity flags. --- nw/enum.py | 9 +++++++++ nw/gui/doceditor.py | 2 +- nw/gui/doctree.py | 9 +++++---- nw/gui/winmain.py | 15 ++++++++++----- nw/project/document.py | 8 ++++++-- nw/project/project.py | 19 +++++++++++-------- tests/nwdummy.py | 8 ++++++-- 7 files changed, 48 insertions(+), 22 deletions(-) diff --git a/nw/enum.py b/nw/enum.py index 66a9b2da..63be9e73 100644 --- a/nw/enum.py +++ b/nw/enum.py @@ -67,3 +67,12 @@ class nwDocAction(Enum): SEL_PARA = 12 # END Enum nwDocAction + +class nwAlert(Enum): + + INFO = 0 + WARN = 1 + ERROR = 2 + BUG = 3 + +# END Enum nwAlert diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 35249817..0f5ded96 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -22,7 +22,7 @@ from PyQt5.QtCore import Qt, QTimer from nw.gui.dochighlight import GuiDocHighlighter from nw.gui.wordcounter import WordCounter -from nw.enum import nwDocAction +from nw.enum import nwDocAction, nwAlert logger = logging.getLogger(__name__) diff --git a/nw/gui/doctree.py b/nw/gui/doctree.py index d448dbd0..0893ad07 100644 --- a/nw/gui/doctree.py +++ b/nw/gui/doctree.py @@ -21,6 +21,7 @@ from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QAbstractItemView, QIn from nw.project.item import NWItem from nw.enum import nwItemType, nwItemClass from nw.constants import nwLabels +from nw.enum import nwAlert logger = logging.getLogger(__name__) @@ -93,13 +94,13 @@ class GuiDocTree(QTreeWidget): pHandle = self.getSelectedHandle() if pHandle is None: - self.makeAlert("No valid parent item selected", 2) + self.makeAlert("No valid parent item selected", nwAlert.ERROR) return False if itemClass is None: itemClass = self.theProject.getItem(pHandle).itemClass if itemClass is None: - self.makeAlert("Failed to find an appropriate item class for item %s" % pHandle, 2) + self.makeAlert("Failed to find an appropriate item class for item %s" % pHandle, nwAlert.BUG) return False logger.verbose("Adding new item of type %s and class %s to handle %s" % ( @@ -234,7 +235,7 @@ class GuiDocTree(QTreeWidget): trItemP.setSelected(True) self.theProject.setProjectChanged(True) else: - self.theParent.makeAlert(["Cannot delete folder.","It is not empty."],2) + self.makeAlert(["Cannot delete folder.","It is not empty."], nwAlert.ERROR) return elif nwItemS.itemType == nwItemType.ROOT: @@ -245,7 +246,7 @@ class GuiDocTree(QTreeWidget): self.theParent.mainMenu.setAvailableRoot() self.theProject.setProjectChanged(True) else: - self.theParent.makeAlert(["Cannot delete root folder.","It is not empty."],2) + self.makeAlert(["Cannot delete root folder.","It is not empty."], nwAlert.ERROR) return return diff --git a/nw/gui/winmain.py b/nw/gui/winmain.py index 673fdb27..1fbdd5ab 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/winmain.py @@ -31,7 +31,7 @@ from nw.project.document import NWDoc from nw.project.item import NWItem from nw.convert.tokenizer import Tokenizer from nw.convert.tohtml import ToHtml -from nw.enum import nwItemType +from nw.enum import nwItemType, nwAlert logger = logging.getLogger(__name__) @@ -124,7 +124,7 @@ class GuiMain(QMainWindow): return - def makeAlert(self, theMessage, theLevel): + def makeAlert(self, theMessage, theLevel=nwAlert.INFO): """Alert both the user and the logger at the same time. Message can be either a string or an array of strings. Severity level is 0 = info, 1 = warning, and 2 = error. """ @@ -137,18 +137,23 @@ class GuiMain(QMainWindow): logMsg = [theMessage] msgBox = QMessageBox() - if theLevel == 0: + if theLevel == nwAlert.INFO: for msgLine in logMsg: logger.info(msgLine) msgBox.information(self, "Information", popMsg) - elif theLevel == 1: + elif theLevel == nwAlert.WARN: for msgLine in logMsg: logger.warning(msgLine) msgBox.warning(self, "Warning", popMsg) - elif theLevel == 2: + elif theLevel == nwAlert.ERROR: for msgLine in logMsg: logger.error(msgLine) msgBox.critical(self, "Error", popMsg) + elif theLevel == nwAlert.BUG: + for msgLine in logMsg: + logger.error(msgLine) + popMsg += "
This is a bug!" + msgBox.critical(self, "Internal Error", popMsg) return diff --git a/nw/project/document.py b/nw/project/document.py index 04854275..1d0a8af3 100644 --- a/nw/project/document.py +++ b/nw/project/document.py @@ -16,6 +16,7 @@ import nw from os import path, mkdir, rename, unlink from nw.tools.analyse import TextAnalysis +from nw.enum import nwAlert logger = logging.getLogger(__name__) @@ -31,6 +32,9 @@ class NWDoc(): self.theItem = None self.docHandle = None + # Internal Mapping + self.makeAlert = self.theParent.makeAlert + return def openDocument(self, tHandle): @@ -49,7 +53,7 @@ class NWDoc(): with open(docPath,mode="r") as inFile: theDoc = inFile.read() except Exception as e: - self.theParent.makeAlert(["Failed to open document file.",str(e)],2) + self.makeAlert(["Failed to open document file.",str(e)], nwAlert.ERROR) return "" else: logger.debug("The requested document does not exist.") @@ -83,7 +87,7 @@ class NWDoc(): with open(docPath,mode="w") as outFile: outFile.write(docText) except Exception as e: - self.theParent.makeAlert(["Could not save document.",str(e)],2) + self.makeAlert(["Could not save document.",str(e)], nwAlert.ERROR) return False if path.isfile(docTemp): unlink(docTemp) diff --git a/nw/project/project.py b/nw/project/project.py index 23821055..1c193b95 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -19,7 +19,7 @@ from hashlib import sha256 from datetime import datetime from time import time -from nw.enum import nwItemType, nwItemClass, nwItemLayout +from nw.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert from nw.common import checkString, checkBool from nw.project.item import NWItem @@ -59,6 +59,9 @@ class NWProject(): # Set Defaults self.clearProject() + # Internal Mapping + self.makeAlert = self.theParent.makeAlert + return ## @@ -67,7 +70,7 @@ class NWProject(): def newRoot(self, rootName, rootClass): if not self.checkRootUnique(rootClass): - self.theParent.makeAlert("Duplicate root item detected!",2) + self.makeAlert("Duplicate root item detected!", nwAlert.ERROR) return None newItem = NWItem() newItem.setName(rootName) @@ -152,7 +155,7 @@ class NWProject(): if not path.isfile(fileName): fileName = path.join(fileName, "nwProject.nwx") if not path.isfile(fileName): - self.theParent.makeAlert("File not found: %s" % fileName,2) + self.makeAlert("File not found: %s" % fileName, nwAlert.ERROR) return False self.clearProject() @@ -176,7 +179,7 @@ class NWProject(): logger.verbose("File version is %s" % fileVersion) if not nwxRoot == "novelWriterXML" or not fileVersion == "1.0": - self.theParent.makeAlert("Project file does not appear to be a novelWriterXML file version 1.0",2) + self.makeAlert("Project file does not appear to be a novelWriterXML file version 1.0", nwAlert.ERROR) return False for xChild in xRoot: @@ -228,7 +231,7 @@ class NWProject(): def saveProject(self): if self.projPath is None: - self.theParent.makeAlert("Project path not set, cannot save.",2) + self.makeAlert("Project path not set, cannot save.", nwAlert.ERROR) return False self.projMeta = path.join(self.projPath,"meta") @@ -275,7 +278,7 @@ class NWProject(): xml_declaration = True )) except Exception as e: - self.theParent.makeAlert(["Failed to save project.",str(e)],2) + self.makeAlert(["Failed to save project.",str(e)], nwAlert.ERROR) return False self.mainConf.setRecent(self.projPath) @@ -367,7 +370,7 @@ class NWProject(): mkdir(thePath) logger.info("Created folder %s" % thePath) except Exception as e: - self.theParent.makeAlert(["Could not create folder.",str(e)],2) + self.makeAlert(["Could not create folder.",str(e)], nwAlert.ERROR) return False return True @@ -414,7 +417,7 @@ class NWProject(): # Report status if len(orphanFiles) > 0: - self.theParent.makeAlert("Found %d orphaned file(s) in project folder!" % len(orphanFiles),1) + self.makeAlert("Found %d orphaned file(s) in project folder!" % len(orphanFiles), nwAlert.WARN) else: logger.debug("File check OK") return diff --git a/tests/nwdummy.py b/tests/nwdummy.py index d033668e..d1e65fb0 100644 --- a/tests/nwdummy.py +++ b/tests/nwdummy.py @@ -2,6 +2,8 @@ """novelWriter Test Dummy GUI Classes """ +from nw.enum import nwAlert + class DummyMain(): def __init__(self): @@ -9,10 +11,12 @@ class DummyMain(): return def makeAlert(self, theMessage, theLevel): - if theLevel == 1: + if theLevel == nwAlert.WARN: lvlMsg = "WARNING: " - elif theLevel == 2: + elif theLevel == nwAlert.ERROR: lvlMsg = "ERROR: " + elif theLevel == nwAlert.BUG: + lvlMsg = "BUG: " else: lvlMsg = "" if isinstance(theMessage, list):