Refactor to use new protocol module and sipo.
[bertos.git] / wizard / BCpuPage.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # This file is part of BeRTOS.
5 #
6 # Bertos is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 #
20 # As a special exception, you may use this file as part of a free software
21 # library without restriction.  Specifically, if other files instantiate
22 # templates or use macros or inline functions from this file, or you compile
23 # this file and link it with other files to produce an executable, this
24 # file does not by itself cause the resulting executable to be covered by
25 # the GNU General Public License.  This exception does not however
26 # invalidate any other reasons why the executable file might be covered by
27 # the GNU General Public License.
28 #
29 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30 #
31 #
32 # Author: Lorenzo Berni <duplo@develer.com>
33 #
34
35
36 from BWizardPage import *
37 import bertos_utils
38 import qvariant_converter
39
40 from const import *
41
42 class BCpuPage(BWizardPage):
43     """
44     Page of the wizard that permits to choose the cpu from the supported ones.
45     """
46     
47     def __init__(self):
48         BWizardPage.__init__(self, UI_LOCATION + "/cpu_select.ui")
49         self.setTitle(self.tr("Select the CPU on your board"))
50         self.freq_modified = False
51     
52     ## Overloaded QWizardPage methods ##
53
54     def isComplete(self):
55         """
56         Overload of the QWizardPage isComplete method.
57         """
58         if self.pageContent.cpuList.currentRow() != -1:
59             self.pageContent.frequencyLabel.setVisible(True)
60             self.pageContent.frequencySpinBox.setVisible(True)
61             infos = qvariant_converter.getDict(self.pageContent.cpuList.currentItem().data(Qt.UserRole))
62             for key, value in infos.items():
63                 if key in CPU_DEF:
64                     if type(CPU_DEF[key]) == list:
65                         infos[key] = qvariant_converter.getStringList(value)
66                     if type(CPU_DEF[key]) == str or type(CPU_DEF) == unicode:
67                         infos[key] = qvariant_converter.getString(value)
68                 elif key.startswith(MK_PARAM_ID):
69                     infos[key] = qvariant_converter.getString(value)
70                 else:
71                     del infos[key]
72             self.setProjectInfo("CPU_INFOS", infos)
73             self.setProjectInfo("CPU_NAME", unicode(self.pageContent.cpuList.currentItem().text()))
74             self.setProjectInfo("SELECTED_FREQ", unicode(long(self.pageContent.frequencySpinBox.value())))
75             tag_dict = self.projectInfo("ALL_CPU_TAGS")
76             for tag in tag_dict:
77                 if tag in infos["CPU_TAGS"] + [infos["CPU_NAME"], infos["TOOLCHAIN"]]:
78                     tag_dict[tag] = True
79                 else:
80                     tag_dict[tag] = False
81             self.setProjectInfo("ALL_CPU_TAGS", tag_dict)
82             return True
83         else:
84             return False
85     
86     ####
87     
88     ## Overloaded BWizardPage methods ##
89
90     def setupUi(self):
91         """
92         Overload of the BWizardPage setupUi method.
93         """
94         self.pageContent.cpuList.setSortingEnabled(True)
95         self.pageContent.frequencyLabel.setVisible(False)
96         self.pageContent.frequencySpinBox.setVisible(False)
97         preset_advanced = self.projectInfo("PRESET_ADVANCED_CONFIG")
98         if preset_advanced:
99             self.pageContent.cpuList.setEnabled(False)
100
101     def connectSignals(self):
102         """
103         Overload of the BWizardPage connectSignals method.
104         """
105         self.connect(self.pageContent.cpuList, SIGNAL("currentItemChanged(QListWidgetItem *, QListWidgetItem*)"), self.rowChanged)
106         self.connect(self.pageContent.frequencySpinBox, SIGNAL("editingFinished()"), self.freqChanged)
107
108     def reloadData(self, previous_id=None):
109         """
110         Overload of the BWizardPage reloadData method.
111         """
112         try:
113             QApplication.instance().setOverrideCursor(Qt.WaitCursor)
114             self.project.loadSourceTree()
115             self.populateCpuList()
116             cpu_name = self.projectInfo("CPU_NAME")
117             selected_freq = self.projectInfo("SELECTED_FREQ")
118             self.setupUi()
119             if cpu_name:
120                 self.selectItem(cpu_name)
121                 if selected_freq:
122                     self.setFrequency(selected_freq)
123                     self.freq_modified = True
124         finally:
125             QApplication.instance().restoreOverrideCursor()
126         self.emit(SIGNAL("completeChanged()"))
127
128     ####
129     
130     ## Slots ##
131
132     def rowChanged(self):
133         """
134         Slot called when the user select an entry from the cpu list.
135         """
136         if self.pageContent.cpuList.currentItem():
137             description = qvariant_converter.getDict(self.pageContent.cpuList.currentItem().data(Qt.UserRole))["CPU_DESC"]
138             description = qvariant_converter.getStringList(description)
139             if not self.freq_modified:
140                 # Retrieve the default cpu frequency when the value isn't already modified
141                 current_freq = qvariant_converter.getDict(self.pageContent.cpuList.currentItem().data(Qt.UserRole))["CPU_DEFAULT_FREQ"]
142                 current_freq = qvariant_converter.getString(current_freq)
143                 current_freq = long(current_freq.replace("U", "").replace("L", ""))
144                 self.pageContent.frequencySpinBox.setValue(long(current_freq))
145             self.pageContent.descriptionLabel.setText("<br>".join(description))
146             self.pageContent.descriptionLabel.setVisible(True)
147             self.emit(SIGNAL("completeChanged()"))
148     
149     def freqChanged(self):
150         """
151         Slot called when the user change the frequency value.
152         """
153         self.freq_modified = True
154         self.emit(SIGNAL("completeChanged()"))
155
156     ####
157     
158     def populateCpuList(self):
159         """
160         Fills the cpu list.
161         """
162         self.pageContent.cpuList.clear()
163         self.pageContent.cpuList.setCurrentItem(None)
164         infos = self.project.getCpuInfos()
165         tag_list = bertos_utils.getTagSet(infos)
166         # Create, fill and store the dict with the tags
167         tag_dict = {}
168         for element in tag_list:
169             tag_dict[element] = False
170         self.setProjectInfo("ALL_CPU_TAGS", tag_dict)
171         for cpu in infos:
172             item = QListWidgetItem(cpu["CPU_NAME"])
173             item.setData(Qt.UserRole, qvariant_converter.convertDict(cpu))
174             self.pageContent.cpuList.addItem(item)
175     
176     def selectItem(self, cpu):
177         """
178         Selects the given cpu from the list.
179         """
180         elements = self.pageContent.cpuList.findItems(cpu, Qt.MatchCaseSensitive)
181         if len(elements) == 1:
182             self.pageContent.cpuList.setCurrentItem(elements[0])
183     
184     def setFrequency(self, frequency):
185         self.pageContent.frequencySpinBox.setValue(long(frequency))