diff --git a/nw/gui/doceditor.py b/nw/gui/doceditor.py index 85ed1702..5ff38d78 100644 --- a/nw/gui/doceditor.py +++ b/nw/gui/doceditor.py @@ -38,6 +38,7 @@ class GuiDocEditor(QTextEdit): self.theParent = theParent self.docChanged = False self.pwlFile = None + self.spellCheck = False # Document Variables self.charCount = 0 @@ -105,7 +106,7 @@ class GuiDocEditor(QTextEdit): def setDocumentChanged(self, bValue): self.docChanged = bValue self.theParent.statusBar.setDocumentStatus(self.docChanged) - return + return self.docChanged def setText(self, theText): self.setPlainText(theText) @@ -120,7 +121,18 @@ class GuiDocEditor(QTextEdit): self.pwlFile = pwlFile self.theDict = enchant.DictWithPWL(self.mainConf.spellLanguage,pwlFile) self.hLight.setDict(self.theDict) - return + return True + + def setSpellCheck(self, theMode): + self.spellCheck = theMode + self.hLight.setSpellCheck(theMode) + self.rehighlightDocument() + return True + + def updateSpellCheck(self): + if self.spellCheck: + self.rehighlightDocument() + return True def getText(self): theText = self.toPlainText() @@ -154,7 +166,8 @@ class GuiDocEditor(QTextEdit): elif theAction == nwDocAction.SEL_PARA: self._makeSelection(QTextCursor.BlockUnderCursor) else: logger.error("Unknown or unsupported document action %s" % str(theAction)) - return + return False + return True def rehighlightDocument(self): self.hLight.rehighlight() @@ -185,6 +198,9 @@ class GuiDocEditor(QTextEdit): def _openContextMenu(self, thePos): + if not self.spellCheck: + return + theCursor = self.cursorForPosition(thePos) theCursor.select(QTextCursor.WordUnderCursor) theWord = theCursor.selectedText() @@ -241,7 +257,7 @@ class GuiDocEditor(QTextEdit): self.wcTimer.start() if self.mainConf.doReplace and not self.hasSelection: self._docAutoReplace(self.theDoc.findBlock(thePos)) - # logger.verbose("Doc change signal took %.3f µs" % ((time()-self.lastEdit)*1e6)) + logger.verbose("Doc change signal took %.3f µs" % ((time()-self.lastEdit)*1e6)) return def _docAutoReplace(self, theBlock): diff --git a/nw/gui/dochighlight.py b/nw/gui/dochighlight.py index 59923808..30787793 100644 --- a/nw/gui/dochighlight.py +++ b/nw/gui/dochighlight.py @@ -24,22 +24,23 @@ class GuiDocHighlighter(QSyntaxHighlighter): QSyntaxHighlighter.__init__(self, theDoc) logger.debug("Initialising DocHighlighter ...") - self.mainConf = nw.CONFIG - self.theDoc = theDoc - self.theDict = None - self.hRules = [] + self.mainConf = nw.CONFIG + self.theDoc = theDoc + self.theDict = None + self.spellCheck = False + self.hRules = [] - self.colHead = QColor( 0,155,200) - self.colHeadH = QColor( 0,105,135) - self.colEmph = QColor(200,120, 0) - self.colDialN = QColor(200, 46, 0) - self.colDialD = QColor(184,200, 0) - self.colDialS = QColor(136,200, 0) - self.colComm = QColor(150,150,150) - self.colKey = QColor(200, 46, 0) - self.colVal = QColor(184,200, 0) + self.colHead = QColor( 0,155,200) + self.colHeadH = QColor( 0,105,135) + self.colEmph = QColor(200,120, 0) + self.colDialN = QColor(200, 46, 0) + self.colDialD = QColor(184,200, 0) + self.colDialS = QColor(136,200, 0) + self.colComm = QColor(150,150,150) + self.colKey = QColor(200, 46, 0) + self.colVal = QColor(184,200, 0) - self.colSpell = QColor(200, 46, 0) + self.colSpell = QColor(200, 46, 0) self.hStyles = { "header1" : self._makeFormat(self.colHead, "bold",1.8), @@ -153,7 +154,11 @@ class GuiDocHighlighter(QSyntaxHighlighter): def setDict(self, theDict): self.theDict = theDict - return + return True + + def setSpellCheck(self, theMode): + self.spellCheck = theMode + return True def _makeFormat(self, fmtCol=None, fmtStyle=None, fmtSize=None): theFormat = QTextCharFormat() @@ -189,7 +194,7 @@ class GuiDocHighlighter(QSyntaxHighlighter): self.setCurrentBlockState(0) - if self.theDict is None: + if self.theDict is None or not self.spellCheck: return rxSpell = self.spellRx.globalMatch(theText.replace("_"," "), 0) diff --git a/nw/gui/mainmenu.py b/nw/gui/mainmenu.py index fdd0c3a1..488cf8ff 100644 --- a/nw/gui/mainmenu.py +++ b/nw/gui/mainmenu.py @@ -61,6 +61,15 @@ class GuiMainMenu(QMenuBar): ) return + ## + # Update Menu on Settings Changed + ## + + def updateMenu(self): + self.updateRecentProjects() + self.updateSpellCheck() + return + def updateRecentProjects(self): self.recentMenu.clear() for n in range(len(self.mainConf.recentList)): @@ -71,6 +80,11 @@ class GuiMainMenu(QMenuBar): self.recentMenu.addAction(menuItem) return + def updateSpellCheck(self): + self.toolsSpellCheck.setChecked(self.theProject.spellCheck) + logger.verbose("Spell check is set to %s" % str(self.theProject.spellCheck)) + return + ## # Menu Action ## @@ -80,6 +94,12 @@ class GuiMainMenu(QMenuBar): qApp.quit() return True + 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)) + return + def _showAbout(self): msgBox = QMessageBox() msgBox.about(self.theParent, "About %s" % nw.__package__, ( @@ -407,8 +427,24 @@ class GuiMainMenu(QMenuBar): self.toolsMoveDown.triggered.connect(lambda : self._moveTreeItem(1)) self.toolsMenu.addAction(self.toolsMoveDown) - # # Tools > Separator - # self.toolsMenu.addSeparator() + # Tools > Separator + self.toolsMenu.addSeparator() + + # Tools > Toggle Spell Check + self.toolsSpellCheck = QAction("Check Spelling", self) + self.toolsSpellCheck.setStatusTip("Toggle Check Spelling") + self.toolsSpellCheck.setCheckable(True) + self.toolsSpellCheck.setChecked(self.theProject.spellCheck) + self.toolsSpellCheck.toggled.connect(self._toggleSpellCheck) + self.toolsSpellCheck.setShortcut("Ctrl+F7") + self.toolsMenu.addAction(self.toolsSpellCheck) + + # Tools > Update Spell Check + menuItem = QAction(QIcon.fromTheme("tools-check-spelling"), "Re-Run Spell Check", self) + menuItem.setStatusTip("Rus the Spell Checker on Current Document") + menuItem.setShortcut("F7") + menuItem.triggered.connect(self.theParent.docEditor.updateSpellCheck) + self.toolsMenu.addAction(menuItem) # # Tools > Settings # menuItem = QAction(QIcon.fromTheme("preferences-system"), "Preferences", self) diff --git a/nw/gui/winmain.py b/nw/gui/winmain.py index ab12372f..673fdb27 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/winmain.py @@ -172,10 +172,11 @@ class GuiMain(QMainWindow): self.treeView.clearTree() self.theProject.openProject(projFile) self.treeView.buildTree() - self.mainMenu.updateRecentProjects() self._setWindowTitle(self.theProject.projName) self._makeStatusIcons() self.docEditor.setPwl(path.join(self.theProject.projMeta,"wordlist.txt")) + self.docEditor.setSpellCheck(self.theProject.spellCheck) + self.mainMenu.updateMenu() return True def saveProject(self): diff --git a/nw/project/project.py b/nw/project/project.py index 1450aad8..23821055 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -313,6 +313,11 @@ class NWProject(): self.setProjectChanged(True) return True + def setSpellCheck(self, theMode): + self.spellCheck = theMode + self.setProjectChanged(True) + return True + def setTreeOrder(self, newOrder): if len(self.treeOrder) != len(newOrder): logger.warning("Size of new and old tree order does not match")