Add comment for each method and class
[bertos.git] / wizard / BToolchainPage.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 import os
13 import collections
14
15 from BWizardPage import *
16 import BToolchainSearch
17 import bertos_utils
18 import qvariant_converter
19
20 from const import *
21
22 class BToolchainPage(BWizardPage):
23     """
24     Page of the wizard that permits to choose the toolchain to use for the
25     project.
26     """
27     
28     def __init__(self):
29         BWizardPage.__init__(self, UI_LOCATION + "/toolchain_select.ui")
30         self.setTitle(self.tr("Select toolchain"))
31         self._validation_process = None
32         self._setupUi()
33         #self._populateToolchainList()
34         self._connectSignals()
35     
36     def _setupUi(self):
37         """
38         Sets up the user interface.
39         """
40         self.pageContent.infoLabel.setVisible(False)
41     
42     def _populateToolchainList(self):
43         """
44         Fills the toolchain list with the toolchains stored in the QSettings.
45         """
46         toolchains = self.toolchains()
47         sel_toolchain = self._projectInfoRetrieve("TOOLCHAIN")
48         for key, value in toolchains.items():
49             item = QListWidgetItem(key)
50             item.setData(Qt.UserRole, qvariant_converter.convertStringDict({"path": key}))
51             self.pageContent.toolchainList.addItem(item)
52             if sel_toolchain is not None and sel_toolchain["path"] == key:
53                 self.pageContent.toolchainList.setCurrentItem(item)
54             if value:
55                 self.validateToolchain(self.pageContent.toolchainList.row(item))
56
57     def _clearList(self):
58         """
59         Removes all the toolchain from the list.
60         """
61         self.pageContent.toolchainList.clear()
62     
63     def _selectionChanged(self):
64         """
65         Slot called when the user click on an entry of the toolchain list.
66         """
67         if self.pageContent.toolchainList.currentRow() != -1:
68             infos = collections.defaultdict(lambda: unicode("not defined"))
69             infos.update(qvariant_converter.getStringDict(self.pageContent.toolchainList.currentItem().data(Qt.UserRole)))
70             self.pageContent.infoLabel.setText("GCC " + infos["version"] + " (" + infos["build"] + ")\nTarget: " + infos["target"] + "\nPath: " + os.path.normpath(infos["path"]))
71             self.pageContent.infoLabel.setVisible(True)
72             self.emit(SIGNAL("completeChanged()"))
73     
74     def _search(self):
75         """
76         Searches for toolchains in the stored directories, and stores them in the
77         QSettings.
78         """
79         dir_list = self.searchDirList()
80         if self.pathSearch():
81             dir_list += [element for element in bertos_utils.getSystemPath()]
82         toolchain_list = bertos_utils.findToolchains(dir_list)
83         stored_toolchains = self.toolchains()
84         for element in toolchain_list:
85             if not element in stored_toolchains.keys():
86                 item = QListWidgetItem(element)
87                 item.setData(Qt.UserRole, qvariant_converter.convertStringDict({"path": element}))
88                 self.pageContent.toolchainList.addItem(item)
89                 stored_toolchains[element] = False
90         self.setToolchains(stored_toolchains)
91         
92     def _connectSignals(self):
93         """
94         Connects the signals with the related slots.
95         """
96         self.connect(self.pageContent.toolchainList, SIGNAL("itemSelectionChanged()"), self._selectionChanged)
97         self.connect(self.pageContent.addButton, SIGNAL("clicked()"), self.addToolchain)
98         self.connect(self.pageContent.removeButton, SIGNAL("clicked()"), self.removeToolchain)
99         self.connect(self.pageContent.searchButton, SIGNAL("clicked()"), self.searchToolchain)
100         self.connect(self.pageContent.checkButton, SIGNAL("clicked()"), self.validateAllToolchains)
101     
102     def _validItem(self, index, infos):
103         """
104         Sets the item at index as a valid item and associates the given info to it.
105         """
106         item = self.pageContent.toolchainList.item(index)
107         new_data = qvariant_converter.getStringDict(self.pageContent.toolchainList.item(index).data(Qt.UserRole))
108         new_data.update(infos)
109         item.setData(Qt.UserRole, qvariant_converter.convertStringDict(new_data))
110         needed = self._projectInfoRetrieve("CPU_INFOS")
111         if "target" in infos.keys() and infos["target"].find(needed["TOOLCHAIN"]) != -1:
112             item.setIcon(QIcon(":/images/ok.png"))
113         else:
114             item.setIcon(QIcon(":/images/warning.png"))
115         if "version" in infos.keys() and "target" in infos.keys():
116             item.setText("GCC " + infos["version"] + " - " + infos["target"])
117     
118     def _invalidItem(self, index):
119         """
120         Sets the item at index as an invalid item.
121         """
122         item = self.pageContent.toolchainList.item(index)
123         item.setIcon(QIcon(":/images/error.png"))
124     
125     def addToolchain(self):
126         """
127         Slot called when the user adds manually a toolchain.
128         """
129         sel_toolchain = QFileDialog.getOpenFileName(self, self.tr("Choose the toolchain"), "")
130         if not sel_toolchain.isEmpty():
131             item = QListWidgetItem(sel_toolchain)
132             item.setData(Qt.UserRole, qvariant_converter.convertString({"path": sel_toolchain}))
133             self.pageContent.toolchainList.addItem(item)
134             toolchains = self.toolchains()
135             toolchains[sel_toolchain] = False
136             self.setToolchains(toolchains)
137     
138     def removeToolchain(self):
139         """
140         Slot called when the user removes manually a toolchain.
141         """
142         if self.pageContent.toolchainList.currentRow() != -1:
143             item = self.pageContent.toolchainList.takeItem(self.pageContent.toolchainList.currentRow())
144             toolchain = qvariant_converter.getStringDict(item.data(Qt.UserRole))["path"]
145             toolchains = self.toolchains()
146             del toolchains[toolchain]
147             self.setToolchains(toolchains)
148     
149     def searchToolchain(self):
150         """
151         Slot called when the user clicks on the 'search' button. It opens the 
152         toolchain search dialog.
153         """
154         search = BToolchainSearch.BToolchainSearch()
155         self.connect(search, SIGNAL("accepted()"), self._search)
156         search.exec_()
157     
158     def validateAllToolchains(self):
159         """
160         Slot called when the user clicks on the validate button. It starts the
161         toolchain validation procedure for all the toolchains.
162         """
163         QApplication.instance().setOverrideCursor(Qt.WaitCursor)
164         for i in range(self.pageContent.toolchainList.count()):
165             self.validateToolchain(i)
166         QApplication.instance().restoreOverrideCursor()
167     
168     def validateToolchain(self, i):
169         """
170         Toolchain validation procedure.
171         """
172         filename = qvariant_converter.getStringDict(self.pageContent.toolchainList.item(i).data(Qt.UserRole))["path"]
173         valid = False
174         info = {}
175         ## Check for the other tools of the toolchain
176         for tool in TOOLCHAIN_ITEMS:
177             if os.path.exists(filename.replace("gcc", tool)):
178                 valid = True
179             else:
180                 valid = False
181                 break
182         ## Try to retrieve the informations about the toolchain only for the valid toolchains
183         if valid:
184             self._validation_process = QProcess()
185             self._validation_process.start(filename, ["-v"])
186             self._validation_process.waitForStarted(1000)
187             if self._validation_process.waitForFinished(200):
188                 description = str(self._validation_process.readAllStandardError())
189                 info = bertos_utils.getToolchainInfo(description)
190                 if len(info.keys()) >= 4:
191                     valid = True
192             else:
193                 self._validation_process.kill()
194         ## Add the item in the list with the appropriate associate data.
195         if valid:
196             self._validItem(i, info)
197         else:
198             self._invalidItem(i)
199         toolchains = self.toolchains()
200         toolchains[filename] = True
201         self.setToolchains(toolchains)
202     
203     def isComplete(self):
204         """
205         Overload of the QWizard isComplete method.
206         """
207         if self.pageContent.toolchainList.currentRow() != -1:
208             self._projectInfoStore("TOOLCHAIN", 
209                 qvariant_converter.getStringDict(self.pageContent.toolchainList.currentItem().data(Qt.UserRole)))
210             return True
211         else:
212             return False
213     
214     def reloadData(self):
215         """
216         Overload of the BWizard reloadData method.
217         """
218         self._clearList()
219         self._setupUi()
220         self._populateToolchainList()