Create the QControlGroup class and use it for the configuration parameter controllers
[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
19 class BModulePage(BWizardPage):
20     
21     def __init__(self):
22         BWizardPage.__init__(self, "module_select.ui")
23         self.setTitle(self.tr("Configure the BeRTOS modules"))
24         self._setupUi()
25         self._controlGroup = QControlGroup()
26         self._connectSignals()
27     
28     def reloadData(self):
29         self._setupButtonGroup()
30         self._loadModuleData()
31         self._fillModuleTable()
32     
33     def _setupButtonGroup(self):
34         self._buttonGroup = QButtonGroup()
35         self._buttonGroup.setExclusive(False)
36         self.connect(self._buttonGroup, SIGNAL("buttonClicked(int)"), self._moduleSelectionChanged)
37     
38     def _loadModuleData(self):
39         modules = bertos_utils.loadModuleInfosDict(self._projectInfoRetrieve("SOURCES_PATH"))
40         lists = bertos_utils.loadDefineListsDict(self._projectInfoRetrieve("SOURCES_PATH"))
41         configurations = {}
42         for module, informations in modules.items():
43             configurations[informations["configuration"]] = bertos_utils.loadConfigurationInfos(self._projectInfoRetrieve("SOURCES_PATH") +
44                                                                                                 "/" + informations["configuration"])
45         self._projectInfoStore("MODULES", modules)
46         self._projectInfoStore("LISTS", lists)
47         self._projectInfoStore("CONFIGURATIONS", configurations)
48     
49     def _fillModuleTable(self):
50         modules = self._projectInfoRetrieve("MODULES")
51         self.pageContent.moduleTable.setRowCount(len(modules))
52         for index, module in enumerate(modules):
53             self.pageContent.moduleTable.setItem(index, 1, QTableWidgetItem(module))
54             checkBox = QCheckBox()
55             self._buttonGroup.addButton(checkBox, index)
56             self.pageContent.moduleTable.setCellWidget(index, 0, checkBox)
57             checkBox.setChecked(modules[module]["enabled"])
58     
59     def _fillPropertyTable(self):
60         module = self._currentModule()
61         self._controlGroup.clear()
62         configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
63         configurations = self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
64         self.pageContent.propertyTable.clear()
65         self.pageContent.propertyTable.setRowCount(len(configurations))
66         for index, property in enumerate(configurations):
67             item = QTableWidgetItem(property)
68             item.setData(Qt.UserRole, qvariant_converter.convertString(property))
69             self.pageContent.propertyTable.setItem(index, 0, item)
70             if "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "boolean":
71                 ## boolean property
72                 checkBox = QCheckBox()
73                 self.pageContent.propertyTable.setCellWidget(index, 1, checkBox)
74                 if configurations[property]["value"] == "1":
75                     checkBox.setChecked(True)
76                 else:
77                     checkBox.setChecked(False)
78                 self._controlGroup.addControl(index, checkBox)
79             elif "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "enum":
80                 ## enum property
81                 comboBox = QComboBox()
82                 self.pageContent.propertyTable.setCellWidget(index, 1, comboBox)
83                 enum = self._projectInfoRetrieve("LISTS")[configurations[property]["informations"]["value_list"]]
84                 for i, element in enumerate(enum):
85                     comboBox.addItem(element)
86                     if element == configurations[property]["value"]:
87                         comboBox.setCurrentIndex(i)
88                 self._controlGroup.addControl(index, comboBox)
89             else:
90                 ## int, long or undefined type property
91                 spinBox = QSpinBox()
92                 self.pageContent.propertyTable.setCellWidget(index, 1, spinBox)
93                 if "min" in configurations[property]["informations"].keys():
94                     minimum = int(configurations[property]["informations"]["min"])
95                 else:
96                     minimum = -32768
97                 spinBox.setMinimum(minimum)
98                 if "max" in configurations[property]["informations"].keys():
99                     maximum = int(configurations[property]["informations"]["max"])
100                 else:
101                     maximum = 32767
102                 spinBox.setMaximum(maximum)
103                 if "long" in configurations[property]["informations"].keys() and configurations[property]["informations"]["long"] == "True":
104                     spinBox.setSuffix("L")
105                 spinBox.setValue(int(configurations[property]["value"].replace("L", "")))
106                 self._controlGroup.addControl(index, spinBox)
107     
108     def _currentModule(self):
109         return unicode(self.pageContent.moduleTable.item(self.pageContent.moduleTable.currentRow(), 1).text())
110     
111     def _currentModuleConfigurations(self):
112         return self._configurations(self._currentModule())
113     
114     def _currentProperty(self):
115         return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
116     
117     def _currentPropertyItem(self):
118         return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
119     
120     def _module(self, row):
121         return unicode(self.pageContent.moduleTable.item(row, 1).text())
122     
123     def _configurations(self, module):
124         configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
125         return self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
126     
127     def _resetPropertyDescription(self):
128         for index in range(self.pageContent.propertyTable.rowCount()):
129             propertyName = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
130             self.pageContent.propertyTable.item(index, 0).setText(propertyName)
131     
132     def _showPropertyDescription(self):
133         self._resetPropertyDescription()
134         configurations = self._currentModuleConfigurations()
135         if self._currentProperty() in configurations.keys():
136             description = configurations[self._currentProperty()]["description"]
137             name = self._currentProperty()
138             self._currentPropertyItem().setText(name + "\n" + description)
139     
140     def _setupUi(self):
141         self.pageContent.moduleTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
142         self.pageContent.moduleTable.horizontalHeader().setStretchLastSection(True)
143         self.pageContent.moduleTable.horizontalHeader().setVisible(False)
144         self.pageContent.moduleTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
145         self.pageContent.moduleTable.verticalHeader().setVisible(False)
146         self.pageContent.moduleTable.setColumnCount(2)
147         self.pageContent.moduleTable.setRowCount(0)
148         self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
149         self.pageContent.propertyTable.horizontalHeader().setVisible(False)
150         self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
151         self.pageContent.propertyTable.verticalHeader().setVisible(False)
152         self.pageContent.propertyTable.setColumnCount(2)
153         self.pageContent.propertyTable.setRowCount(0)
154     
155     def _connectSignals(self):
156         self.connect(self.pageContent.moduleTable, SIGNAL("itemSelectionChanged()"), self._fillPropertyTable)
157         self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self._showPropertyDescription)
158         self.connect(self._controlGroup, SIGNAL("stateChanged"), self._saveValue)
159     
160     def _saveValue(self, index):
161         property = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
162         configuration = self._projectInfoRetrieve("MODULES")[self._currentModule()]["configuration"]
163         configurations = self._projectInfoRetrieve("CONFIGURATIONS")
164         if "type" not in configurations[configuration][property]["informations"].keys() or configurations[configuration][property]["informations"]["type"] == "int":
165             configurations[configuration][property]["value"] = str(self.pageContent.propertyTable.cellWidget(index, 1).value())
166         elif configurations[configuration][property]["informations"]["type"] == "enum":
167             configurations[configuration][property]["value"] = unicode(self.pageContent.propertyTable.cellWidget(index, 1).currentText())
168         elif configurations[configuration][property]["informations"]["type"] == "boolean":
169             if self.pageContent.propertyTable.cellWidget(index, 1).isChecked():
170                 configurations[configuration][property]["value"] = "1"
171             else:
172                 configurations[configuration][property]["value"] = "0"
173         self._projectInfoStore("CONFIGURATIONS", configurations)
174     
175     def _moduleSelectionChanged(self, index):
176         module = unicode(self.pageContent.moduleTable.item(index, 1).text())
177         if self._buttonGroup.button(index).isChecked():
178             self._moduleSelected(module)
179         else:
180             self._moduleUnselected(module)
181     
182     def _moduleSelected(self, selectedModule):
183         modules = self._projectInfoRetrieve("MODULES")
184         modules[selectedModule]["enabled"] = True
185         self._projectInfoStore("MODULES", modules)
186         depends = self._projectInfoRetrieve("MODULES")[selectedModule]["depends"]
187         unsatisfied = self.selectDependencyCheck(selectedModule)
188         if len(unsatisfied) > 0:
189             message = self.tr("The module %1 needs the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
190             message = message.arg(selectedModule).arg(", ".join(unsatisfied))
191             choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
192             if choice == QMessageBox.Yes:
193                 for module in unsatisfied:
194                     modules = self._projectInfoRetrieve("MODULES")
195                     modules[module]["enabled"] = True
196                 for index in range(self.pageContent.moduleTable.rowCount()):
197                     if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
198                         self._buttonGroup.button(index).setChecked(True)
199     
200     def _moduleUnselected(self, unselectedModule):
201         modules = self._projectInfoRetrieve("MODULES")
202         modules[unselectedModule]["enabled"] = False
203         self._projectInfoStore("MODULES", modules)
204         unsatisfied = self.unselectDependencyCheck(unselectedModule)
205         if len(unsatisfied) > 0:
206             message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
207             message = message.arg(unselectedModule).arg(", ".join(unsatisfied))
208             choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
209             if choice == QMessageBox.Yes:
210                 for module in unsatisfied:
211                     modules = self._projectInfoRetrieve("MODULES")
212                     modules[module]["enabled"] = False
213                 for index in range(self.pageContent.moduleTable.rowCount()):
214                     if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
215                         self._buttonGroup.button(index).setChecked(False)
216     
217     
218     def selectDependencyCheck(self, module):
219         unsatisfied = set()
220         modules = self._projectInfoRetrieve("MODULES")
221         for dependency in modules[module]["depends"]:
222             if not modules[dependency]["enabled"]:
223                 unsatisfied |= set([dependency])
224                 if dependency not in unsatisfied:
225                     unsatisfied |= self.selectDependencyCheck(dependency)
226         return unsatisfied
227     
228     def unselectDependencyCheck(self, dependency):
229         unsatisfied = set()
230         modules = self._projectInfoRetrieve("MODULES")
231         for module, informations in modules.items():
232             if dependency in informations["depends"] and informations["enabled"]:
233                 unsatisfied |= set([module])
234                 if dependency not in unsatisfied:
235                     unsatisfied |= self.unselectDependencyCheck(module)
236         return unsatisfied
237
238 class QControlGroup(QObject):
239     def __init__(self):
240         QObject.__init__(self)
241         self._controls = {}
242     
243     def addControl(self, id, control):
244         self._controls[id] = control
245         if type(control) == QCheckBox:
246             self.connect(control, SIGNAL("stateChanged(int)"), lambda: self._stateChanged(id))
247         elif type(control) == QSpinBox:
248             self.connect(control, SIGNAL("valueChanged(int)"), lambda: self._stateChanged(id))
249         elif type(control) == QComboBox:
250             self.connect(control, SIGNAL("currentIndexChanged(int)"), lambda: self._stateChanged(id))
251     
252     def clear(self):
253         self._controls = {}
254     
255     def _stateChanged(self, id):
256         self.emit(SIGNAL("stateChanged"), id)