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._controlGroup = QControlGroup()
27 self._connectSignals()
31 self._loadModuleData()
32 self._fillModuleTree()
34 def _setupButtonGroup(self):
35 self._buttonGroup = QButtonGroup()
36 self._buttonGroup.setExclusive(False)
37 self.connect(self._buttonGroup, SIGNAL("buttonClicked(int)"), self._moduleSelectionChanged)
39 def _loadModuleData(self):
41 bertos_utils.loadModuleData(self._project())
42 except ModuleDefineException, e:
43 self._exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
44 except EnumDefineException, e:
45 self._exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
46 except ConfigurationDefineException, e:
47 self._exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
49 def _fillModuleTree(self):
50 modules = self._projectInfoRetrieve("MODULES")
54 for module, information in modules.items():
55 if information["category"] not in categories.keys():
56 categories[information["category"]] = []
57 categories[information["category"]].append(module)
58 for category, modules in categories.items():
59 item = QTreeWidgetItem(QStringList([category]))
60 for module in modules:
61 moduleItem = QTreeWidgetItem(item, QStringList([module]))
62 moduleItem.setCheckState(0, Qt.Unchecked)
63 self.pageContent.moduleTree.addTopLevelItem(item)
64 self.pageContent.moduleTree.sortItems(0, Qt.AscendingOrder)
67 def _fillPropertyTable(self):
68 module = self._currentModule()
69 if module is not None:
70 self._controlGroup.clear()
71 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
72 moduleDescription = self._projectInfoRetrieve("MODULES")[module]["description"]
73 self.pageContent.moduleLabel.setText(moduleDescription)
74 self.pageContent.moduleLabel.setVisible(True)
75 self.pageContent.propertyTable.clear()
76 if len(configuration) > 0:
77 configurations = self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
78 self.pageContent.propertyTable.setRowCount(len(configurations))
80 for property in configurations:
81 if "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "autoenabled":
82 ## Doesn't show the hidden fields
85 item = QTableWidgetItem(configurations[property]["brief"])
86 item.setData(Qt.UserRole, qvariant_converter.convertString(property))
87 self.pageContent.propertyTable.setItem(index, 0, item)
88 if "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "boolean":
89 self._insertCheckBox(index, configurations[property]["value"])
90 elif "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "enum":
91 self._insertComboBox(index, configurations[property]["value"], configurations[property]["informations"]["value_list"])
92 elif "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "int":
93 self._insertSpinBox(index, configurations[property]["value"], configurations[property]["informations"])
95 # Not defined type, rendered as a text field
96 self.pageContent.propertyTable.setItem(index, 1, QTableWidgetItem(configurations[property]["value"]))
99 self.pageContent.propertyTable.setRowCount(0)
101 def _insertCheckBox(self, index, value):
103 checkBox = QCheckBox()
104 self.pageContent.propertyTable.setCellWidget(index, 1, checkBox)
106 checkBox.setChecked(True)
108 checkBox.setChecked(False)
109 self._controlGroup.addControl(index, checkBox)
111 def _insertComboBox(self, index, value, value_list):
113 comboBox = QComboBox()
114 self.pageContent.propertyTable.setCellWidget(index, 1, comboBox)
115 enum = self._projectInfoRetrieve("LISTS")[value_list]
116 for i, element in enumerate(enum):
117 comboBox.addItem(element)
119 comboBox.setCurrentIndex(i)
120 self._controlGroup.addControl(index, comboBox)
122 def _insertSpinBox(self, index, value, informations):
123 ## int, long or undefined type property
125 if bertos_utils.isLong(informations) or bertos_utils.isUnsignedLong(informations):
126 spinBox = QDoubleSpinBox()
127 spinBox.setDecimals(0)
130 self.pageContent.propertyTable.setCellWidget(index, 1, spinBox)
134 if bertos_utils.isLong(informations):
135 minimum = -2147483648
138 elif bertos_utils.isUnsigned(informations):
142 elif bertos_utils.isUnsignedLong(informations):
146 if "min" in informations.keys():
147 minimum = int(informations["min"])
148 if "max" in informations.keys():
149 maximum = int(informations["max"])
150 spinBox.setRange(minimum, maximum)
151 spinBox.setSuffix(suff)
152 spinBox.setValue(int(value.replace("L", "").replace("U", "")))
153 self._controlGroup.addControl(index, spinBox)
156 def _currentModule(self):
157 currentModule = self.pageContent.moduleTree.currentItem()
158 # return only the child items
159 if currentModule is not None and currentModule.parent() is not None:
160 return unicode(currentModule.text(0))
164 def _currentModuleConfigurations(self):
165 return self._configurations(self._currentModule())
167 def _currentProperty(self):
168 return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
170 def _currentPropertyItem(self):
171 return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
173 def _configurations(self, module):
174 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
175 if len(configuration) > 0:
176 return self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
180 def _resetPropertyDescription(self):
181 for index in range(self.pageContent.propertyTable.rowCount()):
182 propertyName = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
183 # Awful solution! Needed because if the user change the module, the selection changed...
184 if propertyName not in self._currentModuleConfigurations().keys():
186 self.pageContent.propertyTable.item(index, 0).setText(self._currentModuleConfigurations()[propertyName]['brief'])
188 def _showPropertyDescription(self):
189 self._resetPropertyDescription()
190 configurations = self._currentModuleConfigurations()
191 if self._currentProperty() in configurations.keys():
192 description = configurations[self._currentProperty()]["brief"]
193 name = self._currentProperty()
194 self._currentPropertyItem().setText(description + "\n" + name)
197 self.pageContent.moduleTree.clear()
198 self.pageContent.moduleTree.setHeaderHidden(True)
199 self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
200 self.pageContent.propertyTable.horizontalHeader().setVisible(False)
201 self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
202 self.pageContent.propertyTable.verticalHeader().setVisible(False)
203 self.pageContent.propertyTable.setColumnCount(2)
204 self.pageContent.propertyTable.setRowCount(0)
205 self.pageContent.moduleLabel.setVisible(False)
207 def _connectSignals(self):
208 self.connect(self.pageContent.moduleTree, SIGNAL("itemPressed(QTreeWidgetItem*, int)"), self._fillPropertyTable)
209 self.connect(self.pageContent.moduleTree, SIGNAL("itemChanged(QTreeWidgetItem*, int)"), self._dependencyCheck)
210 self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self._showPropertyDescription)
211 self.connect(self._controlGroup, SIGNAL("stateChanged"), self._saveValue)
213 def _saveValue(self, index):
214 property = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
215 configuration = self._projectInfoRetrieve("MODULES")[self._currentModule()]["configuration"]
216 configurations = self._projectInfoRetrieve("CONFIGURATIONS")
217 if "type" not in configurations[configuration][property]["informations"].keys() or configurations[configuration][property]["informations"]["type"] == "int":
218 configurations[configuration][property]["value"] = str(int(self.pageContent.propertyTable.cellWidget(index, 1).value()))
219 elif configurations[configuration][property]["informations"]["type"] == "enum":
220 configurations[configuration][property]["value"] = unicode(self.pageContent.propertyTable.cellWidget(index, 1).currentText())
221 elif configurations[configuration][property]["informations"]["type"] == "boolean":
222 if self.pageContent.propertyTable.cellWidget(index, 1).isChecked():
223 configurations[configuration][property]["value"] = "1"
225 configurations[configuration][property]["value"] = "0"
226 self._projectInfoStore("CONFIGURATIONS", configurations)
228 def _moduleSelectionChanged(self, index):
229 module = unicode(self.pageContent.moduleTable.item(index, 1).text())
230 if self._buttonGroup.button(index).isChecked():
231 self._moduleSelected(module)
233 self._moduleUnselected(module)
235 def _dependencyCheck(self, item):
237 module = unicode(item.text(0))
238 if item.checkState(0) == Qt.Checked:
239 self._moduleSelected(module)
241 self._moduleUnselected(module)
242 self.removeFileDependencies(module)
244 def _moduleSelected(self, selectedModule):
245 modules = self._projectInfoRetrieve("MODULES")
246 modules[selectedModule]["enabled"] = True
247 self._projectInfoStore("MODULES", modules)
248 depends = self._projectInfoRetrieve("MODULES")[selectedModule]["depends"]
250 if self.pageContent.automaticFix.isChecked():
251 unsatisfied = self.selectDependencyCheck(selectedModule)
252 if len(unsatisfied) > 0:
253 for module in unsatisfied:
254 modules = self._projectInfoRetrieve("MODULES")
255 modules[module]["enabled"] = True
256 for category in range(self.pageContent.moduleTree.topLevelItemCount()):
257 item = self.pageContent.moduleTree.topLevelItem(category)
258 for child in range(item.childCount()):
259 if unicode(item.child(child).text(0)) in unsatisfied:
260 item.child(child).setCheckState(0, Qt.Checked)
262 def _moduleUnselected(self, unselectedModule):
263 modules = self._projectInfoRetrieve("MODULES")
264 modules[unselectedModule]["enabled"] = False
265 self._projectInfoStore("MODULES", modules)
267 if self.pageContent.automaticFix.isChecked():
268 unsatisfied = self.unselectDependencyCheck(unselectedModule)
269 if len(unsatisfied) > 0:
270 message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to remove these modules too?")
271 message = message.arg(unselectedModule).arg(", ".join(unsatisfied))
272 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
273 if choice == QMessageBox.Yes:
274 for module in unsatisfied:
275 modules = self._projectInfoRetrieve("MODULES")
276 modules[module]["enabled"] = False
277 for category in range(self.pageContent.moduleTree.topLevelItemCount()):
278 item = self.pageContent.moduleTree.topLevelItem(category)
279 for child in range(item.childCount()):
280 if unicode(item.child(child).text(0)) in unsatisfied:
281 item.child(child).setCheckState(0, Qt.Unchecked)
283 def selectDependencyCheck(self, module):
285 modules = self._projectInfoRetrieve("MODULES")
286 files = self._projectInfoRetrieve("FILES")
287 for dependency in modules[module]["depends"]:
288 if dependency in modules and not modules[dependency]["enabled"]:
289 unsatisfied |= set([dependency])
290 if dependency not in unsatisfied:
291 unsatisfied |= self.selectDependencyCheck(dependency)
292 if dependency not in modules:
293 if dependency in files:
294 files[dependency] += 1
296 files[dependency] = 1
297 self._projectInfoStore("FILES", files)
300 def unselectDependencyCheck(self, dependency):
302 modules = self._projectInfoRetrieve("MODULES")
303 for module, informations in modules.items():
304 if dependency in informations["depends"] and informations["enabled"]:
305 unsatisfied |= set([module])
306 if dependency not in unsatisfied:
307 unsatisfied |= self.unselectDependencyCheck(module)
310 def removeFileDependencies(self, module):
311 modules = self._projectInfoRetrieve("MODULES")
312 files = self._projectInfoRetrieve("FILES")
313 dependencies = modules[module]["depends"]
314 for dependency in dependencies:
315 if dependency in files:
316 files[dependency] -= 1
317 if files[dependency] == 0:
318 del files[dependency]
319 self._projectInfoStore("FILES", files)
321 class QControlGroup(QObject):
323 QObject.__init__(self)
326 def addControl(self, id, control):
327 self._controls[id] = control
328 if type(control) == QCheckBox:
329 self.connect(control, SIGNAL("stateChanged(int)"), lambda: self._stateChanged(id))
330 elif type(control) == QSpinBox:
331 self.connect(control, SIGNAL("valueChanged(int)"), lambda: self._stateChanged(id))
332 elif type(control) == QComboBox:
333 self.connect(control, SIGNAL("currentIndexChanged(int)"), lambda: self._stateChanged(id))
334 elif type(control) == QDoubleSpinBox:
335 self.connect(control, SIGNAL("valueChanged(double)"), lambda: self._stateChanged(id))
340 def _stateChanged(self, id):
341 self.emit(SIGNAL("stateChanged"), id)