Finish the word list dialog and make it possible to reload the user dictionary with pyenchant

This commit is contained in:
Veronica K. B. Olsen
2021-02-12 19:40:10 +01:00
parent 6e64ede90b
commit b115d4f02d
3 changed files with 88 additions and 8 deletions
+11 -1
View File
@@ -123,6 +123,7 @@ class NWSpellCheck():
if len(theLine) > 0 and theLine not in self.projDict: if len(theLine) > 0 and theLine not in self.projDict:
self.projDict.append(theLine) self.projDict.append(theLine)
logger.debug("Project word list contains %d words" % len(self.projDict)) logger.debug("Project word list contains %d words" % len(self.projDict))
except Exception: except Exception:
logger.error("Failed to load project word list") logger.error("Failed to load project word list")
nw.logException() nw.logException()
@@ -141,6 +142,7 @@ class NWSpellEnchant(NWSpellCheck):
def __init__(self): def __init__(self):
NWSpellCheck.__init__(self) NWSpellCheck.__init__(self)
logger.debug("Enchant spell checking activated") logger.debug("Enchant spell checking activated")
self.theBroker = None
return return
def setLanguage(self, theLang, projectDict=None): def setLanguage(self, theLang, projectDict=None):
@@ -150,9 +152,15 @@ class NWSpellEnchant(NWSpellCheck):
""" """
try: try:
import enchant import enchant
self.theDict = enchant.Dict(theLang) if self.theBroker is not None:
logger.verbose("Deleting old pyenchant Broker")
del self.theBroker
self.theBroker = enchant.Broker()
self.theDict = self.theBroker.request_dict(theLang)
self.spellLanguage = theLang self.spellLanguage = theLang
logger.debug("Enchant spell checking for language %s loaded" % theLang) logger.debug("Enchant spell checking for language %s loaded" % theLang)
except Exception: except Exception:
logger.error("Failed to load enchant spell checking for language %s" % theLang) logger.error("Failed to load enchant spell checking for language %s" % theLang)
self.theDict = NWSpellEnchantDummy() self.theDict = NWSpellEnchantDummy()
@@ -258,9 +266,11 @@ class NWSpellSimple(NWSpellCheck):
if len(theLine) == 0 or theLine.startswith("#"): if len(theLine) == 0 or theLine.startswith("#"):
continue continue
self.WORDS.append(theLine.strip().lower()) self.WORDS.append(theLine.strip().lower())
logger.debug("Spell check word list for language %s loaded" % theLang) logger.debug("Spell check word list for language %s loaded" % theLang)
logger.debug("Word list contains %d words" % len(self.WORDS)) logger.debug("Word list contains %d words" % len(self.WORDS))
self.spellLanguage = theLang self.spellLanguage = theLang
except Exception: except Exception:
logger.error("Failed to load spell check word list for language %s" % theLang) logger.error("Failed to load spell check word list for language %s" % theLang)
nw.logException() nw.logException()
+76 -6
View File
@@ -28,11 +28,13 @@ import nw
import logging import logging
import os import os
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QDialog, QDialogButtonBox, QVBoxLayout, QListWidget, QAbstractItemView QDialog, QDialogButtonBox, QVBoxLayout, QHBoxLayout, QListWidget,
QAbstractItemView, QPushButton, QLineEdit, QLabel
) )
from nw.constants import nwFiles from nw.constants import nwFiles, nwAlert
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -46,16 +48,18 @@ class GuiWordList(QDialog):
self.mainConf = nw.CONFIG self.mainConf = nw.CONFIG
self.theParent = theParent self.theParent = theParent
self.theTheme = theParent.theTheme
self.theProject = theProject self.theProject = theProject
self.optState = theProject.optState self.optState = theProject.optState
self.setWindowTitle("Project Word List") self.setWindowTitle("Project Word List")
wW = self.mainConf.pxInt(250) mS = self.mainConf.pxInt(250)
wH = self.mainConf.pxInt(300) wW = self.mainConf.pxInt(320)
wH = self.mainConf.pxInt(340)
self.setMinimumWidth(wW) self.setMinimumWidth(mS)
self.setMinimumHeight(wH) self.setMinimumHeight(mS)
self.resize( self.resize(
self.mainConf.pxInt(self.optState.getInt("GuiWordList", "winWidth", wW)), self.mainConf.pxInt(self.optState.getInt("GuiWordList", "winWidth", wW)),
self.mainConf.pxInt(self.optState.getInt("GuiWordList", "winHeight", wH)) self.mainConf.pxInt(self.optState.getInt("GuiWordList", "winHeight", wH))
@@ -64,10 +68,27 @@ class GuiWordList(QDialog):
# Main Widgets # Main Widgets
# ============ # ============
self.headLabel = QLabel("<b>Project Word List</b>")
self.listBox = QListWidget() self.listBox = QListWidget()
self.listBox.setDragDropMode(QAbstractItemView.NoDragDrop) self.listBox.setDragDropMode(QAbstractItemView.NoDragDrop)
self.listBox.setSortingEnabled(True) self.listBox.setSortingEnabled(True)
self.newEntry = QLineEdit()
self.addButton = QPushButton(self.theTheme.getIcon("add"), "")
self.addButton.setToolTip("Add new entry")
self.addButton.clicked.connect(self._doAdd)
self.delButton = QPushButton(self.theTheme.getIcon("remove"), "")
self.delButton.setToolTip("Delete selected entry")
self.delButton.clicked.connect(self._doDelete)
self.editBox = QHBoxLayout()
self.editBox.addWidget(self.newEntry, 1)
self.editBox.addWidget(self.addButton, 0)
self.editBox.addWidget(self.delButton, 0)
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Save | QDialogButtonBox.Close) self.buttonBox = QDialogButtonBox(QDialogButtonBox.Save | QDialogButtonBox.Close)
self.buttonBox.accepted.connect(self._doSave) self.buttonBox.accepted.connect(self._doSave)
self.buttonBox.rejected.connect(self._doClose) self.buttonBox.rejected.connect(self._doClose)
@@ -76,7 +97,11 @@ class GuiWordList(QDialog):
# ======== # ========
self.outerBox = QVBoxLayout() self.outerBox = QVBoxLayout()
self.outerBox.addWidget(self.headLabel)
self.outerBox.addSpacing(self.mainConf.pxInt(8))
self.outerBox.addWidget(self.listBox, 1) self.outerBox.addWidget(self.listBox, 1)
self.outerBox.addLayout(self.editBox, 0)
self.outerBox.addSpacing(self.mainConf.pxInt(12))
self.outerBox.addWidget(self.buttonBox, 0) self.outerBox.addWidget(self.buttonBox, 0)
self.setLayout(self.outerBox) self.setLayout(self.outerBox)
@@ -91,11 +116,56 @@ class GuiWordList(QDialog):
# Slots # Slots
## ##
def _doAdd(self):
"""Add a new word to the word list.
"""
newWord = self.newEntry.text().strip()
if newWord == "":
self.theParent.makeAlert("Cannot add a blank word.", nwAlert.ERROR)
return False
if self.listBox.findItems(newWord, Qt.MatchExactly):
self.theParent.makeAlert(
"The word '%s' is already in the word list." % newWord, nwAlert.ERROR
)
return False
self.listBox.addItem(newWord)
self.newEntry.setText("")
return True
def _doDelete(self):
"""Delete the selected item.
"""
selItem = self.listBox.selectedItems()
if selItem:
self.listBox.takeItem(self.listBox.row(selItem[0]))
return
def _doSave(self): def _doSave(self):
"""Save the new word list and close. """Save the new word list and close.
""" """
self._saveGuiSettings() self._saveGuiSettings()
dctFile = os.path.join(self.theProject.projMeta, nwFiles.PROJ_DICT)
tmpFile = dctFile + "~"
try:
with open(tmpFile, mode="w", encoding="utf8") as outFile:
for i in range(self.listBox.count()):
outFile.write(self.listBox.item(i).text() + "\n")
except Exception as e:
logger.error("Could not save new word list")
logger.error(str(e))
self.reject()
if os.path.isfile(dctFile):
os.unlink(dctFile)
os.rename(tmpFile, dctFile)
self.accept() self.accept()
return return
def _doClose(self): def _doClose(self):
+1 -1
View File
@@ -1029,7 +1029,7 @@ class GuiMain(QMainWindow):
if dlgWords.result() == QDialog.Accepted: if dlgWords.result() == QDialog.Accepted:
logger.debug("Reloading word list") logger.debug("Reloading word list")
# ToDo self.docEditor.setDictionaries()
return return