|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2018 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to enter some IDL compiler options. |
|
8 """ |
|
9 |
|
10 from PyQt5.QtCore import pyqtSlot, Qt |
|
11 from PyQt5.QtWidgets import QDialog, QTreeWidgetItem, QInputDialog |
|
12 |
|
13 from .Ui_IdlCompilerOptionsDialog import Ui_IdlCompilerOptionsDialog |
|
14 |
|
15 import UI.PixmapCache |
|
16 |
|
17 from E5Gui import E5PathPickerDialog |
|
18 from E5Gui.E5PathPicker import E5PathPickerModes |
|
19 |
|
20 |
|
21 class IdlCompilerOptionsDialog(QDialog, Ui_IdlCompilerOptionsDialog): |
|
22 """ |
|
23 Class implementing a dialog to enter some IDL compiler options. |
|
24 """ |
|
25 def __init__(self, includeDirectories, definedNames, undefinedNames, |
|
26 project=None, parent=None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param includeDirectories list of include directories |
|
31 @type list of str |
|
32 @param definedNames list of defined variables with name and value |
|
33 separated by '=' |
|
34 @type list of str |
|
35 @param undefinedNames list of undefined names |
|
36 @type list of str |
|
37 @param projectDirectory directory name of the project |
|
38 @type str |
|
39 @param parent reference to the parent widget |
|
40 @type QWidget |
|
41 """ |
|
42 super(IdlCompilerOptionsDialog, self).__init__(parent) |
|
43 self.setupUi(self) |
|
44 |
|
45 self.__project = project |
|
46 |
|
47 self.idAddButton.setIcon(UI.PixmapCache.getIcon("plus.png")) |
|
48 self.idDeleteButton.setIcon(UI.PixmapCache.getIcon("minus.png")) |
|
49 self.idEditButton.setIcon(UI.PixmapCache.getIcon("edit.png")) |
|
50 |
|
51 self.dnAddButton.setIcon(UI.PixmapCache.getIcon("plus.png")) |
|
52 self.dnDeleteButton.setIcon(UI.PixmapCache.getIcon("minus.png")) |
|
53 self.dnEditButton.setIcon(UI.PixmapCache.getIcon("edit.png")) |
|
54 |
|
55 self.unAddButton.setIcon(UI.PixmapCache.getIcon("plus.png")) |
|
56 self.unDeleteButton.setIcon(UI.PixmapCache.getIcon("minus.png")) |
|
57 self.unEditButton.setIcon(UI.PixmapCache.getIcon("edit.png")) |
|
58 |
|
59 self.__populateIncludeDirectoriesList(includeDirectories) |
|
60 self.__populateDefineNamesList(definedNames) |
|
61 self.unList.addItems(undefinedNames) |
|
62 |
|
63 self.__updateIncludeDirectoryButtons() |
|
64 self.__updateDefineNameButtons() |
|
65 self.__updateUndefineNameButtons() |
|
66 |
|
67 ####################################################################### |
|
68 ## Methods implementing the 'Include Directory' option |
|
69 ####################################################################### |
|
70 |
|
71 def __updateIncludeDirectoryButtons(self): |
|
72 """ |
|
73 Private method to set the state of the 'Include Directory' buttons. |
|
74 """ |
|
75 enable = len(self.idList.selectedItems()) |
|
76 self.idDeleteButton.setEnabled(enable) |
|
77 self.idEditButton.setEnabled(enable) |
|
78 |
|
79 def __populateIncludeDirectoriesList(self, includeDirectories): |
|
80 """ |
|
81 Private method to populate the 'Include Directories' list. |
|
82 |
|
83 @param includeDirectories list of include directories |
|
84 @type list of str |
|
85 """ |
|
86 for directory in includeDirectories: |
|
87 if self.__project: |
|
88 path = self.__project.getRelativeUniversalPath(directory) |
|
89 if not path: |
|
90 # it is the project directory |
|
91 path = "." |
|
92 self.idList.addItem(path) |
|
93 else: |
|
94 self.idList.addItem(directory) |
|
95 |
|
96 def __generateIncludeDirectoriesList(self): |
|
97 """ |
|
98 Private method to prepare the list of 'Include Directories'. |
|
99 |
|
100 @return list of 'Include Directories' |
|
101 @rtype list of str |
|
102 """ |
|
103 return [ |
|
104 self.idList.item(row).text() |
|
105 for row in range(self.idList.count()) |
|
106 ] |
|
107 |
|
108 def __includeDirectoriesContain(self, directory): |
|
109 """ |
|
110 Private method to test, if the currently defined 'Include Directories' |
|
111 contain a given one. |
|
112 |
|
113 @param directory directory name to be tested |
|
114 @type str |
|
115 @return flag indicating that the given directory is already included |
|
116 @rtype bool |
|
117 """ |
|
118 for row in range(self.idList.count()): |
|
119 if self.idList.item(row).text() == directory: |
|
120 return True |
|
121 |
|
122 return False |
|
123 |
|
124 @pyqtSlot() |
|
125 def on_idList_itemSelectionChanged(self): |
|
126 """ |
|
127 Private slot handling the selection of an 'Include Directory' entry. |
|
128 """ |
|
129 self.__updateIncludeDirectoryButtons() |
|
130 |
|
131 @pyqtSlot() |
|
132 def on_idAddButton_clicked(self): |
|
133 """ |
|
134 Private slot to add an 'Include Directory'. |
|
135 """ |
|
136 if self.__project: |
|
137 defaultDirectory = self.__project.getProjectPath() |
|
138 else: |
|
139 defaultDirectory = "" |
|
140 path, ok = E5PathPickerDialog.getPath( |
|
141 self, |
|
142 self.tr("Include Directory"), |
|
143 self.tr("Select Include Directory"), |
|
144 E5PathPickerModes.DirectoryShowFilesMode, |
|
145 defaultDirectory=defaultDirectory |
|
146 ) |
|
147 if ok and path: |
|
148 if self.__project: |
|
149 path = self.__project.getRelativeUniversalPath(path) |
|
150 if not path: |
|
151 path = "." |
|
152 if not self.__includeDirectoriesContain(path): |
|
153 self.idList.addItem(path) |
|
154 |
|
155 @pyqtSlot() |
|
156 def on_idDeleteButton_clicked(self): |
|
157 """ |
|
158 Private slot to delete the selected 'Include Directory' entry. |
|
159 """ |
|
160 itm = self.idList.selectedItems()[0] |
|
161 row = self.idList.row(itm) |
|
162 self.idList.takeItem(row) |
|
163 del itm |
|
164 |
|
165 @pyqtSlot() |
|
166 def on_idEditButton_clicked(self): |
|
167 """ |
|
168 Private slot to edit the selected 'Include Directory' entry. |
|
169 """ |
|
170 itm = self.idList.selectedItems()[0] |
|
171 if self.__project: |
|
172 path = self.__project.getAbsoluteUniversalPath(itm.text()) |
|
173 defaultDirectory = self.__project.getProjectPath() |
|
174 else: |
|
175 path = itm.text() |
|
176 defaultDirectory = "" |
|
177 path, ok = E5PathPickerDialog.getPath( |
|
178 self, |
|
179 self.tr("Include Directory"), |
|
180 self.tr("Select Include Directory"), |
|
181 E5PathPickerModes.DirectoryShowFilesMode, |
|
182 path=path, |
|
183 defaultDirectory=defaultDirectory |
|
184 ) |
|
185 if ok and path: |
|
186 if self.__project: |
|
187 path = self.__project.getRelativeUniversalPath(path) |
|
188 if not path: |
|
189 path = "." |
|
190 if self.__includeDirectoriesContain(path) and itm.text() != path: |
|
191 # the entry exists already, delete the edited one |
|
192 row = self.idList.row(itm) |
|
193 self.idList.takeItem(row) |
|
194 del itm |
|
195 else: |
|
196 itm.setText(path) |
|
197 |
|
198 ####################################################################### |
|
199 ## Methods implementing the 'Define Name' option |
|
200 ####################################################################### |
|
201 |
|
202 def __updateDefineNameButtons(self): |
|
203 """ |
|
204 Private method to set the state of the 'Define Name' buttons. |
|
205 """ |
|
206 enable = len(self.dnList.selectedItems()) |
|
207 self.dnDeleteButton.setEnabled(enable) |
|
208 self.dnEditButton.setEnabled(enable) |
|
209 |
|
210 def __populateDefineNamesList(self, definedNames): |
|
211 """ |
|
212 Private method to populate the list of defined names. |
|
213 |
|
214 @param definedNames list of defined variables with name and value |
|
215 separated by '=' |
|
216 @type list of str |
|
217 """ |
|
218 for definedName in definedNames: |
|
219 if definedName: |
|
220 nameValueList = definedName.split("=") |
|
221 name = nameValueList[0].strip() |
|
222 if len(nameValueList) > 1: |
|
223 value = nameValueList[1].strip() |
|
224 else: |
|
225 value = "" |
|
226 QTreeWidgetItem(self.dnList, [name, value]) |
|
227 |
|
228 self.dnList.sortItems(0, Qt.AscendingOrder) |
|
229 |
|
230 @pyqtSlot() |
|
231 def on_dnList_itemSelectionChanged(self): |
|
232 """ |
|
233 Private slot handling the selection of a 'Define Name' entry. |
|
234 """ |
|
235 self.__updateDefineNameButtons() |
|
236 |
|
237 @pyqtSlot() |
|
238 def on_dnAddButton_clicked(self): |
|
239 """ |
|
240 Slot documentation goes here. |
|
241 """ |
|
242 # TODO: not implemented yet |
|
243 raise NotImplementedError |
|
244 |
|
245 @pyqtSlot() |
|
246 def on_dnDeleteButton_clicked(self): |
|
247 """ |
|
248 Slot documentation goes here. |
|
249 """ |
|
250 # TODO: not implemented yet |
|
251 raise NotImplementedError |
|
252 |
|
253 @pyqtSlot() |
|
254 def on_dnEditButton_clicked(self): |
|
255 """ |
|
256 Slot documentation goes here. |
|
257 """ |
|
258 # TODO: not implemented yet |
|
259 raise NotImplementedError |
|
260 |
|
261 ####################################################################### |
|
262 ## Methods implementing the 'Undefine Name' option |
|
263 ####################################################################### |
|
264 |
|
265 def __updateUndefineNameButtons(self): |
|
266 """ |
|
267 Private method to set the state of the 'Undefine Name' buttons. |
|
268 """ |
|
269 enable = len(self.unList.selectedItems()) |
|
270 self.unDeleteButton.setEnabled(enable) |
|
271 self.unEditButton.setEnabled(enable) |
|
272 |
|
273 def __generateUndefinedNamesList(self): |
|
274 """ |
|
275 Private method to prepare the list of 'Undefined Names'. |
|
276 |
|
277 @return list of 'Undefined Names' |
|
278 @rtype list of str |
|
279 """ |
|
280 return [ |
|
281 self.unList.item(row).text() |
|
282 for row in range(self.unList.count()) |
|
283 ] |
|
284 |
|
285 def __undefinedNamesContain(self, name): |
|
286 """ |
|
287 Private method to test, if the currently defined 'Undefined Names' |
|
288 contain a given one. |
|
289 |
|
290 @param name variable name to be tested |
|
291 @type str |
|
292 @return flag indicating that the given name is already included |
|
293 @rtype bool |
|
294 """ |
|
295 for row in range(self.unList.count()): |
|
296 if self.unList.item(row).text() == name: |
|
297 return True |
|
298 |
|
299 return False |
|
300 |
|
301 @pyqtSlot() |
|
302 def on_unList_itemSelectionChanged(self): |
|
303 """ |
|
304 Private slot handling the selection of a 'Undefine Name' entry. |
|
305 """ |
|
306 self.__updateUndefineNameButtons() |
|
307 |
|
308 @pyqtSlot() |
|
309 def on_unAddButton_clicked(self): |
|
310 """ |
|
311 Private slot to add a 'Undefine Name' entry. |
|
312 """ |
|
313 name, ok = QInputDialog.getText( |
|
314 self, |
|
315 self.tr("Undefine Name"), |
|
316 self.tr("Enter a variable name to be undefined:") |
|
317 ) |
|
318 name = name.strip() |
|
319 if ok and name and not self.__undefinedNamesContain(name): |
|
320 self.unList.addItem(name) |
|
321 |
|
322 @pyqtSlot() |
|
323 def on_unDeleteButton_clicked(self): |
|
324 """ |
|
325 Private slot to delete the selected 'Undefine Name' entry. |
|
326 """ |
|
327 itm = self.unList.selectedItems()[0] |
|
328 row = self.unList.row(itm) |
|
329 self.unList.takeItem(row) |
|
330 del itm |
|
331 |
|
332 @pyqtSlot() |
|
333 def on_unEditButton_clicked(self): |
|
334 """ |
|
335 Private slot to edit the selected 'Undefine Name' entry. |
|
336 """ |
|
337 itm = self.unList.selectedItems()[0] |
|
338 name, ok = QInputDialog.getText( |
|
339 self, |
|
340 self.tr("Undefine Name"), |
|
341 self.tr("Enter a variable name to be undefined:"), |
|
342 text=itm.text() |
|
343 ) |
|
344 name = name.strip() |
|
345 if ok and name: |
|
346 if self.__undefinedNamesContain(name) and itm.text() != name: |
|
347 # the entry exists already, delete the edited one |
|
348 row = self.unList.row(itm) |
|
349 self.unList.takeItem(row) |
|
350 del itm |
|
351 else: |
|
352 itm.setText(name) |
|
353 |
|
354 ####################################################################### |
|
355 ## Methods implementing the result preparation |
|
356 ####################################################################### |
|
357 |
|
358 def getData(self): |
|
359 """ |
|
360 Public method to return the data entered by the user. |
|
361 |
|
362 @return tuple containing the list of include directories, list of |
|
363 defined names and list of undefined names |
|
364 @rtype tuple of (list of str, list of str, list of str) |
|
365 """ |
|
366 return ( |
|
367 self.__generateIncludeDirectoriesList(), |
|
368 [], |
|
369 self.__generateUndefinedNamesList(), |
|
370 ) |