Skip to content

Instantly share code, notes, and snippets.

@tritium21
Forked from ssokolow/spelltextedit.py
Created September 18, 2019 08:46
Show Gist options
  • Select an option

  • Save tritium21/f09ce417edd035fca488fd2b1f7ec2a8 to your computer and use it in GitHub Desktop.

Select an option

Save tritium21/f09ce417edd035fca488fd2b1f7ec2a8 to your computer and use it in GitHub Desktop.
Spell-checked QPlainTextEdit for PyQt 5.x using PyEnchant
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""QPlainTextEdit With In Line Spell Check
Source:
https://nachtimwald.com/2009/08/22/qplaintextedit-with-in-line-spell-check/
Copyright 2009 John Schember
Copyright 2018 Stephan Sokolow
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
__license__ = 'MIT'
__author__ = 'John Schember; Stephan Sokolow'
__docformat__ = 'restructuredtext en'
import re
import sys
import enchant
# pylint: disable=no-name-in-module
from PyQt5.Qt import Qt
from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat, QTextCursor
from PyQt5.QtWidgets import QAction, QApplication, QMenu, QPlainTextEdit
from PyQt5.QtCore import pyqtSignal
class SpellTextEdit(QPlainTextEdit):
"""QPlainTextEdit subclass which does spell-checking using PyEnchant"""
def __init__(self, *args):
QPlainTextEdit.__init__(self, *args)
# Default dictionary based on the current locale.
self.sp_dict = enchant.Dict()
self.highlighter = Highlighter(self.document())
self.highlighter.setDict(self.sp_dict)
def contextMenuEvent(self, event):
"""Custom context menu handler to add a spelling suggestions submenu"""
popup_menu = self.createStandardContextMenu()
# Select the word under the cursor.
cursor = self.cursorForPosition(event.pos())
cursor.select(QTextCursor.WordUnderCursor)
self.setTextCursor(cursor)
# Check if the selected word is misspelled and offer spelling
# suggestions if it is.
if cursor.hasSelection():
text = cursor.selectedText()
if not self.sp_dict.check(text):
spell_menu = QMenu('Spelling Suggestions')
for word in self.sp_dict.suggest(text):
action = SpellAction(word, spell_menu)
action.correct.connect(self.correctWord)
spell_menu.addAction(action)
# Only add the spelling suggests to the menu if there are
# suggestions.
if len(spell_menu.actions()) != 0:
popup_menu.insertSeparator(popup_menu.actions()[0])
popup_menu.insertMenu(popup_menu.actions()[0], spell_menu)
popup_menu.exec_(event.globalPos())
def correctWord(self, word):
"""Replaces the selected text with word."""
cursor = self.textCursor()
cursor.beginEditBlock()
cursor.removeSelectedText()
cursor.insertText(word)
cursor.endEditBlock()
class Highlighter(QSyntaxHighlighter):
"""QSyntaxHighlighter subclass which consults a PyEnchant dictionary"""
WORDS = r'(?iu)[\w\']+'
def __init__(self, *args):
QSyntaxHighlighter.__init__(self, *args)
self.sp_dict = None
def setDict(self, sp_dict):
"""Sets the spelling dictionary to be used"""
self.sp_dict = sp_dict
def highlightBlock(self, text):
"""Overridden QSyntaxHighlighter method to apply the highlight"""
if not self.sp_dict:
return
char_format = QTextCharFormat()
char_format.setUnderlineColor(Qt.red)
char_format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline)
for word_object in re.finditer(self.WORDS, text):
# Don't spell-check purely numeric "words"
if word_object.group().isdigit():
continue
if not self.sp_dict.check(word_object.group()):
self.setFormat(word_object.start(),
word_object.end() - word_object.start(), char_format)
class SpellAction(QAction): # pylint: disable=too-few-public-methods
"""A special QAction that returns the text in a signal."""
correct = pyqtSignal(str)
def __init__(self, *args):
QAction.__init__(self, *args)
self.triggered.connect(lambda x: self.correct.emit(
self.text()))
if __name__ == '__main__':
app = QApplication(sys.argv)
spellEdit = SpellTextEdit()
spellEdit.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment