spelltextedit: code standards

This commit is contained in:
Andreas Preikschat 2013-03-07 09:58:12 +01:00
parent 32ab855eb1
commit cbb7de3928
1 changed files with 33 additions and 37 deletions

View File

@ -41,8 +41,7 @@ try:
except ImportError: except ImportError:
ENCHANT_AVAILABLE = False ENCHANT_AVAILABLE = False
# based on code from # based on code from http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check
# http://john.nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
@ -56,19 +55,19 @@ class SpellTextEdit(QtGui.QPlainTextEdit):
""" """
Spell checking widget based on QPlanTextEdit. Spell checking widget based on QPlanTextEdit.
""" """
def __init__(self, parent=None, formattingTagsAllowed=True): def __init__(self, parent=None, formatting_tags_allowed=True):
""" """
Constructor. Constructor.
""" """
global ENCHANT_AVAILABLE global ENCHANT_AVAILABLE
QtGui.QPlainTextEdit.__init__(self, parent) QtGui.QPlainTextEdit.__init__(self, parent)
self.formattingTagsAllowed = formattingTagsAllowed self.formatting_tags_allowed = formatting_tags_allowed
# Default dictionary based on the current locale. # Default dictionary based on the current locale.
if ENCHANT_AVAILABLE: if ENCHANT_AVAILABLE:
try: try:
self.dictionary = enchant.Dict() self.dictionary = enchant.Dict()
self.highlighter = Highlighter(self.document()) self.highlighter = Highlighter(self.document())
self.highlighter.spellingDictionary = self.dictionary self.highlighter.spelling_dictionary = self.dictionary
except (Error, DictNotFoundError): except (Error, DictNotFoundError):
ENCHANT_AVAILABLE = False ENCHANT_AVAILABLE = False
log.debug(u'Could not load default dictionary') log.debug(u'Could not load default dictionary')
@ -78,8 +77,7 @@ class SpellTextEdit(QtGui.QPlainTextEdit):
Handle mouse clicks within the text edit region. Handle mouse clicks within the text edit region.
""" """
if event.button() == QtCore.Qt.RightButton: if event.button() == QtCore.Qt.RightButton:
# Rewrite the mouse event to a left button event so the cursor is # Rewrite the mouse event to a left button event so the cursor is moved to the location of the pointer.
# 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, QtCore.Qt.NoModifier) event.pos(), QtCore.Qt.LeftButton, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier)
QtGui.QPlainTextEdit.mousePressEvent(self, event) QtGui.QPlainTextEdit.mousePressEvent(self, event)
@ -88,7 +86,7 @@ class SpellTextEdit(QtGui.QPlainTextEdit):
""" """
Provide the context menu for the text edit region. Provide the context menu for the text edit region.
""" """
popupMenu = self.createStandardContextMenu() popup_menu = self.createStandardContextMenu()
# Select the word under the cursor. # Select the word under the cursor.
cursor = self.textCursor() cursor = self.textCursor()
# only select text if not already selected # only select text if not already selected
@ -97,14 +95,13 @@ class SpellTextEdit(QtGui.QPlainTextEdit):
self.setTextCursor(cursor) self.setTextCursor(cursor)
# Add menu with available languages. # Add menu with available languages.
if ENCHANT_AVAILABLE: if ENCHANT_AVAILABLE:
lang_menu = QtGui.QMenu( lang_menu = QtGui.QMenu(translate('OpenLP.SpellTextEdit', 'Language:'))
translate('OpenLP.SpellTextEdit', 'Language:'))
for lang in enchant.list_languages(): for lang in enchant.list_languages():
action = create_action(lang_menu, lang, text=lang, checked=lang == self.dictionary.tag) action = create_action(lang_menu, lang, text=lang, checked=lang == self.dictionary.tag)
lang_menu.addAction(action) lang_menu.addAction(action)
popupMenu.insertSeparator(popupMenu.actions()[0]) popup_menu.insertSeparator(popup_menu.actions()[0])
popupMenu.insertMenu(popupMenu.actions()[0], lang_menu) popup_menu.insertMenu(popup_menu.actions()[0], lang_menu)
lang_menu.triggered.connect(self.setLanguage) lang_menu.triggered.connect(self.set_language)
# Check if the selected word is misspelled and offer spelling suggestions if it is. # Check if the selected word is misspelled and offer spelling suggestions if it is.
if ENCHANT_AVAILABLE and self.textCursor().hasSelection(): if ENCHANT_AVAILABLE and self.textCursor().hasSelection():
text = self.textCursor().selectedText() text = self.textCursor().selectedText()
@ -112,22 +109,22 @@ class SpellTextEdit(QtGui.QPlainTextEdit):
spell_menu = QtGui.QMenu(translate('OpenLP.SpellTextEdit', 'Spelling Suggestions')) spell_menu = QtGui.QMenu(translate('OpenLP.SpellTextEdit', 'Spelling Suggestions'))
for word in self.dictionary.suggest(text): for word in self.dictionary.suggest(text):
action = SpellAction(word, spell_menu) action = SpellAction(word, spell_menu)
action.correct.connect(self.correctWord) action.correct.connect(self.correct_word)
spell_menu.addAction(action) spell_menu.addAction(action)
# Only add the spelling suggests to the menu if there are suggestions. # Only add the spelling suggests to the menu if there are suggestions.
if spell_menu.actions(): if spell_menu.actions():
popupMenu.insertMenu(popupMenu.actions()[0], spell_menu) popup_menu.insertMenu(popup_menu.actions()[0], spell_menu)
tagMenu = QtGui.QMenu(translate('OpenLP.SpellTextEdit', 'Formatting Tags')) tag_menu = QtGui.QMenu(translate('OpenLP.SpellTextEdit', 'Formatting Tags'))
if self.formattingTagsAllowed: if self.formatting_tags_allowed:
for html in FormattingTags.get_html_tags(): for html in FormattingTags.get_html_tags():
action = SpellAction(html[u'desc'], tagMenu) action = SpellAction(html[u'desc'], tag_menu)
action.correct.connect(self.htmlTag) action.correct.connect(self.html_tag)
tagMenu.addAction(action) tag_menu.addAction(action)
popupMenu.insertSeparator(popupMenu.actions()[0]) popup_menu.insertSeparator(popup_menu.actions()[0])
popupMenu.insertMenu(popupMenu.actions()[0], tagMenu) popup_menu.insertMenu(popup_menu.actions()[0], tag_menu)
popupMenu.exec_(event.globalPos()) popup_menu.exec_(event.globalPos())
def setLanguage(self, action): def set_language(self, action):
""" """
Changes the language for this spelltextedit. Changes the language for this spelltextedit.
@ -135,11 +132,11 @@ class SpellTextEdit(QtGui.QPlainTextEdit):
The action. The action.
""" """
self.dictionary = enchant.Dict(action.text()) self.dictionary = enchant.Dict(action.text())
self.highlighter.spellingDictionary = self.dictionary self.highlighter.spelling_dictionary = self.dictionary
self.highlighter.highlightBlock(self.toPlainText()) self.highlighter.highlight_block(self.toPlainText())
self.highlighter.rehighlight() self.highlighter.rehighlight()
def correctWord(self, word): def correct_word(self, word):
""" """
Replaces the selected text with word. Replaces the selected text with word.
""" """
@ -149,7 +146,7 @@ class SpellTextEdit(QtGui.QPlainTextEdit):
cursor.insertText(word) cursor.insertText(word)
cursor.endEditBlock() cursor.endEditBlock()
def htmlTag(self, tag): def html_tag(self, tag):
""" """
Replaces the selected text with word. Replaces the selected text with word.
""" """
@ -181,22 +178,21 @@ class Highlighter(QtGui.QSyntaxHighlighter):
Constructor Constructor
""" """
QtGui.QSyntaxHighlighter.__init__(self, *args) QtGui.QSyntaxHighlighter.__init__(self, *args)
self.spellingDictionary = None self.spelling_dictionary = None
def highlightBlock(self, text): def highlight_block(self, text):
""" """
Highlight misspelt words in a block of text Highlight misspelt words in a block of text
""" """
if not self.spellingDictionary: if not self.spelling_dictionary:
return return
text = unicode(text) text = unicode(text)
charFormat = QtGui.QTextCharFormat() char_format = QtGui.QTextCharFormat()
charFormat.setUnderlineColor(QtCore.Qt.red) char_format.setUnderlineColor(QtCore.Qt.red)
charFormat.setUnderlineStyle(QtGui.QTextCharFormat.SpellCheckUnderline) char_format.setUnderlineStyle(QtGui.QTextCharFormat.SpellCheckUnderline)
for word_object in re.finditer(self.WORDS, text): for word_object in re.finditer(self.WORDS, text):
if not self.spellingDictionary.check(word_object.group()): if not self.spelling_dictionary.check(word_object.group()):
self.setFormat(word_object.start(), self.setFormat(word_object.start(), word_object.end() - word_object.start(), char_format)
word_object.end() - word_object.start(), charFormat)
class SpellAction(QtGui.QAction): class SpellAction(QtGui.QAction):