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)
66 def _fillPropertyTable(self):
67 module = self._currentModule()
68 if module is not None:
69 self._controlGroup.clear()
70 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
71 moduleDescription = self._projectInfoRetrieve("MODULES")[module]["description"]
72 self.pageContent.moduleLabel.setText(moduleDescription)
73 self.pageContent.moduleLabel.setVisible(True)
74 self.pageContent.propertyTable.clear()
75 if len(configuration) > 0:
76 configurations = self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
77 self.pageContent.propertyTable.setRowCount(len(configurations))
79 for property in configurations:
80 if "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "autoenabled":
81 ## Doesn't show the hidden fields
84 item = QTableWidgetItem(configurations[property]["brief"])
85 item.setData(Qt.UserRole, qvariant_converter.convertString(property))
86 self.pageContent.propertyTable.setItem(index, 0, item)
87 if "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "boolean":
88 self._insertCheckBox(index, configurations[property]["value"])
89 elif "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "enum":
90 self._insertComboBox(index, configurations[property]["value"], configurations[property]["informations"]["value_list"])
91 elif "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "int":
92 self._insertSpinBox(index, configurations[property]["value"], configurations[property]["informations"])
94 # Not defined type, rendered as a text field
95 self.pageContent.propertyTable.setItem(index, 1, QTableWidgetItem(configurations[property]["value"]))
98 self.pageContent.propertyTable.setRowCount(0)
100 def _insertCheckBox(self, index, value):
102 checkBox = QCheckBox()
103 self.pageContent.propertyTable.setCellWidget(index, 1, checkBox)
105 checkBox.setChecked(True)
107 checkBox.setChecked(False)
108 self._controlGroup.addControl(index, checkBox)
110 def _insertComboBox(self, index, value, value_list):
112 comboBox = QComboBox()
113 self.pageContent.propertyTable.setCellWidget(index, 1, comboBox)
114 enum = self._projectInfoRetrieve("LISTS")[value_list]
115 for i, element in enumerate(enum):
116 comboBox.addItem(element)
118 comboBox.setCurrentIndex(i)
119 self._controlGroup.addControl(index, comboBox)
121 def _insertSpinBox(self, index, value, informations):
122 ## int, long or undefined type property
124 if bertos_utils.isLong(informations) or bertos_utils.isUnsignedLong(informations):
125 spinBox = QDoubleSpinBox()
126 spinBox.setDecimals(0)
129 self.pageContent.propertyTable.setCellWidget(index, 1, spinBox)
133 if bertos_utils.isLong(informations):
134 minimum = -2147483648
137 elif bertos_utils.isUnsigned(informations):
141 elif bertos_utils.isUnsignedLong(informations):
145 if "min" in informations.keys():
146 minimum = int(informations["min"])
147 if "max" in informations.keys():
148 maximum = int(informations["max"])
149 spinBox.setRange(minimum, maximum)
150 spinBox.setSuffix(suff)
151 spinBox.setValue(int(value.replace("L", "").replace("U", "")))
152 self._controlGroup.addControl(index, spinBox)
155 def _currentModule(self):
156 currentModule = self.pageContent.moduleTree.currentItem()
157 # return only the child items
158 if currentModule is not None and currentModule.parent() is not None:
159 return unicode(currentModule.text(0))
163 def _currentModuleConfigurations(self):
164 return self._configurations(self._currentModule())
166 def _currentProperty(self):
167 return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
169 def _currentPropertyItem(self):
170 return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
172 def _configurations(self, module):
173 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
174 if len(configuration) > 0:
175 return self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
179 def _resetPropertyDescription(self):
180 for index in range(self.pageContent.propertyTable.rowCount()):
181 propertyName = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
182 # Awful solution! Needed because if the user change the module, the selection changed...
183 if propertyName not in self._currentModuleConfigurations().keys():
185 self.pageContent.propertyTable.item(index, 0).setText(self._currentModuleConfigurations()[propertyName]['brief'])
187 def _showPropertyDescription(self):
188 self._resetPropertyDescription()
189 configurations = self._currentModuleConfigurations()
190 if self._currentProperty() in configurations.keys():
191 description = configurations[self._currentProperty()]["brief"]
192 name = self._currentProperty()
193 self._currentPropertyItem().setText(description + "\n" + name)
196 self.pageContent.moduleTree.clear()
197 self.pageContent.moduleTree.setHeaderHidden(True)
198 self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
199 self.pageContent.propertyTable.horizontalHeader().setVisible(False)
200 self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
201 self.pageContent.propertyTable.verticalHeader().setVisible(False)
202 self.pageContent.propertyTable.setColumnCount(2)
203 self.pageContent.propertyTable.setRowCount(0)
204 self.pageContent.moduleLabel.setVisible(False)
206 def _connectSignals(self):
207 self.connect(self.pageContent.moduleTree, SIGNAL("itemPressed(QTreeWidgetItem*, int)"), self._fillPropertyTable)
208 self.connect(self.pageContent.moduleTree, SIGNAL("itemChanged(QTreeWidgetItem*, int)"), self._dependencyCheck)
209 self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self._showPropertyDescription)
210 self.connect(self._controlGroup, SIGNAL("stateChanged"), self._saveValue)
212 def _saveValue(self, index):
213 property = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
214 configuration = self._projectInfoRetrieve("MODULES")[self._currentModule()]["configuration"]
215 configurations = self._projectInfoRetrieve("CONFIGURATIONS")
216 if "type" not in configurations[configuration][property]["informations"].keys() or configurations[configuration][property]["informations"]["type"] == "int":
217 configurations[configuration][property]["value"] = str(int(self.pageContent.propertyTable.cellWidget(index, 1).value()))
218 elif configurations[configuration][property]["informations"]["type"] == "enum":
219 configurations[configuration][property]["value"] = unicode(self.pageContent.propertyTable.cellWidget(index, 1).currentText())
220 elif configurations[configuration][property]["informations"]["type"] == "boolean":
221 if self.pageContent.propertyTable.cellWidget(index, 1).isChecked():
222 configurations[configuration][property]["value"] = "1"
224 configurations[configuration][property]["value"] = "0"
225 self._projectInfoStore("CONFIGURATIONS", configurations)
227 def _moduleSelectionChanged(self, index):
228 module = unicode(self.pageContent.moduleTable.item(index, 1).text())
229 if self._buttonGroup.button(index).isChecked():
230 self._moduleSelected(module)
232 self._moduleUnselected(module)
234 def _dependencyCheck(self, item):
236 module = unicode(item.text(0))
237 if item.checkState(0) == Qt.Checked:
238 self._moduleSelected(module)
240 self._moduleUnselected(module)
242 def _moduleSelected(self, selectedModule):
243 modules = self._projectInfoRetrieve("MODULES")
244 modules[selectedModule]["enabled"] = True
245 self._projectInfoStore("MODULES", modules)
246 depends = self._projectInfoRetrieve("MODULES")[selectedModule]["depends"]
248 if self.pageContent.automaticFix.isChecked():
249 unsatisfied = self.selectDependencyCheck(selectedModule)
250 if len(unsatisfied) > 0:
251 for module in unsatisfied:
252 modules = self._projectInfoRetrieve("MODULES")
253 modules[module]["enabled"] = True
254 for category in range(self.pageContent.moduleTree.topLevelItemCount()):
255 item = self.pageContent.moduleTree.topLevelItem(category)
256 for child in range(item.childCount()):
257 if unicode(item.child(child).text(0)) in unsatisfied:
258 item.child(child).setCheckState(0, Qt.Checked)
260 def _moduleUnselected(self, unselectedModule):
261 modules = self._projectInfoRetrieve("MODULES")
262 modules[unselectedModule]["enabled"] = False
263 self._projectInfoStore("MODULES", modules)
265 if self.pageContent.automaticFix.isChecked():
266 unsatisfied = self.unselectDependencyCheck(unselectedModule)
267 if len(unsatisfied) > 0:
268 message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to remove these modules too?")
269 message = message.arg(unselectedModule).arg(", ".join(unsatisfied))
270 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
271 if choice == QMessageBox.Yes:
272 for module in unsatisfied:
273 modules = self._projectInfoRetrieve("MODULES")
274 modules[module]["enabled"] = False
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.Unchecked)
281 def selectDependencyCheck(self, module):
283 modules = self._projectInfoRetrieve("MODULES")
284 for dependency in modules[module]["depends"]:
285 if not modules[dependency]["enabled"]:
286 unsatisfied |= set([dependency])
287 if dependency not in unsatisfied:
288 unsatisfied |= self.selectDependencyCheck(dependency)
291 def unselectDependencyCheck(self, dependency):
293 modules = self._projectInfoRetrieve("MODULES")
294 for module, informations in modules.items():
295 if dependency in informations["depends"] and informations["enabled"]:
296 unsatisfied |= set([module])
297 if dependency not in unsatisfied:
298 unsatisfied |= self.unselectDependencyCheck(module)
301 class QControlGroup(QObject):
303 QObject.__init__(self)
306 def addControl(self, id, control):
307 self._controls[id] = control
308 if type(control) == QCheckBox:
309 self.connect(control, SIGNAL("stateChanged(int)"), lambda: self._stateChanged(id))
310 elif type(control) == QSpinBox:
311 self.connect(control, SIGNAL("valueChanged(int)"), lambda: self._stateChanged(id))
312 elif type(control) == QComboBox:
313 self.connect(control, SIGNAL("currentIndexChanged(int)"), lambda: self._stateChanged(id))
314 elif type(control) == QDoubleSpinBox:
315 self.connect(control, SIGNAL("valueChanged(double)"), lambda: self._stateChanged(id))
320 def _stateChanged(self, id):
321 self.emit(SIGNAL("stateChanged"), id)