164f132432121d4d997a7b104c192f80eb319dd9
[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
14 from BWizardPage import *
15 import BToolchainSearch
16 import bertos_utils
17 if PYQT_VERSION_STR > "4.4.3":
18     import qvariant_converter_new as qvariant_converter
19 else:
20     import qvariant_converter_old as qvariant_converter
21
22 class BToolchainPage(BWizardPage):
23     
24     def __init__(self):
25         BWizardPage.__init__(self, "toolchain_select.ui")
26         self.setTitle(self.tr("Select toolchain"))
27         self._validationProcess = None
28         #self._populateToolchainList()
29         self._connectSignals()
30     
31     def _updateUi(self):
32         if self.pageContent.customDirBox.isChecked():
33             self._enableCustomDir()
34         else:
35             self._disableCustomDir()
36         if self.pageContent.pathBox.isChecked() or (self.pageContent.customDirBox.isChecked() and self.pageContent.customDirList.count() != 0):
37             self.pageContent.doSearchButton.setEnabled(True)
38         else:
39             self.pageContent.doSearchButton.setEnabled(False)
40     
41     def _populateToolchainList(self):
42         toolchains = self.toolchains()
43         for key, value in toolchains.items():
44             item = QListWidgetItem(key)
45             item.setData(Qt.UserRole, qvariant_converter.convertString(key))
46             self.pageContent.toolchainList.addItem(item)
47             if value:
48                 self.validateToolchain(self.pageContent.toolchainList.row(item))
49
50     def _clearList(self):
51         self.pageContent.toolchainList.clear()
52     
53     def _selectionChanged(self):
54         self.emit(SIGNAL("completeChanged()"))
55     
56     def _search(self):
57         dirList = self.searchDirList()
58         if(self.pathSearch()):
59             dirList += [element for element in bertos_utils.getSystemPath()]
60         toolchainList = bertos_utils.findToolchains(dirList)
61         storedToolchains = self.toolchains()
62         for element in toolchainList:
63             if not element in storedToolchains.keys():
64                 item = QListWidgetItem(element)
65                 item.setData(Qt.UserRole, qvariant_converter.convertString(element))
66                 self.pageContent.toolchainList.addItem(item)
67                 storedToolchains[element] = False
68         self.setToolchains(storedToolchains)
69         
70     def _connectSignals(self):
71         self.connect(self.pageContent.toolchainList, SIGNAL("itemSelectionChanged()"), self._selectionChanged)
72         self.connect(self.pageContent.addButton, SIGNAL("clicked()"), self.addToolchain)
73         self.connect(self.pageContent.removeButton, SIGNAL("clicked()"), self.removeToolchain)
74         self.connect(self.pageContent.searchButton, SIGNAL("clicked()"), self.searchToolchain)
75         self.connect(self.pageContent.checkButton, SIGNAL("clicked()"), self.validateAllToolchains)
76     
77     def _validItem(self, index, infos):
78         item = self.pageContent.toolchainList.item(index)
79         needed = self._projectInfoRetrieve("CPU_INFOS")
80         if infos["target"].find(needed["TOOLCHAIN"]) != -1:
81             item.setIcon(QIcon(":/images/ok.png"))
82         else:
83             item.setIcon(QIcon(":/images/warning.png"))
84         item.setText("GCC " + infos["version"] + " - " + infos["target"])
85     
86     def _invalidItem(self, index):
87         item = self.pageContent.toolchainList.item(index)
88         item.setIcon(QIcon(":/images/error.png"))
89     
90     def addToolchain(self):
91         sel_toolchain = QFileDialog.getOpenFileName(self, self.tr("Choose the toolchain"), "")
92         if not sel_toolchain.isEmpty():
93             item = QListWidgetItem(sel_toolchain)
94             item.setData(Qt.UserRole, qvariant_converter.convertString(sel_toolchain))
95             self.pageContent.toolchainList.addItem(item)
96             toolchains = self.toolchains()
97             toolchains[sel_toolchain] = False
98             self.setToolchains(toolchains)
99     
100     def removeToolchain(self):
101         if self.pageContent.toolchainList.currentRow() != -1:
102             item = self.pageContent.toolchainList.takeItem(self.pageContent.toolchainList.currentRow())
103             toolchain = qvariant_converter.getString(item.data(Qt.UserRole))
104             toolchains = self.toolchains()
105             del toolchains[toolchain]
106             self.setToolchains(toolchains)
107     
108     def searchToolchain(self):
109         search = BToolchainSearch.BToolchainSearch()
110         self.connect(search, SIGNAL("accepted()"), self._search)
111         search.exec_()
112     
113     def validateAllToolchains(self):
114         for i in range(self.pageContent.toolchainList.count()):
115             self.validateToolchain(i)
116     
117     def validateToolchain(self, i):
118         filename = qvariant_converter.getString(self.pageContent.toolchainList.item(i).data(Qt.UserRole))
119         self._validationProcess = QProcess()
120         self._validationProcess.start(filename, ["-v"])
121         self._validationProcess.waitForStarted(1000)
122         if self._validationProcess.waitForFinished(200):
123             description = str(self._validationProcess.readAllStandardError())
124             infos = bertos_utils.getToolchainInfo(description)
125             if len(infos.keys()) >= 4:
126                 self._validItem(i, infos)
127             else:
128                 self._invalidItem(i)
129         else:
130             self._validationProcess.kill()
131             self._invalidItem(i)
132         toolchains = self.toolchains()
133         toolchains[filename] = True
134         self.setToolchains(toolchains)
135     
136     def isComplete(self):
137         if self.pageContent.toolchainList.currentRow() != -1:
138             self._projectInfoStore("TOOLCHAIN", 
139                 qvariant_converter.getString(self.pageContent.toolchainList.item(self.pageContent.toolchainList.currentRow()).data(Qt.UserRole)))
140             return True
141         else:
142             return False
143     
144     def reloadData(self):
145         self._clearList()
146         self._populateToolchainList()