Make sure no menu items crashes the app when there is no project open

This commit is contained in:
Veronica K. B. Olsen
2019-06-08 15:57:22 +02:00
parent 5ff56a2b4b
commit 07244045cc
4 changed files with 58 additions and 30 deletions
+3
View File
@@ -200,6 +200,9 @@ class GuiDocEditor(QTextEdit):
def docAction(self, theAction):
logger.verbose("Requesting action: %s" % theAction.name)
if not self.theParent.hasProject:
logger.error("No project open")
return False
if theAction == nwDocAction.UNDO: self.undo()
elif theAction == nwDocAction.REDO: self.redo()
elif theAction == nwDocAction.CUT: self.cut()
+19 -9
View File
@@ -92,6 +92,9 @@ class GuiDocTree(QTreeWidget):
pHandle = self.getSelectedHandle()
if not self.theParent.hasProject:
return False
if itemClass is None and pHandle is not None:
itemClass = self.theProject.getItem(pHandle).itemClass
if itemClass is None:
@@ -149,7 +152,7 @@ class GuiDocTree(QTreeWidget):
"""Move an item up or down in the tree, but only if the treeView has focus. This also
applies when the menu is used.
"""
if QApplication.focusWidget() == self:
if QApplication.focusWidget() == self and self.theParent.hasProject:
tHandle = self.getSelectedHandle()
tItem = self._getTreeItem(tHandle)
pItem = tItem.parent()
@@ -157,20 +160,24 @@ class GuiDocTree(QTreeWidget):
tIndex = self.indexOfTopLevelItem(tItem)
nChild = self.topLevelItemCount()
nIndex = tIndex + nStep
if nIndex < 0 or nIndex >= nChild: return
if nIndex < 0 or nIndex >= nChild:
return False
cItem = self.takeTopLevelItem(tIndex)
self.insertTopLevelItem(nIndex, cItem)
else:
tIndex = pItem.indexOfChild(tItem)
nChild = pItem.childCount()
nIndex = tIndex + nStep
if nIndex < 0 or nIndex >= nChild: return
if nIndex < 0 or nIndex >= nChild:
return False
cItem = pItem.takeChild(tIndex)
pItem.insertChild(nIndex, cItem)
self.clearSelection()
cItem.setSelected(True)
self.theProject.setProjectChanged(True)
return
else:
return False
return True
def saveTreeOrder(self):
theList = []
@@ -199,6 +206,9 @@ class GuiDocTree(QTreeWidget):
if tHandle is None:
tHandle = self.getSelectedHandle()
if tHandle is None:
return False
trItemS = self._getTreeItem(tHandle)
nwItemS = self.theProject.getItem(tHandle)
@@ -208,7 +218,7 @@ class GuiDocTree(QTreeWidget):
trItemT = self._addTrashRoot()
if trItemP is None or trItemT is None:
logger.error("Could not move item to trash")
return
return False
tIndex = trItemP.indexOfChild(trItemS)
trItemC = trItemP.takeChild(tIndex)
trItemT.addChild(trItemC)
@@ -222,7 +232,7 @@ class GuiDocTree(QTreeWidget):
trItemP = trItemS.parent()
if trItemP is None:
logger.error("Could not delete folder")
return
return False
tIndex = trItemP.indexOfChild(trItemS)
if trItemS.childCount() == 0:
trItemP.takeChild(tIndex)
@@ -231,7 +241,7 @@ class GuiDocTree(QTreeWidget):
self.theProject.deleteItem(tHandle)
else:
self.makeAlert(["Cannot delete folder.","It is not empty."], nwAlert.ERROR)
return
return False
elif nwItemS.itemType == nwItemType.ROOT:
logger.debug("User requested root folder %s deleted" % tHandle)
@@ -242,9 +252,9 @@ class GuiDocTree(QTreeWidget):
self.theProject.setProjectChanged(True)
else:
self.makeAlert(["Cannot delete root folder.","It is not empty."], nwAlert.ERROR)
return
return False
return
return True
def setTreeItemValues(self, tHandle):
+10 -5
View File
@@ -81,8 +81,9 @@ class GuiMainMenu(QMenuBar):
return
def updateSpellCheck(self):
self.toolsSpellCheck.setChecked(self.theProject.spellCheck)
logger.verbose("Spell check is set to %s" % str(self.theProject.spellCheck))
if self.theParent.hasProject:
self.toolsSpellCheck.setChecked(self.theProject.spellCheck)
logger.verbose("Spell check is set to %s" % str(self.theProject.spellCheck))
return
##
@@ -94,9 +95,12 @@ class GuiMainMenu(QMenuBar):
return
def _toggleSpellCheck(self):
self.theProject.setSpellCheck(self.toolsSpellCheck.isChecked())
self.theParent.docEditor.setSpellCheck(self.toolsSpellCheck.isChecked())
logger.verbose("Spell check is set to %s" % str(self.theProject.spellCheck))
if self.theParent.hasProject:
self.theProject.setSpellCheck(self.toolsSpellCheck.isChecked())
self.theParent.docEditor.setSpellCheck(self.toolsSpellCheck.isChecked())
logger.verbose("Spell check is set to %s" % str(self.theProject.spellCheck))
else:
self.toolsSpellCheck.setChecked(False)
return True
def _showAbout(self):
@@ -167,6 +171,7 @@ class GuiMainMenu(QMenuBar):
# Project > Project Settings
menuItem = QAction(QIcon.fromTheme("document-properties"), "Project Settings", self)
menuItem.setStatusTip("Project settings")
menuItem.setShortcut("Ctrl+Shift+,")
menuItem.triggered.connect(self.theParent.editProjectDialog)
self.projMenu.addAction(menuItem)
+26 -16
View File
@@ -260,6 +260,9 @@ class GuiMain(QMainWindow):
def saveProject(self):
"""Save the current project.
"""
if not self.hasProject:
return False
# If the project is new, it may not have a path, so we need one
if self.theProject.projPath is None:
projPath = self.saveProjectDialog()
@@ -279,22 +282,24 @@ class GuiMain(QMainWindow):
##
def closeDocument(self):
if self.docEditor.docChanged:
self.saveDocument()
self.theDocument.clearDocument()
self.docEditor.clearEditor()
if self.hasProject:
if self.docEditor.docChanged:
self.saveDocument()
self.theDocument.clearDocument()
self.docEditor.clearEditor()
return True
def openDocument(self, tHandle):
self.closeDocument()
self.docEditor.loadText(tHandle)
self.docEditor.changeWidth()
self.docEditor.setFocus()
self.theProject.setLastEdited(tHandle)
if self.hasProject:
self.closeDocument()
self.docEditor.loadText(tHandle)
self.docEditor.changeWidth()
self.docEditor.setFocus()
self.theProject.setLastEdited(tHandle)
return True
def saveDocument(self):
if self.theDocument.theItem is not None:
if self.theDocument.theItem is not None and self.hasProject:
docText = self.docEditor.getText()
cursPos = self.docEditor.getCursorPosition()
theItem = self.theDocument.theItem
@@ -382,6 +387,9 @@ class GuiMain(QMainWindow):
def rebuildIndex(self):
if not self.hasProject:
return False
logger.debug("Rebuilding indices ...")
self.treeView.saveTreeOrder()
@@ -427,7 +435,7 @@ class GuiMain(QMainWindow):
dlgProg.setValue(nItems)
return
return True
##
# Main Dialogs
@@ -468,14 +476,16 @@ class GuiMain(QMainWindow):
return None
def editProjectDialog(self):
dlgProj = GuiProjectEditor(self, self.theProject)
dlgProj.exec_()
self._setWindowTitle(self.theProject.projName)
if self.hasProject:
dlgProj = GuiProjectEditor(self, self.theProject)
dlgProj.exec_()
self._setWindowTitle(self.theProject.projName)
return True
def showTimeLineDialog(self):
dlgTLine = GuiTimeLineView(self, self.theProject, self.theIndex)
dlgTLine.exec_()
if self.hasProject:
dlgTLine = GuiTimeLineView(self, self.theProject, self.theIndex)
dlgTLine.exec_()
return True
def makeAlert(self, theMessage, theLevel=nwAlert.INFO):