Show wizard version.
[bertos.git] / wizard / BVersionPage.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
5 # All rights reserved.
6 #
7 # $Id$
8 #
9 # Author: Lorenzo Berni <duplo@develer.com>
10 #
11
12 import os
13
14 from PyQt4.QtGui import *
15 from BWizardPage import *
16 import bertos_utils
17 import qvariant_converter
18
19 from const import *
20
21 class BVersionPage(BWizardPage):
22     """
23     Page of the wizard that permits to choose which BeRTOS version the user wants
24     to use. This page show some pieces of information about the version.
25     """
26     
27     def __init__(self):
28         BWizardPage.__init__(self, UI_LOCATION + "/bertos_versions.ui")
29         self.setTitle(self.tr("Select the BeRTOS directory"))
30         self.setSubTitle(self.tr("The project created will be based on the BeRTOS version found"))
31
32     ## Overloaded QWizardPage methods ##
33     
34     def isComplete(self):
35         """
36         Overload of the QWizardPage isComplete method.
37         """
38         if self.pageContent.versionList.currentRow() != -1:
39             sources_path = qvariant_converter.getString(self.pageContent.versionList.currentItem().data(Qt.UserRole))
40             # Remove the trailing slash
41             if sources_path.endswith(os.sep):
42                 sources_path = sources_path[:-1]
43             self.setProjectInfo("SOURCES_PATH", sources_path)
44             return True
45         else:
46             return False
47     
48     ####
49     
50     ## Overloaded BWizardPage methods ##
51
52     def connectSignals(self):
53         """
54         Overload of the BWizardPage connectSignals method.
55         """
56         self.connect(self.pageContent.versionList, SIGNAL("itemSelectionChanged()"), self.rowChanged)
57         self.connect(self.pageContent.addButton, SIGNAL("clicked()"), self.addVersion)
58         self.connect(self.pageContent.removeButton, SIGNAL("clicked()"), self.removeVersion)
59         # Fake signal connection for the update button
60         self.connect(self.pageContent.updateButton, SIGNAL("clicked()"), self.updateClicked)
61     
62     def reloadData(self):
63         """
64         Overload of the BWizardPage reloadData method.
65         """
66         self.resetVersionList()
67         self.pageContent.versionList.setCurrentRow(-1)
68         self.fillVersionList()
69     
70     def setupUi(self):
71         """
72         Overload of the BWizardPage setupUi method.
73         """
74         self.pageContent.updateButton.setVisible(False)
75     
76     ####
77     
78     ## Slots ##
79     
80     def addVersion(self):
81         """
82         Slot called when the user add a BeRTOS version.
83         """
84         directory = QFileDialog.getExistingDirectory(self, self.tr("Choose a directory"), "", QFileDialog.ShowDirsOnly | QFileDialog.DontResolveSymlinks)
85         if not directory.isEmpty():
86             self.storeVersion(unicode(directory))
87             self.pageContent.versionList.clear()
88             self.fillVersionList()
89             self.emit(SIGNAL("completeChanged()"))
90     
91     def removeVersion(self):
92         """
93         Slot called when the user remove a BeRTOS version.
94         """
95         item = self.pageContent.versionList.takeItem(self.pageContent.versionList.currentRow())
96         self.deleteVersion(qvariant_converter.getString(item.data(Qt.UserRole)))
97         self.emit(SIGNAL("completeChanged()"))
98     
99     def rowChanged(self):
100         """
101         Slot called when the user select an entry from the version list.
102         """
103         self.emit(SIGNAL("completeChanged()"))
104
105     def updateClicked(self):
106         """
107         Slot called when the user clicks on the 'update' button. It checks for
108         update (TO BE IMPLEMENTED).
109         """
110         pass    
111
112     ####
113     
114     def storeVersion(self, directory):
115         """
116         Stores the directory selected by the user in the QSettings.
117         """
118         versions = self.versions()
119         versions = set(versions + [directory])
120         self.setVersions(list(versions))
121     
122     def deleteVersion(self, directory):
123         """
124         Removes the given directory from the QSettings.
125         """
126         versions = self.versions()
127         versions.remove(directory)
128         self.setVersions(versions)
129     
130     def resetVersionList(self):
131         """
132         Remove all the version entries from the list.
133         """
134         self.pageContent.versionList.clear()
135     
136     def insertListElement(self, directory):
137         """
138         Inserts the given directory in the version list and returns the
139         inserted item.
140         """
141         if bertos_utils.isBertosDir(directory):
142             item = QListWidgetItem(QIcon(":/images/ok.png"), bertos_utils.bertosVersion(directory) + " (\"" + os.path.normpath(directory) + "\")")
143             item.setData(Qt.UserRole, qvariant_converter.convertString(directory))
144             self.pageContent.versionList.addItem(item)
145             return item
146         elif len(directory) > 0:
147             item = QListWidgetItem(QIcon(":/images/warning.png"), "UNKNOWN" + " (\"" + os.path.normpath(directory) + "\")")
148             item.setData(Qt.UserRole, qvariant_converter.convertString(directory))
149             self.pageContent.versionList.addItem(item)
150             return item
151     
152     def fillVersionList(self):
153         """
154         Fills the version list with all the BeRTOS versions founded in the QSettings.
155         """
156         versions = set([])
157         if os.name == "nt":
158             import winreg_importer
159             versions |= set(winreg_importer.getBertosDirs())
160         versions |= set(self.versions())
161         selected = self.projectInfo("SOURCES_PATH")
162         for directory in versions:
163             item = self.insertListElement(directory)
164             if selected and selected == directory:
165                 self.setCurrentItem(item)
166         if not selected:
167             self.setCurrentItem(self.latestVersionItem())
168     
169     def latestVersionItem(self):
170         """
171         Returns the latest BeRTOS version founded.
172         """
173         latest_version_item = QTableWidgetItem("")
174         for index in range(self.pageContent.versionList.count()):
175             item = self.pageContent.versionList.item(index)
176             version = item.text().split(" (")[0]
177             latest = latest_version_item.text().split(" (")[0]
178             if version != "UNKNOWN" and version > latest:
179                 latest_version_item = item
180         return latest_version_item
181     
182     def setCurrentItem(self, item):
183         """
184         Select the given item in the version list.
185         """
186         self.pageContent.versionList.setCurrentItem(item)