Add spell checking support
guix-full.manifest
| 1 | 1 | (specifications->manifest | |
| 2 | 2 | '("arc-icon-theme"; for displaying standard icons | |
| 3 | + | "aspell" | |
| 4 | + | "aspell-dict-en" | |
| 5 | + | "aspell-dict-fr" | |
| 3 | 6 | "coreutils" | |
| 4 | 7 | "diffutils" | |
| 8 | + | "enchant" | |
| 5 | 9 | "git" | |
| 6 | 10 | "gnupg" | |
| 7 | 11 | "grep" | |
| 8 | 12 | "guix" | |
| 9 | 13 | "gzip" | |
| 14 | + | "hunspell" | |
| 15 | + | "hunspell-dict-en-us" | |
| 16 | + | "hunspell-dict-fr" | |
| 10 | 17 | "less" | |
| 11 | 18 | "make" | |
| 12 | 19 | "neovim" | |
… | |||
| 17 | 24 | "python-lxml" | |
| 18 | 25 | "python-neovim" | |
| 19 | 26 | "python-polib" | |
| 27 | + | "python-pyenchant" | |
| 20 | 28 | "python-pyqt" | |
| 21 | 29 | "python-ruamel.yaml" | |
| 22 | 30 | "python-sphinx" | |
guix.manifest
| 6 | 6 | "python-dateutil" | |
| 7 | 7 | "python-lxml" | |
| 8 | 8 | "python-polib" | |
| 9 | + | "python-pyenchant" | |
| 9 | 10 | "python-pyqt" | |
| 10 | 11 | "python-ruamel.yaml" | |
| 11 | 12 | "python-sphinx" |
offlate/spellcheckedit.py unknown status 1
| 1 | + | from PyQt5.QtWidgets import * | |
| 2 | + | from PyQt5.QtGui import * | |
| 3 | + | from PyQt5.QtCore import * | |
| 4 | + | ||
| 5 | + | import enchant | |
| 6 | + | import re | |
| 7 | + | import sys | |
| 8 | + | ||
| 9 | + | class SpellCheckEdit(QTextEdit): | |
| 10 | + | def __init__(self, lang, *args): | |
| 11 | + | QTextEdit.__init__(self, *args) | |
| 12 | + | self.dict = enchant.Dict(lang) | |
| 13 | + | self.highlighter = Highlighter(self.document()) | |
| 14 | + | self.highlighter.setDict(self.dict) | |
| 15 | + | ||
| 16 | + | def contextMenuEvent(self, event): | |
| 17 | + | popup_menu = self.createStandardContextMenu() | |
| 18 | + | ||
| 19 | + | # Select the word under the cursor. | |
| 20 | + | cursor = self.textCursor() | |
| 21 | + | cursor.select(QTextCursor.WordUnderCursor) | |
| 22 | + | self.setTextCursor(cursor) | |
| 23 | + | ||
| 24 | + | if self.textCursor().hasSelection(): | |
| 25 | + | text = self.textCursor().selectedText() | |
| 26 | + | if not self.dict.check(text): | |
| 27 | + | spell_menu = QMenu(self.tr('Spelling Suggestions')) | |
| 28 | + | nospell = QAction(self.tr('No Suggestions')) | |
| 29 | + | nospell.setEnabled(False) | |
| 30 | + | for word in self.dict.suggest(text): | |
| 31 | + | action = QAction(word) | |
| 32 | + | action.triggered.connect((lambda word: (lambda : self.correctWord(word)))(word)) | |
| 33 | + | spell_menu.addAction(action) | |
| 34 | + | # If there are suggestions, use the spell_menu. Otherwise, show | |
| 35 | + | # there is no suggestion. | |
| 36 | + | popup_menu.insertSeparator(popup_menu.actions()[0]) | |
| 37 | + | if len(spell_menu.actions()) != 0: | |
| 38 | + | popup_menu.insertMenu(popup_menu.actions()[0], spell_menu) | |
| 39 | + | else: | |
| 40 | + | popup_menu.insertAction(popup_menu.actions()[0], nospell) | |
| 41 | + | ||
| 42 | + | popup_menu.exec_(event.globalPos()) | |
| 43 | + | ||
| 44 | + | def correctWord(self, word): | |
| 45 | + | cursor = self.textCursor() | |
| 46 | + | cursor.beginEditBlock() | |
| 47 | + | ||
| 48 | + | cursor.removeSelectedText() | |
| 49 | + | cursor.insertText(word) | |
| 50 | + | ||
| 51 | + | cursor.endEditBlock() | |
| 52 | + | ||
| 53 | + | class Highlighter(QSyntaxHighlighter): | |
| 54 | + | def __init__(self, *args): | |
| 55 | + | QSyntaxHighlighter.__init__(self, *args) | |
| 56 | + | ||
| 57 | + | def setDict(self, dico): | |
| 58 | + | self.dict = dico | |
| 59 | + | ||
| 60 | + | def highlightBlock(self, text): | |
| 61 | + | if self.dict == None: | |
| 62 | + | return | |
| 63 | + | ||
| 64 | + | format = QTextCharFormat() | |
| 65 | + | format.setUnderlineColor(Qt.red) | |
| 66 | + | format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline) | |
| 67 | + | ||
| 68 | + | for word_object in re.finditer(r'\b[^\W\d_]+\b', text): | |
| 69 | + | if not self.dict.check(word_object.group()): | |
| 70 | + | self.setFormat(word_object.start(), word_object.end() - word_object.start(), format) | |
| 71 | + | ||
| 72 | + | ||
| 73 | + | if __name__ == '__main__': | |
| 74 | + | app = QApplication(sys.argv) | |
| 75 | + | w = SpellCheckEdit() | |
| 76 | + | w.show() | |
| 77 | + | sys.exit(app.exec_()) |
offlate/window.py
| 7 | 7 | import re | |
| 8 | 8 | ||
| 9 | 9 | from .manager import ProjectManager | |
| 10 | + | from .spellcheckedit import SpellCheckEdit | |
| 10 | 11 | ||
| 11 | 12 | class ProjectTab(QTabWidget): | |
| 12 | 13 | def __init__(self, parent = None): | |
… | |||
| 111 | 112 | self.msgid.addTab(plural, self.tr("Plural")) | |
| 112 | 113 | i = 0 | |
| 113 | 114 | for msgstr in data.msgstrs: | |
| 114 | - | form = QTextEdit() | |
| 115 | + | form = SpellCheckEdit(self.project.lang) | |
| 115 | 116 | form.setText(msgstr) | |
| 116 | 117 | form.textChanged.connect(self.modify) | |
| 117 | 118 | self.msgstr.addTab(form, str(i)) | |
… | |||
| 119 | 120 | else: | |
| 120 | 121 | self.msgid = QTextEdit() | |
| 121 | 122 | self.msgid.setReadOnly(True) | |
| 122 | - | self.msgstr = QTextEdit() | |
| 123 | + | self.msgstr = SpellCheckEdit(self.project.lang) | |
| 123 | 124 | self.msgid.setText(data.msgids[0]) | |
| 124 | 125 | self.msgstr.setText(data.msgstrs[0]) | |
| 125 | 126 | self.msgstr.textChanged.connect(self.modify) | |
… | |||
| 129 | 130 | def modify(self): | |
| 130 | 131 | item = self.treeWidget.currentItem() | |
| 131 | 132 | data = item.data(0, Qt.UserRole) | |
| 132 | - | if self.msgstr.__class__.__name__ == "QTextEdit": | |
| 133 | + | if self.msgstr.__class__.__name__ == "SpellCheckEdit": | |
| 133 | 134 | msgstr = self.msgstr.toPlainText() | |
| 134 | 135 | data.update(0, msgstr) | |
| 135 | 136 | item.setText(1, msgstr) | |