|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2017 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Web Browser Spell Checking configuration page. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 |
|
14 from PyQt5.QtCore import pyqtSlot, Qt, QCoreApplication, QDir, QLibraryInfo, \ |
|
15 QLocale |
|
16 from PyQt5.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(WebBrowserSpellCheckingPage, self).__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.location(QLibraryInfo.DataPath) + |
|
60 "/qtwebengine_dictionaries"), |
|
61 } |
|
62 self.spellCheckDictionaryDirectoriesEdit.setPlainText( |
|
63 "\n".join(self.__dictionaryDirectories)) |
|
64 # try to create these directories, if they don't exist |
|
65 for directory in self.__dictionaryDirectories: |
|
66 if not os.path.exists(directory): |
|
67 try: |
|
68 os.makedirs(directory) |
|
69 except os.error: |
|
70 # ignore it |
|
71 pass |
|
72 |
|
73 self.__writeableDirectories = [] |
|
74 for directory in self.__dictionaryDirectories: |
|
75 if os.access(directory, os.W_OK): |
|
76 self.__writeableDirectories.append(directory) |
|
77 self.manageDictionariesButton.setEnabled( |
|
78 bool(self.__writeableDirectories)) |
|
79 |
|
80 self.__populateDictionariesList() |
|
81 |
|
82 def __populateDictionariesList(self): |
|
83 """ |
|
84 Private method to populate the spell checking dictionaries list. |
|
85 """ |
|
86 self.spellCheckLanguagesList.clear() |
|
87 |
|
88 for path in self.__dictionaryDirectories: |
|
89 directory = QDir(path) |
|
90 fileNames = directory.entryList(["*.bdic"]) |
|
91 for fileName in fileNames: |
|
92 lang = fileName[:-5] |
|
93 langStr = self.__createLanguageString(lang) |
|
94 if self.spellCheckLanguagesList.findItems(langStr, |
|
95 Qt.MatchExactly): |
|
96 continue |
|
97 |
|
98 itm = QListWidgetItem(langStr, self.spellCheckLanguagesList) |
|
99 itm.setData(Qt.UserRole, lang) |
|
100 itm.setFlags(itm.flags() | Qt.ItemIsUserCheckable) |
|
101 itm.setCheckState(Qt.Unchecked) |
|
102 self.spellCheckLanguagesList.sortItems(Qt.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.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.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.Checked: |
|
134 languages.append(itm.data(Qt.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.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 dlg = ManageDictionariesDialog(self.__writeableDirectories, self) |
|
179 dlg.exec_() |
|
180 |
|
181 self.__populateDictionariesList() |
|
182 |
|
183 |
|
184 def create(dlg): |
|
185 """ |
|
186 Module function to create the configuration page. |
|
187 |
|
188 @param dlg reference to the configuration dialog |
|
189 @type Configuration |
|
190 @return reference to the instantiated page |
|
191 @rtype ConfigurationPageBase |
|
192 """ |
|
193 page = WebBrowserSpellCheckingPage() |
|
194 return page |