4 # This file is part of BeRTOS.
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.
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.
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
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.
29 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
33 # Author: Lorenzo Berni <duplo@develer.com>
38 from PyQt4.QtGui import *
39 from BWizardPage import *
41 import qvariant_converter
45 class BVersionPage(BWizardPage):
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.
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"))
56 ## Overloaded QWizardPage methods ##
60 Overload of the QWizardPage isComplete method.
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)
74 ## Overloaded BWizardPage methods ##
76 def connectSignals(self):
78 Overload of the BWizardPage connectSignals method.
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)
88 Overload of the BWizardPage reloadData method.
90 self.resetVersionList()
91 self.pageContent.versionList.setCurrentRow(-1)
92 self.fillVersionList()
96 Overload of the BWizardPage setupUi method.
98 self.pageContent.updateButton.setVisible(False)
104 def addVersion(self):
106 Slot called when the user add a BeRTOS version.
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()"))
115 def removeVersion(self):
117 Slot called when the user remove a BeRTOS version.
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()"))
123 def rowChanged(self):
125 Slot called when the user select an entry from the version list.
127 if self.isDefaultVersion(self.currentVersion()):
128 self.disableRemoveButton()
130 self.enableRemoveButton()
131 self.emit(SIGNAL("completeChanged()"))
133 def updateClicked(self):
135 Slot called when the user clicks on the 'update' button. It checks for
136 update (TO BE IMPLEMENTED).
142 def storeVersion(self, directory):
144 Stores the directory selected by the user in the QSettings.
146 versions = self.versions()
147 versions = set(versions + [directory])
148 self.setVersions(list(versions))
150 def deleteVersion(self, directory):
152 Removes the given directory from the QSettings.
154 versions = self.versions()
155 versions.remove(directory)
156 self.setVersions(versions)
158 def resetVersionList(self):
160 Remove all the version entries from the list.
162 self.pageContent.versionList.clear()
164 def insertListElement(self, directory):
166 Inserts the given directory in the version list and returns the
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)
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)
180 def fillVersionList(self):
182 Fills the version list with all the BeRTOS versions founded in the QSettings.
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)
195 latest_version_item = self.latestVersionItem()
196 if latest_version_item:
197 self.setCurrentItem(latest_version_item)
199 def disableRemoveButton(self):
201 Disable the Remove button.
203 self.pageContent.removeButton.setEnabled(False)
205 def enableRemoveButton(self):
207 Enable the Remove button.
209 self.pageContent.removeButton.setEnabled(True)
211 def latestVersionItem(self):
213 Returns the latest BeRTOS version founded.
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
226 def setCurrentItem(self, item):
228 Select the given item in the version list.
230 self.pageContent.versionList.setCurrentItem(item)
232 def currentItem(self):
234 Returns the current selected item.
236 return self.pageContent.versionList.currentItem()
238 def currentVersion(self):
240 Return the path of the selected version.
242 current = self.currentItem()
244 return qvariant_converter.getString(current.data(Qt.UserRole))
248 def isDefaultVersion(self, version):
250 Returns True if the given version is one of the default versions.
253 import winreg_importer
254 if version in winreg_importer.getBertosDirs():