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 *
19 class BModulePage(BWizardPage):
22 BWizardPage.__init__(self, "module_select.ui")
23 self.setTitle(self.tr("Configure the BeRTOS modules"))
25 self._connectSignals()
28 self._setupButtonGroup()
29 self._loadModuleData()
30 self._fillModuleTable()
32 def _setupButtonGroup(self):
33 self._buttonGroup = QButtonGroup()
34 self._buttonGroup.setExclusive(False)
35 self.connect(self._buttonGroup, SIGNAL("buttonClicked(int)"), self._moduleSelectionChanged)
37 def _loadModuleData(self):
38 modules = bertos_utils.loadModuleInfosDict(self._projectInfoRetrieve("SOURCES_PATH"))
39 lists = bertos_utils.loadDefineListsDict(self._projectInfoRetrieve("SOURCES_PATH"))
41 for module, informations in modules.items():
42 configurations[informations["configuration"]] = bertos_utils.loadConfigurationInfos(self._projectInfoRetrieve("SOURCES_PATH") +
43 "/" + informations["configuration"])
44 self._projectInfoStore("MODULES", modules)
45 self._projectInfoStore("LISTS", lists)
46 self._projectInfoStore("CONFIGURATIONS", configurations)
48 def _fillModuleTable(self):
49 modules = self._projectInfoRetrieve("MODULES")
50 self.pageContent.moduleTable.setRowCount(len(modules))
51 for index, module in enumerate(modules):
52 self.pageContent.moduleTable.setItem(index, 1, QTableWidgetItem(module))
53 checkBox = QCheckBox()
54 self._buttonGroup.addButton(checkBox, index)
55 self.pageContent.moduleTable.setCellWidget(index, 0, checkBox)
56 checkBox.setChecked(modules[module]["enabled"])
58 def _fillPropertyTable(self):
59 module = self._currentModule()
60 configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
61 configurations = self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
62 self.pageContent.propertyTable.clear()
63 self.pageContent.propertyTable.setRowCount(len(configurations))
64 for index, property in enumerate(configurations):
65 item = QTableWidgetItem(property)
66 item.setData(Qt.UserRole, qvariant_converter.convertString(property))
67 self.pageContent.propertyTable.setItem(index, 0, item)
68 if "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "boolean":
70 checkBox = QCheckBox()
71 self.pageContent.propertyTable.setCellWidget(index, 1, checkBox)
72 if configurations[property]["value"] == "1":
73 checkBox.setChecked(True)
75 checkBox.setChecked(False)
76 elif "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "enum":
78 comboBox = QComboBox()
79 self.pageContent.propertyTable.setCellWidget(index, 1, comboBox)
80 enum = self._projectInfoRetrieve("LISTS")[configurations[property]["informations"]["value_list"]]
81 for index, element in enumerate(enum):
82 comboBox.addItem(element)
83 if element == configurations[property]["value"]:
84 comboBox.setCurrentIndex(index)
86 ## int, long or undefined type property
88 self.pageContent.propertyTable.setCellWidget(index, 1, spinBox)
89 if "min" in configurations[property]["informations"].keys():
90 minimum = int(configurations[property]["informations"]["min"])
93 spinBox.setMinimum(minimum)
94 if "max" in configurations[property]["informations"].keys():
95 maximum = int(configurations[property]["informations"]["max"])
98 spinBox.setMaximum(maximum)
99 if "long" in configurations[property]["informations"].keys() and configurations[property]["informations"]["long"] == "True":
100 spinBox.setSuffix("L")
101 spinBox.setValue(int(configurations[property]["value"].replace("L", "")))
103 def _savePage(self, previousRow, previousColumn):
104 module = self._module(previousRow)
105 moduleConfigurations = self._projectInfoRetrieve("CONFIGURATIONS")[self._configurations(module)]
106 for index in range(self.pageContent.propertyTable.rowCount()):
107 parameter = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
108 if "type" not in moduleConfigurations[parameter]["informations"].keys() or moduleConfigurations[parameter]["informations"]["type"] == "int":
109 moduleConfigurations[parameter]["value"] = str(self.pageContent.propertyTable.cellWidget(index, 1).value())
110 elif moduleConfigurations[parameter]["informations"]["type"] == "enum":
111 moduleConfigurations[parameter]["value"] = unicode(self.pageContent.propertyTable.cellWidget(index, 1).currentText())
112 elif moduleConfigurations[parameter]["informations"]["type"] == "boolean":
113 if self.pageContent.propertyTable.cellWidget(index, 1).isChecked():
114 moduleConfigurations[parameter]["value"] = "1"
116 moduleConfigurations[parameter]["value"] = "0"
118 def _pageChanged(self, row, column, previousRow, previousColumn):
119 if previousRow != -1 and previousColumn != -1:
120 self._savePage(previousRow, previousColumn)
121 self._fillPropertyTable()
123 def _currentModule(self):
124 return unicode(self.pageContent.moduleTable.item(self.pageContent.moduleTable.currentRow(), 1).text())
126 def _currentModuleConfigurations(self):
127 return self._configurations(self._currentModule())["configurations"]
129 def _currentProperty(self):
130 return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
132 def _currentPropertyItem(self):
133 return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
135 def _module(self, row):
136 return unicode(self.pageContent.moduleTable.item(row, 1).text())
138 def _configurations(self, module):
139 return self._projectInfoRetrieve("MODULES")[module]["configuration"]
141 def _resetPropertyDescription(self):
142 for index in range(self.pageContent.propertyTable.rowCount()):
143 propertyName = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
144 self.pageContent.propertyTable.item(index, 0).setText(propertyName)
146 def _showPropertyDescription(self):
147 self._resetPropertyDescription()
148 configurations = self._projectInfoRetrieve("CONFIGURATIONS")[self._currentModuleConfigurations()]
149 if self._currentProperty() in configurations.keys():
150 description = configurations[self._currentProperty()]["description"]
151 name = self._currentProperty()
152 self._currentPropertyItem().setText(name + "\n" + description)
155 self.pageContent.moduleTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
156 self.pageContent.moduleTable.horizontalHeader().setStretchLastSection(True)
157 self.pageContent.moduleTable.horizontalHeader().setVisible(False)
158 self.pageContent.moduleTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
159 self.pageContent.moduleTable.verticalHeader().setVisible(False)
160 self.pageContent.moduleTable.setColumnCount(2)
161 self.pageContent.moduleTable.setRowCount(0)
162 self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
163 self.pageContent.propertyTable.horizontalHeader().setVisible(False)
164 self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
165 self.pageContent.propertyTable.verticalHeader().setVisible(False)
166 self.pageContent.propertyTable.setColumnCount(2)
167 self.pageContent.propertyTable.setRowCount(0)
169 def _connectSignals(self):
170 self.connect(self.pageContent.moduleTable, SIGNAL("currentCellChanged(int, int, int, int)"), self._pageChanged)
171 self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self._showPropertyDescription)
173 def _moduleSelectionChanged(self, index):
174 module = unicode(self.pageContent.moduleTable.item(index, 1).text())
175 if self._buttonGroup.button(index).isChecked():
176 self._moduleSelected(module)
178 self._moduleUnselected(module)
180 def _moduleSelected(self, selectedModule):
181 modules = self._projectInfoRetrieve("MODULES")
182 modules[selectedModule]["enabled"] = True
183 self._projectInfoStore("MODULES", modules)
184 depends = self._projectInfoRetrieve("MODULES")[selectedModule]["depends"]
185 unsatisfied = self.selectDependencyCheck(selectedModule)
186 if len(unsatisfied) > 0:
187 message = self.tr("The module %1 needs the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
188 message = message.arg(selectedModule).arg(", ".join(unsatisfied))
189 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
190 if choice == QMessageBox.Yes:
191 for module in unsatisfied:
192 modules = self._projectInfoRetrieve("MODULES")
193 modules[module]["enabled"] = True
194 for index in range(self.pageContent.moduleTable.rowCount()):
195 if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
196 self._buttonGroup.button(index).setChecked(True)
198 def _moduleUnselected(self, unselectedModule):
199 modules = self._projectInfoRetrieve("MODULES")
200 modules[unselectedModule]["enabled"] = False
201 self._projectInfoStore("MODULES", modules)
202 unsatisfied = self.unselectDependencyCheck(unselectedModule)
203 if len(unsatisfied) > 0:
204 message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
205 message = message.arg(unselectedModule).arg(", ".join(unsatisfied))
206 choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
207 if choice == QMessageBox.Yes:
208 for module in unsatisfied:
209 modules = self._projectInfoRetrieve("MODULES")
210 modules[module]["enabled"] = False
211 for index in range(self.pageContent.moduleTable.rowCount()):
212 if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
213 self._buttonGroup.button(index).setChecked(False)
216 def selectDependencyCheck(self, module):
218 modules = self._projectInfoRetrieve("MODULES")
219 for dependency in modules[module]["depends"]:
220 if not modules[dependency]["enabled"]:
221 unsatisfied |= set([dependency])
222 if dependency not in unsatisfied:
223 unsatisfied |= self.selectDependencyCheck(dependency)
226 def unselectDependencyCheck(self, dependency):
228 modules = self._projectInfoRetrieve("MODULES")
229 for module, informations in modules.items():
230 if dependency in informations["depends"] and informations["enabled"]:
231 unsatisfied |= set([module])
232 if dependency not in unsatisfied:
233 unsatisfied |= self.unselectDependencyCheck(module)