Fixed indentation and slight refactor of spellchecking code.

This commit is contained in:
ricky 2015-11-22 08:58:18 -08:00
parent fe28b526d7
commit 838abdf7af
2 changed files with 21 additions and 36 deletions

View File

@ -43,6 +43,7 @@ class InputLineEdit(InputLineSpell):
key = event.key() key = event.key()
modifiers = event.modifiers() modifiers = event.modifiers()
bar = self.scroll_widget.verticalScrollBar() bar = self.scroll_widget.verticalScrollBar()
newline = (key == QtCore.Qt.Key_Enter or key == QtCore.Qt.Key_Return)
if modifiers == QtCore.Qt.ControlModifier: if modifiers == QtCore.Qt.ControlModifier:
if key == QtCore.Qt.Key_PageUp: if key == QtCore.Qt.Key_PageUp:
self.bufferSwitchPrev.emit() self.bufferSwitchPrev.emit()
@ -83,8 +84,7 @@ class InputLineEdit(InputLineSpell):
self._history_navigate(-1) self._history_navigate(-1)
elif key == QtCore.Qt.Key_Down: elif key == QtCore.Qt.Key_Down:
self._history_navigate(1) self._history_navigate(1)
elif ((key == QtCore.Qt.Key_Enter or key == QtCore.Qt.Key_Return) elif (newline and modifiers != QtCore.Qt.ShiftModifier):
and modifiers != QtCore.Qt.ShiftModifier):
self._input_return_pressed() self._input_return_pressed()
else: else:
InputLineSpell.keyPressEvent(self, event) InputLineSpell.keyPressEvent(self, event)

View File

@ -36,6 +36,7 @@ try:
except ImportError: except ImportError:
enchant = None enchant = None
class InputLineSpell(QtGui.QTextEdit): class InputLineSpell(QtGui.QTextEdit):
"""Chat area.""" """Chat area."""
@ -108,7 +109,7 @@ class InputLineSpell(QtGui.QTextEdit):
def initDict(self, lang=None): def initDict(self, lang=None):
if enchant: if enchant:
if lang == None: if lang is None:
# Default dictionary based on the current locale. # Default dictionary based on the current locale.
try: try:
self.spelldict = enchant.Dict() self.spelldict = enchant.Dict()
@ -132,7 +133,8 @@ class InputLineSpell(QtGui.QTextEdit):
# Rewrite the mouse event to a left button event so the cursor # Rewrite the mouse event to a left button event so the cursor
# is moved to the location of the pointer. # is moved to the location of the pointer.
event = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonPress, event = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonPress,
event.pos(), QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, event.pos(), QtCore.Qt.LeftButton,
QtCore.Qt.LeftButton,
QtCore.Qt.NoModifier) QtCore.Qt.NoModifier)
QtGui.QTextEdit.mousePressEvent(self, event) QtGui.QTextEdit.mousePressEvent(self, event)
@ -156,8 +158,6 @@ class InputLineSpell(QtGui.QTextEdit):
if enchant and self.spelldict: if enchant and self.spelldict:
if self.textCursor().hasSelection(): if self.textCursor().hasSelection():
text = unicode(self.textCursor().selectedText()) text = unicode(self.textCursor().selectedText())
#Add to dictionary
#Spell-checker options
if not self.spelldict.check(text): if not self.spelldict.check(text):
suggestions = self.spelldict.suggest(text) suggestions = self.spelldict.suggest(text)
if len(suggestions) != 0: if len(suggestions) != 0:
@ -167,12 +167,15 @@ class InputLineSpell(QtGui.QTextEdit):
action = SpellAction(word, popup_menu) action = SpellAction(word, popup_menu)
action.correct.connect(self.correctWord) action.correct.connect(self.correctWord)
popup_menu.insertAction(topAction, action) popup_menu.insertAction(topAction, action)
popup_menu.insertSeparator(topAction)
add = SpellAddAction(text, popup_menu)
add.add.connect(self.addWord)
popup_menu.insertAction(topAction, add)
popup_menu.insertSeparator(topAction)
addAction = QtGui.QAction("Add to dictionary", self)
addAction.triggered.connect(lambda: self.addWord(text))
popup_menu.insertAction(topAction, addAction)
# FIXME: add change dict and disable spellcheck options # FIXME: add change dict and disable spellcheck options
# spellmenu = QtGui.QMenu('Spell-checker options')
# for lang in enchant.list_languages():
# popup_menu.insertMenu(topAction, spellmenu)
popup_menu.exec_(event.globalPos()) popup_menu.exec_(event.globalPos())
@ -192,10 +195,6 @@ class InputLineSpell(QtGui.QTextEdit):
cursor.endEditBlock() cursor.endEditBlock()
def killDict(self):
self.highlighter.setDocument(None)
self.spelldict = None
class SpellHighlighter(QtGui.QSyntaxHighlighter): class SpellHighlighter(QtGui.QSyntaxHighlighter):
@ -221,8 +220,9 @@ class SpellHighlighter(QtGui.QSyntaxHighlighter):
for word_object in re.finditer(self.WORDS, text): for word_object in re.finditer(self.WORDS, text):
if not self.spelldict.check(word_object.group()): if not self.spelldict.check(word_object.group()):
self.setFormat(word_object.start(), word_len = word_object.end() - word_object.start()
word_object.end() - word_object.start(), format) self.setFormat(word_object.start(), word_len, format)
class SpellAction(QtGui.QAction): class SpellAction(QtGui.QAction):
@ -237,18 +237,3 @@ class SpellAction(QtGui.QAction):
self.triggered.connect(lambda x: self.correct.emit( self.triggered.connect(lambda x: self.correct.emit(
unicode(self.text()))) unicode(self.text())))
class SpellAddAction(QtGui.QAction):
'''
An action to add the given word to a dictionary.
'''
add = qt_compat.Signal(unicode)
def __init__(self, word, *args):
QtGui.QAction.__init__(self, "Add to dictionary", *args)
self._word = word
self.triggered.connect(lambda x: self.add.emit(
unicode(self._word)))