Refactor to use new protocol module and sipo.
[bertos.git] / wizard / BProjectPresets.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 2010 Develer S.r.l. (http://www.develer.com/)
30 #
31 #
32 # Author: Lorenzo Berni <duplo@develer.com>
33 #
34
35 import os
36
37 from PyQt4 import uic
38
39 from PyQt4.QtCore import *
40 from PyQt4.QtGui import *
41
42 from BWizardPage import BWizardPage
43
44 from BCreationPage import BCreationPage
45 from BToolchainPage import BToolchainPage
46
47 from DefineException import ModuleDefineException
48
49 from bertos_utils import _cmp
50 from toolchain_manager import ToolchainManager
51
52 import const
53 import qvariant_converter
54
55 class BProjectPresetsPage(QWidget):
56     def __init__(self, preset_data, parent=None):
57         QWidget.__init__(self, parent)
58         self.pageContent = uic.loadUi(os.path.join(const.DATA_DIR, const.UI_LOCATION, "preset_page.ui"), None)
59         self.project = QApplication.instance().project
60         self.settings = QApplication.instance().settings
61         self.preset_data = preset_data
62         layout = QVBoxLayout()
63         layout.addWidget(self.pageContent)
64         self.setLayout(layout)
65         self.setupUi()
66         self.connectSignals()
67
68     def setupUi(self):
69         self.pageContent.presetList.clear()
70         self.pageContent.categoryDescription.setText(self.preset_data["info"].get("description", ""))
71         for preset in sorted(self.preset_data["children"].values(), _cmp):
72             item_name = preset["info"].get("name", preset["info"]["filename"])
73             item_icon = os.path.join(preset["info"]["path"], const.PREDEFINED_BOARD_ICON_FILE)
74             if not os.path.exists(item_icon):
75                 item_icon = const.PREDEFINED_BOARD_DEFAULT_PROJECT_ICON
76             item_icon = QIcon(item_icon)
77             item = QListWidgetItem(item_icon, item_name)
78             item.setData(Qt.UserRole, qvariant_converter.convertString(preset["info"]["path"]))
79             self.pageContent.presetList.addItem(item)
80         self.pageContent.presetList.setCurrentRow(0)
81         self.updateUi()
82
83     def connectSignals(self):
84         self.connect(self.pageContent.presetList, SIGNAL("currentItemChanged(QListWidgetItem *, QListWidgetItem*)"), self.updateUi)
85         self.connect(self.pageContent.presetList, SIGNAL("currentItemChanged(QListWidgetItem *, QListWidgetItem*)"), self, SIGNAL("completeChanged()"))
86
87     def updateUi(self):
88         if self.selected:
89             preset_path = qvariant_converter.getString(self.selected.data(Qt.UserRole))
90             preset = self.preset_data["children"][preset_path]
91             description = preset["info"].get("description", "")
92             path = unicode(QUrl.fromLocalFile(preset_path).toString())
93             description = description.replace("$path", path)
94             self.pageContent.descriptionArea.setHtml(description)
95     
96     @property
97     def selected(self):
98         return self.pageContent.presetList.currentItem()
99         
100
101 class BProjectPresets(BWizardPage):
102     def __init__(self):
103         BWizardPage.__init__(self, const.UI_LOCATION + "/project_presets.ui")
104
105     ## Overloaded QWizardPage methods ##
106
107     def isComplete(self):
108         preset_path = self.selected_path
109         if preset_path:
110             self.setProjectInfo("PROJECT_PRESET", preset_path)
111             self.setProjectInfo("BASE_MODE", not self.advanced)
112             return True
113         else:
114             self.setProjectInfo("PROJECT_PRESET", None)
115             return False
116
117     def validatePage(self):
118         """
119         This hack permits to load the preset once, when the user go press the
120         Next button.
121         """
122         preset_path = self.selected_path
123         try:
124             QApplication.instance().setOverrideCursor(Qt.WaitCursor)
125             try:
126                 self.project.loadProjectFromPreset(preset_path)
127             except ModuleDefineException, e:
128                 self.exceptionOccurred(self.tr("Error parsing line '%2' in file %1").arg(e.path).arg(e.line))
129             self.setProjectInfo("PRESET_LOADED", True)
130         finally:
131             QApplication.instance().restoreOverrideCursor()
132         # Return always True, this is a fake validation.
133         return True
134
135     def nextId(self):
136         """
137         Overload of the QWizardPage nextId method.
138         """
139         # Route to Toolchain page if the user select advanced
140         # or to Output page if the user select base
141         if self.advanced:
142             return self.wizard().pageIndex(BToolchainPage)
143         else:
144             cpu_info = self.projectInfo("CPU_INFOS")
145             if cpu_info:
146                 target = cpu_info["TOOLCHAIN"]
147                 # Try to find a suitable toolchain automatically
148                 tm = ToolchainManager()
149                 suitable_toolchains = tm.suitableToolchains(target)
150                 if len(suitable_toolchains) == 1:
151                     toolchain = suitable_toolchains.pop()
152                     toolchain_info = tm._validateToolchain(toolchain)
153                     toolchain_info["path"] = toolchain
154                     self.setProjectInfo("TOOLCHAIN", toolchain_info)
155                     return self.wizard().pageIndex(BCreationPage)
156                 else:
157                     return self.wizard().pageIndex(BToolchainPage)
158             else:
159                 # It seems that the nextId method is called before the
160                 # reloadData one (that is called after the page changing.
161                 #
162                 # TODO: fix this awful code lines
163                 target = None
164                 return self.wizard().pageIndex(BToolchainPage)
165
166     ####
167     
168     ## Overloaded BWizardPage methods ##
169     
170     def reloadData(self, previous_id=None):
171         if not self.projectInfo("PRESET_LOADED"):
172             preset_path = self.projectInfo("PROJECT_BOARD")
173             preset_tree = self.projectInfo("PRESET_TREE")
174             preset_list = preset_tree["children"][preset_path]["children"]
175             preset_list = sorted(preset_list.values(), _cmp)
176             self.setTitle(self.tr("Select the project template for %1").arg(preset_tree["children"][preset_path]["info"].get("name", preset_tree["children"][preset_path]["info"]["filename"])))
177             self.setupTabs(preset_list)
178
179     def connectSignals(self):
180         self.connect(self.pageContent.boardTabWidget, SIGNAL("currentChanged(int)"), self, SIGNAL("completeChanged()"))
181
182     ####
183     
184     ## Slots ##
185     ####
186
187     def setupTabs(self, preset_list):
188         self.pageContent.boardTabWidget.clear()
189         for preset in preset_list:
190             icon = os.path.join(preset["info"]["path"], ".icon.png")
191             preset_page = BProjectPresetsPage(preset)
192             if os.path.exists(icon):
193                 self.pageContent.boardTabWidget.addTab(preset_page, QIcon(icon), preset["info"].get("name", preset["info"]["filename"]))
194             else:
195                 self.pageContent.boardTabWidget.addTab(preset_page, preset["info"].get("name", preset["info"]["filename"]))
196             self.connect(preset_page, SIGNAL("completeChanged()"), self, SIGNAL("completeChanged()"))
197
198     @property
199     def advanced(self):
200         if self.selected_data:
201             return self.selected_data["info"].get("advanced", False)
202         else:
203             return None
204
205     @property
206     def selected_path(self):
207         current_widget = self.pageContent.boardTabWidget.currentWidget()
208         preset_path = None
209         if current_widget:
210             current_item = current_widget.pageContent.presetList.currentItem()
211             if current_item:
212                 preset_path = current_item.data(Qt.UserRole)
213                 preset_path = qvariant_converter.getString(preset_path)
214         return preset_path
215
216     @property
217     def selected_data(self):
218         if self.selected_path:
219             current_widget = self.pageContent.boardTabWidget.currentWidget()
220             return current_widget.preset_data["children"][self.selected_path]
221         else:
222             return None