Add title for Preset page.
[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 bertos_utils import _cmp
46
47 import const
48 import qvariant_converter
49
50 class BProjectPresetsPage(QWidget):
51     def __init__(self, preset_data, parent=None):
52         QWidget.__init__(self, parent)
53         self.pageContent = uic.loadUi(os.path.join(const.DATA_DIR, const.UI_LOCATION, "preset_page.ui"), None)
54         self.project = QApplication.instance().project
55         self.settings = QApplication.instance().settings
56         self.preset_data = preset_data
57         layout = QVBoxLayout()
58         layout.addWidget(self.pageContent)
59         self.setLayout(layout)
60         self.setupUi()
61         self.connectSignals()
62
63     def setupUi(self):
64         self.pageContent.presetList.clear()
65         for preset in sorted(self.preset_data["children"].values(), _cmp):
66             item_name = preset["info"].get("name", preset["info"]["filename"])
67             item_icon = os.path.join(preset["info"]["path"], const.PREDEFINED_BOARD_ICON_FILE)
68             if not os.path.exists(item_icon):
69                 item_icon = const.PREDEFINED_BOARD_DEFAULT_PROJECT_ICON
70             item_icon = QIcon(item_icon)
71             item = QListWidgetItem(item_icon, item_name)
72             item.setData(Qt.UserRole, qvariant_converter.convertString(preset["info"]["path"]))
73             self.pageContent.presetList.addItem(item)
74         self.pageContent.presetList.setCurrentRow(0)
75         self.updateUi()
76
77     def connectSignals(self):
78         self.connect(self.pageContent.presetList, SIGNAL("itemSelectionChanged()"), self.updateUi)
79         self.connect(self.pageContent.presetList, SIGNAL("itemSelectionChanged()"), self, SIGNAL("completeChanged()"))
80
81     def updateUi(self):
82         if self.selected:
83             preset_path = qvariant_converter.getString(self.selected.data(Qt.UserRole))
84             preset = self.preset_data["children"][preset_path]
85             self.pageContent.descriptionLabel.setText(preset["info"].get("description", ""))
86             image = os.path.join(preset["info"]["path"], const.PREDEFINED_BOARD_IMAGE_FILE)
87             if os.path.exists(image):
88                 self.pageContent.imageLabel.setPixmap(QPixmap(image))
89                 self.pageContent.imageLabel.setVisible(True)
90             else:
91                 self.pageContent.imageLabel.setVisible(False)
92     
93     @property
94     def selected(self):
95         return self.pageContent.presetList.currentItem()
96         
97
98 class BProjectPresets(BWizardPage):
99     def __init__(self):
100         BWizardPage.__init__(self, const.UI_LOCATION + "/project_presets.ui")
101
102     ## Overloaded QWizardPage methods ##
103
104     def isComplete(self):
105         current_widget = self.pageContent.boardTabWidget.currentWidget()
106         preset_path = None
107         if current_widget:
108             current_item = current_widget.pageContent.presetList.currentItem()
109             if current_item:
110                 preset_path = current_item.data(Qt.UserRole)
111                 preset_path = qvariant_converter.getString(preset_path)
112         if preset_path:
113             self.setProjectInfo("PROJECT_PRESET", preset_path)
114             return True
115         else:
116             self.setProjectInfo("PROJECT_PRESET", None)
117             return False
118     ####
119     
120     ## Overloaded BWizardPage methods ##
121     
122     def reloadData(self):
123         preset_path = self.projectInfo("PROJECT_BOARD")
124         preset_tree = self.projectInfo("PRESET_TREE")
125         preset_list = preset_tree["children"][preset_path]["children"]
126         preset_list = sorted(preset_list.values(), _cmp)
127         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"])))
128         self.setupTabs(preset_list)
129
130     def connectSignals(self):
131         self.connect(self.pageContent.boardTabWidget, SIGNAL("currentChanged(int)"), self, SIGNAL("completeChanged()"))
132
133     ####
134     
135     ## Slots ##
136     ####
137
138     def setupTabs(self, preset_list):
139         self.pageContent.boardTabWidget.clear()
140         for preset in preset_list:
141             icon = os.path.join(preset["info"]["path"], ".icon.png")
142             preset_page = BProjectPresetsPage(preset)
143             if os.path.exists(icon):
144                 self.pageContent.boardTabWidget.addTab(preset_page, QIcon(icon), preset["info"].get("name", preset["info"]["filename"]))
145             else:
146                 self.pageContent.boardTabWidget.addTab(preset_page, preset["info"].get("name", preset["info"]["filename"]))
147             self.connect(preset_page, SIGNAL("completeChanged()"), self, SIGNAL("completeChanged()"))