Correct if caluses
[bertos.git] / wizard / BCpuPage.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # Copyright 2008 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
13 from BWizardPage import *
14 import bertos_utils
15 import qvariant_converter
16
17 from const import *
18
19 class BCpuPage(BWizardPage):
20     """
21     Page of the wizard that permits to choose the cpu from the supported ones.
22     """
23     
24     def __init__(self):
25         BWizardPage.__init__(self, UI_LOCATION + "/cpu_select.ui")
26         self.setTitle(self.tr("Select the CPU"))
27         self.freq_modified = False
28     
29     ## Overloaded QWizardPage methods ##
30
31     def isComplete(self):
32         """
33         Overload of the QWizardPage isComplete method.
34         """
35         if self.pageContent.cpuList.currentRow() != -1:
36             self.pageContent.frequencyLabel.setVisible(True)
37             self.pageContent.frequencySpinBox.setVisible(True)
38             infos = qvariant_converter.getDict(self.pageContent.cpuList.currentItem().data(Qt.UserRole))
39             for key, value in infos.items():
40                 if type(CPU_DEF[key]) == list:
41                     infos[key] = qvariant_converter.getStringList(value)
42                 if type(CPU_DEF[key]) == str or type(CPU_DEF) == unicode:
43                     infos[key] = qvariant_converter.getString(value)
44             self.setProjectInfo("CPU_INFOS", infos)
45             self.setProjectInfo("CPU_NAME", unicode(self.pageContent.cpuList.currentItem().text()))
46             self.setProjectInfo("SELECTED_FREQ", unicode(long(self.pageContent.frequencySpinBox.value())))
47             tag_dict = self.projectInfo("ALL_CPU_TAGS")
48             for tag in tag_dict:
49                 if tag in infos["CPU_TAGS"] + [infos["CPU_NAME"], infos["CORE_CPU"], infos["TOOLCHAIN"]]:
50                     tag_dict[tag] = True
51                 else:
52                     tag_dict[tag] = False
53             self.setProjectInfo("ALL_CPU_TAGS", tag_dict)
54             return True
55         else:
56             return False
57     
58     ####
59     
60     ## Overloaded BWizardPage methods ##
61
62     def setupUi(self):
63         """
64         Overload of the BWizardPage setupUi method.
65         """
66         self.pageContent.cpuList.setSortingEnabled(True)
67         self.pageContent.descriptionLabel.setVisible(False)
68         self.pageContent.descriptionLabel.setText("")
69         self.pageContent.frequencyLabel.setVisible(False)
70         self.pageContent.frequencySpinBox.setVisible(False)
71
72     def connectSignals(self):
73         """
74         Overload of the BWizardPage connectSignals method.
75         """
76         self.connect(self.pageContent.cpuList, SIGNAL("itemSelectionChanged()"), self.rowChanged)
77         self.connect(self.pageContent.frequencySpinBox, SIGNAL("editingFinished()"), self.freqChanged)
78
79     def reloadData(self):
80         """
81         Overload of the BWizardPage reloadData method.
82         """
83         QApplication.instance().setOverrideCursor(Qt.WaitCursor)
84         bertos_utils.loadSourceTree(self.project())
85         self.populateCpuList()
86         cpu_name = self.projectInfo("CPU_NAME")
87         selected_freq = self.projectInfo("SELECTED_FREQ")
88         self.setupUi()
89         if cpu_name:
90             self.selectItem(cpu_name)
91             if selected_freq:
92                 self.setFrequency(selected_freq)
93                 self.freq_modified = True
94         QApplication.instance().restoreOverrideCursor()
95         self.emit(SIGNAL("completeChanged()"))
96
97     ####
98     
99     ## Slots ##
100
101     def rowChanged(self):
102         """
103         Slot called when the user select an entry from the cpu list.
104         """
105         description = qvariant_converter.getDict(self.pageContent.cpuList.currentItem().data(Qt.UserRole))["CPU_DESC"]
106         description = qvariant_converter.getStringList(description)
107         if not self.freq_modified:
108             # Retrieve the default cpu frequency when the value isn't already modified
109             current_freq = qvariant_converter.getDict(self.pageContent.cpuList.currentItem().data(Qt.UserRole))["CPU_DEFAULT_FREQ"]
110             current_freq = qvariant_converter.getString(current_freq)
111             current_freq = long(current_freq.replace("U", "").replace("L", ""))
112             self.pageContent.frequencySpinBox.setValue(long(current_freq))
113         self.pageContent.descriptionLabel.setText("<br>".join(description))
114         self.pageContent.descriptionLabel.setVisible(True)
115         self.emit(SIGNAL("completeChanged()"))
116     
117     def freqChanged(self):
118         """
119         Slot called when the user change the frequency value.
120         """
121         self.freq_modified = True
122         self.emit(SIGNAL("completeChanged()"))
123
124     ####
125     
126     def populateCpuList(self):
127         """
128         Fills the cpu list.
129         """
130         self.pageContent.cpuList.clear()
131         self.pageContent.cpuList.setCurrentItem(None)
132         infos = bertos_utils.loadCpuInfos(self.project())
133         tag_list = bertos_utils.getTagSet(infos)
134         # Create, fill and store the dict with the tags
135         tag_dict = {}
136         for element in tag_list:
137             tag_dict[element] = False
138         self.setProjectInfo("ALL_CPU_TAGS", tag_dict)
139         for cpu in infos:
140             item = QListWidgetItem(cpu["CPU_NAME"])
141             item.setData(Qt.UserRole, qvariant_converter.convertDict(cpu))
142             self.pageContent.cpuList.addItem(item)
143     
144     def selectItem(self, cpu):
145         """
146         Selects the given cpu from the list.
147         """
148         elements = self.pageContent.cpuList.findItems(cpu, Qt.MatchCaseSensitive)
149         if len(elements) == 1:
150             self.pageContent.cpuList.setCurrentItem(elements[0])
151     
152     def setFrequency(self, frequency):
153         self.pageContent.frequencySpinBox.setValue(long(frequency))