4 # Copyright 2009 Develer S.r.l. (http://www.develer.com/)
9 # Author: Lorenzo Berni <duplo@develer.com>
14 from PyQt4.QtGui import *
15 from BWizardPage import *
18 from bertos_utils import SupportedException
19 from DefineException import *
22 class BModulePage(BWizardPage):
24 Page of the wizard that permits to select and configurate the BeRTOS modules.
28 BWizardPage.__init__(self, UI_LOCATION + "/module_select.ui")
29 self.setTitle(self.tr("Configure the BeRTOS modules"))
30 self._control_group = QControlGroup()
31 ## special connection needed for the QControlGroup
32 self.connect(self._control_group, SIGNAL("stateChanged"), self.saveValue)
34 ## Overloaded BWizardPage methods ##
38 Overload of BWizardPage setupUi method.
40 self.pageContent.moduleTree.clear()
41 self.pageContent.moduleTree.setHeaderHidden(True)
42 self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
43 self.pageContent.propertyTable.horizontalHeader().setVisible(False)
44 self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
45 self.pageContent.propertyTable.verticalHeader().setVisible(False)
46 self.pageContent.propertyTable.setColumnCount(2)
47 self.pageContent.propertyTable.setRowCount(0)
48 self.pageContent.moduleLabel.setVisible(False)
49 self.pageContent.warningLabel.setVisible(False)
51 def connectSignals(self):
53 Overload of the BWizardPage connectSignals method.
55 self.connect(self.pageContent.moduleTree, SIGNAL("itemPressed(QTreeWidgetItem*, int)"), self.fillPropertyTable)
56 self.connect(self.pageContent.moduleTree, SIGNAL("itemChanged(QTreeWidgetItem*, int)"), self.dependencyCheck)
57 self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self.showPropertyDescription)
61 Overload of the BWizardPage reloadData method.
63 QApplication.instance().setOverrideCursor(Qt.WaitCursor)
67 QApplication.instance().restoreOverrideCursor()
73 def fillPropertyTable(self):
75 Slot called when the user selects a module from the module tree.
76 Fills the property table using the configuration parameters defined in
79 module = self.currentModule()
82 supported = bertos_utils.isSupported(self.project(), module=module)
83 except SupportedException, e:
84 self.exceptionOccurred(self.tr("Error evaluating \"%1\" for module %2").arg(e.support_string).arg(module))
86 self._control_group.clear()
87 configuration = self.projectInfo("MODULES")[module]["configuration"]
88 module_description = self.projectInfo("MODULES")[module]["description"]
89 self.pageContent.moduleLabel.setText(module_description)
90 self.pageContent.moduleLabel.setVisible(True)
92 self.pageContent.warningLabel.setVisible(True)
93 selected_cpu = self.projectInfo("CPU_NAME")
94 self.pageContent.warningLabel.setText(self.tr("<font color='#FF0000'>Warning: the selected module, \
95 is not completely supported by the %1.</font>").arg(selected_cpu))
97 self.pageContent.warningLabel.setVisible(False)
98 self.pageContent.propertyTable.clear()
99 self.pageContent.propertyTable.setRowCount(0)
100 if configuration != "":
101 configurations = self.projectInfo("CONFIGURATIONS")[configuration]
102 param_list = sorted(configurations["paramlist"])
104 for i, property in param_list:
105 if "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "autoenabled":
106 # Doesn't show the hidden fields
109 param_supported = bertos_utils.isSupported(self.project(), property_id=(configuration, property))
110 except SupportedException, e:
111 self.exceptionOccurred(self.tr("Error evaluating \"%1\" for parameter %2").arg(e.support_string).arg(property))
112 param_supported = True
113 if not param_supported:
114 # Doesn't show the unsupported parameters
116 # Set the row count to the current index + 1
117 self.pageContent.propertyTable.setRowCount(index + 1)
118 item = QTableWidgetItem(configurations[property]["brief"])
119 item.setData(Qt.UserRole, qvariant_converter.convertString(property))
120 self.pageContent.propertyTable.setItem(index, 0, item)
121 if "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "boolean":
122 self.insertCheckBox(index, configurations[property]["value"])
123 elif "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "enum":
124 self.insertComboBox(index, configurations[property]["value"], configurations[property]["informations"]["value_list"])
125 elif "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "int":
126 self.insertSpinBox(index, configurations[property]["value"], configurations[property]["informations"])
128 # Not defined type, rendered as a text field
129 self.pageContent.propertyTable.setItem(index, 1, QTableWidgetItem(configurations[property]["value"]))
131 if self.pageContent.propertyTable.rowCount() == 0:
132 module_label = self.pageContent.moduleLabel.text()
133 module_label += "\n\nNo configuration needed."
134 self.pageContent.moduleLabel.setText(module_label)
136 def dependencyCheck(self, item):
138 Checks the dependencies of the module associated with the given item.
141 module = unicode(item.text(0))
142 if item.checkState(0) == Qt.Checked:
143 self.moduleSelected(module)
145 self.moduleUnselected(module)
146 self.removeFileDependencies(module)
148 def showPropertyDescription(self):
150 Slot called when the property selection changes. Shows the description
151 of the selected property.
153 self.resetPropertyDescription()
154 configurations = self.currentModuleConfigurations()
155 if self.currentProperty() in configurations:
156 description = configurations[self.currentProperty()]["brief"]
157 name = self.currentProperty()
158 self.currentPropertyItem().setText(description + "\n" + name)
160 def saveValue(self, index):
162 Slot called when the user modifies one of the configuration parameters.
163 It stores the new value."""
164 property = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
165 configuration = self.projectInfo("MODULES")[self.currentModule()]["configuration"]
166 configurations = self.projectInfo("CONFIGURATIONS")
167 if "type" not in configurations[configuration][property]["informations"] or configurations[configuration][property]["informations"]["type"] == "int":
168 configurations[configuration][property]["value"] = str(int(self.pageContent.propertyTable.cellWidget(index, 1).value()))
169 elif configurations[configuration][property]["informations"]["type"] == "enum":
170 configurations[configuration][property]["value"] = unicode(self.pageContent.propertyTable.cellWidget(index, 1).currentText())
171 elif configurations[configuration][property]["informations"]["type"] == "boolean":
172 if self.pageContent.propertyTable.cellWidget(index, 1).isChecked():
173 configurations[configuration][property]["value"] = "1"
175 configurations[configuration][property]["value"] = "0"
176 self.setProjectInfo("CONFIGURATIONS", configurations)
180 def loadModuleData(self):
182 Loads the module data.
184 # Load the module data only if it isn't already loaded
185 if not self.projectInfo("MODULES") \
186 and not self.projectInfo("LISTS") \
187 and not self.projectInfo("CONFIGURATIONS"):
189 bertos_utils.loadModuleData(self.project())
190 except ModuleDefineException, e:
191 self.exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
192 except EnumDefineException, e:
193 self.exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
194 except ConfigurationDefineException, e:
195 self.exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
197 def fillModuleTree(self):
199 Fills the module tree with the module entries separated in categories.
201 modules = self.projectInfo("MODULES")
205 for module, information in modules.items():
206 if information["category"] not in categories:
207 categories[information["category"]] = []
208 categories[information["category"]].append(module)
209 for category, module_list in categories.items():
210 item = QTreeWidgetItem(QStringList([category]))
211 for module in module_list:
212 enabled = modules[module]["enabled"]
213 module_item = QTreeWidgetItem(item, QStringList([module]))
215 supported = bertos_utils.isSupported(self.project(), module=module)
216 except SupportedException, e:
217 self.exceptionOccurred(self.tr("Error evaluating \"%1\" for module %2").arg(e.support_string).arg(module))
220 module_item.setForeground(0, QBrush(QColor(Qt.red)))
222 module_item.setCheckState(0, Qt.Checked)
224 module_item.setCheckState(0, Qt.Unchecked)
225 self.pageContent.moduleTree.addTopLevelItem(item)
226 self.pageContent.moduleTree.sortItems(0, Qt.AscendingOrder)
228 def insertCheckBox(self, index, value):
230 Inserts in the table at index a checkbox for a boolean property setted
233 check_box = QCheckBox()
234 self.pageContent.propertyTable.setCellWidget(index, 1, check_box)
236 check_box.setChecked(True)
238 check_box.setChecked(False)
239 self._control_group.addControl(index, check_box)
241 def insertComboBox(self, index, value, value_list):
243 Inserts in the table at index a combobox for an enum property setted
247 enum = self.projectInfo("LISTS")[value_list]
248 combo_box = QComboBox()
249 self.pageContent.propertyTable.setCellWidget(index, 1, combo_box)
250 for i, element in enumerate(enum):
251 combo_box.addItem(element)
253 combo_box.setCurrentIndex(i)
254 self._control_group.addControl(index, combo_box)
256 self.exceptionOccurred(self.tr("Define list \"%1\" not found. Check definition files.").arg(value_list))
257 self.pageContent.propertyTable.setItem(index, 1, QTableWidgetItem(value))
259 def insertSpinBox(self, index, value, informations):
261 Inserts in the table at index a spinbox for an int, a long or an unsigned
262 long property setted to value.
264 # int, long or undefined type property
266 if bertos_utils.isLong(informations) or bertos_utils.isUnsignedLong(informations):
267 spin_box = QDoubleSpinBox()
268 spin_box.setDecimals(0)
270 spin_box = QSpinBox()
271 self.pageContent.propertyTable.setCellWidget(index, 1, spin_box)
275 if bertos_utils.isLong(informations):
276 minimum = -2147483648
279 elif bertos_utils.isUnsigned(informations):
283 elif bertos_utils.isUnsignedLong(informations):
287 if "min" in informations:
288 minimum = int(informations["min"])
289 if "max" in informations:
290 maximum = int(informations["max"])
291 spin_box.setRange(minimum, maximum)
292 spin_box.setSuffix(suff)
293 spin_box.setValue(int(value.replace("L", "").replace("U", "")))
294 self._control_group.addControl(index, spin_box)
297 def currentModule(self):
299 Retuns the current module name.
301 current_module = self.pageContent.moduleTree.currentItem()
302 # return only the child items
303 if current_module and current_module.parent():
304 return unicode(current_module.text(0))
308 def currentModuleConfigurations(self):
310 Returns the current module configuration.
312 return self.configurations(self.currentModule())
314 def currentProperty(self):
316 Rerturns the current property from the property table.
318 return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
320 def currentPropertyItem(self):
322 Returns the QTableWidgetItem of the current property.
324 return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
326 def configurations(self, module):
328 Returns the configuration for the selected module.
330 configuration = self.projectInfo("MODULES")[module]["configuration"]
331 if len(configuration) > 0:
332 return self.projectInfo("CONFIGURATIONS")[configuration]
336 def resetPropertyDescription(self):
338 Resets the label for each property table entry.
340 for index in range(self.pageContent.propertyTable.rowCount()):
341 property_name = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
342 # Awful solution! Needed because if the user change the module, the selection changed...
343 if property_name not in self.currentModuleConfigurations():
345 self.pageContent.propertyTable.item(index, 0).setText(self.currentModuleConfigurations()[property_name]['brief'])
347 def moduleSelected(self, selectedModule):
349 Resolves the selection dependencies.
351 modules = self.projectInfo("MODULES")
352 modules[selectedModule]["enabled"] = True
353 self.setProjectInfo("MODULES", modules)
354 depends = self.projectInfo("MODULES")[selectedModule]["depends"]
356 if self.pageContent.automaticFix.isChecked():
357 unsatisfied = self.selectDependencyCheck(selectedModule)
358 if len(unsatisfied) > 0:
359 for module in unsatisfied:
360 modules = self.projectInfo("MODULES")
361 modules[module]["enabled"] = True
362 for category in range(self.pageContent.moduleTree.topLevelItemCount()):
363 item = self.pageContent.moduleTree.topLevelItem(category)
364 for child in range(item.childCount()):
365 if unicode(item.child(child).text(0)) in unsatisfied:
366 item.child(child).setCheckState(0, Qt.Checked)
368 def moduleUnselected(self, unselectedModule):
370 Resolves the unselection dependencies.
372 modules = self.projectInfo("MODULES")
373 modules[unselectedModule]["enabled"] = False
374 self.setProjectInfo("MODULES", modules)
376 if self.pageContent.automaticFix.isChecked():
377 unsatisfied = self.unselectDependencyCheck(unselectedModule)
378 if len(unsatisfied) > 0:
379 message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to remove these modules too?")
380 message = message.arg(unselectedModule).arg(", ".join(unsatisfied))
381 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
382 if choice == QMessageBox.Yes:
383 for module in unsatisfied:
384 modules = self.projectInfo("MODULES")
385 modules[module]["enabled"] = False
386 for category in range(self.pageContent.moduleTree.topLevelItemCount()):
387 item = self.pageContent.moduleTree.topLevelItem(category)
388 for child in range(item.childCount()):
389 if unicode(item.child(child).text(0)) in unsatisfied:
390 item.child(child).setCheckState(0, Qt.Unchecked)
392 def selectDependencyCheck(self, module):
394 Returns the list of unsatisfied dependencies after a selection.
397 modules = self.projectInfo("MODULES")
398 files = self.projectInfo("FILES")
399 for dependency in modules[module]["depends"]:
400 if dependency in modules and not modules[dependency]["enabled"]:
401 unsatisfied |= set([dependency])
402 if dependency not in unsatisfied:
403 unsatisfied |= self.selectDependencyCheck(dependency)
404 if dependency not in modules:
405 if dependency in files:
406 files[dependency] += 1
408 files[dependency] = 1
409 self.setProjectInfo("FILES", files)
412 def unselectDependencyCheck(self, dependency):
414 Returns the list of unsatisfied dependencies after an unselection.
417 modules = self.projectInfo("MODULES")
418 for module, informations in modules.items():
419 if dependency in informations["depends"] and informations["enabled"]:
420 unsatisfied |= set([module])
421 if dependency not in unsatisfied:
422 unsatisfied |= self.unselectDependencyCheck(module)
425 def removeFileDependencies(self, module):
427 Removes the files dependencies of the given module.
429 modules = self.projectInfo("MODULES")
430 files = self.projectInfo("FILES")
431 dependencies = modules[module]["depends"]
432 for dependency in dependencies:
433 if dependency in files:
434 files[dependency] -= 1
435 if files[dependency] == 0:
436 del files[dependency]
437 self.setProjectInfo("FILES", files)
439 class QControlGroup(QObject):
441 Simple class that permit to connect different signals of different widgets
442 with a slot that emit a signal. Permits to group widget and to understand which of
443 them has sent the signal.
447 QObject.__init__(self)
450 def addControl(self, id, control):
454 self._controls[id] = control
455 if type(control) == QCheckBox:
456 self.connect(control, SIGNAL("stateChanged(int)"), lambda: self.stateChanged(id))
457 elif type(control) == QSpinBox:
458 self.connect(control, SIGNAL("valueChanged(int)"), lambda: self.stateChanged(id))
459 elif type(control) == QComboBox:
460 self.connect(control, SIGNAL("currentIndexChanged(int)"), lambda: self.stateChanged(id))
461 elif type(control) == QDoubleSpinBox:
462 self.connect(control, SIGNAL("valueChanged(double)"), lambda: self.stateChanged(id))
466 Remove all the controls.
470 def stateChanged(self, id):
472 Slot called when the value of one of the stored widget changes. It emits
475 self.emit(SIGNAL("stateChanged"), id)