Change the int type identification
[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                 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))
54         else:
55             self._projectInfoStore("MODULES", modules)
56             self._projectInfoStore("LISTS", lists)
57             self._projectInfoStore("CONFIGURATIONS", configurations)
58     
59     def _fillModuleTable(self):
60         modules = self._projectInfoRetrieve("MODULES")
61         if modules is None:
62             return
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"])
70     
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":
83                 ## boolean property
84                 checkBox = QCheckBox()
85                 self.pageContent.propertyTable.setCellWidget(index, 1, checkBox)
86                 if configurations[property]["value"] == "1":
87                     checkBox.setChecked(True)
88                 else:
89                     checkBox.setChecked(False)
90                 self._controlGroup.addControl(index, checkBox)
91             elif "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "enum":
92                 ## enum property
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)
101             else:
102                 ## int, long or undefined type property
103                 spinBox = QSpinBox()
104                 self.pageContent.propertyTable.setCellWidget(index, 1, spinBox)
105                 if bertos_utils.isInt(configurations[property]):
106                     minimum = -32768
107                     maximmum = 32767
108                     suff = ""
109                 elif bertos_utils.isLong(configurations[property]):
110                     minimum = -2147483648
111                     maximum = 2147483647
112                     suff = "L"
113                 elif bertos_utils.isUnsigned(configurations[property]):
114                     minimum = 0
115                     maximum = 65535
116                     suff = "U"
117                 elif bertos_utils.isUnsignedLong(configurations[property]):
118                     minimum = 0
119                     maximum = 4294967295
120                     suff = "UL"
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)
129     
130     def _currentModule(self):
131         return unicode(self.pageContent.moduleTable.item(self.pageContent.moduleTable.currentRow(), 1).text())
132     
133     def _currentModuleConfigurations(self):
134         return self._configurations(self._currentModule())
135     
136     def _currentProperty(self):
137         return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
138     
139     def _currentPropertyItem(self):
140         return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
141     
142     def _module(self, row):
143         return unicode(self.pageContent.moduleTable.item(row, 1).text())
144     
145     def _configurations(self, module):
146         configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
147         return self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
148     
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)
153     
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)
161     
162     def _setupUi(self):
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)
176     
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)
181     
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"
193             else:
194                 configurations[configuration][property]["value"] = "0"
195         self._projectInfoStore("CONFIGURATIONS", configurations)
196     
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)
201         else:
202             self._moduleUnselected(module)
203     
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)
221     
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)
238     
239     
240     def selectDependencyCheck(self, module):
241         unsatisfied = set()
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)
248         return unsatisfied
249     
250     def unselectDependencyCheck(self, dependency):
251         unsatisfied = set()
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)
258         return unsatisfied
259
260 class QControlGroup(QObject):
261     def __init__(self):
262         QObject.__init__(self)
263         self._controls = {}
264     
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))
273     
274     def clear(self):
275         self._controls = {}
276     
277     def _stateChanged(self, id):
278         self.emit(SIGNAL("stateChanged"), id)