PluginAssistantEric.py

branch
eric7
changeset 190
3104a5a3ea13
parent 187
8674276e0031
child 191
6798a98189da
equal deleted inserted replaced
189:c0d638327085 190:3104a5a3ea13
40 40
41 41
42 def createAutoCompletionPage(configDlg): 42 def createAutoCompletionPage(configDlg):
43 """ 43 """
44 Module function to create the autocompletion configuration page. 44 Module function to create the autocompletion configuration page.
45 45
46 @param configDlg reference to the configuration dialog 46 @param configDlg reference to the configuration dialog
47 @type ConfigurationWidget 47 @type ConfigurationWidget
48 @return reference to the configuration page 48 @return reference to the configuration page
49 @rtype QWidget 49 @rtype QWidget
50 """ 50 """
51 global assistantEricPluginObject 51 global assistantEricPluginObject
52 from AssistantEric.ConfigurationPages.AutoCompletionEricPage import ( 52 from AssistantEric.ConfigurationPages.AutoCompletionEricPage import (
53 AutoCompletionEricPage 53 AutoCompletionEricPage,
54 ) 54 )
55
55 return AutoCompletionEricPage(assistantEricPluginObject) 56 return AutoCompletionEricPage(assistantEricPluginObject)
56 57
57 58
58 def createCallTipsPage(configDlg): 59 def createCallTipsPage(configDlg):
59 """ 60 """
60 Module function to create the calltips configuration page. 61 Module function to create the calltips configuration page.
61 62
62 @param configDlg reference to the configuration dialog 63 @param configDlg reference to the configuration dialog
63 @type ConfigurationWidget 64 @type ConfigurationWidget
64 @return reference to the configuration page 65 @return reference to the configuration page
65 @rtype QWidget 66 @rtype QWidget
66 """ 67 """
67 global assistantEricPluginObject 68 global assistantEricPluginObject
68 from AssistantEric.ConfigurationPages.CallTipsEricPage import ( 69 from AssistantEric.ConfigurationPages.CallTipsEricPage import CallTipsEricPage
69 CallTipsEricPage 70
70 )
71 return CallTipsEricPage(assistantEricPluginObject) 71 return CallTipsEricPage(assistantEricPluginObject)
72 72
73 73
74 def getConfigData(): 74 def getConfigData():
75 """ 75 """
76 Module function returning data as required by the configuration dialog. 76 Module function returning data as required by the configuration dialog.
77 77
78 @return dictionary containing the relevant data 78 @return dictionary containing the relevant data
79 @rtype dict 79 @rtype dict
80 """ 80 """
81 try: 81 try:
82 usesDarkPalette = ericApp().usesDarkPalette() 82 usesDarkPalette = ericApp().usesDarkPalette()
83 except AttributeError: 83 except AttributeError:
84 from PyQt6.QtGui import QPalette 84 from PyQt6.QtGui import QPalette
85
85 palette = ericApp().palette() 86 palette = ericApp().palette()
86 lightness = palette.color(QPalette.ColorRole.Window).lightness() 87 lightness = palette.color(QPalette.ColorRole.Window).lightness()
87 usesDarkPalette = lightness <= 128 88 usesDarkPalette = lightness <= 128
88 iconSuffix = "dark" if usesDarkPalette else "light" 89 iconSuffix = "dark" if usesDarkPalette else "light"
89 90
90 return { 91 return {
91 "ericAutoCompletionPage": [ 92 "ericAutoCompletionPage": [
92 QCoreApplication.translate("AssistantEricPlugin", "Eric"), 93 QCoreApplication.translate("AssistantEricPlugin", "Eric"),
93 os.path.join("AssistantEric", "ConfigurationPages", 94 os.path.join(
94 "eric-{0}".format(iconSuffix)), 95 "AssistantEric", "ConfigurationPages", "eric-{0}".format(iconSuffix)
95 createAutoCompletionPage, "1editorAutocompletionPage", None], 96 ),
97 createAutoCompletionPage,
98 "1editorAutocompletionPage",
99 None,
100 ],
96 "ericCallTipsPage": [ 101 "ericCallTipsPage": [
97 QCoreApplication.translate("AssistantEricPlugin", "Eric"), 102 QCoreApplication.translate("AssistantEricPlugin", "Eric"),
98 os.path.join("AssistantEric", "ConfigurationPages", 103 os.path.join(
99 "eric-{0}".format(iconSuffix)), 104 "AssistantEric", "ConfigurationPages", "eric-{0}".format(iconSuffix)
100 createCallTipsPage, "1editorCalltipsPage", None], 105 ),
106 createCallTipsPage,
107 "1editorCalltipsPage",
108 None,
109 ],
101 } 110 }
102 111
103 112
104 def prepareUninstall(): 113 def prepareUninstall():
105 """ 114 """
106 Module function to prepare for an uninstallation. 115 Module function to prepare for an uninstallation.
107 """ 116 """
108 Preferences.Prefs.settings.remove(AssistantEricPlugin.PreferencesKey) 117 Preferences.Prefs.settings.remove(AssistantEricPlugin.PreferencesKey)
109 118
110 119
111 class AssistantEricPlugin(QObject): 120 class AssistantEricPlugin(QObject):
112 """ 121 """
113 Class implementing the Eric assistant plugin. 122 Class implementing the Eric assistant plugin.
114 """ 123 """
124
115 PreferencesKey = "AssistantEric" 125 PreferencesKey = "AssistantEric"
116 126
117 def __init__(self, ui): 127 def __init__(self, ui):
118 """ 128 """
119 Constructor 129 Constructor
120 130
121 @param ui reference to the user interface object 131 @param ui reference to the user interface object
122 @type UserInterface 132 @type UserInterface
123 """ 133 """
124 QObject.__init__(self, ui) 134 QObject.__init__(self, ui)
125 self.__ui = ui 135 self.__ui = ui
126 self.__initialize() 136 self.__initialize()
127 137
128 self.__defaults = { 138 self.__defaults = {
129 "AutoCompletionEnabled": False, 139 "AutoCompletionEnabled": False,
130 "AutoCompletionSource": AcsAPIs | AcsProject, 140 "AutoCompletionSource": AcsAPIs | AcsProject,
131 "AutoCompletionFollowHierarchy": False, 141 "AutoCompletionFollowHierarchy": False,
132 "CalltipsEnabled": False, 142 "CalltipsEnabled": False,
133 "CallTipsContextShown": True, 143 "CallTipsContextShown": True,
134 "CallTipsFollowHierarchy": False, 144 "CallTipsFollowHierarchy": False,
135 } 145 }
136 146
137 self.__translator = None 147 self.__translator = None
138 self.__loadTranslator() 148 self.__loadTranslator()
139 149
140 def __initialize(self): 150 def __initialize(self):
141 """ 151 """
142 Private slot to (re)initialize the plugin. 152 Private slot to (re)initialize the plugin.
143 """ 153 """
144 self.__object = None 154 self.__object = None
145 155
146 def __checkQSql(self): 156 def __checkQSql(self):
147 """ 157 """
148 Private method to perform some checks on QSql. 158 Private method to perform some checks on QSql.
149 159
150 @return flag indicating QSql is ok 160 @return flag indicating QSql is ok
151 @rtype bool 161 @rtype bool
152 """ 162 """
153 global error 163 global error
154 164
155 try: 165 try:
156 from PyQt6.QtSql import QSqlDatabase 166 from PyQt6.QtSql import QSqlDatabase
157 except ImportError: 167 except ImportError:
158 error = self.tr("PyQt6.QtSql is not available.") 168 error = self.tr("PyQt6.QtSql is not available.")
159 return False 169 return False
160 170
161 drivers = QSqlDatabase.drivers() 171 drivers = QSqlDatabase.drivers()
162 if "QSQLITE" not in drivers: 172 if "QSQLITE" not in drivers:
163 error = self.tr("The SQLite database driver is not available.") 173 error = self.tr("The SQLite database driver is not available.")
164 return False 174 return False
165 175
166 return True 176 return True
167 177
168 def activate(self): 178 def activate(self):
169 """ 179 """
170 Public method to activate this plugin. 180 Public method to activate this plugin.
171 181
172 @return tuple of None and activation status 182 @return tuple of None and activation status
173 @rtype bool 183 @rtype bool
174 """ 184 """
175 global error 185 global error
176 error = "" # clear previous error 186 error = "" # clear previous error
177 187
178 if not self.__checkQSql(): 188 if not self.__checkQSql():
179 return None, False 189 return None, False
180 190
181 global assistantEricPluginObject 191 global assistantEricPluginObject
182 assistantEricPluginObject = self 192 assistantEricPluginObject = self
183 193
184 from AssistantEric.Assistant import Assistant 194 from AssistantEric.Assistant import Assistant
185 195
186 self.__object = Assistant(self, self.__ui) 196 self.__object = Assistant(self, self.__ui)
187 ericApp().registerPluginObject("AssistantEric", self.__object) 197 ericApp().registerPluginObject("AssistantEric", self.__object)
188 198
189 self.__object.activate() 199 self.__object.activate()
190 200
191 return None, True 201 return None, True
192 202
193 def deactivate(self): 203 def deactivate(self):
194 """ 204 """
195 Public method to deactivate this plugin. 205 Public method to deactivate this plugin.
196 """ 206 """
197 ericApp().unregisterPluginObject("AssistantEric") 207 ericApp().unregisterPluginObject("AssistantEric")
198 208
199 self.__object.deactivate() 209 self.__object.deactivate()
200 210
201 self.__initialize() 211 self.__initialize()
202 212
203 def __loadTranslator(self): 213 def __loadTranslator(self):
204 """ 214 """
205 Private method to load the translation file. 215 Private method to load the translation file.
206 """ 216 """
207 if self.__ui is not None: 217 if self.__ui is not None:
208 loc = self.__ui.getLocale() 218 loc = self.__ui.getLocale()
209 if loc and loc != "C": 219 if loc and loc != "C":
210 locale_dir = os.path.join( 220 locale_dir = os.path.join(
211 os.path.dirname(__file__), "AssistantEric", "i18n") 221 os.path.dirname(__file__), "AssistantEric", "i18n"
222 )
212 translation = "assistant_{0}".format(loc) 223 translation = "assistant_{0}".format(loc)
213 translator = QTranslator(None) 224 translator = QTranslator(None)
214 loaded = translator.load(translation, locale_dir) 225 loaded = translator.load(translation, locale_dir)
215 if loaded: 226 if loaded:
216 self.__translator = translator 227 self.__translator = translator
217 ericApp().installTranslator(self.__translator) 228 ericApp().installTranslator(self.__translator)
218 else: 229 else:
219 print("Warning: translation file '{0}' could not be" 230 print(
220 " loaded.".format(translation)) 231 "Warning: translation file '{0}' could not be"
232 " loaded.".format(translation)
233 )
221 print("Using default.") 234 print("Using default.")
222 235
223 def getPreferences(self, key): 236 def getPreferences(self, key):
224 """ 237 """
225 Public method to retrieve the various settings. 238 Public method to retrieve the various settings.
226 239
227 @param key the key of the value to get 240 @param key the key of the value to get
228 @type str 241 @type str
229 @return value of the requested setting 242 @return value of the requested setting
230 @rtype Any 243 @rtype Any
231 """ 244 """
232 if key in ["AutoCompletionEnabled", "AutoCompletionFollowHierarchy", 245 if key in [
233 "CalltipsEnabled", "CallTipsContextShown", 246 "AutoCompletionEnabled",
234 "CallTipsFollowHierarchy"]: 247 "AutoCompletionFollowHierarchy",
235 return Preferences.toBool(Preferences.Prefs.settings.value( 248 "CalltipsEnabled",
236 self.PreferencesKey + "/" + key, self.__defaults[key])) 249 "CallTipsContextShown",
250 "CallTipsFollowHierarchy",
251 ]:
252 return Preferences.toBool(
253 Preferences.Prefs.settings.value(
254 self.PreferencesKey + "/" + key, self.__defaults[key]
255 )
256 )
237 else: 257 else:
238 return int(Preferences.Prefs.settings.value( 258 return int(
239 self.PreferencesKey + "/" + key, self.__defaults[key])) 259 Preferences.Prefs.settings.value(
240 260 self.PreferencesKey + "/" + key, self.__defaults[key]
261 )
262 )
263
241 def setPreferences(self, key, value): 264 def setPreferences(self, key, value):
242 """ 265 """
243 Public method to store the various settings. 266 Public method to store the various settings.
244 267
245 @param key the key of the setting to be set 268 @param key the key of the setting to be set
246 @type str 269 @type str
247 @param value value to be set 270 @param value value to be set
248 @type Any 271 @type Any
249 """ 272 """
250 Preferences.Prefs.settings.setValue( 273 Preferences.Prefs.settings.setValue(self.PreferencesKey + "/" + key, value)
251 self.PreferencesKey + "/" + key, value) 274
252
253 if key in ["AutoCompletionEnabled", "CalltipsEnabled"]: 275 if key in ["AutoCompletionEnabled", "CalltipsEnabled"]:
254 self.__object.setEnabled(key, value) 276 self.__object.setEnabled(key, value)
255 277
278
256 # 279 #
257 # eflag: noqa = M801 280 # eflag: noqa = M801

eric ide

mercurial