37 MaxSuggestions = 20 |
37 MaxSuggestions = 20 |
38 |
38 |
39 # default language to be used when no other is set |
39 # default language to be used when no other is set |
40 DefaultLanguage = None |
40 DefaultLanguage = None |
41 |
41 |
|
42 # default user lists |
|
43 DefaultUserWordList = None |
|
44 DefaultUserExceptionList = None |
|
45 |
42 def __init__(self): |
46 def __init__(self): |
43 """ |
47 """ |
44 Constructor |
48 Constructor |
45 """ |
49 """ |
46 self.__highlighter = EnchantHighlighter(self.document()) |
50 self.__highlighter = EnchantHighlighter(self.document()) |
47 try: |
51 try: |
48 # Start with a default dictionary based on the current locale. |
52 # Start with a default dictionary based on the current locale |
49 spellDict = ( |
53 # or the configured default language. |
50 enchant.Dict(SpellCheckMixin.DefaultLanguage) |
54 spellDict = enchant.DictWithPWL( |
51 if bool(SpellCheckMixin.DefaultLanguage) else |
55 SpellCheckMixin.DefaultLanguage, |
52 enchant.Dict() |
56 SpellCheckMixin.DefaultUserWordList, |
|
57 SpellCheckMixin.DefaultUserExceptionList |
53 ) |
58 ) |
54 except DictNotFoundError: |
59 except DictNotFoundError: |
55 # Use English dictionary if no locale dictionary is available |
60 # Use English dictionary if no locale dictionary is available |
56 # or the default one could not be found. |
61 # or the default one could not be found. |
57 spellDict = enchant.Dict("en") |
62 spellDict = enchant.DictWithPWL( |
|
63 "en", |
|
64 SpellCheckMixin.DefaultUserWordList, |
|
65 SpellCheckMixin.DefaultUserExceptionList |
|
66 ) |
58 self.__highlighter.setDict(spellDict) |
67 self.__highlighter.setDict(spellDict) |
59 |
68 |
60 def contextMenuEvent(self, evt): |
69 def contextMenuEvent(self, evt): |
61 """ |
70 """ |
62 Protected method to handle context menu events to add a spelling |
71 Protected method to handle context menu events to add a spelling |
151 act.setCheckable(True) |
160 act.setCheckable(True) |
152 act.setChecked(language.lower() == curLanguage) |
161 act.setChecked(language.lower() == curLanguage) |
153 act.setData(language) |
162 act.setData(language) |
154 languageMenu.addAction(act) |
163 languageMenu.addAction(act) |
155 |
164 |
|
165 # TODO: add action to add a word to the lists |
156 languageMenu.triggered.connect(self.__setLanguage) |
166 languageMenu.triggered.connect(self.__setLanguage) |
157 return languageMenu |
167 return languageMenu |
158 |
168 |
159 def __createFormatsMenu(self, parent=None): |
169 def __createFormatsMenu(self, parent=None): |
160 """ |
170 """ |
239 |
249 |
240 @param act reference to the selected action |
250 @param act reference to the selected action |
241 @type QAction |
251 @type QAction |
242 """ |
252 """ |
243 language = act.data() |
253 language = act.data() |
244 self.__highlighter.setDict(enchant.Dict(language)) |
254 self.__highlighter.setDict(enchant.Dict( |
|
255 language, |
|
256 SpellCheckMixin.DefaultUserWordList, |
|
257 SpellCheckMixin.DefaultUserExceptionList |
|
258 )) |
245 |
259 |
246 @pyqtSlot(QAction) |
260 @pyqtSlot(QAction) |
247 def __setFormat(self, act): |
261 def __setFormat(self, act): |
248 """ |
262 """ |
249 Private slot to set the selected document format. |
263 Private slot to set the selected document format. |
284 @type emchant.Dict |
298 @type emchant.Dict |
285 """ |
299 """ |
286 self.__highlighter.setDict(spellDict) |
300 self.__highlighter.setDict(spellDict) |
287 |
301 |
288 @classmethod |
302 @classmethod |
289 def setDefaultLanguage(cls, language): |
303 def setDefaultLanguage(cls, language, pwl="", pel=""): |
290 """ |
304 """ |
291 Class method to set the default spell-check language. |
305 Class method to set the default spell-check language. |
292 |
306 |
293 @param language language to be set as default |
307 @param language language to be set as default |
294 @type str |
308 @type str |
|
309 @param pwl file name of the personal word list |
|
310 @type str |
|
311 @param pel name of the personal exclude list |
|
312 @type str |
295 """ |
313 """ |
296 with contextlib.suppress(DictNotFoundError): |
314 with contextlib.suppress(DictNotFoundError): |
|
315 cls.DefaultUserWordList = pwl |
|
316 cls.DefaultUserExceptionList = pel |
|
317 |
297 # set default language only, if a dictionary is available |
318 # set default language only, if a dictionary is available |
298 enchant.Dict(language) |
319 enchant.Dict(language) |
299 cls.DefaultLanguage = language |
320 cls.DefaultLanguage = language |
300 |
321 |
301 class EnchantHighlighter(QSyntaxHighlighter): |
322 class EnchantHighlighter(QSyntaxHighlighter): |