PluginAssistantEric.py

changeset 2
89cbc07f4bf0
child 5
e1a56c9a9c9d
equal deleted inserted replaced
1:3a4123edc944 2:89cbc07f4bf0
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2008 - 2010 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the Eric assistant plugin.
8 """
9
10 import sys
11 import os
12
13 from PyQt4.QtCore import QObject, SIGNAL, QTranslator
14 from PyQt4.QtGui import QApplication
15
16 from E5Gui.E5Application import e5App
17
18 import Preferences
19
20 from AssistantEric.Assistant import AcsAPIs, AcsProject
21
22 # Start-Of-Header
23 name = "Assistant Eric Plugin"
24 author = "Detlev Offenbach <detlev@die-offenbachs.de>"
25 autoactivate = True
26 deactivateable = True
27 version = "2.0.0"
28 className = "AssistantEricPlugin"
29 packageName = "AssistantEric"
30 shortDescription = "Alternative autocompletion and calltips provider."
31 longDescription = """This plugin implements an alternative autocompletion and""" \
32 """ calltips provider."""
33 needsRestart = True
34 pyqtApi = 2
35 # End-Of-Header
36
37 error = ""
38
39 assistantEricPluginObject = None
40
41 def createAutoCompletionPage(configDlg):
42 """
43 Module function to create the autocompletion configuration page.
44
45 @return reference to the configuration page
46 """
47 global assistantEricPluginObject
48 from AssistantEric.ConfigurationPages.AutoCompletionEricPage \
49 import AutoCompletionEricPage
50 page = AutoCompletionEricPage(assistantEricPluginObject)
51 return page
52
53 def createCallTipsPage(configDlg):
54 """
55 Module function to create the calltips configuration page.
56
57 @return reference to the configuration page
58 """
59 global assistantEricPluginObject
60 from AssistantEric.ConfigurationPages.CallTipsEricPage \
61 import CallTipsEricPage
62 page = CallTipsEricPage(assistantEricPluginObject)
63 return page
64
65 def getConfigData():
66 """
67 Module function returning data as required by the configuration dialog.
68
69 @return dictionary containing the relevant data
70 """
71 return {
72 "ericAutoCompletionPage" : \
73 [QApplication.translate("AssistantEricPlugin", "Eric"),
74 os.path.join("AssistantEric", "ConfigurationPages",
75 "eric.png"),
76 createAutoCompletionPage, "editorAutocompletionPage", None],
77 "ericCallTipsPage" : \
78 [QApplication.translate("AssistantEricPlugin", "Eric"),
79 os.path.join("AssistantEric", "ConfigurationPages",
80 "eric.png"),
81 createCallTipsPage, "editorCalltipsPage", None],
82 }
83
84 def prepareUninstall():
85 """
86 Module function to prepare for an uninstallation.
87 """
88 assistant = AssistantEricPlugin(None)
89 assistant.prepareUninstall()
90
91 class AssistantEricPlugin(QObject):
92 """
93 Class implementing the Eric assistant plugin.
94 """
95 def __init__(self, ui):
96 """
97 Constructor
98
99 @param ui reference to the user interface object (UI.UserInterface)
100 """
101 QObject.__init__(self, ui)
102 self.__ui = ui
103 self.__initialize()
104
105 self.__defaults = {
106 "AutoCompletionEnabled" : False,
107 "AutoCompletionSource" : AcsAPIs | AcsProject,
108 "CalltipsEnabled" : False,
109 "CallTipsContextShown" : 1,
110 }
111
112 self.__translator = None
113 self.__loadTranslator()
114
115 def __initialize(self):
116 """
117 Private slot to (re)initialize the plugin.
118 """
119 self.__object = None
120
121 def __checkQSql(self):
122 """
123 Private method to perform some checks on QSql.
124
125 @return flag indicating QSql is ok (boolean)
126 """
127 global error
128
129 try:
130 from PyQt4.QtSql import QSqlDatabase
131 except ImportError:
132 error = self.trUtf8("PyQt4.QtSql is not available.")
133 return False
134
135 drivers = QSqlDatabase.drivers()
136 if "QSQLITE" in drivers:
137 return True
138 else:
139 error = self.trUtf8("The SQLite database driver is not available.")
140 return False
141
142 def activate(self):
143 """
144 Public method to activate this plugin.
145
146 @return tuple of None and activation status (boolean)
147 """
148 global error
149 error = "" # clear previous error
150
151 if not self.__checkQSql():
152 return None, False
153
154 global assistantEricPluginObject
155 assistantEricPluginObject = self
156
157 from AssistantEric.Assistant import Assistant
158
159 self.__object = Assistant(self, self.__ui)
160 e5App().registerPluginObject("AssistantEric", self.__object)
161
162 self.__object.activate()
163
164 return None, True
165
166 def deactivate(self):
167 """
168 Public method to deactivate this plugin.
169 """
170 e5App().unregisterPluginObject("AssistantEric")
171
172 self.__object.deactivate()
173
174 self.__initialize()
175
176 def __loadTranslator(self):
177 """
178 Private method to load the translation file.
179 """
180 if self.__ui is not None:
181 loc = self.__ui.getLocale()
182 if loc and loc != "C":
183 locale_dir = \
184 os.path.join(os.path.dirname(__file__), "AssistantEric", "i18n")
185 translation = "assistant_%s" % loc
186 translator = QTranslator(None)
187 loaded = translator.load(translation, locale_dir)
188 if loaded:
189 self.__translator = translator
190 e5App().installTranslator(self.__translator)
191 else:
192 print("Warning: translation file '%s' could not be loaded." % \
193 translation)
194 print("Using default.")
195
196 def getPreferences(self, key):
197 """
198 Public method to retrieve the various refactoring settings.
199
200 @param key the key of the value to get
201 @param prefClass preferences class used as the storage area
202 @return the requested refactoring setting
203 """
204 if key in ["AutoCompletionEnabled", "CalltipsEnabled"]:
205 return Preferences.toBool(Preferences.Prefs.settings.value(
206 "AssistantEric/" + key, self.__defaults[key]))
207 else:
208 return int(Preferences.Prefs.settings.value("AssistantEric/" + key,
209 self.__defaults[key]))
210
211 def setPreferences(self, key, value):
212 """
213 Public method to store the various refactoring settings.
214
215 @param key the key of the setting to be set (string)
216 @param value the value to be set
217 @param prefClass preferences class used as the storage area
218 """
219 Preferences.Prefs.settings.setValue("AssistantEric/" + key, value)
220
221 if key in ["AutoCompletionEnabled", "CalltipsEnabled"]:
222 self.__object.setEnabled(key, value)
223
224 def prepareUninstall(self):
225 """
226 Public method to prepare for an uninstallation.
227 """
228 Preferences.Prefs.settings.remove("AssistantEric")
229

eric ide

mercurial