Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spellcheck updates #3

Merged
merged 3 commits into from
May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions nw/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
"""novelWriter Common Functions

novelWriter – Common Functions
================================
Various functions used multiple places

File History:
Created: 2019-05-12 [0.1.0]

"""

import logging
import nw

logger = logging.getLogger(__name__)

def checkString(checkValue, defaultValue, allowNone=False):
if allowNone:
if checkValue == None: return None
if checkValue == "None": return None
if isinstance(checkValue,str): return str(checkValue)
return defaultValue

def checkInt(checkValue, defaultValue, allowNone=False):
if allowNone:
if checkValue == None: return None
if checkValue == "None": return None
try:
return int(checkValue)
except:
return defaultValue

def checkBool(checkValue, defaultValue, allowNone=False):
if allowNone:
if checkValue == None: return None
if checkValue == "None": return None
if isinstance(checkValue, str):
if checkValue == "True":
return True
elif checkValue == "False":
return False
else:
return defaultValue
elif isinstance(checkValue, int):
if checkValue == 1:
return True
elif checkValue == 0:
return False
else:
return defaultValue
return defaultValue
26 changes: 23 additions & 3 deletions nw/gui/doceditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(self, theParent):
self.theParent = theParent
self.docChanged = False
self.pwlFile = None
self.spellCheck = False

# Document Variables
self.charCount = 0
Expand Down Expand Up @@ -105,7 +106,7 @@ def __init__(self, theParent):
def setDocumentChanged(self, bValue):
self.docChanged = bValue
self.theParent.statusBar.setDocumentStatus(self.docChanged)
return
return self.docChanged

def setText(self, theText):
self.setPlainText(theText)
Expand All @@ -120,7 +121,18 @@ def setPwl(self, pwlFile):
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()
Expand Down Expand Up @@ -154,6 +166,11 @@ def docAction(self, theAction):
elif theAction == nwDocAction.SEL_PARA: self._makeSelection(QTextCursor.BlockUnderCursor)
else:
logger.error("Unknown or unsupported document action %s" % str(theAction))
return False
return True

def rehighlightDocument(self):
self.hLight.rehighlight()
return

##
Expand Down Expand Up @@ -181,6 +198,9 @@ def _openSpellContext(self):

def _openContextMenu(self, thePos):

if not self.spellCheck:
return

theCursor = self.cursorForPosition(thePos)
theCursor.select(QTextCursor.WordUnderCursor)
theWord = theCursor.selectedText()
Expand Down Expand Up @@ -237,7 +257,7 @@ def _docChange(self, thePos, charsRemoved, charsAdded):
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):
Expand Down
41 changes: 23 additions & 18 deletions nw/gui/dochighlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@ def __init__(self, theDoc):
QSyntaxHighlighter.__init__(self, theDoc)

logger.debug("Initialising DocHighlighter ...")
self.mainConf = nw.CONFIG
self.theDoc = theDoc
self.theDict = None
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.colSpell = QColor(200, 46, 0)
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.colSpell = QColor(200, 46, 0)

self.hStyles = {
"header1" : self._makeFormat(self.colHead, "bold",1.8),
Expand Down Expand Up @@ -153,7 +154,11 @@ def __init__(self, theDoc):

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()
Expand Down Expand Up @@ -189,7 +194,7 @@ def highlightBlock(self, theText):

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)
Expand Down
40 changes: 38 additions & 2 deletions nw/gui/mainmenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def setAvailableRoot(self):
)
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)):
Expand All @@ -71,6 +80,11 @@ def updateRecentProjects(self):
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
##
Expand All @@ -80,6 +94,12 @@ def _menuExit(self):
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__, (
Expand Down Expand Up @@ -407,8 +427,24 @@ def _buildToolsMenu(self):
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)
Expand Down
3 changes: 2 additions & 1 deletion nw/gui/winmain.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,11 @@ def openProject(self, projFile=None):
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):
Expand Down
30 changes: 9 additions & 21 deletions nw/project/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
import logging
import nw

from os import path, mkdir
from lxml import etree
from datetime import datetime
from os import path, mkdir
from lxml import etree
from datetime import datetime

from nw.enum import nwItemType, nwItemClass, nwItemLayout
from nw.enum import nwItemType, nwItemClass, nwItemLayout
from nw.common import checkInt

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -153,7 +154,7 @@ def setLayout(self, theLayout):
return

def setStatus(self, theStatus):
theStatus = self._checkInt(theStatus,0)
theStatus = checkInt(theStatus,0)
self.itemStatus = theStatus
return

Expand All @@ -169,31 +170,18 @@ def setExpanded(self, expState):
##

def setCharCount(self, theCount):
theCount = self._checkInt(theCount,0)
theCount = checkInt(theCount,0)
self.charCount = theCount
return

def setWordCount(self, theCount):
theCount = self._checkInt(theCount,0)
theCount = checkInt(theCount,0)
self.wordCount = theCount
return

def setParaCount(self, theCount):
theCount = self._checkInt(theCount,0)
theCount = checkInt(theCount,0)
self.paraCount = theCount
return

##
# Internal Functions
##

def _checkInt(self,checkValue,defaultValue,allowNone=False):
if allowNone:
if checkValue == None: return None
if checkValue == "None": return None
try:
return int(checkValue)
except:
return defaultValue

# END Class NWItem
Loading