Add stub of sources merge functionality
[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             latest_version_item = self.latestVersionItem()
196             if latest_version_item:
197                 self.setCurrentItem(latest_version_item)
198     
199     def disableRemoveButton(self):
200         """
201         Disable the Remove button.
202         """
203         self.pageContent.removeButton.setEnabled(False)
204
205     def enableRemoveButton(self):
206         """
207         Enable the Remove button.
208         """
209         self.pageContent.removeButton.setEnabled(True)
210     
211     def latestVersionItem(self):
212         """
213         Returns the latest BeRTOS version founded.
214         """
215         latest_version_item = None
216         for index in range(self.pageContent.versionList.count()):
217             item = self.pageContent.versionList.item(index)
218             if not latest_version_item:
219                 latest_version_item = item
220             version = item.text().split(" (")[0]
221             latest = latest_version_item.text().split(" (")[0]
222             if version != "UNKNOWN" and version > latest:
223                 latest_version_item = item
224         return latest_version_item
225     
226     def setCurrentItem(self, item):
227         """
228         Select the given item in the version list.
229         """
230         self.pageContent.versionList.setCurrentItem(item)
231     
232     def currentItem(self):
233         """
234         Returns the current selected item.
235         """
236         return self.pageContent.versionList.currentItem()
237     
238     def currentVersion(self):
239         """
240         Return the path of the selected version.
241         """
242         current = self.currentItem()
243         return qvariant_converter.getString(current.data(Qt.UserRole))
244     
245     def isDefaultVersion(self, version):
246         """
247         Returns True if the given version is one of the default versions.
248         """
249         if os.name == "nt":
250             import winreg_importer
251             if version in winreg_importer.getBertosDirs():
252                 return True
253         return False
254