|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2017 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Web Browser Spell Checking configuration page. |
|
8 """ |
|
9 |
|
10 import os |
|
11 import contextlib |
|
12 |
|
13 from PyQt6.QtCore import ( |
|
14 pyqtSlot, Qt, QCoreApplication, QDir, QLibraryInfo, QLocale |
|
15 ) |
|
16 from PyQt6.QtWidgets import QListWidgetItem |
|
17 |
|
18 from .ConfigurationPageBase import ConfigurationPageBase |
|
19 from .Ui_WebBrowserSpellCheckingPage import Ui_WebBrowserSpellCheckingPage |
|
20 |
|
21 import Preferences |
|
22 import Globals |
|
23 |
|
24 |
|
25 class WebBrowserSpellCheckingPage(ConfigurationPageBase, |
|
26 Ui_WebBrowserSpellCheckingPage): |
|
27 """ |
|
28 Class implementing the Web Browser Spell Checking page. |
|
29 """ |
|
30 def __init__(self): |
|
31 """ |
|
32 Constructor |
|
33 """ |
|
34 super().__init__() |
|
35 self.setupUi(self) |
|
36 self.setObjectName("WebBrowserSpellCheckingPage") |
|
37 |
|
38 # set initial values |
|
39 self.spellCheckEnabledCheckBox.setChecked( |
|
40 Preferences.getWebBrowser("SpellCheckEnabled")) |
|
41 self.on_spellCheckEnabledCheckBox_clicked() |
|
42 |
|
43 if Globals.isMacPlatform(): |
|
44 self.__dictionaryDirectories = { |
|
45 QDir.cleanPath( |
|
46 QCoreApplication.applicationDirPath() + |
|
47 "/../Resources/qtwebengine_dictionaries"), |
|
48 QDir.cleanPath( |
|
49 QCoreApplication.applicationDirPath() + |
|
50 "/../Frameworks/QtWebEngineCore.framework" |
|
51 "/Resources/qtwebengine_dictionaries"), |
|
52 } |
|
53 else: |
|
54 self.__dictionaryDirectories = { |
|
55 QDir.cleanPath( |
|
56 QCoreApplication.applicationDirPath() + |
|
57 "/qtwebengine_dictionaries"), |
|
58 QDir.cleanPath( |
|
59 QLibraryInfo.path( |
|
60 QLibraryInfo.LibraryPath.DataPath) + |
|
61 "/qtwebengine_dictionaries" |
|
62 ), |
|
63 } |
|
64 self.spellCheckDictionaryDirectoriesEdit.setPlainText( |
|
65 "\n".join(self.__dictionaryDirectories)) |
|
66 # try to create these directories, if they don't exist |
|
67 for directory in self.__dictionaryDirectories: |
|
68 if not os.path.exists(directory): |
|
69 with contextlib.suppress(os.error): |
|
70 os.makedirs(directory) |
|
71 |
|
72 self.__writeableDirectories = [] |
|
73 for directory in self.__dictionaryDirectories: |
|
74 if os.access(directory, os.W_OK): |
|
75 self.__writeableDirectories.append(directory) |
|
76 self.manageDictionariesButton.setEnabled( |
|
77 bool(self.__writeableDirectories)) |
|
78 |
|
79 self.__populateDictionariesList() |
|
80 |
|
81 def __populateDictionariesList(self): |
|
82 """ |
|
83 Private method to populate the spell checking dictionaries list. |
|
84 """ |
|
85 self.spellCheckLanguagesList.clear() |
|
86 |
|
87 for path in self.__dictionaryDirectories: |
|
88 directory = QDir(path) |
|
89 fileNames = directory.entryList(["*.bdic"]) |
|
90 for fileName in fileNames: |
|
91 lang = fileName[:-5] |
|
92 langStr = self.__createLanguageString(lang) |
|
93 if self.spellCheckLanguagesList.findItems( |
|
94 langStr, Qt.MatchFlag.MatchExactly |
|
95 ): |
|
96 continue |
|
97 |
|
98 itm = QListWidgetItem(langStr, self.spellCheckLanguagesList) |
|
99 itm.setData(Qt.ItemDataRole.UserRole, lang) |
|
100 itm.setFlags(itm.flags() | Qt.ItemFlag.ItemIsUserCheckable) |
|
101 itm.setCheckState(Qt.CheckState.Unchecked) |
|
102 self.spellCheckLanguagesList.sortItems(Qt.SortOrder.AscendingOrder) |
|
103 |
|
104 spellCheckLanguages = Preferences.getWebBrowser("SpellCheckLanguages") |
|
105 topIndex = 0 |
|
106 for lang in spellCheckLanguages: |
|
107 items = self.spellCheckLanguagesList.findItems( |
|
108 self.__createLanguageString(lang), Qt.MatchFlag.MatchExactly) |
|
109 if items: |
|
110 itm = items[0] |
|
111 self.spellCheckLanguagesList.takeItem( |
|
112 self.spellCheckLanguagesList.row(itm)) |
|
113 self.spellCheckLanguagesList.insertItem(topIndex, itm) |
|
114 itm.setCheckState(Qt.CheckState.Checked) |
|
115 topIndex += 1 |
|
116 |
|
117 if self.spellCheckLanguagesList.count(): |
|
118 self.noLanguagesLabel.hide() |
|
119 self.spellCheckLanguagesList.show() |
|
120 else: |
|
121 # no dictionaries available, disable spell checking |
|
122 self.noLanguagesLabel.show() |
|
123 self.spellCheckLanguagesList.hide() |
|
124 self.spellCheckEnabledCheckBox.setChecked(False) |
|
125 |
|
126 def save(self): |
|
127 """ |
|
128 Public slot to save the Help Viewers configuration. |
|
129 """ |
|
130 languages = [] |
|
131 for row in range(self.spellCheckLanguagesList.count()): |
|
132 itm = self.spellCheckLanguagesList.item(row) |
|
133 if itm.checkState() == Qt.CheckState.Checked: |
|
134 languages.append(itm.data(Qt.ItemDataRole.UserRole)) |
|
135 |
|
136 Preferences.setWebBrowser( |
|
137 "SpellCheckEnabled", |
|
138 self.spellCheckEnabledCheckBox.isChecked()) |
|
139 Preferences.setWebBrowser( |
|
140 "SpellCheckLanguages", |
|
141 languages) |
|
142 |
|
143 @pyqtSlot() |
|
144 def on_spellCheckEnabledCheckBox_clicked(self): |
|
145 """ |
|
146 Private slot handling a change of the spell checking enabled state. |
|
147 """ |
|
148 enable = self.spellCheckEnabledCheckBox.isChecked() |
|
149 self.noLanguagesLabel.setEnabled(enable) |
|
150 self.spellCheckLanguagesList.setEnabled(enable) |
|
151 |
|
152 def __createLanguageString(self, language): |
|
153 """ |
|
154 Private method to create a language string given a language identifier. |
|
155 |
|
156 @param language language identifier |
|
157 @type str |
|
158 @return language string |
|
159 @rtype str |
|
160 """ |
|
161 loc = QLocale(language) |
|
162 |
|
163 if loc.language() == QLocale.Language.C: |
|
164 return language |
|
165 |
|
166 country = QLocale.countryToString(loc.country()) |
|
167 lang = QLocale.languageToString(loc.language()) |
|
168 languageString = "{0}/{1} [{2}]".format(lang, country, language) |
|
169 return languageString |
|
170 |
|
171 @pyqtSlot() |
|
172 def on_manageDictionariesButton_clicked(self): |
|
173 """ |
|
174 Private slot to manage spell checking dictionaries. |
|
175 """ |
|
176 from WebBrowser.SpellCheck.ManageDictionariesDialog import ( |
|
177 ManageDictionariesDialog |
|
178 ) |
|
179 dlg = ManageDictionariesDialog(self.__writeableDirectories, self) |
|
180 dlg.exec() |
|
181 |
|
182 self.__populateDictionariesList() |
|
183 |
|
184 |
|
185 def create(dlg): |
|
186 """ |
|
187 Module function to create the configuration page. |
|
188 |
|
189 @param dlg reference to the configuration dialog |
|
190 @type Configuration |
|
191 @return reference to the instantiated page |
|
192 @rtype ConfigurationPageBase |
|
193 """ |
|
194 page = WebBrowserSpellCheckingPage() |
|
195 return page |