|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2006 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the Editor APIs configuration page. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import QDir, pyqtSlot, SIGNAL, QFileInfo |
|
11 from PyQt4.QtGui import QFileDialog, QInputDialog |
|
12 |
|
13 from E4Gui.E4Application import e4App |
|
14 from E4Gui.E4Completers import E4FileCompleter |
|
15 |
|
16 from ConfigurationPageBase import ConfigurationPageBase |
|
17 from Ui_EditorAPIsPage import Ui_EditorAPIsPage |
|
18 |
|
19 from QScintilla.APIsManager import APIsManager |
|
20 import QScintilla.Lexers |
|
21 |
|
22 import Preferences |
|
23 import Utilities |
|
24 |
|
25 class EditorAPIsPage(ConfigurationPageBase, Ui_EditorAPIsPage): |
|
26 """ |
|
27 Class implementing the Editor APIs configuration page. |
|
28 """ |
|
29 def __init__(self): |
|
30 """ |
|
31 Constructor |
|
32 """ |
|
33 ConfigurationPageBase.__init__(self) |
|
34 self.setupUi(self) |
|
35 self.setObjectName("EditorAPIsPage") |
|
36 |
|
37 self.prepareApiButton.setText(self.trUtf8("Compile APIs")) |
|
38 self.__apisManager = APIsManager() |
|
39 self.__currentAPI = None |
|
40 self.__inPreparation = False |
|
41 |
|
42 self.apiFileCompleter = E4FileCompleter(self.apiFileEdit) |
|
43 |
|
44 # set initial values |
|
45 self.pluginManager = e4App().getObject("PluginManager") |
|
46 self.apiAutoPrepareCheckBox.setChecked(\ |
|
47 Preferences.getEditor("AutoPrepareAPIs")) |
|
48 |
|
49 self.apis = {} |
|
50 apiLanguages = [''] + QScintilla.Lexers.getSupportedLanguages().keys() |
|
51 apiLanguages.sort() |
|
52 for lang in apiLanguages: |
|
53 if lang != "Guessed": |
|
54 self.apiLanguageComboBox.addItem(lang) |
|
55 self.currentApiLanguage = '' |
|
56 self.on_apiLanguageComboBox_activated(self.currentApiLanguage) |
|
57 |
|
58 for lang in apiLanguages[1:]: |
|
59 self.apis[lang] = Preferences.getEditorAPI(lang)[:] |
|
60 |
|
61 def save(self): |
|
62 """ |
|
63 Public slot to save the Editor APIs configuration. |
|
64 """ |
|
65 Preferences.setEditor("AutoPrepareAPIs", |
|
66 int(self.apiAutoPrepareCheckBox.isChecked())) |
|
67 |
|
68 lang = self.apiLanguageComboBox.currentText() |
|
69 self.apis[lang] = self.__editorGetApisFromApiList() |
|
70 |
|
71 for lang, apis in self.apis.items(): |
|
72 Preferences.setEditorAPI(lang, apis) |
|
73 |
|
74 @pyqtSlot(str) |
|
75 def on_apiLanguageComboBox_activated(self, language): |
|
76 """ |
|
77 Private slot to fill the api listbox of the api page. |
|
78 |
|
79 @param language selected API language (string) |
|
80 """ |
|
81 if self.currentApiLanguage == language: |
|
82 return |
|
83 |
|
84 self.apis[self.currentApiLanguage] = self.__editorGetApisFromApiList() |
|
85 self.currentApiLanguage = language |
|
86 self.apiList.clear() |
|
87 |
|
88 if not language: |
|
89 self.apiGroup.setEnabled(False) |
|
90 return |
|
91 |
|
92 self.apiGroup.setEnabled(True) |
|
93 for api in self.apis[self.currentApiLanguage]: |
|
94 if api: |
|
95 self.apiList.addItem(api) |
|
96 self.__currentAPI = self.__apisManager.getAPIs(self.currentApiLanguage) |
|
97 if self.__currentAPI is not None: |
|
98 self.connect(self.__currentAPI, SIGNAL('apiPreparationFinished()'), |
|
99 self.__apiPreparationFinished) |
|
100 self.connect(self.__currentAPI, SIGNAL('apiPreparationCancelled()'), |
|
101 self.__apiPreparationCancelled) |
|
102 self.connect(self.__currentAPI, SIGNAL('apiPreparationStarted()'), |
|
103 self.__apiPreparationStarted) |
|
104 self.addInstalledApiFileButton.setEnabled(\ |
|
105 self.__currentAPI.installedAPIFiles() != "") |
|
106 else: |
|
107 self.addInstalledApiFileButton.setEnabled(False) |
|
108 |
|
109 self.addPluginApiFileButton.setEnabled( |
|
110 len(self.pluginManager.getPluginApiFiles(self.currentApiLanguage)) > 0) |
|
111 |
|
112 def __editorGetApisFromApiList(self): |
|
113 """ |
|
114 Private slot to retrieve the api filenames from the list. |
|
115 |
|
116 @return list of api filenames (list of strings) |
|
117 """ |
|
118 apis = [] |
|
119 for row in range(self.apiList.count()): |
|
120 apis.append(self.apiList.item(row).text()) |
|
121 return apis |
|
122 |
|
123 @pyqtSlot() |
|
124 def on_apiFileButton_clicked(self): |
|
125 """ |
|
126 Private method to select an api file. |
|
127 """ |
|
128 file = QFileDialog.getOpenFileName(\ |
|
129 self, |
|
130 self.trUtf8("Select API file"), |
|
131 self.apiFileEdit.text(), |
|
132 self.trUtf8("API File (*.api);;All Files (*)")) |
|
133 |
|
134 if file: |
|
135 self.apiFileEdit.setText(Utilities.toNativeSeparators(file)) |
|
136 |
|
137 @pyqtSlot() |
|
138 def on_addApiFileButton_clicked(self): |
|
139 """ |
|
140 Private slot to add the api file displayed to the listbox. |
|
141 """ |
|
142 file = self.apiFileEdit.text() |
|
143 if file: |
|
144 self.apiList.addItem(Utilities.toNativeSeparators(file)) |
|
145 self.apiFileEdit.clear() |
|
146 |
|
147 @pyqtSlot() |
|
148 def on_deleteApiFileButton_clicked(self): |
|
149 """ |
|
150 Private slot to delete the currently selected file of the listbox. |
|
151 """ |
|
152 crow = self.apiList.currentRow() |
|
153 if crow >= 0: |
|
154 itm = self.apiList.takeItem(crow) |
|
155 del itm |
|
156 |
|
157 @pyqtSlot() |
|
158 def on_addInstalledApiFileButton_clicked(self): |
|
159 """ |
|
160 Private slot to add an API file from the list of installed API files |
|
161 for the selected lexer language. |
|
162 """ |
|
163 installedAPIFiles = self.__currentAPI.installedAPIFiles() |
|
164 installedAPIFilesPath = QFileInfo(installedAPIFiles[0]).path() |
|
165 installedAPIFilesShort = [] |
|
166 for installedAPIFile in installedAPIFiles: |
|
167 installedAPIFilesShort.append(QFileInfo(installedAPIFile).fileName()) |
|
168 file, ok = QInputDialog.getItem(\ |
|
169 self, |
|
170 self.trUtf8("Add from installed APIs"), |
|
171 self.trUtf8("Select from the list of installed API files"), |
|
172 installedAPIFilesShort, |
|
173 0, False) |
|
174 if ok: |
|
175 self.apiList.addItem(Utilities.toNativeSeparators(\ |
|
176 QFileInfo(QDir(installedAPIFilesPath), file).absoluteFilePath())) |
|
177 |
|
178 @pyqtSlot() |
|
179 def on_addPluginApiFileButton_clicked(self): |
|
180 """ |
|
181 Private slot to add an API file from the list of API files installed |
|
182 by plugins for the selected lexer language. |
|
183 """ |
|
184 pluginAPIFiles = self.pluginManager.getPluginApiFiles(self.currentApiLanguage) |
|
185 pluginAPIFilesDict = {} |
|
186 for apiFile in pluginAPIFiles: |
|
187 pluginAPIFilesDict[QFileInfo(apiFile).fileName()] = apiFile |
|
188 file, ok = QInputDialog.getItem(\ |
|
189 self, |
|
190 self.trUtf8("Add from Plugin APIs"), |
|
191 self.trUtf8( |
|
192 "Select from the list of API files installed by plugins"), |
|
193 sorted(pluginAPIFilesDict.keys()), |
|
194 0, False) |
|
195 if ok: |
|
196 self.apiList.addItem(Utilities.toNativeSeparators(\ |
|
197 pluginAPIFilesDict[file])) |
|
198 |
|
199 @pyqtSlot() |
|
200 def on_prepareApiButton_clicked(self): |
|
201 """ |
|
202 Private slot to prepare the API file for the currently selected language. |
|
203 """ |
|
204 if self.__inPreparation: |
|
205 self.__currentAPI and self.__currentAPI.cancelPreparation() |
|
206 else: |
|
207 if self.__currentAPI is not None: |
|
208 self.__currentAPI.prepareAPIs(\ |
|
209 ondemand = True, |
|
210 rawList = [self.__editorGetApisFromApiList()]) |
|
211 |
|
212 def __apiPreparationFinished(self): |
|
213 """ |
|
214 Private method called after the API preparation has finished. |
|
215 """ |
|
216 self.prepareApiProgressBar.reset() |
|
217 self.prepareApiProgressBar.setRange(0, 100) |
|
218 self.prepareApiProgressBar.setValue(0) |
|
219 self.prepareApiButton.setText(self.trUtf8("Compile APIs")) |
|
220 self.__inPreparation = False |
|
221 |
|
222 def __apiPreparationCancelled(self): |
|
223 """ |
|
224 Private slot called after the API preparation has been cancelled. |
|
225 """ |
|
226 self.__apiPreparationFinished() |
|
227 |
|
228 def __apiPreparationStarted(self): |
|
229 """ |
|
230 Private method called after the API preparation has started. |
|
231 """ |
|
232 self.prepareApiProgressBar.setRange(0, 0) |
|
233 self.prepareApiProgressBar.setValue(0) |
|
234 self.prepareApiButton.setText(self.trUtf8("Cancel compilation")) |
|
235 self.__inPreparation = True |
|
236 |
|
237 def saveState(self): |
|
238 """ |
|
239 Public method to save the current state of the widget. |
|
240 |
|
241 @return index of the selected lexer language (integer) |
|
242 """ |
|
243 return self.apiLanguageComboBox.currentIndex() |
|
244 |
|
245 def setState(self, state): |
|
246 """ |
|
247 Public method to set the state of the widget. |
|
248 |
|
249 @param state state data generated by saveState |
|
250 """ |
|
251 self.apiLanguageComboBox.setCurrentIndex(state) |
|
252 self.on_apiLanguageComboBox_activated(self.apiLanguageComboBox.currentText()) |
|
253 |
|
254 def create(dlg): |
|
255 """ |
|
256 Module function to create the configuration page. |
|
257 |
|
258 @param dlg reference to the configuration dialog |
|
259 """ |
|
260 page = EditorAPIsPage() |
|
261 return page |