Add the module description in the module page
[bertos.git] / wizard / BModulePage.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # Copyright 2009 Develer S.r.l. (http://www.develer.com/)
5 # All rights reserved.
6 #
7 # $Id:$
8 #
9 # Author: Lorenzo Berni <duplo@develer.com>
10 #
11
12 import os
13
14 from PyQt4.QtGui import *
15 from BWizardPage import *
16 import bertos_utils
17
18 from DefineException import *
19 from const import *
20
21 class BModulePage(BWizardPage):
22     
23     def __init__(self):
24         BWizardPage.__init__(self, UI_LOCATION + "/module_select.ui")
25         self.setTitle(self.tr("Configure the BeRTOS modules"))
26         self._setupUi()
27         self._controlGroup = QControlGroup()
28         self._connectSignals()
29     
30     def reloadData(self):
31         self._setupButtonGroup()
32         self._loadModuleData()
33         self._fillModuleTable()
34     
35     def _setupButtonGroup(self):
36         self._buttonGroup = QButtonGroup()
37         self._buttonGroup.setExclusive(False)
38         self.connect(self._buttonGroup, SIGNAL("buttonClicked(int)"), self._moduleSelectionChanged)
39     
40     def _loadModuleData(self):
41         try:
42             modules = bertos_utils.loadModuleInfosDict(self._projectInfoRetrieve("SOURCES_PATH"))
43             lists = bertos_utils.loadDefineListsDict(self._projectInfoRetrieve("SOURCES_PATH"))
44             configurations = {}
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))
55         else:
56             self._projectInfoStore("MODULES", modules)
57             self._projectInfoStore("LISTS", lists)
58             self._projectInfoStore("CONFIGURATIONS", configurations)
59     
60     def _fillModuleTable(self):
61         modules = self._projectInfoRetrieve("MODULES")
62         if modules is None:
63             return
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"])
71     
72     def _fillPropertyTable(self):
73         module = self._currentModule()
74         self._controlGroup.clear()
75         configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
76         moduleDescription = self._projectInfoRetrieve("MODULES")[module]["description"]
77         self.pageContent.moduleLabel.setText(moduleDescription)
78         self.pageContent.moduleLabel.setVisible(True)
79         self.pageContent.propertyTable.clear()
80         if len(configuration) > 0:
81             configurations = self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
82             self.pageContent.propertyTable.setRowCount(len(configurations) + 1)
83             for index, property in enumerate(configurations):
84                 item = QTableWidgetItem(property)
85                 item.setData(Qt.UserRole, qvariant_converter.convertString(property))
86                 self.pageContent.propertyTable.setItem(index, 0, item)
87                 if "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "boolean":
88                     ## boolean property
89                     checkBox = QCheckBox()
90                     self.pageContent.propertyTable.setCellWidget(index, 1, checkBox)
91                     if configurations[property]["value"] == "1":
92                         checkBox.setChecked(True)
93                     else:
94                         checkBox.setChecked(False)
95                     self._controlGroup.addControl(index, checkBox)
96                 elif "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "enum":
97                     ## enum property
98                     comboBox = QComboBox()
99                     self.pageContent.propertyTable.setCellWidget(index, 1, comboBox)
100                     enum = self._projectInfoRetrieve("LISTS")[configurations[property]["informations"]["value_list"]]
101                     for i, element in enumerate(enum):
102                         comboBox.addItem(element)
103                         if element == configurations[property]["value"]:
104                             comboBox.setCurrentIndex(i)
105                     self._controlGroup.addControl(index, comboBox)
106                 else:
107                     ## int, long or undefined type property
108                     spinBox = None
109                     if bertos_utils.isLong(configurations[property]) or bertos_utils.isUnsignedLong(configurations[property]):
110                         spinBox = QDoubleSpinBox()
111                         spinBox.setDecimals(0)
112                     else:
113                         spinBox = QSpinBox()
114                     self.pageContent.propertyTable.setCellWidget(index, 1, spinBox)
115                     minimum = -32768
116                     maximum = 32767
117                     suff = ""
118                     if bertos_utils.isLong(configurations[property]):
119                         minimum = -2147483648
120                         maximum = 2147483647
121                         suff = "L"
122                     elif bertos_utils.isUnsigned(configurations[property]):
123                         minimum = 0
124                         maximum = 65535
125                         suff = "U"
126                     elif bertos_utils.isUnsignedLong(configurations[property]):
127                         minimum = 0
128                         maximum = 4294967295
129                         suff = "UL"
130                     if "min" in configurations[property]["informations"].keys():
131                         minimum = int(configurations[property]["informations"]["min"])
132                     if "max" in configurations[property]["informations"].keys():
133                         maximum = int(configurations[property]["informations"]["max"])
134                     spinBox.setRange(minimum, maximum)
135                     spinBox.setSuffix(suff)
136                     spinBox.setValue(int(configurations[property]["value"].replace("L", "").replace("U", "")))
137                     self._controlGroup.addControl(index, spinBox)
138         else:
139             self.pageContent.propertyTable.setRowCount(0)
140     
141     def _currentModule(self):
142         return unicode(self.pageContent.moduleTable.item(self.pageContent.moduleTable.currentRow(), 1).text())
143     
144     def _currentModuleConfigurations(self):
145         return self._configurations(self._currentModule())
146     
147     def _currentProperty(self):
148         return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
149     
150     def _currentPropertyItem(self):
151         return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
152     
153     def _module(self, row):
154         return unicode(self.pageContent.moduleTable.item(row, 1).text())
155     
156     def _configurations(self, module):
157         configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
158         return self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
159     
160     def _resetPropertyDescription(self):
161         for index in range(self.pageContent.propertyTable.rowCount()):
162             propertyName = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
163             self.pageContent.propertyTable.item(index, 0).setText(propertyName)
164     
165     def _showPropertyDescription(self):
166         self._resetPropertyDescription()
167         configurations = self._currentModuleConfigurations()
168         if self._currentProperty() in configurations.keys():
169             description = configurations[self._currentProperty()]["description"]
170             name = self._currentProperty()
171             self._currentPropertyItem().setText(name + "\n" + description)
172     
173     def _setupUi(self):
174         self.pageContent.moduleTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
175         self.pageContent.moduleTable.horizontalHeader().setStretchLastSection(True)
176         self.pageContent.moduleTable.horizontalHeader().setVisible(False)
177         self.pageContent.moduleTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
178         self.pageContent.moduleTable.verticalHeader().setVisible(False)
179         self.pageContent.moduleTable.setColumnCount(2)
180         self.pageContent.moduleTable.setRowCount(0)
181         self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
182         self.pageContent.propertyTable.horizontalHeader().setVisible(False)
183         self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
184         self.pageContent.propertyTable.verticalHeader().setVisible(False)
185         self.pageContent.propertyTable.setColumnCount(2)
186         self.pageContent.propertyTable.setRowCount(0)
187         self.pageContent.moduleLabel.setVisible(False)
188     
189     def _connectSignals(self):
190         self.connect(self.pageContent.moduleTable, SIGNAL("itemSelectionChanged()"), self._fillPropertyTable)
191         self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self._showPropertyDescription)
192         self.connect(self._controlGroup, SIGNAL("stateChanged"), self._saveValue)
193     
194     def _saveValue(self, index):
195         property = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
196         configuration = self._projectInfoRetrieve("MODULES")[self._currentModule()]["configuration"]
197         configurations = self._projectInfoRetrieve("CONFIGURATIONS")
198         if "type" not in configurations[configuration][property]["informations"].keys() or configurations[configuration][property]["informations"]["type"] == "int":
199             configurations[configuration][property]["value"] = str(int(self.pageContent.propertyTable.cellWidget(index, 1).value()))
200         elif configurations[configuration][property]["informations"]["type"] == "enum":
201             configurations[configuration][property]["value"] = unicode(self.pageContent.propertyTable.cellWidget(index, 1).currentText())
202         elif configurations[configuration][property]["informations"]["type"] == "boolean":
203             if self.pageContent.propertyTable.cellWidget(index, 1).isChecked():
204                 configurations[configuration][property]["value"] = "1"
205             else:
206                 configurations[configuration][property]["value"] = "0"
207         self._projectInfoStore("CONFIGURATIONS", configurations)
208     
209     def _moduleSelectionChanged(self, index):
210         module = unicode(self.pageContent.moduleTable.item(index, 1).text())
211         if self._buttonGroup.button(index).isChecked():
212             self._moduleSelected(module)
213         else:
214             self._moduleUnselected(module)
215     
216     def _moduleSelected(self, selectedModule):
217         modules = self._projectInfoRetrieve("MODULES")
218         modules[selectedModule]["enabled"] = True
219         self._projectInfoStore("MODULES", modules)
220         depends = self._projectInfoRetrieve("MODULES")[selectedModule]["depends"]
221         unsatisfied = self.selectDependencyCheck(selectedModule)
222         if len(unsatisfied) > 0:
223             message = self.tr("The module %1 needs the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
224             message = message.arg(selectedModule).arg(", ".join(unsatisfied))
225             choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
226             if choice == QMessageBox.Yes:
227                 for module in unsatisfied:
228                     modules = self._projectInfoRetrieve("MODULES")
229                     modules[module]["enabled"] = True
230                 for index in range(self.pageContent.moduleTable.rowCount()):
231                     if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
232                         self._buttonGroup.button(index).setChecked(True)
233     
234     def _moduleUnselected(self, unselectedModule):
235         modules = self._projectInfoRetrieve("MODULES")
236         modules[unselectedModule]["enabled"] = False
237         self._projectInfoStore("MODULES", modules)
238         unsatisfied = self.unselectDependencyCheck(unselectedModule)
239         if len(unsatisfied) > 0:
240             message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
241             message = message.arg(unselectedModule).arg(", ".join(unsatisfied))
242             choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
243             if choice == QMessageBox.Yes:
244                 for module in unsatisfied:
245                     modules = self._projectInfoRetrieve("MODULES")
246                     modules[module]["enabled"] = False
247                 for index in range(self.pageContent.moduleTable.rowCount()):
248                     if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
249                         self._buttonGroup.button(index).setChecked(False)
250     
251     
252     def selectDependencyCheck(self, module):
253         unsatisfied = set()
254         modules = self._projectInfoRetrieve("MODULES")
255         for dependency in modules[module]["depends"]:
256             if not modules[dependency]["enabled"]:
257                 unsatisfied |= set([dependency])
258                 if dependency not in unsatisfied:
259                     unsatisfied |= self.selectDependencyCheck(dependency)
260         return unsatisfied
261     
262     def unselectDependencyCheck(self, dependency):
263         unsatisfied = set()
264         modules = self._projectInfoRetrieve("MODULES")
265         for module, informations in modules.items():
266             if dependency in informations["depends"] and informations["enabled"]:
267                 unsatisfied |= set([module])
268                 if dependency not in unsatisfied:
269                     unsatisfied |= self.unselectDependencyCheck(module)
270         return unsatisfied
271
272 class QControlGroup(QObject):
273     def __init__(self):
274         QObject.__init__(self)
275         self._controls = {}
276     
277     def addControl(self, id, control):
278         self._controls[id] = control
279         if type(control) == QCheckBox:
280             self.connect(control, SIGNAL("stateChanged(int)"), lambda: self._stateChanged(id))
281         elif type(control) == QSpinBox:
282             self.connect(control, SIGNAL("valueChanged(int)"), lambda: self._stateChanged(id))
283         elif type(control) == QComboBox:
284             self.connect(control, SIGNAL("currentIndexChanged(int)"), lambda: self._stateChanged(id))
285         elif type(control) == QDoubleSpinBox:
286             self.connect(control, SIGNAL("valueChanged(double)"), lambda: self._stateChanged(id))
287     
288     def clear(self):
289         self._controls = {}
290     
291     def _stateChanged(self, id):
292         self.emit(SIGNAL("stateChanged"), id)