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()):
168 propertyName = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
169 self.pageContent.propertyTable.item(index, 0).setText(propertyName)
171 def _showPropertyDescription(self):
172 self._resetPropertyDescription()
173 configurations = self._currentModuleConfigurations()
174 if self._currentProperty() in configurations.keys():
175 description = configurations[self._currentProperty()]["description"]
176 name = self._currentProperty()
177 self._currentPropertyItem().setText(name + "\n" + description)
180 self.pageContent.moduleTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
181 self.pageContent.moduleTable.horizontalHeader().setStretchLastSection(True)
182 self.pageContent.moduleTable.horizontalHeader().setVisible(False)
183 self.pageContent.moduleTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
184 self.pageContent.moduleTable.verticalHeader().setVisible(False)
185 self.pageContent.moduleTable.setColumnCount(2)
186 self.pageContent.moduleTable.setRowCount(0)
187 self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
188 self.pageContent.propertyTable.horizontalHeader().setVisible(False)
189 self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
190 self.pageContent.propertyTable.verticalHeader().setVisible(False)
191 self.pageContent.propertyTable.setColumnCount(2)
192 self.pageContent.propertyTable.setRowCount(0)
193 self.pageContent.moduleLabel.setVisible(False)
195 def _connectSignals(self):
196 self.connect(self.pageContent.moduleTable, SIGNAL("itemSelectionChanged()"), self._fillPropertyTable)
197 self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self._showPropertyDescription)
198 self.connect(self._controlGroup, SIGNAL("stateChanged"), self._saveValue)
200 def _saveValue(self, index):
201 property = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
202 configuration = self._projectInfoRetrieve("MODULES")[self._currentModule()]["configuration"]
203 configurations = self._projectInfoRetrieve("CONFIGURATIONS")
204 if "type" not in configurations[configuration][property]["informations"].keys() or configurations[configuration][property]["informations"]["type"] == "int":
205 configurations[configuration][property]["value"] = str(int(self.pageContent.propertyTable.cellWidget(index, 1).value()))
206 elif configurations[configuration][property]["informations"]["type"] == "enum":
207 configurations[configuration][property]["value"] = unicode(self.pageContent.propertyTable.cellWidget(index, 1).currentText())
208 elif configurations[configuration][property]["informations"]["type"] == "boolean":
209 if self.pageContent.propertyTable.cellWidget(index, 1).isChecked():
210 configurations[configuration][property]["value"] = "1"
212 configurations[configuration][property]["value"] = "0"
213 self._projectInfoStore("CONFIGURATIONS", configurations)
215 def _moduleSelectionChanged(self, index):
216 module = unicode(self.pageContent.moduleTable.item(index, 1).text())
217 if self._buttonGroup.button(index).isChecked():
218 self._moduleSelected(module)
220 self._moduleUnselected(module)
222 def _moduleSelected(self, selectedModule):
223 modules = self._projectInfoRetrieve("MODULES")
224 modules[selectedModule]["enabled"] = True
225 self._projectInfoStore("MODULES", modules)
226 depends = self._projectInfoRetrieve("MODULES")[selectedModule]["depends"]
227 unsatisfied = self.selectDependencyCheck(selectedModule)
228 if len(unsatisfied) > 0:
229 message = self.tr("The module %1 needs the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
230 message = message.arg(selectedModule).arg(", ".join(unsatisfied))
231 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
232 if choice == QMessageBox.Yes:
233 for module in unsatisfied:
234 modules = self._projectInfoRetrieve("MODULES")
235 modules[module]["enabled"] = True
236 for index in range(self.pageContent.moduleTable.rowCount()):
237 if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
238 self._buttonGroup.button(index).setChecked(True)
240 def _moduleUnselected(self, unselectedModule):
241 modules = self._projectInfoRetrieve("MODULES")
242 modules[unselectedModule]["enabled"] = False
243 self._projectInfoStore("MODULES", modules)
244 unsatisfied = self.unselectDependencyCheck(unselectedModule)
245 if len(unsatisfied) > 0:
246 message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
247 message = message.arg(unselectedModule).arg(", ".join(unsatisfied))
248 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
249 if choice == QMessageBox.Yes:
250 for module in unsatisfied:
251 modules = self._projectInfoRetrieve("MODULES")
252 modules[module]["enabled"] = False
253 for index in range(self.pageContent.moduleTable.rowCount()):
254 if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
255 self._buttonGroup.button(index).setChecked(False)
258 def selectDependencyCheck(self, module):
260 modules = self._projectInfoRetrieve("MODULES")
261 for dependency in modules[module]["depends"]:
262 if not modules[dependency]["enabled"]:
263 unsatisfied |= set([dependency])
264 if dependency not in unsatisfied:
265 unsatisfied |= self.selectDependencyCheck(dependency)
268 def unselectDependencyCheck(self, dependency):
270 modules = self._projectInfoRetrieve("MODULES")
271 for module, informations in modules.items():
272 if dependency in informations["depends"] and informations["enabled"]:
273 unsatisfied |= set([module])
274 if dependency not in unsatisfied:
275 unsatisfied |= self.unselectDependencyCheck(module)
278 class QControlGroup(QObject):
280 QObject.__init__(self)
283 def addControl(self, id, control):
284 self._controls[id] = control
285 if type(control) == QCheckBox:
286 self.connect(control, SIGNAL("stateChanged(int)"), lambda: self._stateChanged(id))
287 elif type(control) == QSpinBox:
288 self.connect(control, SIGNAL("valueChanged(int)"), lambda: self._stateChanged(id))
289 elif type(control) == QComboBox:
290 self.connect(control, SIGNAL("currentIndexChanged(int)"), lambda: self._stateChanged(id))
291 elif type(control) == QDoubleSpinBox:
292 self.connect(control, SIGNAL("valueChanged(double)"), lambda: self._stateChanged(id))
297 def _stateChanged(self, id):
298 self.emit(SIGNAL("stateChanged"), id)