4 # This file is part of BeRTOS.
6 # Bertos is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 # As a special exception, you may use this file as part of a free software
21 # library without restriction. Specifically, if other files instantiate
22 # templates or use macros or inline functions from this file, or you compile
23 # this file and link it with other files to produce an executable, this
24 # file does not by itself cause the resulting executable to be covered by
25 # the GNU General Public License. This exception does not however
26 # invalidate any other reasons why the executable file might be covered by
27 # the GNU General Public License.
29 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
33 # Author: Lorenzo Berni <duplo@develer.com>
38 from PyQt4.QtGui import *
39 from BWizardPage import *
42 from bertos_utils import SupportedException
43 from DefineException import *
46 class BModulePage(BWizardPage):
48 Page of the wizard that permits to select and configurate the BeRTOS modules.
52 BWizardPage.__init__(self, UI_LOCATION + "/module_select.ui")
53 self.setTitle(self.tr("Configure the BeRTOS modules"))
54 self._control_group = QControlGroup()
55 ## special connection needed for the QControlGroup
56 self.connect(self._control_group, SIGNAL("stateChanged"), self.saveValue)
58 ## Overloaded BWizardPage methods ##
62 Overload of BWizardPage setupUi method.
64 self.pageContent.moduleTree.clear()
65 self.pageContent.moduleTree.setHeaderHidden(True)
66 self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
67 self.pageContent.propertyTable.horizontalHeader().setVisible(False)
68 self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
69 self.pageContent.propertyTable.verticalHeader().setVisible(False)
70 self.pageContent.propertyTable.setColumnCount(2)
71 self.pageContent.propertyTable.setRowCount(0)
72 self.pageContent.moduleLabel.setVisible(False)
73 self.pageContent.warningLabel.setVisible(False)
75 def connectSignals(self):
77 Overload of the BWizardPage connectSignals method.
79 self.connect(self.pageContent.moduleTree, SIGNAL("itemPressed(QTreeWidgetItem*, int)"), self.fillPropertyTable)
80 self.connect(self.pageContent.moduleTree, SIGNAL("itemPressed(QTreeWidgetItem*, int)"), self.moduleClicked)
81 self.connect(self.pageContent.moduleTree, SIGNAL("itemChanged(QTreeWidgetItem*, int)"), self.dependencyCheck)
82 self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self.showPropertyDescription)
86 Overload of the BWizardPage reloadData method.
88 QApplication.instance().setOverrideCursor(Qt.WaitCursor)
92 QApplication.instance().restoreOverrideCursor()
98 def moduleClicked(self, item, column):
99 self.setBold(item, False)
101 def fillPropertyTable(self):
103 Slot called when the user selects a module from the module tree.
104 Fills the property table using the configuration parameters defined in
107 module = self.currentModule()
110 supported = bertos_utils.isSupported(self.project(), module=module)
111 except SupportedException, e:
112 self.exceptionOccurred(self.tr("Error evaluating \"%1\" for module %2").arg(e.support_string).arg(module))
114 self._control_group.clear()
115 configuration = self.projectInfo("MODULES")[module]["configuration"]
116 module_description = self.projectInfo("MODULES")[module]["description"]
117 self.pageContent.moduleLabel.setText(module_description)
118 self.pageContent.moduleLabel.setVisible(True)
120 self.pageContent.warningLabel.setVisible(True)
121 selected_cpu = self.projectInfo("CPU_NAME")
122 self.pageContent.warningLabel.setText(self.tr("<font color='#FF0000'>Warning: the selected module, \
123 is not completely supported by the %1.</font>").arg(selected_cpu))
125 self.pageContent.warningLabel.setVisible(False)
126 self.pageContent.propertyTable.clear()
127 self.pageContent.propertyTable.setRowCount(0)
128 if configuration != "":
129 configurations = self.projectInfo("CONFIGURATIONS")[configuration]
130 param_list = sorted(configurations["paramlist"])
132 for i, property in param_list:
133 if "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "autoenabled":
134 # Doesn't show the hidden fields
137 param_supported = bertos_utils.isSupported(self.project(), property_id=(configuration, property))
138 except SupportedException, e:
139 self.exceptionOccurred(self.tr("Error evaluating \"%1\" for parameter %2").arg(e.support_string).arg(property))
140 param_supported = True
141 if not param_supported:
142 # Doesn't show the unsupported parameters
144 # Set the row count to the current index + 1
145 self.pageContent.propertyTable.setRowCount(index + 1)
146 item = QTableWidgetItem(configurations[property]["brief"])
147 item.setData(Qt.UserRole, qvariant_converter.convertString(property))
148 self.pageContent.propertyTable.setItem(index, 0, item)
149 if "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "boolean":
150 self.insertCheckBox(index, configurations[property]["value"])
151 elif "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "enum":
152 self.insertComboBox(index, configurations[property]["value"], configurations[property]["informations"]["value_list"])
153 elif "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "int":
154 self.insertSpinBox(index, configurations[property]["value"], configurations[property]["informations"])
156 # Not defined type, rendered as a text field
157 self.pageContent.propertyTable.setItem(index, 1, QTableWidgetItem(configurations[property]["value"]))
159 if self.pageContent.propertyTable.rowCount() == 0:
160 module_label = self.pageContent.moduleLabel.text()
161 module_label += "\n\nNo configuration needed."
162 self.pageContent.moduleLabel.setText(module_label)
164 self.pageContent.moduleLabel.setText("")
165 self.pageContent.moduleLabel.setVisible(False)
166 self.pageContent.propertyTable.clear()
167 self.pageContent.propertyTable.setRowCount(0)
169 def dependencyCheck(self, item):
171 Checks the dependencies of the module associated with the given item.
174 module = unicode(item.text(0))
175 if item.checkState(0) == Qt.Checked:
176 self.moduleSelected(module)
178 self.moduleUnselected(module)
179 self.removeFileDependencies(module)
181 def showPropertyDescription(self):
183 Slot called when the property selection changes. Shows the description
184 of the selected property.
186 self.resetPropertyDescription()
187 configurations = self.currentModuleConfigurations()
188 if self.currentProperty() in configurations:
189 description = configurations[self.currentProperty()]["brief"]
190 name = self.currentProperty()
191 self.currentPropertyItem().setText(description + "\n" + name)
193 def saveValue(self, index):
195 Slot called when the user modifies one of the configuration parameters.
196 It stores the new value."""
197 property = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
198 configuration = self.projectInfo("MODULES")[self.currentModule()]["configuration"]
199 configurations = self.projectInfo("CONFIGURATIONS")
200 if "type" not in configurations[configuration][property]["informations"] or configurations[configuration][property]["informations"]["type"] == "int":
201 configurations[configuration][property]["value"] = unicode(int(self.pageContent.propertyTable.cellWidget(index, 1).value()))
202 elif configurations[configuration][property]["informations"]["type"] == "enum":
203 configurations[configuration][property]["value"] = unicode(self.pageContent.propertyTable.cellWidget(index, 1).currentText())
204 elif configurations[configuration][property]["informations"]["type"] == "boolean":
205 if self.pageContent.propertyTable.cellWidget(index, 1).isChecked():
206 configurations[configuration][property]["value"] = "1"
208 configurations[configuration][property]["value"] = "0"
209 self.setProjectInfo("CONFIGURATIONS", configurations)
213 def loadModuleData(self):
215 Loads the module data.
217 # Load the module data only if it isn't already loaded
218 if not self.projectInfo("MODULES") \
219 and not self.projectInfo("LISTS") \
220 and not self.projectInfo("CONFIGURATIONS"):
222 bertos_utils.loadModuleData(self.project())
223 except ModuleDefineException, e:
224 self.exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
225 except EnumDefineException, e:
226 self.exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
227 except ConfigurationDefineException, e:
228 self.exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
230 def fillModuleTree(self):
232 Fills the module tree with the module entries separated in categories.
234 self.pageContent.moduleTree.clear()
235 modules = self.projectInfo("MODULES")
239 for module, information in modules.items():
240 if information["category"] not in categories:
241 categories[information["category"]] = []
242 categories[information["category"]].append(module)
243 for category, module_list in categories.items():
244 item = QTreeWidgetItem(QStringList([category]))
245 for module in module_list:
246 enabled = modules[module]["enabled"]
247 module_item = QTreeWidgetItem(item, QStringList([module]))
249 supported = bertos_utils.isSupported(self.project(), module=module)
250 except SupportedException, e:
251 self.exceptionOccurred(self.tr("Error evaluating \"%1\" for module %2").arg(e.support_string).arg(module))
254 module_item.setForeground(0, QBrush(QColor(Qt.red)))
256 module_item.setCheckState(0, Qt.Checked)
258 module_item.setCheckState(0, Qt.Unchecked)
259 self.pageContent.moduleTree.addTopLevelItem(item)
260 self.pageContent.moduleTree.sortItems(0, Qt.AscendingOrder)
261 self.fillPropertyTable()
263 def insertCheckBox(self, index, value):
265 Inserts in the table at index a checkbox for a boolean property setted
268 check_box = QCheckBox()
269 self.pageContent.propertyTable.setCellWidget(index, 1, check_box)
271 check_box.setChecked(True)
273 check_box.setChecked(False)
274 self._control_group.addControl(index, check_box)
276 def insertComboBox(self, index, value, value_list):
278 Inserts in the table at index a combobox for an enum property setted
282 enum = self.projectInfo("LISTS")[value_list]
283 combo_box = QComboBox()
284 self.pageContent.propertyTable.setCellWidget(index, 1, combo_box)
285 for i, element in enumerate(enum):
286 combo_box.addItem(element)
288 combo_box.setCurrentIndex(i)
289 self._control_group.addControl(index, combo_box)
291 self.exceptionOccurred(self.tr("Define list \"%1\" not found. Check definition files.").arg(value_list))
292 self.pageContent.propertyTable.setItem(index, 1, QTableWidgetItem(value))
294 def insertSpinBox(self, index, value, informations):
296 Inserts in the table at index a spinbox for an int, a long or an unsigned
297 long property setted to value.
299 # int, long or undefined type property
301 if bertos_utils.isLong(informations) or bertos_utils.isUnsignedLong(informations):
302 spin_box = QDoubleSpinBox()
303 spin_box.setDecimals(0)
305 spin_box = QSpinBox()
306 self.pageContent.propertyTable.setCellWidget(index, 1, spin_box)
310 if bertos_utils.isLong(informations):
311 minimum = -2147483648
314 elif bertos_utils.isUnsigned(informations):
318 elif bertos_utils.isUnsignedLong(informations):
322 if "min" in informations:
323 minimum = int(informations["min"])
324 if "max" in informations:
325 maximum = int(informations["max"])
326 spin_box.setRange(minimum, maximum)
327 spin_box.setSuffix(suff)
328 spin_box.setValue(int(value.replace("L", "").replace("U", "")))
329 self._control_group.addControl(index, spin_box)
332 def currentModule(self):
334 Retuns the current module name.
336 current_module = self.pageContent.moduleTree.currentItem()
337 # return only the child items
338 if current_module and current_module.parent():
339 return unicode(current_module.text(0))
343 def currentModuleConfigurations(self):
345 Returns the current module configuration.
347 return self.configurations(self.currentModule())
349 def currentProperty(self):
351 Rerturns the current property from the property table.
353 return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
355 def currentPropertyItem(self):
357 Returns the QTableWidgetItem of the current property.
359 return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
361 def configurations(self, module):
363 Returns the configuration for the selected module.
365 configuration = self.projectInfo("MODULES")[module]["configuration"]
366 if len(configuration) > 0:
367 return self.projectInfo("CONFIGURATIONS")[configuration]
371 def resetPropertyDescription(self):
373 Resets the label for each property table entry.
375 for index in range(self.pageContent.propertyTable.rowCount()):
376 property_name = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
377 # Awful solution! Needed because if the user change the module, the selection changed...
378 if property_name not in self.currentModuleConfigurations():
380 self.pageContent.propertyTable.item(index, 0).setText(self.currentModuleConfigurations()[property_name]['brief'])
382 def setBold(self, item, bold):
383 self.pageContent.moduleTree.blockSignals(True)
386 item.setFont(0, font)
387 self.pageContent.moduleTree.blockSignals(False)
389 def moduleSelected(self, selectedModule):
391 Resolves the selection dependencies.
393 modules = self.projectInfo("MODULES")
394 modules[selectedModule]["enabled"] = True
395 self.setProjectInfo("MODULES", modules)
396 depends = self.projectInfo("MODULES")[selectedModule]["depends"]
398 if self.pageContent.automaticFix.isChecked():
399 unsatisfied = self.selectDependencyCheck(selectedModule)
400 if len(unsatisfied) > 0:
401 for module in unsatisfied:
402 modules = self.projectInfo("MODULES")
403 modules[module]["enabled"] = True
404 for category in range(self.pageContent.moduleTree.topLevelItemCount()):
405 item = self.pageContent.moduleTree.topLevelItem(category)
406 for child in range(item.childCount()):
407 if unicode(item.child(child).text(0)) in unsatisfied:
408 self.setBold(item.child(child), True)
409 self.setBold(item, True)
410 item.child(child).setCheckState(0, Qt.Checked)
412 def moduleUnselected(self, unselectedModule):
414 Resolves the unselection dependencies.
416 modules = self.projectInfo("MODULES")
417 modules[unselectedModule]["enabled"] = False
418 self.setProjectInfo("MODULES", modules)
420 if self.pageContent.automaticFix.isChecked():
421 unsatisfied = self.unselectDependencyCheck(unselectedModule)
422 if len(unsatisfied) > 0:
423 message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to remove these modules too?")
424 message = message.arg(unselectedModule).arg(", ".join(unsatisfied))
425 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
426 if choice == QMessageBox.Yes:
427 for module in unsatisfied:
428 modules = self.projectInfo("MODULES")
429 modules[module]["enabled"] = False
430 for category in range(self.pageContent.moduleTree.topLevelItemCount()):
431 item = self.pageContent.moduleTree.topLevelItem(category)
432 for child in range(item.childCount()):
433 if unicode(item.child(child).text(0)) in unsatisfied:
434 item.child(child).setCheckState(0, Qt.Unchecked)
436 def selectDependencyCheck(self, module):
438 Returns the list of unsatisfied dependencies after a selection.
441 modules = self.projectInfo("MODULES")
442 files = self.projectInfo("FILES")
443 for dependency in modules[module]["depends"]:
444 if dependency in modules and not modules[dependency]["enabled"]:
445 unsatisfied |= set([dependency])
446 if dependency not in unsatisfied:
447 unsatisfied |= self.selectDependencyCheck(dependency)
448 if dependency not in modules:
449 if dependency in files:
450 files[dependency] += 1
452 files[dependency] = 1
453 self.setProjectInfo("FILES", files)
456 def unselectDependencyCheck(self, dependency):
458 Returns the list of unsatisfied dependencies after an unselection.
461 modules = self.projectInfo("MODULES")
462 for module, informations in modules.items():
463 if dependency in informations["depends"] and informations["enabled"]:
464 unsatisfied |= set([module])
465 if dependency not in unsatisfied:
466 unsatisfied |= self.unselectDependencyCheck(module)
469 def removeFileDependencies(self, module):
471 Removes the files dependencies of the given module.
473 modules = self.projectInfo("MODULES")
474 files = self.projectInfo("FILES")
475 dependencies = modules[module]["depends"]
476 for dependency in dependencies:
477 if dependency in files:
478 files[dependency] -= 1
479 if files[dependency] == 0:
480 del files[dependency]
481 self.setProjectInfo("FILES", files)
483 class QControlGroup(QObject):
485 Simple class that permit to connect different signals of different widgets
486 with a slot that emit a signal. Permits to group widget and to understand which of
487 them has sent the signal.
491 QObject.__init__(self)
494 def addControl(self, id, control):
498 self._controls[id] = control
499 if type(control) == QCheckBox:
500 self.connect(control, SIGNAL("stateChanged(int)"), lambda: self.stateChanged(id))
501 elif type(control) == QSpinBox:
502 self.connect(control, SIGNAL("valueChanged(int)"), lambda: self.stateChanged(id))
503 elif type(control) == QComboBox:
504 self.connect(control, SIGNAL("currentIndexChanged(int)"), lambda: self.stateChanged(id))
505 elif type(control) == QDoubleSpinBox:
506 self.connect(control, SIGNAL("valueChanged(double)"), lambda: self.stateChanged(id))
510 Remove all the controls.
514 def stateChanged(self, id):
516 Slot called when the value of one of the stored widget changes. It emits
519 self.emit(SIGNAL("stateChanged"), id)