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 "min" in configurations[property]["informations"].keys():
106 minimum = int(configurations[property]["informations"]["min"])
109 spinBox.setMinimum(minimum)
110 if "max" in configurations[property]["informations"].keys():
111 maximum = int(configurations[property]["informations"]["max"])
114 spinBox.setMaximum(maximum)
115 if "unsigned" in configurations[property]["informations"].keys() and configurations[property]["informations"]["unsigned"]:
116 suff = str(spinBox.suffix())
118 spinBox.setSuffix(suff)
119 if "long" in configurations[property]["informations"].keys() and configurations[property]["informations"]["long"]:
120 suff = str(spinBox.suffix())
122 spinBox.setSuffix(suff)
123 spinBox.setValue(int(configurations[property]["value"].replace("L", "").replace("U", "")))
124 self._controlGroup.addControl(index, spinBox)
126 def _currentModule(self):
127 return unicode(self.pageContent.moduleTable.item(self.pageContent.moduleTable.currentRow(), 1).text())
129 def _currentModuleConfigurations(self):
130 return self._configurations(self._currentModule())
132 def _currentProperty(self):
133 return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
135 def _currentPropertyItem(self):
136 return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
138 def _module(self, row):
139 return unicode(self.pageContent.moduleTable.item(row, 1).text())
141 def _configurations(self, module):
142 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
143 return self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
145 def _resetPropertyDescription(self):
146 for index in range(self.pageContent.propertyTable.rowCount()):
147 propertyName = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
148 self.pageContent.propertyTable.item(index, 0).setText(propertyName)
150 def _showPropertyDescription(self):
151 self._resetPropertyDescription()
152 configurations = self._currentModuleConfigurations()
153 if self._currentProperty() in configurations.keys():
154 description = configurations[self._currentProperty()]["description"]
155 name = self._currentProperty()
156 self._currentPropertyItem().setText(name + "\n" + description)
159 self.pageContent.moduleTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
160 self.pageContent.moduleTable.horizontalHeader().setStretchLastSection(True)
161 self.pageContent.moduleTable.horizontalHeader().setVisible(False)
162 self.pageContent.moduleTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
163 self.pageContent.moduleTable.verticalHeader().setVisible(False)
164 self.pageContent.moduleTable.setColumnCount(2)
165 self.pageContent.moduleTable.setRowCount(0)
166 self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
167 self.pageContent.propertyTable.horizontalHeader().setVisible(False)
168 self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
169 self.pageContent.propertyTable.verticalHeader().setVisible(False)
170 self.pageContent.propertyTable.setColumnCount(2)
171 self.pageContent.propertyTable.setRowCount(0)
173 def _connectSignals(self):
174 self.connect(self.pageContent.moduleTable, SIGNAL("itemSelectionChanged()"), self._fillPropertyTable)
175 self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self._showPropertyDescription)
176 self.connect(self._controlGroup, SIGNAL("stateChanged"), self._saveValue)
178 def _saveValue(self, index):
179 property = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
180 configuration = self._projectInfoRetrieve("MODULES")[self._currentModule()]["configuration"]
181 configurations = self._projectInfoRetrieve("CONFIGURATIONS")
182 if "type" not in configurations[configuration][property]["informations"].keys() or configurations[configuration][property]["informations"]["type"] == "int":
183 configurations[configuration][property]["value"] = str(self.pageContent.propertyTable.cellWidget(index, 1).value())
184 elif configurations[configuration][property]["informations"]["type"] == "enum":
185 configurations[configuration][property]["value"] = unicode(self.pageContent.propertyTable.cellWidget(index, 1).currentText())
186 elif configurations[configuration][property]["informations"]["type"] == "boolean":
187 if self.pageContent.propertyTable.cellWidget(index, 1).isChecked():
188 configurations[configuration][property]["value"] = "1"
190 configurations[configuration][property]["value"] = "0"
191 self._projectInfoStore("CONFIGURATIONS", configurations)
193 def _moduleSelectionChanged(self, index):
194 module = unicode(self.pageContent.moduleTable.item(index, 1).text())
195 if self._buttonGroup.button(index).isChecked():
196 self._moduleSelected(module)
198 self._moduleUnselected(module)
200 def _moduleSelected(self, selectedModule):
201 modules = self._projectInfoRetrieve("MODULES")
202 modules[selectedModule]["enabled"] = True
203 self._projectInfoStore("MODULES", modules)
204 depends = self._projectInfoRetrieve("MODULES")[selectedModule]["depends"]
205 unsatisfied = self.selectDependencyCheck(selectedModule)
206 if len(unsatisfied) > 0:
207 message = self.tr("The module %1 needs the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
208 message = message.arg(selectedModule).arg(", ".join(unsatisfied))
209 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
210 if choice == QMessageBox.Yes:
211 for module in unsatisfied:
212 modules = self._projectInfoRetrieve("MODULES")
213 modules[module]["enabled"] = True
214 for index in range(self.pageContent.moduleTable.rowCount()):
215 if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
216 self._buttonGroup.button(index).setChecked(True)
218 def _moduleUnselected(self, unselectedModule):
219 modules = self._projectInfoRetrieve("MODULES")
220 modules[unselectedModule]["enabled"] = False
221 self._projectInfoStore("MODULES", modules)
222 unsatisfied = self.unselectDependencyCheck(unselectedModule)
223 if len(unsatisfied) > 0:
224 message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
225 message = message.arg(unselectedModule).arg(", ".join(unsatisfied))
226 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
227 if choice == QMessageBox.Yes:
228 for module in unsatisfied:
229 modules = self._projectInfoRetrieve("MODULES")
230 modules[module]["enabled"] = False
231 for index in range(self.pageContent.moduleTable.rowCount()):
232 if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
233 self._buttonGroup.button(index).setChecked(False)
236 def selectDependencyCheck(self, module):
238 modules = self._projectInfoRetrieve("MODULES")
239 for dependency in modules[module]["depends"]:
240 if not modules[dependency]["enabled"]:
241 unsatisfied |= set([dependency])
242 if dependency not in unsatisfied:
243 unsatisfied |= self.selectDependencyCheck(dependency)
246 def unselectDependencyCheck(self, dependency):
248 modules = self._projectInfoRetrieve("MODULES")
249 for module, informations in modules.items():
250 if dependency in informations["depends"] and informations["enabled"]:
251 unsatisfied |= set([module])
252 if dependency not in unsatisfied:
253 unsatisfied |= self.unselectDependencyCheck(module)
256 class QControlGroup(QObject):
258 QObject.__init__(self)
261 def addControl(self, id, control):
262 self._controls[id] = control
263 if type(control) == QCheckBox:
264 self.connect(control, SIGNAL("stateChanged(int)"), lambda: self._stateChanged(id))
265 elif type(control) == QSpinBox:
266 self.connect(control, SIGNAL("valueChanged(int)"), lambda: self._stateChanged(id))
267 elif type(control) == QComboBox:
268 self.connect(control, SIGNAL("currentIndexChanged(int)"), lambda: self._stateChanged(id))
273 def _stateChanged(self, id):
274 self.emit(SIGNAL("stateChanged"), id)