Skip to content

Commit e2895ce

Browse files
committed
Improved the alert to user interface, and added an enum class for severity flags.
1 parent 9d180bf commit e2895ce

File tree

7 files changed

+48
-22
lines changed

7 files changed

+48
-22
lines changed

nw/enum.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,12 @@ class nwDocAction(Enum):
6767
SEL_PARA = 12
6868

6969
# END Enum nwDocAction
70+
71+
class nwAlert(Enum):
72+
73+
INFO = 0
74+
WARN = 1
75+
ERROR = 2
76+
BUG = 3
77+
78+
# END Enum nwAlert

nw/gui/doceditor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from nw.gui.dochighlight import GuiDocHighlighter
2424
from nw.gui.wordcounter import WordCounter
25-
from nw.enum import nwDocAction
25+
from nw.enum import nwDocAction, nwAlert
2626

2727
logger = logging.getLogger(__name__)
2828

nw/gui/doctree.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from nw.project.item import NWItem
2222
from nw.enum import nwItemType, nwItemClass
2323
from nw.constants import nwLabels
24+
from nw.enum import nwAlert
2425

2526
logger = logging.getLogger(__name__)
2627

@@ -93,13 +94,13 @@ def newTreeItem(self, itemType, itemClass):
9394

9495
pHandle = self.getSelectedHandle()
9596
if pHandle is None:
96-
self.makeAlert("No valid parent item selected", 2)
97+
self.makeAlert("No valid parent item selected", nwAlert.ERROR)
9798
return False
9899

99100
if itemClass is None:
100101
itemClass = self.theProject.getItem(pHandle).itemClass
101102
if itemClass is None:
102-
self.makeAlert("Failed to find an appropriate item class for item %s" % pHandle, 2)
103+
self.makeAlert("Failed to find an appropriate item class for item %s" % pHandle, nwAlert.BUG)
103104
return False
104105

