Add BeRTOS header in sources files
[bertos.git] / wizard / BVersionPage.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 2008 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.QtGui import *
39 from BWizardPage import *
40 import bertos_utils
41 import qvariant_converter
42
43 from const import *
44
45 class BVersionPage(BWizardPage):
46     """
47     Page of the wizard that permits to choose which BeRTOS version the user wants
48     to use. This page show some pieces of information about the version.
49     """
50     
51     def __init__(self):
52         BWizardPage.__init__(self, UI_LOCATION + "/bertos_versions.ui")
53         self.setTitle(self.tr("Select the BeRTOS directory"))
54         self.setSubTitle(self.tr("The project created will be based on the BeRTOS version found"))
55
56     ## Overloaded QWizardPage methods ##
57     
58     def isComplete(self):
59         """
60         Overload of the QWizardPage isComplete method.
61         """
62         if self.pageContent.versionList.currentRow() != -1:
63             sources_path = qvariant_converter.getString(self.pageContent.versionList.currentItem().data(Qt.UserRole))
64             # Remove the trailing slash
65             if sources_path.endswith(os.sep):
66                 sources_path = sources_path[:-1]
67             self.setProjectInfo("SOURCES_PATH", sources_path)
68             return True
69         else:
70             return False
71     
72     ####
73     
74     ## Overloaded BWizardPage methods ##
75
76     def connectSignals(self):
77         """
78         Overload of the BWizardPage connectSignals method.
79         """
80         self.connect(self.pageContent.versionList, SIGNAL("itemSelectionChanged()"), self.rowChanged)
81         self.connect(self.pageContent.addButton, SIGNAL("clicked()"), self.addVersion)
82         self.connect(self.pageContent.removeButton, SIGNAL("clicked()"), self.removeVersion)
83         # Fake signal connection for the update button
84         self.connect(self.pageContent.updateButton, SIGNAL("clicked()"), self.updateClicked)
85     
86     def reloadData(self):
87         """
88         Overload of the BWizardPage reloadData method.
89         """
90         self.resetVersionList()
91         self.pageContent.versionList.setCurrentRow(-1)
92         self.fillVersionList()
93     
94     def setupUi(self):
95         """
96         Overload of the BWizardPage setupUi method.
97         """
98         self.pageContent.updateButton.setVisible(False)
99     
100     ####
101     
102     ## Slots ##
103     
104     def addVersion(self):
105         """
106         Slot called when the user add a BeRTOS version.
107         """
108         directory = QFileDialog.getExistingDirectory(self, self.tr("Choose a directory"), "", QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks)
109         if not directory.isEmpty():
110             self.storeVersion(unicode(directory))
111             self.pageContent.versionList.clear()
112             self.fillVersionList()
113             self.emit(SIGNAL("completeChanged()"))
114     
115     def removeVersion(self):
116         """
117         Slot called when the user remove a BeRTOS version.
118         """
119         item = self.pageContent.versionList.takeItem(self.pageContent.versionList.currentRow())
120         self.deleteVersion(qvariant_converter.getString(item.data(Qt.UserRole)))
121         self.emit(SIGNAL("completeChanged()"))
122     
123     def rowChanged(self):
124         """
125         Slot called when the user select an entry from the version list.
126         """
127         if self.isDefaultVersion(self.currentVersion()):
128             self.disableRemoveButton()
129         else:
130             self.enableRemoveButton()
131         self.emit(SIGNAL("completeChanged()"))
132
133     def updateClicked(self):
134         """
135         Slot called when the user clicks on the 'update' button. It checks for
136         update (TO BE IMPLEMENTED).
137         """
138         pass    
139
140     ####
141     
142     def storeVersion(self, directory):
143         """
144         Stores the directory selected by the user in the QSettings.
145         """
146         versions = self.versions()
147         versions = set(versions + [directory])
148         self.setVersions(list(versions))
149     
150     def deleteVersion(self, directory):
151         """
152         Removes the given directory from the QSettings.
153         """
154         versions = self.versions()
155         versions.remove(directory)
156         self.setVersions(versions)
157     
158     def resetVersionList(self):
159         """
160         Remove all the version entries from the list.
161         """
162         self.pageContent.versionList.clear()
163     
164     def insertListElement(self, directory):
165         """
166         Inserts the given directory in the version list and returns the
167         inserted item.
168         """
169         if bertos_utils.isBertosDir(directory):
170             item = QListWidgetItem(QIcon(":/images/ok.png"), bertos_utils.bertosVersion(directory) + " (\"" + os.path.normpath(directory) + "\")")
171             item.setData(Qt.UserRole, qvariant_converter.convertString(directory))
172             self.pageContent.versionList.addItem(item)
173             return item
174         elif len(directory) > 0:
175             item = QListWidgetItem(QIcon(":/images/warning.png"), "UNKNOWN" + " (\"" + os.path.normpath(directory) + "\")")
176             item.setData(Qt.UserRole, qvariant_converter.convertString(directory))
177             self.pageContent.versionList.addItem(item)
178             return item
179     
180     def fillVersionList(self):
181         """
182         Fills the version list with all the BeRTOS versions founded in the QSettings.
183         """
184         versions = set([])
185         if os.name == "nt":
186             import winreg_importer
187             versions |= set(winreg_importer.getBertosDirs())
188         versions |= set(self.versions())
189         selected = self.projectInfo("SOURCES_PATH")
190         for directory in versions:
191             item = self.insertListElement(directory)
192             if selected and selected == directory:
193                 self.setCurrentItem(item)
194         if not selected:
195             self.setCurrentItem(self.latestVersionItem())
196     
197     def disableRemoveButton(self):
198         """
199         Disable the Remove button.
200         """
201         self.pageContent.removeButton.setEnabled(False)
202
203     def enableRemoveButton(self):
204         """
205         Enable the Remove button.
206         """
207         self.pageContent.removeButton.setEnabled(True)
208     
209     def latestVersionItem(self):
210         """
211         Returns the latest BeRTOS version founded.
212         """
213         latest_version_item = QTableWidgetItem("")
214         for index in range(self.pageContent.versionList.count()):
215             item = self.pageContent.versionList.item(index)
216             version = item.text().split(" (")[0]
217             latest = latest_version_item.text().split(" (")[0]
218             if version != "UNKNOWN" and version > latest:
219                 latest_version_item = item
220         return latest_version_item
221     
222     def setCurrentItem(self, item):
223         """
224         Select the given item in the version list.
225         """
226         self.pageContent.versionList.setCurrentItem(item)
227     
228     def currentItem(self):
229         """
230         Returns the current selected item.
231         """
232         return self.pageContent.versionList.currentItem()
233     
234     def currentVersion(self):
235         """
236         Return the path of the selected version.
237         """
238         current = self.currentItem()
239         return qvariant_converter.getString(current.data(Qt.UserRole))
240     
241     def isDefaultVersion(self, version):
242         """
243         Returns True if the given version is one of the default versions.
244         """
245         if os.name == "nt":
246             import winreg_importer
247             if version in winreg_importer.getBertosDirs():
248                 return True
249         return False