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