Reload the module data every time the user enter into the module configuration 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
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         self._last_selected = None
61
62     ## Overloaded QWizardPage methods ##
63
64     def isComplete(self):
65         """
66         Overload of the QWizardPage isComplete method.
67         """
68         if self.selected:
69             _info_dict = qvariant_converter.getDict(self.selected.data(0, Qt.UserRole))
70             _type = _info_dict["type"]
71             type = qvariant_converter.getString(_type)
72             if type == "project":
73                 self.setProjectInfo("PROJECT_PRESET", qvariant_converter.getString(_info_dict["path"]))
74                 return True
75             else:
76                 return False
77         else:
78             return False
79
80     def nextId(self):
81         """
82         Overload of the QWizardPage nextId method.
83         """
84         return self.wizard().pageIndex(BRoutePage)
85
86     ####
87
88     ## Overloaded BWizardPage methods ##
89
90     def setupUi(self):
91         """
92         Overload of the BWizardPage setupUi method.
93         """
94         pass
95
96     def connectSignals(self):
97         """
98         Overload of the BWizardPage connectSignals method.
99         """
100         self.connect(self.pageContent.boardTree, SIGNAL("itemSelectionChanged()"), self, SIGNAL("completeChanged()"))
101
102     def reloadData(self):
103         """
104         Overload of the BWizardPage reloadData method.
105         """
106         self._fillPresetTree()
107
108     ####
109
110     ## Slots ##
111
112
113     ####
114
115     @property
116     def selected(self):
117         # We can only take the selected item list (not the single item)
118         _selected_items = self.pageContent.boardTree.selectedItems()
119         if _selected_items:
120             return _selected_items[0]
121         else:
122             return None
123
124     def _fillPresetTree(self):
125         self.pageContent.boardTree.clear()
126         self.project.loadProjectPresets()
127         preset_tree = self.project.info("PRESET_TREE")
128         for obj in preset_tree["children"]:
129             self._createPresetNode(self.pageContent.boardTree, obj)
130
131     def _createPresetNode(self, parent, obj):
132         selected_item_name = self.projectInfo("PROJECT_PRESET")
133         item_name = obj["info"].get("name", obj["info"]["filename"])
134         item = QTreeWidgetItem(parent, [item_name]) 
135         item.setIcon(0, QIcon(self._getNodeIcon(obj)))
136         children_dict = obj["children"]
137         item.setData(0, Qt.UserRole, qvariant_converter.convertDict(obj["info"]))
138         if obj["info"]["path"] == selected_item_name:
139             self.pageContent.boardTree.setCurrentItem(item)
140         for child in children_dict:
141             self._createPresetNode(item, child)
142
143     def _getNodeIcon(self, obj):
144         icon_file = os.path.join(obj["info"]["path"], const.PREDEFINED_BOARD_ICON_FILE)
145         if os.path.exists(icon_file):
146             return icon_file
147         elif obj["info"]["type"] == "dir":
148             return const.PREDEFINED_BOARD_DEFAULT_DIR_ICON
149         else:
150             return const.PREDEFINED_BOARD_DEFAULT_PROJECT_ICON