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._setupButtonGroup()
32 self._loadModuleData()
33 self._fillModuleTable()
35 def _setupButtonGroup(self):
36 self._buttonGroup = QButtonGroup()
37 self._buttonGroup.setExclusive(False)
38 self.connect(self._buttonGroup, SIGNAL("buttonClicked(int)"), self._moduleSelectionChanged)
40 def _loadModuleData(self):
42 bertos_utils.loadModuleData(self._project())
43 except ModuleDefineException, e:
44 self._exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
45 except EnumDefineException, e:
46 self._exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
47 except ConfigurationDefineException, e:
48 self._exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
50 def _fillModuleTable(self):
51 modules = self._projectInfoRetrieve("MODULES")
54 self.pageContent.moduleTable.setRowCount(len(modules))
55 for index, module in enumerate(modules):
56 self.pageContent.moduleTable.setItem(index, 1, QTableWidgetItem(module))
57 checkBox = QCheckBox()
58 self._buttonGroup.addButton(checkBox, index)
59 self.pageContent.moduleTable.setCellWidget(index, 0, checkBox)
60 checkBox.setChecked(modules[module]["enabled"])
62 def _fillPropertyTable(self):
63 module = self._currentModule()
64 if module is not None:
65 self._controlGroup.clear()
66 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
67 moduleDescription = self._projectInfoRetrieve("MODULES")[module]["description"]
68 self.pageContent.moduleLabel.setText(moduleDescription)
69 self.pageContent.moduleLabel.setVisible(True)
70 self.pageContent.propertyTable.clear()
71 if len(configuration) > 0:
72 configurations = self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
73 self.pageContent.propertyTable.setRowCount(len(configurations))
74 for index, property in enumerate(configurations):
75 item = QTableWidgetItem(configurations[property]["brief"])
76 item.setData(Qt.UserRole, qvariant_converter.convertString(property))
77 self.pageContent.propertyTable.setItem(index, 0, item)
78 if "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "boolean":
79 self._insertCheckBox(index, configurations[property]["value"])
80 elif "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "enum":
81 self._insertComboBox(index, configurations[property]["value"], configurations[property]["informations"]["value_list"])
82 elif "type" in configurations[property]["informations"] and configurations[property]["informations"]["type"] == "int":
83 self._insertSpinBox(index, configurations[property]["value"], configurations[property]["informations"])
85 # Not defined type, rendered as a text field
86 self.pageContent.propertyTable.setItem(index, 1, QTableWidgetItem(property))
88 self.pageContent.propertyTable.setRowCount(0)
90 def _insertCheckBox(self, index, value):
92 checkBox = QCheckBox()
93 self.pageContent.propertyTable.setCellWidget(index, 1, checkBox)
95 checkBox.setChecked(True)
97 checkBox.setChecked(False)
98 self._controlGroup.addControl(index, checkBox)
100 def _insertComboBox(self, index, value, value_list):
102 comboBox = QComboBox()
103 self.pageContent.propertyTable.setCellWidget(index, 1, comboBox)
104 enum = self._projectInfoRetrieve("LISTS")[value_list]
105 for i, element in enumerate(enum):
106 comboBox.addItem(element)
108 comboBox.setCurrentIndex(i)
109 self._controlGroup.addControl(index, comboBox)
111 def _insertSpinBox(self, index, value, informations):
112 ## int, long or undefined type property
114 if bertos_utils.isLong(informations) or bertos_utils.isUnsignedLong(informations):
115 spinBox = QDoubleSpinBox()
116 spinBox.setDecimals(0)
119 self.pageContent.propertyTable.setCellWidget(index, 1, spinBox)
123 if bertos_utils.isLong(informations):
124 minimum = -2147483648
127 elif bertos_utils.isUnsigned(informations):
131 elif bertos_utils.isUnsignedLong(informations):
135 if "min" in informations.keys():
136 minimum = int(informations["min"])
137 if "max" in informations.keys():
138 maximum = int(informations["max"])
139 spinBox.setRange(minimum, maximum)
140 spinBox.setSuffix(suff)
141 spinBox.setValue(int(value.replace("L", "").replace("U", "")))
142 self._controlGroup.addControl(index, spinBox)
145 def _currentModule(self):
146 currentModule = self.pageContent.moduleTable.item(self.pageContent.moduleTable.currentRow(), 1)
147 if currentModule is not None:
148 return unicode(currentModule.text())
152 def _currentModuleConfigurations(self):
153 return self._configurations(self._currentModule())
155 def _currentProperty(self):
156 return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
158 def _currentPropertyItem(self):
159 return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
161 def _module(self, row):
162 return unicode(self.pageContent.moduleTable.item(row, 1).text())
164 def _configurations(self, module):
165 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
166 if len(configuration) > 0:
167 return self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
171 def _resetPropertyDescription(self):
172 for index in range(self.pageContent.propertyTable.rowCount()):
173 propertyName = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
174 # Awful solution! Needed because if the user change the module, the selection changed...
175 if propertyName not in self._currentModuleConfigurations().keys():
177 self.pageContent.propertyTable.item(index, 0).setText(self._currentModuleConfigurations()[propertyName]['brief'])
179 def _showPropertyDescription(self):
180 self._resetPropertyDescription()
181 configurations = self._currentModuleConfigurations()
182 if self._currentProperty() in configurations.keys():
183 description = configurations[self._currentProperty()]["brief"]
184 name = self._currentProperty()
185 self._currentPropertyItem().setText(description + "\n" + name)
188 self.pageContent.moduleTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
189 self.pageContent.moduleTable.horizontalHeader().setStretchLastSection(True)
190 self.pageContent.moduleTable.horizontalHeader().setVisible(False)
191 self.pageContent.moduleTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
192 self.pageContent.moduleTable.verticalHeader().setVisible(False)
193 self.pageContent.moduleTable.setColumnCount(2)
194 self.pageContent.moduleTable.setRowCount(0)
195 self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
196 self.pageContent.propertyTable.horizontalHeader().setVisible(False)
197 self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
198 self.pageContent.propertyTable.verticalHeader().setVisible(False)
199 self.pageContent.propertyTable.setColumnCount(2)
200 self.pageContent.propertyTable.setRowCount(0)
201 self.pageContent.moduleLabel.setVisible(False)
203 def _connectSignals(self):
204 self.connect(self.pageContent.moduleTable, SIGNAL("itemSelectionChanged()"), self._fillPropertyTable)
205 self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self._showPropertyDescription)
206 self.connect(self._controlGroup, SIGNAL("stateChanged"), self._saveValue)
208 def _saveValue(self, index):
209 property = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
210 configuration = self._projectInfoRetrieve("MODULES")[self._currentModule()]["configuration"]
211 configurations = self._projectInfoRetrieve("CONFIGURATIONS")
212 if "type" not in configurations[configuration][property]["informations"].keys() or configurations[configuration][property]["informations"]["type"] == "int":
213 configurations[configuration][property]["value"] = str(int(self.pageContent.propertyTable.cellWidget(index, 1).value()))
214 elif configurations[configuration][property]["informations"]["type"] == "enum":
215 configurations[configuration][property]["value"] = unicode(self.pageContent.propertyTable.cellWidget(index, 1).currentText())
216 elif configurations[configuration][property]["informations"]["type"] == "boolean":
217 if self.pageContent.propertyTable.cellWidget(index, 1).isChecked():
218 configurations[configuration][property]["value"] = "1"
220 configurations[configuration][property]["value"] = "0"
221 self._projectInfoStore("CONFIGURATIONS", configurations)
223 def _moduleSelectionChanged(self, index):
224 module = unicode(self.pageContent.moduleTable.item(index, 1).text())
225 if self._buttonGroup.button(index).isChecked():
226 self._moduleSelected(module)
228 self._moduleUnselected(module)
230 def _moduleSelected(self, selectedModule):
231 modules = self._projectInfoRetrieve("MODULES")
232 modules[selectedModule]["enabled"] = True
233 self._projectInfoStore("MODULES", modules)
234 depends = self._projectInfoRetrieve("MODULES")[selectedModule]["depends"]
236 if self.pageContent.automaticFix.isChecked():
237 unsatisfied = self.selectDependencyCheck(selectedModule)
238 if len(unsatisfied) > 0:
239 for module in unsatisfied:
240 modules = self._projectInfoRetrieve("MODULES")
241 modules[module]["enabled"] = True
242 for index in range(self.pageContent.moduleTable.rowCount()):
243 if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
244 self._buttonGroup.button(index).setChecked(True)
246 def _moduleUnselected(self, unselectedModule):
247 modules = self._projectInfoRetrieve("MODULES")
248 modules[unselectedModule]["enabled"] = False
249 self._projectInfoStore("MODULES", modules)
251 if self.pageContent.automaticFix.isChecked():
252 unsatisfied = self.unselectDependencyCheck(unselectedModule)
253 if len(unsatisfied) > 0:
254 message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
255 message = message.arg(unselectedModule).arg(", ".join(unsatisfied))
256 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
257 if choice == QMessageBox.Yes:
258 for module in unsatisfied:
259 modules = self._projectInfoRetrieve("MODULES")
260 modules[module]["enabled"] = False
261 for index in range(self.pageContent.moduleTable.rowCount()):
262 if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
263 self._buttonGroup.button(index).setChecked(False)
266 def selectDependencyCheck(self, module):
268 modules = self._projectInfoRetrieve("MODULES")
269 for dependency in modules[module]["depends"]:
270 if not modules[dependency]["enabled"]:
271 unsatisfied |= set([dependency])
272 if dependency not in unsatisfied:
273 unsatisfied |= self.selectDependencyCheck(dependency)
276 def unselectDependencyCheck(self, dependency):
278 modules = self._projectInfoRetrieve("MODULES")
279 for module, informations in modules.items():
280 if dependency in informations["depends"] and informations["enabled"]:
281 unsatisfied |= set([module])
282 if dependency not in unsatisfied:
283 unsatisfied |= self.unselectDependencyCheck(module)
286 class QControlGroup(QObject):
288 QObject.__init__(self)
291 def addControl(self, id, control):
292 self._controls[id] = control
293 if type(control) == QCheckBox:
294 self.connect(control, SIGNAL("stateChanged(int)"), lambda: self._stateChanged(id))
295 elif type(control) == QSpinBox:
296 self.connect(control, SIGNAL("valueChanged(int)"), lambda: self._stateChanged(id))
297 elif type(control) == QComboBox:
298 self.connect(control, SIGNAL("currentIndexChanged(int)"), lambda: self._stateChanged(id))
299 elif type(control) == QDoubleSpinBox:
300 self.connect(control, SIGNAL("valueChanged(double)"), lambda: self._stateChanged(id))
305 def _stateChanged(self, id):
306 self.emit(SIGNAL("stateChanged"), id)