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