PluginProjectPyramid.py

branch
eric7
changeset 147
eb28b4b6f7f5
parent 144
5c3684ee818e
child 148
dcbd3a96f03c
equal deleted inserted replaced
146:7d955b1995d5 147:eb28b4b6f7f5
9 9
10 import os 10 import os
11 import glob 11 import glob
12 import fnmatch 12 import fnmatch
13 13
14 from PyQt5.QtCore import QCoreApplication, QObject, QTranslator 14 from PyQt6.QtCore import QCoreApplication, QObject, QTranslator
15 15
16 from E5Gui.E5Application import e5App 16 from EricWidgets.EricApplication import ericApp
17 17
18 import Preferences 18 import Preferences
19 19
20 from Globals import isWindowsPlatform, isMacPlatform 20 from Globals import isWindowsPlatform, isMacPlatform
21 21
24 # Start-of-Header 24 # Start-of-Header
25 name = "Pyramid Project Plugin" 25 name = "Pyramid Project Plugin"
26 author = "Detlev Offenbach <detlev@die-offenbachs.de>" 26 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
27 autoactivate = True 27 autoactivate = True
28 deactivateable = True 28 deactivateable = True
29 version = "4.0.0" 29 version = "1.0.0"
30 className = "ProjectPyramidPlugin" 30 className = "ProjectPyramidPlugin"
31 packageName = "ProjectPyramid" 31 packageName = "ProjectPyramid"
32 shortDescription = "Project support for Pyramid projects." 32 shortDescription = "Project support for Pyramid projects."
33 longDescription = ( 33 longDescription = (
34 """This plugin implements project support for Pyramid projects.""" 34 """This plugin implements project support for Pyramid projects."""
45 def createPyramidPage(configDlg): 45 def createPyramidPage(configDlg):
46 """ 46 """
47 Module function to create the Pyramid configuration page. 47 Module function to create the Pyramid configuration page.
48 48
49 @param configDlg reference to the configuration dialog 49 @param configDlg reference to the configuration dialog
50 @type ConfigurationWidget
50 @return reference to the configuration page 51 @return reference to the configuration page
52 @rtype PyramidPage
51 """ 53 """
52 global pyramidPluginObject 54 global pyramidPluginObject
53 from ProjectPyramid.ConfigurationPage.PyramidPage import PyramidPage 55 from ProjectPyramid.ConfigurationPage.PyramidPage import PyramidPage
54 return PyramidPage(pyramidPluginObject) 56 return PyramidPage(pyramidPluginObject)
55 57
57 def getConfigData(): 59 def getConfigData():
58 """ 60 """
59 Module function returning data as required by the configuration dialog. 61 Module function returning data as required by the configuration dialog.
60 62
61 @return dictionary containing the relevant data 63 @return dictionary containing the relevant data
62 """ 64 @rtype dict
63 try: 65 """
64 usesDarkPalette = e5App().usesDarkPalette() 66 usesDarkPalette = ericApp().usesDarkPalette()
65 except AttributeError:
66 # code for eric < 20.4
67 from PyQt5.QtGui import QPalette
68 palette = e5App().palette()
69 lightness = palette.color(QPalette.Window).lightness()
70 usesDarkPalette = lightness <= 128
71 iconSuffix = "dark" if usesDarkPalette else "light" 67 iconSuffix = "dark" if usesDarkPalette else "light"
72 68
73 return { 69 return {
74 "pyramidPage": [ 70 "pyramidPage": [
75 QCoreApplication.translate("ProjectPyramidPlugin", "Pyramid"), 71 QCoreApplication.translate("ProjectPyramidPlugin", "Pyramid"),
81 77
82 def apiFiles(language): 78 def apiFiles(language):
83 """ 79 """
84 Module function to return the API files made available by this plugin. 80 Module function to return the API files made available by this plugin.
85 81
86 @param language language to get APIs for (string) 82 @param language language to get APIs for
87 @return list of API filenames (list of string) 83 @type str
84 @return list of API filenames
85 @rtype list of str
88 """ 86 """
89 if language == "Python3": 87 if language == "Python3":
90 apisDir = os.path.join(os.path.dirname(__file__), 88 apisDir = os.path.join(os.path.dirname(__file__),
91 "ProjectPyramid", "APIs") 89 "ProjectPyramid", "APIs")
92 return glob.glob(os.path.join(apisDir, '*.api')) 90 return glob.glob(os.path.join(apisDir, '*.api'))
118 116
119 def __init__(self, ui): 117 def __init__(self, ui):
120 """ 118 """
121 Constructor 119 Constructor
122 120
123 @param ui reference to the user interface object (UI.UserInterface) 121 @param ui reference to the user interface object
122 @type UserInterface
124 """ 123 """
125 QObject.__init__(self, ui) 124 QObject.__init__(self, ui)
126 self.__ui = ui 125 self.__ui = ui
127 self.__initialize() 126 self.__initialize()
128 127
156 155
157 self.__mainMenu = None 156 self.__mainMenu = None
158 self.__mainAct = None 157 self.__mainAct = None
159 self.__separatorAct = None 158 self.__separatorAct = None
160 159
161 self.__e5project = e5App().getObject("Project") 160 self.__ericProject = ericApp().getObject("Project")
162 161
163 self.__supportedVariants = [] 162 self.__supportedVariants = []
164 163
165 def activate(self): 164 def activate(self):
166 """ 165 """
167 Public method to activate this plugin. 166 Public method to activate this plugin.
168 167
169 @return tuple of None and activation status (boolean) 168 @return tuple of None and activation status
169 @rtype (None, bool)
170 """ 170 """
171 global pyramidPluginObject 171 global pyramidPluginObject
172 pyramidPluginObject = self 172 pyramidPluginObject = self
173 173
174 try: 174 usesDarkPalette = ericApp().usesDarkPalette()
175 usesDarkPalette = e5App().usesDarkPalette()
176 except AttributeError:
177 # code for eric < 20.4
178 from PyQt5.QtGui import QPalette
179 palette = e5App().palette()
180 lightness = palette.color(QPalette.Window).lightness()
181 usesDarkPalette = lightness <= 128
182 iconSuffix = "dark" if usesDarkPalette else "light" 175 iconSuffix = "dark" if usesDarkPalette else "light"
183 176
184 self.__object = Project(self, iconSuffix, self.__ui) 177 self.__object = Project(self, iconSuffix, self.__ui)
185 self.__object.initActions() 178 self.__object.initActions()
186 e5App().registerPluginObject("ProjectPyramid", self.__object) 179 ericApp().registerPluginObject("ProjectPyramid", self.__object)
187 180
188 self.__mainMenu = self.__object.initMenu() 181 self.__mainMenu = self.__object.initMenu()
189 182
190 self.__supportedVariants = self.__object.supportedPythonVariants() 183 self.__supportedVariants = self.__object.supportedPythonVariants()
191 184
192 if self.__supportedVariants: 185 if self.__supportedVariants:
193 self.__e5project.registerProjectType( 186 self.__ericProject.registerProjectType(
194 "Pyramid", self.tr("Pyramid"), 187 "Pyramid", self.tr("Pyramid"),
195 self.fileTypesCallback, 188 self.fileTypesCallback,
196 lexerAssociationCallback=self.lexerAssociationCallback, 189 lexerAssociationCallback=self.lexerAssociationCallback,
197 binaryTranslationsCallback=self.binaryTranslationsCallback, 190 binaryTranslationsCallback=self.binaryTranslationsCallback,
198 progLanguages=self.__supportedVariants[:]) 191 progLanguages=self.__supportedVariants[:])
205 "Pyramid", 198 "Pyramid",
206 SourcesBrowserFlag | FormsBrowserFlag | 199 SourcesBrowserFlag | FormsBrowserFlag |
207 TranslationsBrowserFlag | OthersBrowserFlag, 200 TranslationsBrowserFlag | OthersBrowserFlag,
208 ) 201 )
209 202
210 if self.__e5project.isOpen(): 203 if self.__ericProject.isOpen():
211 self.__projectOpened() 204 self.__projectOpened()
212 self.__object.projectOpenedHooks() 205 self.__object.projectOpenedHooks()
213 206
214 e5App().getObject("Project").projectOpened.connect( 207 ericApp().getObject("Project").projectOpened.connect(
215 self.__projectOpened) 208 self.__projectOpened)
216 e5App().getObject("Project").projectClosed.connect( 209 ericApp().getObject("Project").projectClosed.connect(
217 self.__projectClosed) 210 self.__projectClosed)
218 e5App().getObject("Project").newProject.connect( 211 ericApp().getObject("Project").newProject.connect(
219 self.__projectOpened) 212 self.__projectOpened)
220 213
221 e5App().getObject("Project").projectOpenedHooks.connect( 214 ericApp().getObject("Project").projectOpenedHooks.connect(
222 self.__object.projectOpenedHooks) 215 self.__object.projectOpenedHooks)
223 e5App().getObject("Project").projectClosedHooks.connect( 216 ericApp().getObject("Project").projectClosedHooks.connect(
224 self.__object.projectClosedHooks) 217 self.__object.projectClosedHooks)
225 e5App().getObject("Project").newProjectHooks.connect( 218 ericApp().getObject("Project").newProjectHooks.connect(
226 self.__object.projectOpenedHooks) 219 self.__object.projectOpenedHooks)
227 220
228 return None, True 221 return None, True
229 222
230 def deactivate(self): 223 def deactivate(self):
231 """ 224 """
232 Public method to deactivate this plugin. 225 Public method to deactivate this plugin.
233 """ 226 """
234 e5App().unregisterPluginObject("ProjectPyramid") 227 ericApp().unregisterPluginObject("ProjectPyramid")
235 228
236 e5App().getObject("Project").projectOpened.disconnect( 229 ericApp().getObject("Project").projectOpened.disconnect(
237 self.__projectOpened) 230 self.__projectOpened)
238 e5App().getObject("Project").projectClosed.disconnect( 231 ericApp().getObject("Project").projectClosed.disconnect(
239 self.__projectClosed) 232 self.__projectClosed)
240 e5App().getObject("Project").newProject.disconnect( 233 ericApp().getObject("Project").newProject.disconnect(
241 self.__projectOpened) 234 self.__projectOpened)
242 235
243 e5App().getObject("Project").projectOpenedHooks.disconnect( 236 ericApp().getObject("Project").projectOpenedHooks.disconnect(
244 self.__object.projectOpenedHooks) 237 self.__object.projectOpenedHooks)
245 e5App().getObject("Project").projectClosedHooks.disconnect( 238 ericApp().getObject("Project").projectClosedHooks.disconnect(
246 self.__object.projectClosedHooks) 239 self.__object.projectClosedHooks)
247 e5App().getObject("Project").newProjectHooks.disconnect( 240 ericApp().getObject("Project").newProjectHooks.disconnect(
248 self.__object.projectOpenedHooks) 241 self.__object.projectOpenedHooks)
249 242
250 self.__e5project.unregisterProjectType("Pyramid") 243 self.__ericProject.unregisterProjectType("Pyramid")
251 244
252 self.__object.projectClosedHooks() 245 self.__object.projectClosedHooks()
253 self.__projectClosed() 246 self.__projectClosed()
254 247
255 self.__initialize() 248 self.__initialize()
266 translation = "pyramid_{0}".format(loc) 259 translation = "pyramid_{0}".format(loc)
267 translator = QTranslator(None) 260 translator = QTranslator(None)
268 loaded = translator.load(translation, locale_dir) 261 loaded = translator.load(translation, locale_dir)
269 if loaded: 262 if loaded:
270 self.__translator = translator 263 self.__translator = translator
271 e5App().installTranslator(self.__translator) 264 ericApp().installTranslator(self.__translator)
272 else: 265 else:
273 print("Warning: translation file '{0}' could not be" # __IGNORE_WARNING__ 266 print("Warning: translation file '{0}' could not be" # __IGNORE_WARNING__
274 " loaded.".format(translation)) 267 " loaded.".format(translation))
275 print("Using default.") # __IGNORE_WARNING__ 268 print("Using default.") # __IGNORE_WARNING__
276 269
277 def __projectOpened(self): 270 def __projectOpened(self):
278 """ 271 """
279 Private slot to handle the projectOpened signal. 272 Private slot to handle the projectOpened signal.
280 """ 273 """
281 if self.__e5project.getProjectType() == "Pyramid": 274 if self.__ericProject.getProjectType() == "Pyramid":
282 projectToolsMenu = self.__ui.getMenu("project_tools") 275 projectToolsMenu = self.__ui.getMenu("project_tools")
283 if projectToolsMenu is not None: 276 insertBeforeAct = projectToolsMenu.actions()[0]
284 insertBeforeAct = projectToolsMenu.actions()[0] 277 self.__mainAct = projectToolsMenu.insertMenu(
285 self.__mainAct = projectToolsMenu.insertMenu( 278 insertBeforeAct, self.__mainMenu)
286 insertBeforeAct, self.__mainMenu) 279 self.__separatorAct = projectToolsMenu.insertSeparator(
287 self.__separatorAct = projectToolsMenu.insertSeparator( 280 insertBeforeAct)
288 insertBeforeAct)
289 else:
290 projectAct = self.__ui.getMenuBarAction("project")
291 actions = self.__ui.menuBar().actions()
292 insertBeforeAct = actions[actions.index(projectAct) + 1]
293 self.__mainAct = self.__ui.menuBar().insertMenu(
294 insertBeforeAct, self.__mainMenu)
295 281
296 def __projectClosed(self): 282 def __projectClosed(self):
297 """ 283 """
298 Private slot to handle the projectClosed signal. 284 Private slot to handle the projectClosed signal.
299 """ 285 """
300 if self.__mainAct is not None: 286 if self.__mainAct is not None:
301 projectToolsMenu = self.__ui.getMenu("project_tools") 287 projectToolsMenu = self.__ui.getMenu("project_tools")
302 if projectToolsMenu is not None: 288 projectToolsMenu.removeAction(self.__separatorAct)
303 projectToolsMenu.removeAction(self.__separatorAct) 289 projectToolsMenu.removeAction(self.__mainAct)
304 projectToolsMenu.removeAction(self.__mainAct) 290 self.__mainAct = None
305 self.__mainAct = None 291 self.__separatorAct = None
306 self.__separatorAct = None
307 else:
308 self.__ui.menuBar().removeAction(self.__mainAct)
309 self.__mainAct = None
310 self.__object.projectClosed() 292 self.__object.projectClosed()
311 293
312 def fileTypesCallback(self): 294 def fileTypesCallback(self):
313 """ 295 """
314 Public method get the filetype associations of the Pyramid project 296 Public method get the filetype associations of the Pyramid project
315 type. 297 type.
316 298
317 @return dictionary with file type associations 299 @return dictionary with file type associations
318 """ 300 @rtype dict
319 if self.__e5project.getProjectType() == "Pyramid": 301 """
302 if self.__ericProject.getProjectType() == "Pyramid":
320 return { 303 return {
321 "*.mako": "FORMS", 304 "*.mako": "FORMS",
322 "*.mak": "FORMS", 305 "*.mak": "FORMS",
323 "*.pt": "FORMS", 306 "*.pt": "FORMS",
324 "*.html": "FORMS", 307 "*.html": "FORMS",
334 def lexerAssociationCallback(self, filename): 317 def lexerAssociationCallback(self, filename):
335 """ 318 """
336 Public method to get the lexer association of the Pyramid project type 319 Public method to get the lexer association of the Pyramid project type
337 for a file. 320 for a file.
338 321
339 @param filename name of the file (string) 322 @param filename name of the file
340 @return name of the lexer (string) (Pygments lexers are prefixed with 323 @type str
324 @return name of the lexer (Pygments lexers are prefixed with
341 'Pygments|') 325 'Pygments|')
326 @rtype str
342 """ 327 """
343 for pattern, language in self.lexerAssociations.items(): 328 for pattern, language in self.lexerAssociations.items():
344 if fnmatch.fnmatch(filename, pattern): 329 if fnmatch.fnmatch(filename, pattern):
345 return language 330 return language
346 331
349 def binaryTranslationsCallback(self, filename): 334 def binaryTranslationsCallback(self, filename):
350 """ 335 """
351 Public method to determine the filename of a compiled translation file 336 Public method to determine the filename of a compiled translation file
352 given the translation source file. 337 given the translation source file.
353 338
354 @param filename name of the translation source file (string) 339 @param filename name of the translation source file
355 @return name of the binary translation file (string) 340 @type str
341 @return name of the binary translation file
342 @rtype str
356 """ 343 """
357 if filename.endswith(".po"): 344 if filename.endswith(".po"):
358 return filename.replace(".po", ".mo") 345 return filename.replace(".po", ".mo")
359 346
360 return filename 347 return filename
361 348
362 def getDefaultPreference(self, key): 349 def getDefaultPreference(self, key):
363 """ 350 """
364 Public method to get the default value for a setting. 351 Public method to get the default value for a setting.
365 352
366 @param key the key of the value to get 353 @param key key of the value to get
367 @return the requested setting 354 @type str
355 @return value of the requested setting
356 @rtype Any
368 """ 357 """
369 return self.__defaults[key] 358 return self.__defaults[key]
370 359
371 def getPreferences(self, key): 360 def getPreferences(self, key):
372 """ 361 """
373 Public method to retrieve the various settings. 362 Public method to retrieve the various settings.
374 363
375 @param key the key of the value to get 364 @param key key of the value to get
376 @return the requested setting 365 @type str
366 @return value of the requested setting
367 @rtype Any
377 """ 368 """
378 if key in ["UseExternalBrowser"]: 369 if key in ["UseExternalBrowser"]:
379 return Preferences.toBool(Preferences.Prefs.settings.value( 370 return Preferences.toBool(Preferences.Prefs.settings.value(
380 self.PreferencesKey + "/" + key, self.__defaults[key])) 371 self.PreferencesKey + "/" + key, self.__defaults[key]))
381 else: 372 else:
384 375
385 def setPreferences(self, key, value): 376 def setPreferences(self, key, value):
386 """ 377 """
387 Public method to store the various settings. 378 Public method to store the various settings.
388 379
389 @param key the key of the setting to be set (string) 380 @param key key of the setting to be set
390 @param value the value to be set 381 @type str
382 @param value value to be set
383 @type Any
391 """ 384 """
392 Preferences.Prefs.settings.setValue( 385 Preferences.Prefs.settings.setValue(
393 self.PreferencesKey + "/" + key, value) 386 self.PreferencesKey + "/" + key, value)
394 387
395 if key in ["VirtualEnvironmentNamePy3"]: 388 if key in ["VirtualEnvironmentNamePy3"]:
402 Private method to re-register the project type. 395 Private method to re-register the project type.
403 """ 396 """
404 supportedVariants = self.__object.supportedPythonVariants() 397 supportedVariants = self.__object.supportedPythonVariants()
405 if supportedVariants != self.__supportedVariants: 398 if supportedVariants != self.__supportedVariants:
406 # step 1: unregister 399 # step 1: unregister
407 self.__e5project.unregisterProjectType("Pyramid") 400 self.__ericProject.unregisterProjectType("Pyramid")
408 401
409 # step 2: register again with new language settings 402 # step 2: register again with new language settings
410 self.__supportedVariants = supportedVariants 403 self.__supportedVariants = supportedVariants
411 if self.__supportedVariants: 404 if self.__supportedVariants:
412 self.__e5project.registerProjectType( 405 self.__ericProject.registerProjectType(
413 "Pyramid", 406 "Pyramid",
414 self.tr("Pyramid"), self.fileTypesCallback, 407 self.tr("Pyramid"), self.fileTypesCallback,
415 lexerAssociationCallback=self.lexerAssociationCallback, 408 lexerAssociationCallback=self.lexerAssociationCallback,
416 binaryTranslationsCallback=self.binaryTranslationsCallback, 409 binaryTranslationsCallback=self.binaryTranslationsCallback,
417 progLanguages=self.__supportedVariants[:]) 410 progLanguages=self.__supportedVariants[:])
418 411
419 def getMenu(self, name): 412 def getMenu(self, name):
420 """ 413 """
421 Public method to get a reference to the requested menu. 414 Public method to get a reference to the requested menu.
422 415
423 @param name name of the menu (string) 416 @param name name of the menu
424 @return reference to the menu (QMenu) or None, if no 417 @type str
425 menu with the given name exists 418 @return reference to the menu or None, if no menu with the given
419 name exists
420 @rtype QMenu
426 """ 421 """
427 if self.__object is not None: 422 if self.__object is not None:
428 return self.__object.getMenu(name) 423 return self.__object.getMenu(name)
429 else: 424 else:
430 return None 425 return None
431 426
432 def getMenuNames(self): 427 def getMenuNames(self):
433 """ 428 """
434 Public method to get the names of all menus. 429 Public method to get the names of all menus.
435 430
436 @return menu names (list of string) 431 @return menu names
432 @rtype list of str
437 """ 433 """
438 if self.__object is not None: 434 if self.__object is not None:
439 return list(self.__menus.keys()) 435 return list(self.__menus.keys())
440 else: 436 else:
441 return [] 437 return []

eric ide

mercurial