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"))
27 self._controlGroup = QControlGroup()
28 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 configurations[informations["configuration"]] = bertos_utils.loadConfigurationInfos(self._projectInfoRetrieve("SOURCES_PATH") +
47 "/" + informations["configuration"])
48 except ModuleDefineException, e:
49 self._exceptionOccurred(self.tr("Error parsing module information in file %1").arg(e.parameter))
50 except EnumDefineException, e:
51 self._exceptionOccurred(self.tr("Error parsing enum informations in file %1").arg(e.parameter))
52 except ConfigurationDefineException, e:
53 self._exceptionOccurred(self.tr("Error parsing configuration informations in file %1").arg(e.parameter))
55 self._projectInfoStore("MODULES", modules)
56 self._projectInfoStore("LISTS", lists)
57 self._projectInfoStore("CONFIGURATIONS", configurations)
59 def _fillModuleTable(self):
60 modules = self._projectInfoRetrieve("MODULES")
63 self.pageContent.moduleTable.setRowCount(len(modules))
64 for index, module in enumerate(modules):
65 self.pageContent.moduleTable.setItem(index, 1, QTableWidgetItem(module))
66 checkBox = QCheckBox()
67 self._buttonGroup.addButton(checkBox, index)
68 self.pageContent.moduleTable.setCellWidget(index, 0, checkBox)
69 checkBox.setChecked(modules[module]["enabled"])
71 def _fillPropertyTable(self):
72 module = self._currentModule()
73 self._controlGroup.clear()
74 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
75 configurations = self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
76 self.pageContent.propertyTable.clear()
77 self.pageContent.propertyTable.setRowCount(len(configurations))
78 for index, property in enumerate(configurations):
79 item = QTableWidgetItem(property)
80 item.setData(Qt.UserRole, qvariant_converter.convertString(property))
81 self.pageContent.propertyTable.setItem(index, 0, item)
82 if "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "boolean":
84 checkBox = QCheckBox()
85 self.pageContent.propertyTable.setCellWidget(index, 1, checkBox)
86 if configurations[property]["value"] == "1":
87 checkBox.setChecked(True)
89 checkBox.setChecked(False)
90 self._controlGroup.addControl(index, checkBox)
91 elif "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "enum":
93 comboBox = QComboBox()
94 self.pageContent.propertyTable.setCellWidget(index, 1, comboBox)
95 enum = self._projectInfoRetrieve("LISTS")[configurations[property]["informations"]["value_list"]]
96 for i, element in enumerate(enum):
97 comboBox.addItem(element)
98 if element == configurations[property]["value"]:
99 comboBox.setCurrentIndex(i)
100 self._controlGroup.addControl(index, comboBox)
102 ## int, long or undefined type property
104 self.pageContent.propertyTable.setCellWidget(index, 1, spinBox)
105 if bertos_utils.isInt(configurations[property]):
109 elif bertos_utils.isLong(configurations[property]):
110 minimum = -2147483648L
111 maximum = 2147483647L
113 elif bertos_utils.isUnsigned(configurations[property]):
117 elif bertos_utils.isUnsignedLong(configurations[property]):
119 maximum = 4294967295L
121 if "min" in configurations[property]["informations"].keys():
122 minimum = int(configurations[property]["informations"]["min"])
123 if "max" in configurations[property]["informations"].keys():
124 maximum = int(configurations[property]["informations"]["max"])
125 spinBox.setRange(minimum, maximum)
126 spinBox.setSuffix(suff)
127 spinBox.setValue(int(configurations[property]["value"].replace("L", "").replace("U", "")))
128 self._controlGroup.addControl(index, spinBox)
130 def _currentModule(self):
131 return unicode(self.pageContent.moduleTable.item(self.pageContent.moduleTable.currentRow(), 1).text())
133 def _currentModuleConfigurations(self):
134 return self._configurations(self._currentModule())
136 def _currentProperty(self):
137 return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
139 def _currentPropertyItem(self):
140 return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
142 def _module(self, row):
143 return unicode(self.pageContent.moduleTable.item(row, 1).text())
145 def _configurations(self, module):
146 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
147 return self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
149 def _resetPropertyDescription(self):
150 for index in range(self.pageContent.propertyTable.rowCount()):
151 propertyName = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
152 self.pageContent.propertyTable.item(index, 0).setText(propertyName)
154 def _showPropertyDescription(self):
155 self._resetPropertyDescription()
156 configurations = self._currentModuleConfigurations()
157 if self._currentProperty() in configurations.keys():
158 description = configurations[self._currentProperty()]["description"]
159 name = self._currentProperty()
160 self._currentPropertyItem().setText(name + "\n" + description)
163 self.pageContent.moduleTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
164 self.pageContent.moduleTable.horizontalHeader().setStretchLastSection(True)
165 self.pageContent.moduleTable.horizontalHeader().setVisible(False)
166 self.pageContent.moduleTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
167 self.pageContent.moduleTable.verticalHeader().setVisible(False)
168 self.pageContent.moduleTable.setColumnCount(2)
169 self.pageContent.moduleTable.setRowCount(0)
170 self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
171 self.pageContent.propertyTable.horizontalHeader().setVisible(False)
172 self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
173 self.pageContent.propertyTable.verticalHeader().setVisible(False)
174 self.pageContent.propertyTable.setColumnCount(2)
175 self.pageContent.propertyTable.setRowCount(0)
177 def _connectSignals(self):
178 self.connect(self.pageContent.moduleTable, SIGNAL("itemSelectionChanged()"), self._fillPropertyTable)
179 self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self._showPropertyDescription)
180 self.connect(self._controlGroup, SIGNAL("stateChanged"), self._saveValue)
182 def _saveValue(self, index):
183 property = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
184 configuration = self._projectInfoRetrieve("MODULES")[self._currentModule()]["configuration"]
185 configurations = self._projectInfoRetrieve("CONFIGURATIONS")
186 if "type" not in configurations[configuration][property]["informations"].keys() or configurations[configuration][property]["informations"]["type"] == "int":
187 configurations[configuration][property]["value"] = str(self.pageContent.propertyTable.cellWidget(index, 1).value())
188 elif configurations[configuration][property]["informations"]["type"] == "enum":
189 configurations[configuration][property]["value"] = unicode(self.pageContent.propertyTable.cellWidget(index, 1).currentText())
190 elif configurations[configuration][property]["informations"]["type"] == "boolean":
191 if self.pageContent.propertyTable.cellWidget(index, 1).isChecked():
192 configurations[configuration][property]["value"] = "1"
194 configurations[configuration][property]["value"] = "0"
195 self._projectInfoStore("CONFIGURATIONS", configurations)
197 def _moduleSelectionChanged(self, index):
198 module = unicode(self.pageContent.moduleTable.item(index, 1).text())
199 if self._buttonGroup.button(index).isChecked():
200 self._moduleSelected(module)
202 self._moduleUnselected(module)
204 def _moduleSelected(self, selectedModule):
205 modules = self._projectInfoRetrieve("MODULES")
206 modules[selectedModule]["enabled"] = True
207 self._projectInfoStore("MODULES", modules)
208 depends = self._projectInfoRetrieve("MODULES")[selectedModule]["depends"]
209 unsatisfied = self.selectDependencyCheck(selectedModule)
210 if len(unsatisfied) > 0:
211 message = self.tr("The module %1 needs the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
212 message = message.arg(selectedModule).arg(", ".join(unsatisfied))
213 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
214 if choice == QMessageBox.Yes:
215 for module in unsatisfied:
216 modules = self._projectInfoRetrieve("MODULES")
217 modules[module]["enabled"] = True
218 for index in range(self.pageContent.moduleTable.rowCount()):
219 if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
220 self._buttonGroup.button(index).setChecked(True)
222 def _moduleUnselected(self, unselectedModule):
223 modules = self._projectInfoRetrieve("MODULES")
224 modules[unselectedModule]["enabled"] = False
225 self._projectInfoStore("MODULES", modules)
226 unsatisfied = self.unselectDependencyCheck(unselectedModule)
227 if len(unsatisfied) > 0:
228 message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
229 message = message.arg(unselectedModule).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"] = False
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(False)
240 def selectDependencyCheck(self, module):
242 modules = self._projectInfoRetrieve("MODULES")
243 for dependency in modules[module]["depends"]:
244 if not modules[dependency]["enabled"]:
245 unsatisfied |= set([dependency])
246 if dependency not in unsatisfied:
247 unsatisfied |= self.selectDependencyCheck(dependency)
250 def unselectDependencyCheck(self, dependency):
252 modules = self._projectInfoRetrieve("MODULES")
253 for module, informations in modules.items():
254 if dependency in informations["depends"] and informations["enabled"]:
255 unsatisfied |= set([module])
256 if dependency not in unsatisfied:
257 unsatisfied |= self.unselectDependencyCheck(module)
260 class QControlGroup(QObject):
262 QObject.__init__(self)
265 def addControl(self, id, control):
266 self._controls[id] = control
267 if type(control) == QCheckBox:
268 self.connect(control, SIGNAL("stateChanged(int)"), lambda: self._stateChanged(id))
269 elif type(control) == QSpinBox:
270 self.connect(control, SIGNAL("valueChanged(int)"), lambda: self._stateChanged(id))
271 elif type(control) == QComboBox:
272 self.connect(control, SIGNAL("currentIndexChanged(int)"), lambda: self._stateChanged(id))
277 def _stateChanged(self, id):
278 self.emit(SIGNAL("stateChanged"), id)