|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2014 - 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Translator plugin. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 |
|
14 from PyQt5.QtCore import pyqtSignal, QObject, QCoreApplication, QDateTime, Qt |
|
15 |
|
16 from E5Gui.E5Application import e5App |
|
17 |
|
18 import Preferences |
|
19 |
|
20 from UiExtensionPlugins.Translator.Translator import Translator |
|
21 |
|
22 import UI.Info |
|
23 |
|
24 # Start-Of-Header |
|
25 name = "Translator Plugin" |
|
26 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
|
27 autoactivate = True |
|
28 deactivateable = True |
|
29 version = UI.Info.VersionOnly |
|
30 className = "TranslatorPlugin" |
|
31 packageName = "__core__" |
|
32 shortDescription = "Translation utility using various translators." |
|
33 longDescription = \ |
|
34 """This plug-in implements a utility to translate text using""" \ |
|
35 """ various online translation services.""" |
|
36 needsRestart = False |
|
37 pyqtApi = 2 |
|
38 python2Compatible = True |
|
39 # End-Of-Header |
|
40 |
|
41 error = "" |
|
42 |
|
43 translatorPluginObject = None |
|
44 |
|
45 |
|
46 def createTranslatorPage(configDlg): |
|
47 """ |
|
48 Module function to create the Translator configuration page. |
|
49 |
|
50 @param configDlg reference to the configuration dialog |
|
51 @return reference to the configuration page |
|
52 """ |
|
53 from UiExtensionPlugins.Translator.ConfigurationPage.TranslatorPage \ |
|
54 import TranslatorPage |
|
55 page = TranslatorPage(translatorPluginObject) |
|
56 return page |
|
57 |
|
58 |
|
59 def getConfigData(): |
|
60 """ |
|
61 Module function returning data as required by the configuration dialog. |
|
62 |
|
63 @return dictionary containing the relevant data |
|
64 """ |
|
65 return { |
|
66 "translatorPage": [ |
|
67 QCoreApplication.translate("TranslatorPlugin", |
|
68 "Translator"), |
|
69 os.path.join("UiExtensionPlugins", "Translator", "icons", |
|
70 "flag.png"), |
|
71 createTranslatorPage, None, None], |
|
72 } |
|
73 |
|
74 |
|
75 def prepareUninstall(): |
|
76 """ |
|
77 Module function to prepare for an uninstallation. |
|
78 """ |
|
79 Preferences.Prefs.settings.remove(TranslatorPlugin.PreferencesKey) |
|
80 |
|
81 |
|
82 class TranslatorPlugin(QObject): |
|
83 """ |
|
84 Class implementing the Translator plugin. |
|
85 """ |
|
86 PreferencesKey = "Translator" |
|
87 |
|
88 updateLanguages = pyqtSignal() |
|
89 |
|
90 def __init__(self, ui): |
|
91 """ |
|
92 Constructor |
|
93 |
|
94 @param ui reference to the user interface object (UI.UserInterface) |
|
95 """ |
|
96 super(TranslatorPlugin, self).__init__(ui) |
|
97 self.__ui = ui |
|
98 self.__initialize() |
|
99 |
|
100 self.__defaults = { |
|
101 "OriginalLanguage": "en", |
|
102 "TranslationLanguage": "de", |
|
103 "SelectedEngine": "deepl", |
|
104 "EnabledLanguages": ["en", "de", "fr", "cs", "es", "pt", |
|
105 "ru", "tr", "zh-CN", "zh-TW"], |
|
106 # service specific settings below |
|
107 # Google |
|
108 "GoogleEnableDictionary": False, |
|
109 "GoogleV2Key": "", |
|
110 # Microsoft |
|
111 "MsTranslatorKey": "", |
|
112 "MsAuthToken": "", |
|
113 "MsAuthTokenExpire": QDateTime(), |
|
114 # MyMemory |
|
115 "MyMemoryKey": "", |
|
116 "MyMemoryEmail": "", |
|
117 # Yandex |
|
118 "YandexKey": "", |
|
119 } |
|
120 |
|
121 def __initialize(self): |
|
122 """ |
|
123 Private slot to (re)initialize the plugin. |
|
124 """ |
|
125 self.__object = None |
|
126 |
|
127 def activate(self): |
|
128 """ |
|
129 Public method to activate this plugin. |
|
130 |
|
131 @return tuple of None and activation status (boolean) |
|
132 """ |
|
133 global error |
|
134 error = "" # clear previous error |
|
135 |
|
136 global translatorPluginObject |
|
137 translatorPluginObject = self |
|
138 |
|
139 self.__object = Translator(self, self.__ui) |
|
140 self.__object.activate() |
|
141 e5App().registerPluginObject("Translator", self.__object) |
|
142 |
|
143 return None, True |
|
144 |
|
145 def deactivate(self): |
|
146 """ |
|
147 Public method to deactivate this plugin. |
|
148 """ |
|
149 e5App().unregisterPluginObject("Translator") |
|
150 self.__object.deactivate() |
|
151 |
|
152 self.__initialize() |
|
153 |
|
154 def getPreferencesDefault(self, key): |
|
155 """ |
|
156 Public method to retrieve the various default settings. |
|
157 |
|
158 @param key the key of the value to get |
|
159 @return the requested setting |
|
160 """ |
|
161 return self.__defaults[key] |
|
162 |
|
163 def getPreferences(self, key): |
|
164 """ |
|
165 Public method to retrieve the various settings. |
|
166 |
|
167 @param key the key of the value to get |
|
168 @return the requested setting |
|
169 """ |
|
170 if key in ["EnabledLanguages"]: |
|
171 return Preferences.toList( |
|
172 Preferences.Prefs.settings.value( |
|
173 self.PreferencesKey + "/" + key, self.__defaults[key])) |
|
174 elif key in ["GoogleEnableDictionary"]: |
|
175 return Preferences.toBool( |
|
176 Preferences.Prefs.settings.value( |
|
177 self.PreferencesKey + "/" + key, self.__defaults[key])) |
|
178 elif key in ["MsAuthTokenExpire"]: |
|
179 value = Preferences.Prefs.settings.value( |
|
180 self.PreferencesKey + "/" + key, self.__defaults[key]) |
|
181 if isinstance(value, str): |
|
182 if value.startswith("@QDateTime"): |
|
183 # old value, replace with default |
|
184 value = self.__defaults[key] |
|
185 else: |
|
186 value = QDateTime.fromString(value, Qt.ISODate) |
|
187 return value |
|
188 else: |
|
189 return Preferences.Prefs.settings.value( |
|
190 self.PreferencesKey + "/" + key, self.__defaults[key]) |
|
191 |
|
192 def setPreferences(self, key, value): |
|
193 """ |
|
194 Public method to store the various settings. |
|
195 |
|
196 @param key the key of the setting to be set (string) |
|
197 @param value the value to be set |
|
198 """ |
|
199 if key in ["MsAuthTokenExpire"]: |
|
200 Preferences.Prefs.settings.setValue( |
|
201 self.PreferencesKey + "/" + key, value.toString(Qt.ISODate)) |
|
202 else: |
|
203 Preferences.Prefs.settings.setValue( |
|
204 self.PreferencesKey + "/" + key, value) |
|
205 |
|
206 if key in ["EnabledLanguages"]: |
|
207 self.updateLanguages.emit() |
|
208 |
|
209 # |
|
210 # eflag: noqa = M801 |