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