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