105106
logger.verbose("Adding new item of type %s and class %s to handle %s" % (
@@ -234,7 +235,7 @@ def deleteItem(self, tHandle=None):
234235
trItemP.setSelected(True)
235236
self.theProject.setProjectChanged(True)
236237
else:
237-
self.theParent.makeAlert(["Cannot delete folder.","It is not empty."],2)
238+
self.makeAlert(["Cannot delete folder.","It is not empty."], nwAlert.ERROR)
238239
return
239240

240241
elif nwItemS.itemType == nwItemType.ROOT:
@@ -245,7 +246,7 @@ def deleteItem(self, tHandle=None):
245246
self.theParent.mainMenu.setAvailableRoot()
246247
self.theProject.setProjectChanged(True)
247248
else:
248-
self.theParent.makeAlert(["Cannot delete root folder.","It is not empty."],2)
249+
self.makeAlert(["Cannot delete root folder.","It is not empty."], nwAlert.ERROR)
249250
return
250251

251252
return

nw/gui/winmain.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from nw.project.item import NWItem
3232
from nw.convert.tokenizer import Tokenizer
3333
from nw.convert.tohtml import ToHtml
34-
from nw.enum import nwItemType
34+
from nw.enum import nwItemType, nwAlert
3535

3636
logger = logging.getLogger(__name__)
3737

@@ -124,7 +124,7 @@ def __init__(self):
124124

125125
return
126126

127-
def makeAlert(self, theMessage, theLevel):
127+
def makeAlert(self, theMessage, theLevel=nwAlert.INFO):
128128
"""Alert both the user and the logger at the same time. Message can be either a string or an
129129
array of strings. Severity level is 0 = info, 1 = warning, and 2 = error.
130130
"""
@@ -137,18 +137,23 @@ def makeAlert(self, theMessage, theLevel):
137137
logMsg = [theMessage]
138138

139139
msgBox = QMessageBox()
140-
if theLevel == 0:
140+
if theLevel == nwAlert.INFO:
141141
for msgLine in logMsg:
142142
logger.info(msgLine)
143143
msgBox.information(self, "Information", popMsg)
144-
elif theLevel == 1:
144+
elif theLevel == nwAlert.WARN:
145145
for msgLine in logMsg:
146146
logger.warning(msgLine)
147147
msgBox.warning(self, "Warning", popMsg)
148-
elif theLevel == 2:
148+
elif theLevel == nwAlert.ERROR:
149149
for msgLine in logMsg:
150150
logger.error(msgLine)
151151
msgBox.critical(self, "Error", popMsg)
152+
elif theLevel == nwAlert.BUG:
153+
for msgLine in logMsg:
154+
logger.error(msgLine)
155+
popMsg += "<br>This is a bug!"
156+
msgBox.critical(self, "Internal Error", popMsg)
152157

153158
return
154159

nw/project/document.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from os import path, mkdir, rename, unlink
1717

1818
from nw.tools.analyse import TextAnalysis
19+
from nw.enum import nwAlert
1920

2021
logger = logging.getLogger(__name__)
2122

@@ -31,6 +32,9 @@ def __init__(self, theProject, theParent):
3132
self.theItem = None
3233
self.docHandle = None
3334

35+
# Internal Mapping
36+
self.makeAlert = self.theParent.makeAlert
37+
3438
return
3539

3640
def openDocument(self, tHandle):
@@ -49,7 +53,7 @@ def openDocument(self, tHandle):
4953
with open(docPath,mode="r") as inFile:
5054
theDoc = inFile.read()
5155
except Exception as e:
52-
self.theParent.makeAlert(["Failed to open document file.",str(e)],2)
56+
self.makeAlert(["Failed to open document file.",str(e)], nwAlert.ERROR)
5357
return ""
5458
else:
5559
logger.debug("The requested document does not exist.")
@@ -83,7 +87,7 @@ def saveDocument(self, docText):
8387
with open(docPath,mode="w") as outFile:
8488
outFile.write(docText)
8589
except Exception as e:
86-
self.theParent.makeAlert(["Could not save document.",str(e)],2)
90+
self.makeAlert(["Could not save document.",str(e)], nwAlert.ERROR)
8791
return False
8892

8993
if path.isfile(docTemp): unlink(docTemp)

nw/project/project.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from datetime import datetime
2020
from time import time
2121

22-
from nw.enum import nwItemType, nwItemClass, nwItemLayout
22+
from nw.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert
2323
from nw.common import checkString, checkBool
2424
from nw.project.item import NWItem
2525

@@ -59,6 +59,9 @@ def __init__(self, theParent):
5959
# Set Defaults
6060
self.clearProject()
6161

62+
# Internal Mapping
63+
self.makeAlert = self.theParent.makeAlert
64+
6265
return
6366

6467
##
@@ -67,7 +70,7 @@ def __init__(self, theParent):
6770

6871
def newRoot(self, rootName, rootClass):
6972
if not self.checkRootUnique(rootClass):
70-
self.theParent.makeAlert("Duplicate root item detected!",2)
73+
self.makeAlert("Duplicate root item detected!", nwAlert.ERROR)
7174
return None
7275
newItem = NWItem()
7376
newItem.setName(rootName)
@@ -152,7 +155,7 @@ def openProject(self, fileName):
152155
if not path.isfile(fileName):
153156
fileName = path.join(fileName, "nwProject.nwx")
154157
if not path.isfile(fileName):
155-
self.theParent.makeAlert("File not found: %s" % fileName,2)
158+
self.makeAlert("File not found: %s" % fileName, nwAlert.ERROR)
156159
return False
157160

158161
self.clearProject()
@@ -176,7 +179,7 @@ def openProject(self, fileName):
176179
logger.verbose("File version is %s" % fileVersion)
177180

178181
if not nwxRoot == "novelWriterXML" or not fileVersion == "1.0":
179-
self.theParent.makeAlert("Project file does not appear to be a novelWriterXML file version 1.0",2)
182+
self.makeAlert("Project file does not appear to be a novelWriterXML file version 1.0", nwAlert.ERROR)
180183
return False
181184

182185
for xChild in xRoot:
@@ -228,7 +231,7 @@ def openProject(self, fileName):
228231
def saveProject(self):
229232

230233
if self.projPath is None:
231-
self.theParent.makeAlert("Project path not set, cannot save.",2)
234+
self.makeAlert("Project path not set, cannot save.", nwAlert.ERROR)
232235
return False
233236

234237
self.projMeta = path.join(self.projPath,"meta")
@@ -275,7 +278,7 @@ def saveProject(self):
275278
xml_declaration = True
276279
))
277280
except Exception as e:
278-
self.theParent.makeAlert(["Failed to save project.",str(e)],2)
281+
self.makeAlert(["Failed to save project.",str(e)], nwAlert.ERROR)
279282
return False
280283

281284
self.mainConf.setRecent(self.projPath)
@@ -367,7 +370,7 @@ def _checkFolder(self, thePath):
367370
mkdir(thePath)
368371
logger.info("Created folder %s" % thePath)
369372
except Exception as e:
370-
self.theParent.makeAlert(["Could not create folder.",str(e)],2)
373+
self.makeAlert(["Could not create folder.",str(e)], nwAlert.ERROR)
371374
return False
372375
return True
373376

@@ -414,7 +417,7 @@ def _scanProjectFolder(self):
414417

415418
# Report status
416419
if len(orphanFiles) > 0:
417-
self.theParent.makeAlert("Found %d orphaned file(s) in project folder!" % len(orphanFiles),1)
420+
self.makeAlert("Found %d orphaned file(s) in project folder!" % len(orphanFiles), nwAlert.WARN)
418421
else:
419422
logger.debug("File check OK")
420423
return

tests/nwdummy.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
"""novelWriter Test Dummy GUI Classes
33
"""
44

5+
from nw.enum import nwAlert
6+
57
class DummyMain():
68

79
def __init__(self):
810
self.mainConf = None
911
return
1012

1113
def makeAlert(self, theMessage, theLevel):
12-
if theLevel == 1:
14+
if theLevel == nwAlert.WARN:
1315
lvlMsg = "WARNING: "
14-
elif theLevel == 2:
16+
elif theLevel == nwAlert.ERROR:
1517
lvlMsg = "ERROR: "
18+
elif theLevel == nwAlert.BUG:
19+
lvlMsg = "BUG: "
1620
else:
1721
lvlMsg = ""
1822
if isinstance(theMessage, list):

0 commit comments

Comments
 (0)