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