Remove the base/advance choice page.
[bertos.git] / wizard / BBoardPage.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 # $Id$
32 #
33 # Author: Lorenzo Berni <duplo@develer.com>
34 #
35
36 import os
37
38 from PyQt4.QtCore import *
39 from PyQt4.QtGui import *
40
41 from BWizardPage import BWizardPage
42
43 from BCpuPage import BCpuPage
44 from BOutputPage import BOutputPage
45 from BRoutePage import BRoutePage
46
47 import const
48 import qvariant_converter
49 from bertos_utils import presetList, _cmp
50
51 class BBoardPage(BWizardPage):
52     """
53     Initial page of the alternative route. Permit to select one of the
54     predefined projects for supported board.
55     """
56     
57     def __init__(self):
58         BWizardPage.__init__(self, const.UI_LOCATION + "/board_select.ui")
59         self.setTitle(self.tr("Select the board from the predefined ones"))
60
61     ## Overloaded QWizardPage methods ##
62
63     def isComplete(self):
64         """
65         Overload of the QWizardPage isComplete method.
66         """
67         if self.selected:
68             preset_path = qvariant_converter.getDict(self.selected.data(Qt.UserRole))
69             preset_path = qvariant_converter.getStringDict(preset_path["info"])
70             preset_path = preset_path["path"]
71             self.setProjectInfo("PROJECT_BOARD", preset_path)
72             self.setProjectInfo("PROJECT_FROM_PRESET", True)
73             return True
74         else:
75             return False
76
77     def nextId(self):
78         wizard = self.wizard()
79         if not self.projectInfo("PROJECT_FROM_PRESET"):
80             return wizard.pageIndex(BCpuPage)
81         else:
82             return QWizardPage.nextId(self)
83
84     ####
85
86     ## Overloaded BWizardPage methods ##
87
88     def setupUi(self):
89         """
90         Overload of the BWizardPage setupUi method.
91         """
92         pass
93
94     def connectSignals(self):
95         """
96         Overload of the BWizardPage connectSignals method.
97         """
98         self.connect(self.pageContent.boardList, SIGNAL("itemSelectionChanged()"), self.updateUi)
99         self.connect(self.pageContent.boardList, SIGNAL("itemSelectionChanged()"), self, SIGNAL("completeChanged()"))
100         self.connect(self.pageContent.customButton, SIGNAL("clicked()"), self.customButtonClicked)
101
102     def reloadData(self):
103         """
104         Overload of the BWizardPage reloadData method.
105         """
106         self.project.loadProjectPresets()
107         preset_list = self.projectInfo("PRESET_TREE")
108         preset_list = preset_list["children"]
109         preset_list = sorted(preset_list.values(), _cmp)
110         self.setItems(preset_list)
111
112     ####
113
114     ## Slots ##
115
116     def updateUi(self):
117         if self.selected:
118             info_dict = qvariant_converter.getDict(self.selected.data(Qt.UserRole))
119             info_dict = qvariant_converter.getStringDict(info_dict["info"])
120             description = info_dict.get("description", "")
121             image = os.path.join(info_dict["path"], ".image.png")
122             if os.path.exists(image):
123                 self.pageContent.imageLabel.setPixmap(QPixmap(image))
124                 self.pageContent.imageLabel.setVisible(True)
125             else:
126                 self.pageContent.imageLabel.setVisible(False)
127             self.pageContent.descriptionLabel.setText(description)
128
129     def customButtonClicked(self):
130         self.setProjectInfo("PROJECT_FROM_PRESET", False)
131         self.wizard().next()
132
133     ####
134
135     def setItems(self, preset_list):
136         self.pageContent.boardList.clear()
137         selected_board = self.projectInfo("PROJECT_BOARD")
138         for item_data in preset_list:
139             item_name = item_data["info"].get("name", item_data["info"]["filename"])
140             item_icon = os.path.join(item_data["info"]["path"], const.PREDEFINED_BOARD_ICON_FILE)
141             if not os.path.exists(item_icon):
142                 item_icon = const.PREDEFINED_BOARD_DEFAULT_ICON
143             item = QListWidgetItem(QIcon(item_icon), item_name)
144             item.setData(Qt.UserRole, qvariant_converter.convertDict(item_data))
145             self.pageContent.boardList.addItem(item)
146             if selected_board and selected_board == item_data["info"]["path"]:
147                 self.pageContent.boardList.setCurrentItem(item)
148
149     @property
150     def selected(self):
151         return self.pageContent.boardList.currentItem()