Attempt to fix the "Always checked no codelite files checkbox" issue.
[bertos.git] / wizard / BToolchainPage.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 import os
37 import collections
38
39 from BWizardPage import *
40 from BCreationPage import BCreationPage
41
42 import BToolchainSearch
43 import bertos_utils
44 import qvariant_converter
45
46 from toolchain_manager import ToolchainManager
47
48 from const import *
49
50 class BToolchainPage(BWizardPage):
51     """
52     Page of the wizard that permits to choose the toolchain to use for the
53     project.
54     """
55
56     def __init__(self):
57         BWizardPage.__init__(self, UI_LOCATION + "/toolchain_select.ui")
58         self.setTitle(self.tr("Select toolchain"))
59         self.setSubTitle(self.tr("You can look for more toolchains in your system by pressing the \"Search\" button, or manually add them with the \"+\" button"))
60         self._valid_items = []
61         self._toolchain_manager = ToolchainManager()
62
63     ## Overloaded QWizardPage methods. ##
64
65     def isComplete(self):
66         """
67         Overload of the QWizard isComplete method.
68         """
69         if self.pageContent.toolchainList.currentRow() != -1:
70             self.setProjectInfo("TOOLCHAIN",
71                 qvariant_converter.getStringDict(self.pageContent.toolchainList.currentItem().data(Qt.UserRole)))
72             return True
73         else:
74             return False
75
76     def nextId(self):
77         """
78         Overload of the QWizardPage nextId method.
79         """
80         # Route to Output page if it's a predefined easy project.
81         if self.projectInfo("PROJECT_FROM_PRESET") and self.projectInfo("BASE_MODE"):
82             return self.wizard().pageIndex(BCreationPage)
83         else:
84             return QWizardPage.nextId(self)
85
86     ####
87
88     ## Overloaded BWizardPage methods. ##
89
90     def setupUi(self):
91         """
92         Sets up the user interface.
93         """
94         self.pageContent.infoLabel.setVisible(False)
95
96     def connectSignals(self):
97         """
98         Connects the signals with the related slots.
99         """
100         self.connect(self.pageContent.toolchainList, SIGNAL("currentItemChanged(QListWidgetItem *, QListWidgetItem*)"), self.selectionChanged)
101         self.connect(self.pageContent.addButton, SIGNAL("clicked()"), self.addToolchain)
102         self.connect(self.pageContent.removeButton, SIGNAL("clicked()"), self.removeToolchain)
103         self.connect(self.pageContent.searchButton, SIGNAL("clicked()"), self.searchToolchain)
104         self.connect(self.pageContent.checkButton, SIGNAL("clicked()"), self.validateAllToolchains)
105
106     def reloadData(self, previous_id=None):
107         """
108         Overload of the BWizard reloadData method.
109         """
110         if previous_id is None or previous_id < self.wizard().currentId():
111             self._clearList()
112             self.setupUi()
113             self._populateToolchainList()
114             if len(self._valid_items) >= 1:
115                 self.pageContent.toolchainList.setCurrentItem(self._valid_items[0])
116
117     ####
118
119     ## Slots ##
120
121     def selectionChanged(self):
122         """
123         Slot called when the user click on an entry of the toolchain list.
124         """
125         if self.pageContent.toolchainList.currentRow() != -1:
126             infos = collections.defaultdict(lambda: unicode("not defined"))
127             infos.update(qvariant_converter.getStringDict(self.pageContent.toolchainList.currentItem().data(Qt.UserRole)))
128             self.pageContent.infoLabel.setText("GCC " + infos["version"] + " (" + infos["build"] + ")\nTarget: " + infos["target"] + "\nPath: " + os.path.normpath(infos["path"]))
129             self.pageContent.infoLabel.setVisible(True)
130             if self.isDefaultToolchain(infos):
131                 self.disableRemoveButton()
132             else:
133                 self.enableRemoveButton()
134             self.emit(SIGNAL("completeChanged()"))
135
136     def addToolchain(self):
137         """
138         Slot called when the user adds manually a toolchain.
139         """
140         sel_toolchain = unicode(QFileDialog.getOpenFileName(self, self.tr("Choose the toolchain"), ""))
141         if sel_toolchain != "":
142             item = QListWidgetItem(sel_toolchain)
143             item.setData(Qt.UserRole, qvariant_converter.convertStringDict({"path": sel_toolchain}))
144             self.pageContent.toolchainList.addItem(item)
145             self._toolchain_manager.addToolchain(sel_toolchain)
146
147     def removeToolchain(self):
148         """
149         Slot called when the user removes manually a toolchain.
150         """
151         if self.pageContent.toolchainList.currentRow() != -1:
152             item = self.pageContent.toolchainList.takeItem(self.pageContent.toolchainList.currentRow())
153             toolchain = qvariant_converter.getStringDict(item.data(Qt.UserRole))["path"]
154             self._toolchain_manager.removeToolchain(toolchain)
155
156     def searchToolchain(self):
157         """
158         Slot called when the user clicks on the 'search' button. It opens the
159         toolchain search dialog.
160         """
161         search = BToolchainSearch.BToolchainSearch()
162         self.connect(search, SIGNAL("accepted()"), self._search)
163         search.exec_()
164
165     def validateAllToolchains(self):
166         """
167         Slot called when the user clicks on the validate button. It starts the
168         toolchain validation procedure for all the toolchains.
169         """
170         try:
171             QApplication.instance().setOverrideCursor(Qt.WaitCursor)
172             for i in range(self.pageContent.toolchainList.count()):
173                 self.validateToolchain(i)
174         finally:
175             QApplication.instance().restoreOverrideCursor()
176
177     ####
178
179     def _populateToolchainList(self):
180         """
181         Fills the toolchain list with the toolchains stored in the QSettings.
182         """
183         self.pageContent.toolchainList.clear()
184         self._valid_items = []
185         toolchains = self._toolchain_manager.predefined_toolchains + self._toolchain_manager.toolchains
186         sel_toolchain = self.projectInfo("TOOLCHAIN")
187         for key, value in toolchains:
188             if os.path.exists(key):
189                 item = QListWidgetItem(key)
190                 item_data = {"path":key}
191                 if value:
192                     item_data.update(value)
193                 item.setData(Qt.UserRole, qvariant_converter.convertStringDict(item_data))
194                 self.pageContent.toolchainList.addItem(item)
195                 if sel_toolchain and sel_toolchain["path"] == key:
196                     self.pageContent.toolchainList.setCurrentItem(item)
197                 if value is not None:
198                     self.validateToolchain(self.pageContent.toolchainList.row(item))
199
200     def currentToolchain(self):
201         selected_toolchain = qvariant_converter.getStringDict(self.pageContent.toolchainList.currentItem().data(Qt.UserRole))
202         return selected_toolchain
203
204     def _clearList(self):
205         """
206         Removes all the toolchain from the list.
207         """
208         self.pageContent.toolchainList.clear()
209
210     def _search(self):
211         """
212         Searches for toolchains in the stored directories, and stores them in the
213         QSettings.
214         """
215         dir_list = self.searchDirList()
216         if self.pathSearch():
217             dir_list += [element for element in bertos_utils.getSystemPath()]
218         _toolchain_dict = self._toolchain_manager.storedToolchainDict()
219         toolchain_list = bertos_utils.findToolchains(dir_list)
220         for toolchain in toolchain_list:
221             self._toolchain_manager.addToolchain(toolchain, _toolchain_dict.get(toolchain, False))
222         self._populateToolchainList()
223         self.showMessage(self.tr("Toolchain search result."), self.tr("%1 toolchains found").arg(len(toolchain_list)))
224
225     def _validItem(self, index, infos):
226         """
227         Sets the item at index as a valid item and associates the given info to it.
228         """
229         item = self.pageContent.toolchainList.item(index)
230         new_data = qvariant_converter.getStringDict(self.pageContent.toolchainList.item(index).data(Qt.UserRole))
231         new_data.update(infos)
232         item.setData(Qt.UserRole, qvariant_converter.convertStringDict(new_data))
233         needed = self.projectInfo("CPU_INFOS")
234         if "target" in infos and infos["target"].find(needed["TOOLCHAIN"]) != -1:
235             item.setIcon(QIcon(":/images/ok.png"))
236             self._valid_items.append(item)
237         else:
238             item.setIcon(QIcon(":/images/warning.png"))
239         if "version" in infos and "target" in infos:
240             item.setText("GCC " + infos["version"] + " - " + infos["target"].strip())
241
242     def _invalidItem(self, index):
243         """
244         Sets the item at index as an invalid item.
245         """
246         item = self.pageContent.toolchainList.item(index)
247         item.setIcon(QIcon(":/images/error.png"))
248
249     def validateToolchain(self, i):
250         """
251         Toolchain validation procedure.
252         """
253         filename = qvariant_converter.getStringDict(self.pageContent.toolchainList.item(i).data(Qt.UserRole))["path"]
254         info = self._toolchain_manager.validateToolchain(filename)
255
256         # Add the item in the list with the appropriate associate data.
257         if info:
258             self._validItem(i, info)
259         else:
260             self._invalidItem(i)
261     
262     def isDefaultToolchain(self, toolchain):
263         """
264         Returns True if the given toolchain is one of the default toolchains.
265         """
266         return toolchain["path"] in self._toolchain_manager._predefined_toolchain_set
267     
268     def disableRemoveButton(self):
269         """
270         Disable the remove button.
271         """
272         self.pageContent.removeButton.setEnabled(False)
273     
274     def enableRemoveButton(self):
275         """
276         Enable the remove button.
277         """
278         self.pageContent.removeButton.setEnabled(True)
279         
280     def currentItem(self):
281         return self.pageContent.toolchainList.currentItem()