Improved the alert to user interface, and added an enum class for severity flags.

This commit is contained in:
Veronica K. B. Olsen
2019-05-12 20:34:52 +02:00
parent 9d180bf101
commit e2895ce4f7
7 changed files with 48 additions and 22 deletions
+9
View File
@@ -67,3 +67,12 @@ class nwDocAction(Enum):
SEL_PARA = 12 SEL_PARA = 12
# END Enum nwDocAction # END Enum nwDocAction
class nwAlert(Enum):
INFO = 0
WARN = 1
ERROR = 2
BUG = 3
# END Enum nwAlert
+1 -1
View File
@@ -22,7 +22,7 @@ from PyQt5.QtCore import Qt, QTimer
from nw.gui.dochighlight import GuiDocHighlighter from nw.gui.dochighlight import GuiDocHighlighter
from nw.gui.wordcounter import WordCounter from nw.gui.wordcounter import WordCounter
from nw.enum import nwDocAction from nw.enum import nwDocAction, nwAlert
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
+5 -4
View File
@@ -21,6 +21,7 @@ from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QAbstractItemView, QIn
from nw.project.item import NWItem from nw.project.item import NWItem
from nw.enum import nwItemType, nwItemClass from nw.enum import nwItemType, nwItemClass
from nw.constants import nwLabels from nw.constants import nwLabels
from nw.enum import nwAlert
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -93,13 +94,13 @@ class GuiDocTree(QTreeWidget):
pHandle = self.getSelectedHandle() pHandle = self.getSelectedHandle()
if pHandle is None: if pHandle is None:
self.makeAlert("No valid parent item selected", 2) self.makeAlert("No valid parent item selected", nwAlert.ERROR)
return False return False
if itemClass is None: if itemClass is None:
itemClass = self.theProject.getItem(pHandle).itemClass itemClass = self.theProject.getItem(pHandle).itemClass
if itemClass is None: 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 return False
logger.verbose("Adding new item of type %s and class %s to handle %s" % ( logger.verbose("Adding new item of type %s and class %s to handle %s" % (
@@ -234,7 +235,7 @@ class GuiDocTree(QTreeWidget):
trItemP.setSelected(True) trItemP.setSelected(True)
self.theProject.setProjectChanged(True) self.theProject.setProjectChanged(True)
else: else:
self.theParent.makeAlert(["Cannot delete folder.","It is not empty."],2) self.makeAlert(["Cannot delete folder.","It is not empty."], nwAlert.ERROR)
return return
elif nwItemS.itemType == nwItemType.ROOT: elif nwItemS.itemType == nwItemType.ROOT:
@@ -245,7 +246,7 @@ class GuiDocTree(QTreeWidget):
self.theParent.mainMenu.setAvailableRoot() self.theParent.mainMenu.setAvailableRoot()
self.theProject.setProjectChanged(True) self.theProject.setProjectChanged(True)
else: 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
return return
+10 -5
View File
@@ -31,7 +31,7 @@ from nw.project.document import NWDoc
from nw.project.item import NWItem from nw.project.item import NWItem
from nw.convert.tokenizer import Tokenizer from nw.convert.tokenizer import Tokenizer
from nw.convert.tohtml import ToHtml from nw.convert.tohtml import ToHtml
from nw.enum import nwItemType from nw.enum import nwItemType, nwAlert
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -124,7 +124,7 @@ class GuiMain(QMainWindow):
return 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 """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. array of strings. Severity level is 0 = info, 1 = warning, and 2 = error.
""" """
@@ -137,18 +137,23 @@ class GuiMain(QMainWindow):
logMsg = [theMessage] logMsg = [theMessage]
msgBox = QMessageBox() msgBox = QMessageBox()
if theLevel == 0: if theLevel == nwAlert.INFO:
for msgLine in logMsg: for msgLine in logMsg:
logger.info(msgLine) logger.info(msgLine)
msgBox.information(self, "Information", popMsg) msgBox.information(self, "Information", popMsg)
elif theLevel == 1: elif theLevel == nwAlert.WARN:
for msgLine in logMsg: for msgLine in logMsg:
logger.warning(msgLine) logger.warning(msgLine)
msgBox.warning(self, "Warning", popMsg) msgBox.warning(self, "Warning", popMsg)
elif theLevel == 2: elif theLevel == nwAlert.ERROR:
for msgLine in logMsg: for msgLine in logMsg:
logger.error(msgLine) logger.error(msgLine)
msgBox.critical(self, "Error", popMsg) msgBox.critical(self, "Error", popMsg)
elif theLevel == nwAlert.BUG:
for msgLine in logMsg:
logger.error(msgLine)
popMsg += "<br>This is a bug!"
msgBox.critical(self, "Internal Error", popMsg)
return return
+6 -2
View File
@@ -16,6 +16,7 @@ import nw
from os import path, mkdir, rename, unlink from os import path, mkdir, rename, unlink
from nw.tools.analyse import TextAnalysis from nw.tools.analyse import TextAnalysis
from nw.enum import nwAlert
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -31,6 +32,9 @@ class NWDoc():
self.theItem = None self.theItem = None
self.docHandle = None self.docHandle = None
# Internal Mapping
self.makeAlert = self.theParent.makeAlert
return return
def openDocument(self, tHandle): def openDocument(self, tHandle):
@@ -49,7 +53,7 @@ class NWDoc():
with open(docPath,mode="r") as inFile: with open(docPath,mode="r") as inFile:
theDoc = inFile.read() theDoc = inFile.read()
except Exception as e: 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 "" return ""
else: else:
logger.debug("The requested document does not exist.") logger.debug("The requested document does not exist.")
@@ -83,7 +87,7 @@ class NWDoc():
with open(docPath,mode="w") as outFile: with open(docPath,mode="w") as outFile:
outFile.write(docText) outFile.write(docText)
except Exception as e: 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 return False
if path.isfile(docTemp): unlink(docTemp) if path.isfile(docTemp): unlink(docTemp)
+11 -8
View File
@@ -19,7 +19,7 @@ from hashlib import sha256
from datetime import datetime from datetime import datetime
from time import time 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.common import checkString, checkBool
from nw.project.item import NWItem from nw.project.item import NWItem
@@ -59,6 +59,9 @@ class NWProject():
# Set Defaults # Set Defaults
self.clearProject() self.clearProject()
# Internal Mapping
self.makeAlert = self.theParent.makeAlert
return return
## ##
@@ -67,7 +70,7 @@ class NWProject():
def newRoot(self, rootName, rootClass): def newRoot(self, rootName, rootClass):
if not self.checkRootUnique(rootClass): if not self.checkRootUnique(rootClass):
self.theParent.makeAlert("Duplicate root item detected!",2) self.makeAlert("Duplicate root item detected!", nwAlert.ERROR)
return None return None
newItem = NWItem() newItem = NWItem()
newItem.setName(rootName) newItem.setName(rootName)
@@ -152,7 +155,7 @@ class NWProject():
if not path.isfile(fileName): if not path.isfile(fileName):
fileName = path.join(fileName, "nwProject.nwx") fileName = path.join(fileName, "nwProject.nwx")
if not path.isfile(fileName): 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 return False
self.clearProject() self.clearProject()
@@ -176,7 +179,7 @@ class NWProject():
logger.verbose("File version is %s" % fileVersion) logger.verbose("File version is %s" % fileVersion)
if not nwxRoot == "novelWriterXML" or not fileVersion == "1.0": 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 return False
for xChild in xRoot: for xChild in xRoot:
@@ -228,7 +231,7 @@ class NWProject():
def saveProject(self): def saveProject(self):
if self.projPath is None: 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 return False
self.projMeta = path.join(self.projPath,"meta") self.projMeta = path.join(self.projPath,"meta")
@@ -275,7 +278,7 @@ class NWProject():
xml_declaration = True xml_declaration = True
)) ))
except Exception as e: 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 return False
self.mainConf.setRecent(self.projPath) self.mainConf.setRecent(self.projPath)
@@ -367,7 +370,7 @@ class NWProject():
mkdir(thePath) mkdir(thePath)
logger.info("Created folder %s" % thePath) logger.info("Created folder %s" % thePath)
except Exception as e: 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 False
return True return True
@@ -414,7 +417,7 @@ class NWProject():
# Report status # Report status
if len(orphanFiles) > 0: 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: else:
logger.debug("File check OK") logger.debug("File check OK")
return return
+6 -2
View File
@@ -2,6 +2,8 @@
"""novelWriter Test Dummy GUI Classes """novelWriter Test Dummy GUI Classes
""" """
from nw.enum import nwAlert
class DummyMain(): class DummyMain():
def __init__(self): def __init__(self):
@@ -9,10 +11,12 @@ class DummyMain():
return return
def makeAlert(self, theMessage, theLevel): def makeAlert(self, theMessage, theLevel):
if theLevel == 1: if theLevel == nwAlert.WARN:
lvlMsg = "WARNING: " lvlMsg = "WARNING: "
elif theLevel == 2: elif theLevel == nwAlert.ERROR:
lvlMsg = "ERROR: " lvlMsg = "ERROR: "
elif theLevel == nwAlert.BUG:
lvlMsg = "BUG: "
else: else:
lvlMsg = "" lvlMsg = ""
if isinstance(theMessage, list): if isinstance(theMessage, list):