Refactor makefile variables architecture.
[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 # $Id$
32 #
33 # Author: Lorenzo Berni <duplo@develer.com>
34 #
35
36
37 from BWizardPage import *
38 import bertos_utils
39 import qvariant_converter
40
41 from const import *
42
43 class BCpuPage(BWizardPage):
44     """
45     Page of the wizard that permits to choose the cpu from the supported ones.
46     """
47     
48     def __init__(self):
49         BWizardPage.__init__(self, UI_LOCATION + "/cpu_select.ui")
50         self.setTitle(self.tr("Select the CPU"))
51         self.freq_modified = False
52     
53     ## Overloaded QWizardPage methods ##
54
55     def isComplete(self):
56         """
57         Overload of the QWizardPage isComplete method.
58         """
59         if self.pageContent.cpuList.currentRow() != -1:
60             self.pageContent.frequencyLabel.setVisible(True)
61             self.pageContent.frequencySpinBox.setVisible(True)
62             infos = qvariant_converter.getDict(self.pageContent.cpuList.currentItem().data(Qt.UserRole))
63             for key, value in infos.items():
64                 if key in CPU_DEF:
65                     if type(CPU_DEF[key]) == list:
66                         infos[key] = qvariant_converter.getStringList(value)
67                     if type(CPU_DEF[key]) == str or type(CPU_DEF) == unicode:
68                         infos[key] = qvariant_converter.getString(value)
69                 else:
70                     del infos[key]
71             self.setProjectInfo("CPU_INFOS", infos)
72             self.setProjectInfo("CPU_NAME", unicode(self.pageContent.cpuList.currentItem().text()))
73             self.setProjectInfo("SELECTED_FREQ", unicode(long(self.pageContent.frequencySpinBox.value())))
74             tag_dict = self.projectInfo("ALL_CPU_TAGS")
75             for tag in tag_dict:
76                 if tag in infos["CPU_TAGS"] + [infos["CPU_NAME"], infos["TOOLCHAIN"]]:
77                     tag_dict[tag] = True
78                 else:
79                     tag_dict[tag] = False
80             self.setProjectInfo("ALL_CPU_TAGS", tag_dict)
81             return True
82         else:
83             return False
84     
85     ####
86     
87     ## Overloaded BWizardPage methods ##
88
89     def setupUi(self):
90         """
91         Overload of the BWizardPage setupUi method.
92         """
93         self.pageContent.cpuList.setSortingEnabled(True)
94         self.pageContent.descriptionLabel.setVisible(False)
95         self.pageContent.descriptionLabel.setText("")
96         self.pageContent.frequencyLabel.setVisible(False)
97         self.pageContent.frequencySpinBox.setVisible(False)
98
99     def connectSignals(self):
100         """
101         Overload of the BWizardPage connectSignals method.
102         """
103         self.connect(self.pageContent.cpuList, SIGNAL("itemSelectionChanged()"), self.rowChanged)
104         self.connect(self.pageContent.frequencySpinBox, SIGNAL("editingFinished()"), self.freqChanged)
105
106     def reloadData(self):
107         """
108         Overload of the BWizardPage reloadData method.
109         """
110         QApplication.instance().setOverrideCursor(Qt.WaitCursor)
111         bertos_utils.loadSourceTree(self.project())
112         self.populateCpuList()
113         cpu_name = self.projectInfo("CPU_NAME")
114         selected_freq = self.projectInfo("SELECTED_FREQ")
115         self.setupUi()
116         if cpu_name:
117             self.selectItem(cpu_name)
118             if selected_freq:
119                 self.setFrequency(selected_freq)
120                 self.freq_modified = True
121         QApplication.instance().restoreOverrideCursor()
122         self.emit(SIGNAL("completeChanged()"))
123
124     ####
125     
126     ## Slots ##
127
128     def rowChanged(self):
129         """
130         Slot called when the user select an entry from the cpu list.
131         """
132         description = qvariant_converter.getDict(self.pageContent.cpuList.currentItem().data(Qt.UserRole))["CPU_DESC"]
133         description = qvariant_converter.getStringList(description)
134         if not self.freq_modified:
135             # Retrieve the default cpu frequency when the value isn't already modified
136             current_freq = qvariant_converter.getDict(self.pageContent.cpuList.currentItem().data(Qt.UserRole))["CPU_DEFAULT_FREQ"]
137             current_freq = qvariant_converter.getString(current_freq)
138             current_freq = long(current_freq.replace("U", "").replace("L", ""))
139             self.pageContent.frequencySpinBox.setValue(long(current_freq))
140         self.pageContent.descriptionLabel.setText("<br>".join(description))
141         self.pageContent.descriptionLabel.setVisible(True)
142         self.emit(SIGNAL("completeChanged()"))
143     
144     def freqChanged(self):
145         """
146         Slot called when the user change the frequency value.
147         """
148         self.freq_modified = True
149         self.emit(SIGNAL("completeChanged()"))
150
151     ####
152     
153     def populateCpuList(self):
154         """
155         Fills the cpu list.
156         """
157         self.pageContent.cpuList.clear()
158         self.pageContent.cpuList.setCurrentItem(None)
159         infos = bertos_utils.loadCpuInfos(self.project())
160         tag_list = bertos_utils.getTagSet(infos)
161         # Create, fill and store the dict with the tags
162         tag_dict = {}
163         for element in tag_list:
164             tag_dict[element] = False
165         self.setProjectInfo("ALL_CPU_TAGS", tag_dict)
166         for cpu in infos:
167             item = QListWidgetItem(cpu["CPU_NAME"])
168             item.setData(Qt.UserRole, qvariant_converter.convertDict(cpu))
169             self.pageContent.cpuList.addItem(item)
170     
171     def selectItem(self, cpu):
172         """
173         Selects the given cpu from the list.
174         """
175         elements = self.pageContent.cpuList.findItems(cpu, Qt.MatchCaseSensitive)
176         if len(elements) == 1:
177             self.pageContent.cpuList.setCurrentItem(elements[0])
178     
179     def setFrequency(self, frequency):
180         self.pageContent.frequencySpinBox.setValue(long(frequency))