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