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 DefineException import *
21 class BModulePage(BWizardPage):
24 BWizardPage.__init__(self, UI_LOCATION + "/module_select.ui")
25 self.setTitle(self.tr("Configure the BeRTOS modules"))
26 self._control_group = QControlGroup()
27 self._connectSignals()
30 QApplication.instance().setOverrideCursor(Qt.WaitCursor)
32 self._loadModuleData()
33 self._fillModuleTree()
34 QApplication.instance().restoreOverrideCursor()
36 def _setupButtonGroup(self):
37 self._button_group = QButtonGroup()
38 self._button_group.setExclusive(False)
39 self.connect(self._button_group, SIGNAL("buttonClicked(int)"), self._moduleSelectionChanged)
41 def _loadModuleData(self):
42 ## Load the module data only if it isn't already loaded
43 if self._projectInfoRetrieve("MODULES") == None \
44 and self._projectInfoRetrieve("LISTS") == None \
45 and self._projectInfoRetrieve("CONFIGURATIONS") == None:
47 bertos_utils.loadModuleData(self._project())
48 except ModuleDefineException, e:
49 self._exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
50 except EnumDefineException, e:
51 self._exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
52 except ConfigurationDefineException, e:
53 self._exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
55 def _fillModuleTree(self):
56 modules = self._projectInfoRetrieve("MODULES")
60 for module, information in modules.items():
61 if information["category"] not in categories.keys():
62 categories[information["category"]] = []
63 categories[information["category"]].append(module)
64 for category, module_list in categories.items():
65 item = QTreeWidgetItem(QStringList([category]))
66 for module in module_list:
67 enabled = modules[module]["enabled"]
68 module_item = QTreeWidgetItem(item, QStringList([module]))
70 module_item.setCheckState(0, Qt.Checked)
72 module_item.setCheckState(0, Qt.Unchecked)
73 self.pageContent.moduleTree.addTopLevelItem(item)
74 self.pageContent.moduleTree.sortItems(0, Qt.AscendingOrder)
77 def _fillPropertyTable(self):
78 module = self._currentModule()
79 if module is not None:
80 self._control_group.clear()
81 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
82 module_description = self._projectInfoRetrieve("MODULES")[module]["description"]
83 self.pageContent.moduleLabel.setText(module_description)
84 self.pageContent.moduleLabel.setVisible(True)
85 self.pageContent.propertyTable.clear()
86 self.pageContent.propertyTable.setRowCount(0)
87 if configuration != "":
88 configurations = self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
89 param_list = sorted(configurations["paramlist"])
91 for i, property in param_list:
92 if "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "autoenabled":
93 ## Doesn't show the hidden fields
96 ## Set the row count to the current index + 1
97 self.pageContent.propertyTable.setRowCount(index + 1)
98 item = QTableWidgetItem(configurations[property]["brief"])
99 item.setData(Qt.UserRole, qvariant_converter.convertString(property))
100 self.pageContent.propertyTable.setItem(index, 0, item)
101 if "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "boolean":
102 self._insertCheckBox(index, configurations[property]["value"])
103 elif "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "enum":
104 self._insertComboBox(index, configurations[property]["value"], configurations[property]["informations"]["value_list"])
105 elif "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "int":
106 self._insertSpinBox(index, configurations[property]["value"], configurations[property]["informations"])
108 # Not defined type, rendered as a text field
109 self.pageContent.propertyTable.setItem(index, 1, QTableWidgetItem(configurations[property]["value"]))
111 if self.pageContent.propertyTable.rowCount() == 0:
112 module_label = self.pageContent.moduleLabel.text()
113 module_label += "\n\nNo configuration needed."
114 self.pageContent.moduleLabel.setText(module_label)
116 def _insertCheckBox(self, index, value):
118 check_box = QCheckBox()
119 self.pageContent.propertyTable.setCellWidget(index, 1, check_box)
121 check_box.setChecked(True)
123 check_box.setChecked(False)
124 self._control_group.addControl(index, check_box)
126 def _insertComboBox(self, index, value, value_list):
129 enum = self._projectInfoRetrieve("LISTS")[value_list]
130 combo_box = QComboBox()
131 self.pageContent.propertyTable.setCellWidget(index, 1, combo_box)
132 for i, element in enumerate(enum):
133 combo_box.addItem(element)
135 combo_box.setCurrentIndex(i)
136 self._control_group.addControl(index, combo_box)
138 self._exceptionOccurred(self.tr("Define list \"%1\" not found. Check definition files.").arg(value_list))
139 self.pageContent.propertyTable.setItem(index, 1, QTableWidgetItem(value))
141 def _insertSpinBox(self, index, value, informations):
142 ## int, long or undefined type property
144 if bertos_utils.isLong(informations) or bertos_utils.isUnsignedLong(informations):
145 spin_box = QDoubleSpinBox()
146 spin_box.setDecimals(0)
148 spin_box = QSpinBox()
149 self.pageContent.propertyTable.setCellWidget(index, 1, spin_box)
153 if bertos_utils.isLong(informations):
154 minimum = -2147483648
157 elif bertos_utils.isUnsigned(informations):
161 elif bertos_utils.isUnsignedLong(informations):
165 if "min" in informations.keys():
166 minimum = int(informations["min"])
167 if "max" in informations.keys():
168 maximum = int(informations["max"])
169 spin_box.setRange(minimum, maximum)
170 spin_box.setSuffix(suff)
171 spin_box.setValue(int(value.replace("L", "").replace("U", "")))
172 self._control_group.addControl(index, spin_box)
175 def _currentModule(self):
176 current_module = self.pageContent.moduleTree.currentItem()
177 # return only the child items
178 if current_module is not None and current_module.parent() is not None:
179 return unicode(current_module.text(0))
183 def _currentModuleConfigurations(self):
184 return self._configurations(self._currentModule())
186 def _currentProperty(self):
187 return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
189 def _currentPropertyItem(self):
190 return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
192 def _configurations(self, module):
193 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
194 if len(configuration) > 0:
195 return self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
199 def _resetPropertyDescription(self):
200 for index in range(self.pageContent.propertyTable.rowCount()):
201 property_name = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
202 # Awful solution! Needed because if the user change the module, the selection changed...
203 if property_name not in self._currentModuleConfigurations().keys():
205 self.pageContent.propertyTable.item(index, 0).setText(self._currentModuleConfigurations()[property_name]['brief'])
207 def _showPropertyDescription(self):
208 self._resetPropertyDescription()
209 configurations = self._currentModuleConfigurations()
210 if self._currentProperty() in configurations.keys():
211 description = configurations[self._currentProperty()]["brief"]
212 name = self._currentProperty()
213 self._currentPropertyItem().setText(description + "\n" + name)
216 self.pageContent.moduleTree.clear()
217 self.pageContent.moduleTree.setHeaderHidden(True)
218 self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
219 self.pageContent.propertyTable.horizontalHeader().setVisible(False)
220 self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
221 self.pageContent.propertyTable.verticalHeader().setVisible(False)
222 self.pageContent.propertyTable.setColumnCount(2)
223 self.pageContent.propertyTable.setRowCount(0)
224 self.pageContent.moduleLabel.setVisible(False)
226 def _connectSignals(self):
227 self.connect(self.pageContent.moduleTree, SIGNAL("itemPressed(QTreeWidgetItem*, int)"), self._fillPropertyTable)
228 self.connect(self.pageContent.moduleTree, SIGNAL("itemChanged(QTreeWidgetItem*, int)"), self._dependencyCheck)
229 self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self._showPropertyDescription)
230 self.connect(self._control_group, SIGNAL("stateChanged"), self._saveValue)
232 def _saveValue(self, index):
233 property = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
234 configuration = self._projectInfoRetrieve("MODULES")[self._currentModule()]["configuration"]
235 configurations = self._projectInfoRetrieve("CONFIGURATIONS")
236 if "type" not in configurations[configuration][property]["informations"].keys() or configurations[configuration][property]["informations"]["type"] == "int":
237 configurations[configuration][property]["value"] = str(int(self.pageContent.propertyTable.cellWidget(index, 1).value()))
238 elif configurations[configuration][property]["informations"]["type"] == "enum":
239 configurations[configuration][property]["value"] = unicode(self.pageContent.propertyTable.cellWidget(index, 1).currentText())
240 elif configurations[configuration][property]["informations"]["type"] == "boolean":
241 if self.pageContent.propertyTable.cellWidget(index, 1).isChecked():
242 configurations[configuration][property]["value"] = "1"
244 configurations[configuration][property]["value"] = "0"
245 self._projectInfoStore("CONFIGURATIONS", configurations)
247 def _moduleSelectionChanged(self, index):
248 module = unicode(self.pageContent.moduleTable.item(index, 1).text())
249 if self._button_group.button(index).isChecked():
250 self._moduleSelected(module)
252 self._moduleUnselected(module)
254 def _dependencyCheck(self, item):
256 module = unicode(item.text(0))
257 if item.checkState(0) == Qt.Checked:
258 self._moduleSelected(module)
260 self._moduleUnselected(module)
261 self.removeFileDependencies(module)
263 def _moduleSelected(self, selectedModule):
264 modules = self._projectInfoRetrieve("MODULES")
265 modules[selectedModule]["enabled"] = True
266 self._projectInfoStore("MODULES", modules)
267 depends = self._projectInfoRetrieve("MODULES")[selectedModule]["depends"]
269 if self.pageContent.automaticFix.isChecked():
270 unsatisfied = self.selectDependencyCheck(selectedModule)
271 if len(unsatisfied) > 0:
272 for module in unsatisfied:
273 modules = self._projectInfoRetrieve("MODULES")
274 modules[module]["enabled"] = True
275 for category in range(self.pageContent.moduleTree.topLevelItemCount()):
276 item = self.pageContent.moduleTree.topLevelItem(category)
277 for child in range(item.childCount()):
278 if unicode(item.child(child).text(0)) in unsatisfied:
279 item.child(child).setCheckState(0, Qt.Checked)
281 def _moduleUnselected(self, unselectedModule):
282 modules = self._projectInfoRetrieve("MODULES")
283 modules[unselectedModule]["enabled"] = False
284 self._projectInfoStore("MODULES", modules)
286 if self.pageContent.automaticFix.isChecked():
287 unsatisfied = self.unselectDependencyCheck(unselectedModule)
288 if len(unsatisfied) > 0:
289 message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to remove these modules too?")
290 message = message.arg(unselectedModule).arg(", ".join(unsatisfied))
291 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
292 if choice == QMessageBox.Yes:
293 for module in unsatisfied:
294 modules = self._projectInfoRetrieve("MODULES")
295 modules[module]["enabled"] = False
296 for category in range(self.pageContent.moduleTree.topLevelItemCount()):
297 item = self.pageContent.moduleTree.topLevelItem(category)
298 for child in range(item.childCount()):
299 if unicode(item.child(child).text(0)) in unsatisfied:
300 item.child(child).setCheckState(0, Qt.Unchecked)
302 def selectDependencyCheck(self, module):
304 modules = self._projectInfoRetrieve("MODULES")
305 files = self._projectInfoRetrieve("FILES")
306 for dependency in modules[module]["depends"]:
307 if dependency in modules and not modules[dependency]["enabled"]:
308 unsatisfied |= set([dependency])
309 if dependency not in unsatisfied:
310 unsatisfied |= self.selectDependencyCheck(dependency)
311 if dependency not in modules:
312 if dependency in files:
313 files[dependency] += 1
315 files[dependency] = 1
316 self._projectInfoStore("FILES", files)
319 def unselectDependencyCheck(self, dependency):
321 modules = self._projectInfoRetrieve("MODULES")
322 for module, informations in modules.items():
323 if dependency in informations["depends"] and informations["enabled"]:
324 unsatisfied |= set([module])
325 if dependency not in unsatisfied:
326 unsatisfied |= self.unselectDependencyCheck(module)
329 def removeFileDependencies(self, module):
330 modules = self._projectInfoRetrieve("MODULES")
331 files = self._projectInfoRetrieve("FILES")
332 dependencies = modules[module]["depends"]
333 for dependency in dependencies:
334 if dependency in files:
335 files[dependency] -= 1
336 if files[dependency] == 0:
337 del files[dependency]
338 self._projectInfoStore("FILES", files)
340 class QControlGroup(QObject):
342 QObject.__init__(self)
345 def addControl(self, id, control):
346 self._controls[id] = control
347 if type(control) == QCheckBox:
348 self.connect(control, SIGNAL("stateChanged(int)"), lambda: self._stateChanged(id))
349 elif type(control) == QSpinBox:
350 self.connect(control, SIGNAL("valueChanged(int)"), lambda: self._stateChanged(id))
351 elif type(control) == QComboBox:
352 self.connect(control, SIGNAL("currentIndexChanged(int)"), lambda: self._stateChanged(id))
353 elif type(control) == QDoubleSpinBox:
354 self.connect(control, SIGNAL("valueChanged(double)"), lambda: self._stateChanged(id))
359 def _stateChanged(self, id):
360 self.emit(SIGNAL("stateChanged"), id)