Add a stub of the configuration storing procedure
[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._connectSignals()
26     
27     def reloadData(self):
28         self._setupButtonGroup()
29         self._loadModuleData()
30         self._fillModuleTable()
31     
32     def _setupButtonGroup(self):
33         self._buttonGroup = QButtonGroup()
34         self._buttonGroup.setExclusive(False)
35         self.connect(self._buttonGroup, SIGNAL("buttonClicked(int)"), self._moduleSelectionChanged)
36     
37     def _loadModuleData(self):
38         modules = bertos_utils.loadModuleInfosDict(self._projectInfoRetrieve("SOURCES_PATH"))
39         lists = bertos_utils.loadDefineListsDict(self._projectInfoRetrieve("SOURCES_PATH"))
40         configurations = {}
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)
47     
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"])
57     
58     def _fillPropertyTable(self):
59         self.savePage()
60         module = self._currentModule()
61         configuration = self._projectInfoRetrieve("MODULES")[module]["configuration"]
62         configurations = self._projectInfoRetrieve("CONFIGURATIONS")[configuration]
63         self.pageContent.propertyTable.clear()
64         self.pageContent.propertyTable.setRowCount(len(configurations))
65         for index, property in enumerate(configurations):
66             item = QTableWidgetItem(property)
67             item.setData(Qt.UserRole, qvariant_converter.convertString(property))
68             self.pageContent.propertyTable.setItem(index, 0, item)
69             if "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "boolean":
70                 ## boolean property
71                 checkBox = QCheckBox()
72                 self.pageContent.propertyTable.setCellWidget(index, 1, checkBox)
73                 if configurations[property]["value"] == "1":
74                     checkBox.setChecked(True)
75                 else:
76                     checkBox.setChecked(False)
77             elif "type" in configurations[property]["informations"].keys() and configurations[property]["informations"]["type"] == "enum":
78                 ## enum property
79                 comboBox = QComboBox()
80                 self.pageContent.propertyTable.setCellWidget(index, 1, comboBox)
81                 enum = self._projectInfoRetrieve("LISTS")[configurations[property]["informations"]["value_list"]]
82                 for element in enum:
83                     comboBox.addItem(element)
84             else:
85                 ## int, long or undefined type property
86                 spinBox = QSpinBox()
87                 self.pageContent.propertyTable.setCellWidget(index, 1, spinBox)
88                 if "min" in configurations[property]["informations"].keys():
89                     minimum = int(configurations[property]["informations"]["min"])
90                 else:
91                     minimum = -32768
92                 spinBox.setMinimum(minimum)
93                 if "max" in configurations[property]["informations"].keys():
94                     maximum = int(configurations[property]["informations"]["max"])
95                 else:
96                     maximum = 32767
97                 spinBox.setMaximum(maximum)
98                 if "long" in configurations[property]["informations"].keys() and configurations[property]["informations"]["long"] == "True":
99                     spinBox.setSuffix("L")
100                 spinBox.setValue(int(configurations[property]["value"].replace("L", "")))
101     
102     def _currentModule(self):
103         return unicode(self.pageContent.moduleTable.item(self.pageContent.moduleTable.currentRow(), 1).text())
104     
105     def _currentModuleConfigurations(self):
106         return self._projectInfoRetrieve("MODULES")[self._currentModule()]["configuration"]
107     
108     def _currentProperty(self):
109         return qvariant_converter.getString(self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0).data(Qt.UserRole))
110     
111     def _currentPropertyItem(self):
112         return self.pageContent.propertyTable.item(self.pageContent.propertyTable.currentRow(), 0)
113     
114     def _resetPropertyDescription(self):
115         for index in range(self.pageContent.propertyTable.rowCount()):
116             propertyName = qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))
117             self.pageContent.propertyTable.item(index, 0).setText(propertyName)
118     
119     def _showPropertyDescription(self):
120         self._resetPropertyDescription()
121         description = self._projectInfoRetrieve("CONFIGURATIONS")[self._currentModuleConfigurations()][self._currentProperty()]["description"]
122         name = self._currentProperty()
123         self._currentPropertyItem().setText(name + "\n" + description)
124     
125     def _setupUi(self):
126         self.pageContent.moduleTable.horizontalHeader().setResizeMode(QHeaderView.ResizeToContents)
127         self.pageContent.moduleTable.horizontalHeader().setStretchLastSection(True)
128         self.pageContent.moduleTable.horizontalHeader().setVisible(False)
129         self.pageContent.moduleTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
130         self.pageContent.moduleTable.verticalHeader().setVisible(False)
131         self.pageContent.moduleTable.setColumnCount(2)
132         self.pageContent.moduleTable.setRowCount(0)
133         self.pageContent.propertyTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)
134         self.pageContent.propertyTable.horizontalHeader().setVisible(False)
135         self.pageContent.propertyTable.verticalHeader().setResizeMode(QHeaderView.ResizeToContents)
136         self.pageContent.propertyTable.verticalHeader().setVisible(False)
137         self.pageContent.propertyTable.setColumnCount(2)
138         self.pageContent.propertyTable.setRowCount(0)
139     
140     def _connectSignals(self):
141         self.connect(self.pageContent.moduleTable, SIGNAL("itemSelectionChanged()"), self._fillPropertyTable)
142         self.connect(self.pageContent.propertyTable, SIGNAL("itemSelectionChanged()"), self._showPropertyDescription)
143
144     def _moduleSelectionChanged(self, index):
145         module = unicode(self.pageContent.moduleTable.item(index, 1).text())
146         if self._buttonGroup.button(index).isChecked():
147             self._moduleSelected(module)
148         else:
149             self._moduleUnselected(module)
150     
151     def _moduleSelected(self, selectedModule):
152         modules = self._projectInfoRetrieve("MODULES")
153         modules[selectedModule]["enabled"] = True
154         self._projectInfoStore("MODULES", modules)
155         depends = self._projectInfoRetrieve("MODULES")[selectedModule]["depends"]
156         unsatisfied = self.selectDependencyCheck(selectedModule)
157         if len(unsatisfied) > 0:
158             message = self.tr("The module %1 needs the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
159             message = message.arg(selectedModule).arg(", ".join(unsatisfied))
160             choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
161             if choice == QMessageBox.Yes:
162                 for module in unsatisfied:
163                     modules = self._projectInfoRetrieve("MODULES")
164                     modules[module]["enabled"] = True
165                 for index in range(self.pageContent.moduleTable.rowCount()):
166                     if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
167                         self._buttonGroup.button(index).setChecked(True)
168     
169     def _moduleUnselected(self, unselectedModule):
170         modules = self._projectInfoRetrieve("MODULES")
171         modules[unselectedModule]["enabled"] = False
172         self._projectInfoStore("MODULES", modules)
173         unsatisfied = self.unselectDependencyCheck(unselectedModule)
174         if len(unsatisfied) > 0:
175             message = self.tr("The module %1 is needed by the following modules:\n%2.\n\nDo you want to resolve automatically the problem?")
176             message = message.arg(unselectedModule).arg(", ".join(unsatisfied))
177             choice = QMessageBox.warning(self, self.tr("Dependency error"), message, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
178             if choice == QMessageBox.Yes:
179                 for module in unsatisfied:
180                     modules = self._projectInfoRetrieve("MODULES")
181                     modules[module]["enabled"] = False
182                 for index in range(self.pageContent.moduleTable.rowCount()):
183                     if unicode(self.pageContent.moduleTable.item(index, 1).text()) in unsatisfied:
184                         self._buttonGroup.button(index).setChecked(False)
185     
186     
187     def selectDependencyCheck(self, module):
188         unsatisfied = set()
189         modules = self._projectInfoRetrieve("MODULES")
190         for dependency in modules[module]["depends"]:
191             if not modules[dependency]["enabled"]:
192                 unsatisfied |= set([dependency])
193                 if dependency not in unsatisfied:
194                     unsatisfied |= self.selectDependencyCheck(dependency)
195         return unsatisfied
196     
197     def unselectDependencyCheck(self, dependency):
198         unsatisfied = set()
199         modules = self._projectInfoRetrieve("MODULES")
200         for module, informations in modules.items():
201             if dependency in informations["depends"] and informations["enabled"]:
202                 unsatisfied |= set([module])
203                 if dependency not in unsatisfied:
204                     unsatisfied |= self.unselectDependencyCheck(module)
205         return unsatisfied
206     
207     def savePage(self):
208         for index in range(self.pageContent.propertyTable.rowCount()):
209             print qvariant_converter.getString(self.pageContent.propertyTable.item(index, 0).data(Qt.UserRole))