Store the frequency
[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.default_freqs = {}
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             return True
48         else:
49             return False
50     
51     ####
52     
53     ## Overloaded BWizardPage methods ##
54
55     def setupUi(self):
56         """
57         Overload of the BWizardPage setupUi method.
58         """
59         self.pageContent.cpuList.setSortingEnabled(True)
60         self.pageContent.descriptionLabel.setVisible(False)
61         self.pageContent.descriptionLabel.setText("")
62         self.pageContent.frequencyLabel.setVisible(False)
63         self.pageContent.frequencySpinBox.setVisible(False)
64
65     def connectSignals(self):
66         """
67         Overload of the BWizardPage connectSignals method.
68         """
69         self.connect(self.pageContent.cpuList, SIGNAL("itemSelectionChanged()"), self.rowChanged)
70         self.connect(self.pageContent.frequencySpinBox, SIGNAL("valueChanged(double)"), self.freqChanged)
71
72     def reloadData(self):
73         """
74         Overload of the BWizardPage reloadData method.
75         """
76         QApplication.instance().setOverrideCursor(Qt.WaitCursor)
77         bertos_utils.loadSourceTree(self.project())
78         self.populateCpuList()
79         cpu_name = self.projectInfo("CPU_NAME")
80         self.setupUi()
81         if not cpu_name is None:
82             self.selectItem(cpu_name)
83         if self.default_freqs == {}:
84             for index in range(self.pageContent.cpuList.count()):
85                 default_freq = qvariant_converter.getStringDict(self.pageContent.cpuList.item(index).data(Qt.UserRole))["CPU_DEFAULT_FREQ"]
86                 default_freq = long(default_freq.replace("U", "").replace("L", ""))
87                 self.default_freqs[default_freq] = default_freq
88         QApplication.instance().restoreOverrideCursor()
89         self.emit(SIGNAL("completeChanged()"))
90
91     ####
92     
93     ## Slots ##
94
95     def rowChanged(self):
96         """
97         Slot called when the user select an entry from the cpu list.
98         """
99         description = qvariant_converter.getDict(self.pageContent.cpuList.currentItem().data(Qt.UserRole))["CPU_DESC"]
100         description = qvariant_converter.getStringList(description)
101         # Retrieve the default cpu frequency
102         default_freq = qvariant_converter.getDict(self.pageContent.cpuList.currentItem().data(Qt.UserRole))["CPU_DEFAULT_FREQ"]
103         default_freq = qvariant_converter.getString(default_freq)
104         default_freq = long(default_freq.replace("U", "").replace("L", ""))
105         current_freq = self.default_freqs[default_freq]
106         self.pageContent.descriptionLabel.setText("<br>".join(description))
107         self.pageContent.descriptionLabel.setVisible(True)
108         self.pageContent.frequencySpinBox.setValue(long(current_freq))
109         self.emit(SIGNAL("completeChanged()"))
110     
111     def freqChanged(self):
112         """
113         Slot called when the user change the frequency value.
114         """
115         default_freq = qvariant_converter.getDict(self.pageContent.cpuList.currentItem().data(Qt.UserRole))["CPU_DEFAULT_FREQ"]
116         default_freq = qvariant_converter.getString(default_freq)
117         default_freq = long(default_freq.replace("U", "").replace("L", ""))
118         self.default_freqs[default_freq] = long(self.pageContent.frequencySpinBox.value())
119         self.emit(SIGNAL("completeChanged()"))
120
121     ####
122     
123     def populateCpuList(self):
124         """
125         Fills the cpu list.
126         """
127         self.pageContent.cpuList.clear()
128         self.pageContent.cpuList.setCurrentItem(None)
129         infos = bertos_utils.loadCpuInfos(self.project())
130         for cpu in infos:
131             item = QListWidgetItem(cpu["CPU_NAME"])
132             item.setData(Qt.UserRole, qvariant_converter.convertDict(cpu))
133             self.pageContent.cpuList.addItem(item)
134     
135     def selectItem(self, cpu):
136         """
137         Selects the given cpu from the list.
138         """
139         elements = self.pageContent.cpuList.findItems(cpu, Qt.MatchCaseSensitive)
140         if len(elements) == 1:
141             self.pageContent.cpuList.setCurrentItem(elements[0])