|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2008 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the spell checking dialog. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import pyqtSlot |
|
13 from PyQt5.QtWidgets import QDialog |
|
14 |
|
15 from .Ui_SpellCheckingDialog import Ui_SpellCheckingDialog |
|
16 |
|
17 import Utilities |
|
18 |
|
19 |
|
20 class SpellCheckingDialog(QDialog, Ui_SpellCheckingDialog): |
|
21 """ |
|
22 Class implementing the spell checking dialog. |
|
23 """ |
|
24 def __init__(self, spellChecker, startPos, endPos, parent=None): |
|
25 """ |
|
26 Constructor |
|
27 |
|
28 @param spellChecker reference to the spell checker (SpellChecker) |
|
29 @param startPos position to start spell checking (integer) |
|
30 @param endPos end position for spell checking (integer) |
|
31 @param parent reference to the parent widget (QWidget) |
|
32 """ |
|
33 super(SpellCheckingDialog, self).__init__(parent) |
|
34 self.setupUi(self) |
|
35 |
|
36 self.__spell = spellChecker |
|
37 self.languageLabel.setText( |
|
38 "<b>{0}</b>".format(self.__spell.getLanguage())) |
|
39 if not self.__spell.initCheck(startPos, endPos): |
|
40 self.__enableButtons(False) |
|
41 else: |
|
42 self.__advance() |
|
43 |
|
44 def __enableButtons(self, enable): |
|
45 """ |
|
46 Private method to set the buttons enabled state. |
|
47 |
|
48 @param enable enable state (boolean) |
|
49 """ |
|
50 self.addButton.setEnabled(enable) |
|
51 self.ignoreButton.setEnabled(enable) |
|
52 self.ignoreAllButton.setEnabled(enable) |
|
53 self.replaceButton.setEnabled(enable) |
|
54 self.replaceAllButton.setEnabled(enable) |
|
55 |
|
56 def __advance(self): |
|
57 """ |
|
58 Private method to advance to the next error. |
|
59 """ |
|
60 try: |
|
61 next(self.__spell) |
|
62 except StopIteration: |
|
63 self.__enableButtons(False) |
|
64 self.contextLabel.setText("") |
|
65 self.changeEdit.setText("") |
|
66 self.suggestionsList.clear() |
|
67 return |
|
68 |
|
69 self.__enableButtons(True) |
|
70 self.word, self.wordStart, self.wordEnd = self.__spell.getError() |
|
71 lcontext, rcontext = self.__spell.getContext( |
|
72 self.wordStart, self.wordEnd) |
|
73 self.changeEdit.setText(self.word) |
|
74 self.contextLabel.setText( |
|
75 '{0}<font color="#FF0000">{1}</font>{2}'.format( |
|
76 Utilities.html_encode(lcontext), |
|
77 self.word, |
|
78 Utilities.html_encode(rcontext))) |
|
79 suggestions = self.__spell.getSuggestions(self.word) |
|
80 self.suggestionsList.clear() |
|
81 self.suggestionsList.addItems(suggestions) |
|
82 |
|
83 @pyqtSlot(str) |
|
84 def on_changeEdit_textChanged(self, text): |
|
85 """ |
|
86 Private method to handle a change of the replacement text. |
|
87 |
|
88 @param text contents of the line edit (string) |
|
89 """ |
|
90 self.replaceButton.setEnabled(text != "") |
|
91 self.replaceAllButton.setEnabled(text != "") |
|
92 |
|
93 @pyqtSlot(str) |
|
94 def on_suggestionsList_currentTextChanged(self, currentText): |
|
95 """ |
|
96 Private method to handle the selection of a suggestion. |
|
97 |
|
98 @param currentText the currently selected text (string) |
|
99 """ |
|
100 if currentText: |
|
101 self.changeEdit.setText(currentText) |
|
102 |
|
103 @pyqtSlot() |
|
104 def on_ignoreButton_clicked(self): |
|
105 """ |
|
106 Private slot to ignore the found error. |
|
107 """ |
|
108 self.__advance() |
|
109 |
|
110 @pyqtSlot() |
|
111 def on_ignoreAllButton_clicked(self): |
|
112 """ |
|
113 Private slot to always ignore the found error. |
|
114 """ |
|
115 self.__spell.ignoreAlways() |
|
116 self.__advance() |
|
117 |
|
118 @pyqtSlot() |
|
119 def on_addButton_clicked(self): |
|
120 """ |
|
121 Private slot to add the current word to the personal word list. |
|
122 """ |
|
123 self.__spell.add() |
|
124 self.__advance() |
|
125 |
|
126 @pyqtSlot() |
|
127 def on_replaceButton_clicked(self): |
|
128 """ |
|
129 Private slot to replace the current word with the given replacement. |
|
130 """ |
|
131 self.__spell.replace(self.changeEdit.text()) |
|
132 self.__advance() |
|
133 |
|
134 @pyqtSlot() |
|
135 def on_replaceAllButton_clicked(self): |
|
136 """ |
|
137 Private slot to replace the current word with the given replacement. |
|
138 """ |
|
139 self.__spell.replaceAlways(self.changeEdit.text()) |
|
140 self.__advance() |