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 modules = bertos_utils.loadModuleInfosDict(self._projectInfoRetrieve("SOURCES_PATH"))
43 lists = bertos_utils.loadDefineListsDict(self._projectInfoRetrieve("SOURCES_PATH"))
45 for module, informations in modules.items():
46 if len(informations["configuration"]) > 0:
47 configurations[informations["configuration"]] = bertos_utils.loadConfigurationInfos(self._projectInfoRetrieve("SOURCES_PATH") +
48 "/" + informations["configuration"])
49 except ModuleDefineException, e:
50 self._exceptionOccurred(self.tr("Error parsing module information in file %1").arg(e.parameter))
51 except EnumDefineException, e:
52 self._exceptionOccurred(self.tr("Error parsing enum informations in file %1").arg(e.parameter))
53 except ConfigurationDefineException, e:
54 self._exceptionOccurred(self.tr("Error parsing configuration informations in file %1").arg(e.parameter))
56 self._projectInfoStore("MODULES", modules)
57 self._projectInfoStore("LISTS", lists)
58 self._projectInfoStore("CONFIGURATIONS", configurations)
60 def _fillModuleTable(self):
61 modules = self._projectInfoRetrieve("MODULES")
64 self.pageContent.moduleTable.setRowCount(len(modules))
65 for index, module in enumerate(modules):
66 self.pageContent.moduleTable.setItem(index, 1, QTableWidgetItem(module))
67 checkBox = QCheckBox()
68 self._buttonGroup.addButton(checkBox, index)
69 self.pageContent.moduleTable.setCellWidget(index, 0, checkBox)
70 checkBox.setChecked(modules[module]["enabled"])
72 def _fillPropertyTable(self):
73 module = self._currentModule()
74 if module is not None:
75 self._controlGroup.clear()
76 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
77 moduleDescription = self._projectInfoRetrieve("MODULES")[module]["description"]
78 self.pageContent.moduleLabel.setText(moduleDescription)
79 self.pageContent.moduleLabel.setVisible(True)
80 self.pageContent.propertyTable.clear()
81 if len(configuration) > 0:
82 configurations = self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
83 self.pageContent.propertyTable.setRowCount(len(configurations))
84 for index, property in enumerate(configurations):
85 item = QTableWidgetItem(property)
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":
90 checkBox = QCheckBox()
91 self.pageContent.propertyTable.setCellWidget(index, 1, checkBox)
92 if configurations[property]["value"] == "1":
93 checkBox.setChecked(True)
95 checkBox.setChecked(False)
96 self._controlGroup.addControl(index, checkBox)
97 elif "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "enum":
99 comboBox = QComboBox()
100 self.pageContent.propertyTable.setCellWidget(index, 1, comboBox)
101 enum = self._projectInfoRetrieve("LISTS")[configurations[property]["informations"]["value_list"]]
102 for i, element in enumerate(enum):
103 comboBox.addItem(element)
104 if element == configurations[property]["value"]:
105 comboBox.setCurrentIndex(i)
106 self._controlGroup.addControl(index, comboBox)
108 ## int, long or undefined type property
110 if bertos_utils.isLong(configurations[property]) or bertos_utils.isUnsignedLong(configurations[property]):
111 spinBox = QDoubleSpinBox()
112 spinBox.setDecimals(0)
115 self.pageContent.propertyTable.setCellWidget(index, 1, spinBox)
119 if bertos_utils.isLong(configurations[property]):
120 minimum = -2147483648
123 elif bertos_utils.isUnsigned(configurations[property]):
127 elif bertos_utils.isUnsignedLong(configurations[property]):
131 if "min" in configurations[property]["informations"].keys():
132 minimum = int(configurations[property]["informations"]["min"])
133 if "max" in configurations[property]["informations"].keys():
134 maximum = int(configurations[property]["informations"]["max"])
135 spinBox.setRange(minimum, maximum)
136 spinBox.setSuffix(suff)
137 spinBox.setValue(int(configurations[property]["value"].replace("L", "").replace("U", "")))
138 self._controlGroup.addControl(index, spinBox)
140 self.pageContent.propertyTable.setRowCount(0)
142 def _currentModule(self):
143 currentModule = self.pageContent.moduleTable.item(self.pageContent.moduleTable.currentRow(), 1)
144 if currentModule is not None:
145 return unicode(currentModule.text())
149 def _currentModuleConfigurations(self):
150 return self._configurations(self._currentModule())
152 def _currentProperty(self):
153 return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
155 def _currentPropertyItem(self):
156 return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
158 def _module(self, row):
159 return unicode(self.pageContent.moduleTable.item(row, 1).text())
161 def _configurations(self, module):
162 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
163 return self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
165 def _resetPropertyDescription(self):
166 for index in range(self.pageContent.propertyTable.rowCount()):
167 propertyName = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
168 self.pageContent.propertyTable.item(index, 0).setText(propertyName)
170 def _showPropertyDescription(self):
171 self._resetPropertyDescription()
172 configurations = self._currentModuleConfigurations()
173 if self._currentProperty() in configurations.keys():
174 description = configurations[self._currentProperty()]["description"]
175 name = self._currentProperty()
176 self._currentPropertyItem().setText(name + "\n" + description)
179 self.pageContent.moduleTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
180 self.pageContent.moduleTable.horizontalHeader().setStretchLastSection(True)
181 self.pageContent.moduleTable.horizontalHeader().setVisible(False)
182 self.pageContent.moduleTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
183 self.pageContent.moduleTable.verticalHeader().setVisible(False)
184 self.pageContent.moduleTable.setColumnCount(2)
185 self.pageContent.moduleTable.setRowCount(0)
186 self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
187 self.pageContent.propertyTable.horizontalHeader().setVisible(False)
188 self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
189 self.pageContent.propertyTable.verticalHeader().setVisible(False)
190 self.pageContent.propertyTable.setColumnCount(2)
191 self.pageContent.propertyTable.setRowCount(0)
192 self.pageContent.moduleLabel.setVisible(False)
194 def _connectSignals(self):
195 self.connect(self.pageContent.moduleTable, SIGNAL("itemSelectionChanged()"), self._fillPropertyTable)
196 self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self._showPropertyDescription)
197 self.connect(self._controlGroup, SIGNAL("stateChanged"), self._saveValue)
199 def _saveValue(self, index):
200 property = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
201 configuration = self._projectInfoRetrieve("MODULES")[self._currentModule()]["configuration"]
202 configurations = self._projectInfoRetrieve("CONFIGURATIONS")
203 if "type" not in configurations[configuration][property]["informations"].keys() or configurations[configuration][property]["informations"]["type"] == "int":
204 configurations[configuration][property]["value"] = str(int(self.pageContent.propertyTable.cellWidget(index, 1).value()))
205 elif configurations[configuration][property]["informations"]["type"] == "enum":
206 configurations[configuration][property]["value"] = unicode(self.pageContent.propertyTable.cellWidget(index, 1).currentText())
207 elif configurations[configuration][property]["informations"]["type"] == "boolean":
208 if self.pageContent.propertyTable.cellWidget(index, 1).isChecked():
209 configurations[configuration][property]["value"] = "1"
211 configurations[configuration][property]["value"] = "0"
212 self._projectInfoStore("CONFIGURATIONS", configurations)
214 def _moduleSelectionChanged(self, index):
215 module = unicode(self.pageContent.moduleTable.item(index, 1).text())
216 if self._buttonGroup.button(index).isChecked():
217 self._moduleSelected(module)
219 self._moduleUnselected(module)
221 def _moduleSelected(self, selectedModule):
222 modules = self._projectInfoRetrieve("MODULES")
223 modules[selectedModule]["enabled"] = True
224 self._projectInfoStore("MODULES", modules)
225 depends = self._projectInfoRetrieve("MODULES")[selectedModule]["depends"]
226 unsatisfied = self.selectDependencyCheck(selectedModule)
227 if len(unsatisfied) > 0:
228 message = self.tr("The module %1 needs the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
229 message = message.arg(selectedModule).arg(", ".join(unsatisfied))
230 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
231 if choice == QMessageBox.Yes:
232 for module in unsatisfied:
233 modules = self._projectInfoRetrieve("MODULES")
234 modules[module]["enabled"] = True
235 for index in range(self.pageContent.moduleTable.rowCount()):
236 if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
237 self._buttonGroup.button(index).setChecked(True)
239 def _moduleUnselected(self, unselectedModule):
240 modules = self._projectInfoRetrieve("MODULES")
241 modules[unselectedModule]["enabled"] = False
242 self._projectInfoStore("MODULES", modules)
243 unsatisfied = self.unselectDependencyCheck(unselectedModule)
244 if len(unsatisfied) > 0:
245 message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
246 message = message.arg(unselectedModule).arg(", ".join(unsatisfied))
247 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
248 if choice == QMessageBox.Yes:
249 for module in unsatisfied:
250 modules = self._projectInfoRetrieve("MODULES")
251 modules[module]["enabled"] = False
252 for index in range(self.pageContent.moduleTable.rowCount()):
253 if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
254 self._buttonGroup.button(index).setChecked(False)
257 def selectDependencyCheck(self, module):
259 modules = self._projectInfoRetrieve("MODULES")
260 for dependency in modules[module]["depends"]:
261 if not modules[dependency]["enabled"]:
262 unsatisfied |= set([dependency])
263 if dependency not in unsatisfied:
264 unsatisfied |= self.selectDependencyCheck(dependency)
267 def unselectDependencyCheck(self, dependency):
269 modules = self._projectInfoRetrieve("MODULES")
270 for module, informations in modules.items():
271 if dependency in informations["depends"] and informations["enabled"]:
272 unsatisfied |= set([module])
273 if dependency not in unsatisfied:
274 unsatisfied |= self.unselectDependencyCheck(module)
277 class QControlGroup(QObject):
279 QObject.__init__(self)
282 def addControl(self, id, control):
283 self._controls[id] = control
284 if type(control) == QCheckBox:
285 self.connect(control, SIGNAL("stateChanged(int)"), lambda: self._stateChanged(id))
286 elif type(control) == QSpinBox:
287 self.connect(control, SIGNAL("valueChanged(int)"), lambda: self._stateChanged(id))
288 elif type(control) == QComboBox:
289 self.connect(control, SIGNAL("currentIndexChanged(int)"), lambda: self._stateChanged(id))
290 elif type(control) == QDoubleSpinBox:
291 self.connect(control, SIGNAL("valueChanged(double)"), lambda: self._stateChanged(id))
296 def _stateChanged(self, id):
297 self.emit(SIGNAL("stateChanged"), id)