Added an alert wrapper for both popup box and logger
This commit is contained in:
+29
-1
@@ -14,7 +14,7 @@ import logging
|
|||||||
import nw
|
import nw
|
||||||
|
|
||||||
from os import path
|
from os import path
|
||||||
from PyQt5.QtWidgets import QWidget, QMainWindow, QVBoxLayout, QFrame, QSplitter, QFileDialog, QStackedWidget, QShortcut
|
from PyQt5.QtWidgets import QWidget, QMainWindow, QVBoxLayout, QFrame, QSplitter, QFileDialog, QStackedWidget, QShortcut, QMessageBox
|
||||||
from PyQt5.QtGui import QIcon
|
from PyQt5.QtGui import QIcon
|
||||||
from PyQt5.QtCore import Qt
|
from PyQt5.QtCore import Qt
|
||||||
|
|
||||||
@@ -97,6 +97,34 @@ class GuiMain(QMainWindow):
|
|||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
def makeAlert(self, theMessage, theLevel):
|
||||||
|
"""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.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if isinstance(theMessage, list):
|
||||||
|
popMsg = "<br>".join(theMessage)
|
||||||
|
logMsg = theMessage
|
||||||
|
else:
|
||||||
|
popMsg = theMessage
|
||||||
|
logMsg = [theMessage]
|
||||||
|
|
||||||
|
msgBox = QMessageBox()
|
||||||
|
if theLevel == 0:
|
||||||
|
for msgLine in logMsg:
|
||||||
|
logger.info(msgLine)
|
||||||
|
msgBox.information(self, "Information", popMsg)
|
||||||
|
elif theLevel == 1:
|
||||||
|
for msgLine in logMsg:
|
||||||
|
logger.warning(msgLine)
|
||||||
|
msgBox.warning(self, "Warning", popMsg)
|
||||||
|
elif theLevel == 2:
|
||||||
|
for msgLine in logMsg:
|
||||||
|
logger.error(msgLine)
|
||||||
|
msgBox.critical(self, "Error", popMsg)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
##
|
##
|
||||||
# Project Actions
|
# Project Actions
|
||||||
##
|
##
|
||||||
|
|||||||
+12
-4
@@ -45,8 +45,12 @@ class NWDoc():
|
|||||||
docPath = path.join(dataDir, docFile)
|
docPath = path.join(dataDir, docFile)
|
||||||
|
|
||||||
if path.isfile(docPath):
|
if path.isfile(docPath):
|
||||||
with open(docPath,mode="r") as inFile:
|
try:
|
||||||
theDoc = inFile.read()
|
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)
|
||||||
|
return ""
|
||||||
else:
|
else:
|
||||||
logger.debug("The requested document does not exist.")
|
logger.debug("The requested document does not exist.")
|
||||||
return ""
|
return ""
|
||||||
@@ -75,8 +79,12 @@ class NWDoc():
|
|||||||
if path.isfile(docBack): rename(docBack,docTemp)
|
if path.isfile(docBack): rename(docBack,docTemp)
|
||||||
if path.isfile(docPath): rename(docPath,docBack)
|
if path.isfile(docPath): rename(docPath,docBack)
|
||||||
|
|
||||||
with open(docPath,mode="w") as outFile:
|
try:
|
||||||
outFile.write(docText)
|
with open(docPath,mode="w") as outFile:
|
||||||
|
outFile.write(docText)
|
||||||
|
except Exception as e:
|
||||||
|
self.theParent.makeAlert(["Could not save document.",str(e)],2)
|
||||||
|
return False
|
||||||
|
|
||||||
if path.isfile(docTemp): unlink(docTemp)
|
if path.isfile(docTemp): unlink(docTemp)
|
||||||
|
|
||||||
|
|||||||
@@ -128,7 +128,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):
|
||||||
logger.error("File not found: %s" % fileName)
|
self.theParent.makeAlert("File not found: %s" % fileName,2)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.clearProject()
|
self.clearProject()
|
||||||
@@ -146,7 +146,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":
|
||||||
logger.error("Project file does not appear to be a novelWriterXML file version 1.0")
|
self.theParent.makeAlert("Project file does not appear to be a novelWriterXML file version 1.0",2)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
for xChild in xRoot:
|
for xChild in xRoot:
|
||||||
@@ -192,15 +192,15 @@ class NWProject():
|
|||||||
def saveProject(self):
|
def saveProject(self):
|
||||||
|
|
||||||
if self.projPath is None:
|
if self.projPath is None:
|
||||||
logger.error("Project path not set, cannot save.")
|
self.theParent.makeAlert("Project path not set, cannot save.",2)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not path.isdir(self.projPath):
|
if not path.isdir(self.projPath):
|
||||||
try:
|
try:
|
||||||
mkdir(self.projPath)
|
mkdir(self.projPath)
|
||||||
logger.info("Created folder %s" % self.projPath)
|
logger.info("Created folder %s" % self.projPath)
|
||||||
except:
|
except Exception as e:
|
||||||
logger.error("Could not create folder %s" % self.projPath)
|
self.theParent.makeAlert(["Could not create folder.",str(e)],2)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
logger.debug("Saving project: %s" % self.projPath)
|
logger.debug("Saving project: %s" % self.projPath)
|
||||||
@@ -239,8 +239,7 @@ class NWProject():
|
|||||||
xml_declaration = True
|
xml_declaration = True
|
||||||
))
|
))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Failed to save project to %s" % saveFile)
|
self.theParent.makeAlert(["Failed to save project.",str(e)],2)
|
||||||
logger.error(str(e))
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.mainConf.setRecent(self.projPath)
|
self.mainConf.setRecent(self.projPath)
|
||||||
|
|||||||
Reference in New Issue
Block a user