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