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/)
32 # Author: Lorenzo Berni <duplo@develer.com>
37 from PyQt4.QtGui import *
38 from BWizardPage import *
40 import qvariant_converter
44 class BVersionPage(BWizardPage):
46 Page of the wizard that permits to choose which BeRTOS version the user wants
47 to use. This page show some pieces of information about the version.
50 def __init__(self, edit=False):
52 BWizardPage.__init__(self, UI_LOCATION + "/bertos_versions.ui")
53 self.setTitle(self.tr("Select BeRTOS version"))
54 self.setSubTitle(self.tr("Your project will be created with the specified BeRTOS version"))
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("BERTOS_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("currentItemChanged(QListWidgetItem *, QListWidgetItem*)"), 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)
86 def reloadData(self, previous_id=None):
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())
121 self.deleteVersion(qvariant_converter.getString(item.data(Qt.UserRole)))
122 self.emit(SIGNAL("completeChanged()"))
124 def rowChanged(self):
126 Slot called when the user select an entry from the version list.
128 if self.isDefaultVersion(self.currentVersion()):
129 self.disableRemoveButton()
131 self.enableRemoveButton()
132 self.emit(SIGNAL("completeChanged()"))
134 def updateClicked(self):
136 Slot called when the user clicks on the 'update' button. It checks for
137 update (TO BE IMPLEMENTED).
143 def storeVersion(self, directory):
145 Stores the directory selected by the user in the QSettings.
147 versions = self.versions()
148 versions = set(versions + [directory])
149 self.setVersions(list(versions))
151 def deleteVersion(self, directory):
153 Removes the given directory from the QSettings.
155 versions = [os.path.normpath(path) for path in self.versions()]
156 versions.remove(os.path.normpath(directory))
157 self.setVersions(versions)
159 def resetVersionList(self):
161 Remove all the version entries from the list.
163 self.pageContent.versionList.clear()
165 def insertListElement(self, directory):
167 Inserts the given directory in the version list and returns the
170 if bertos_utils.isBertosDir(directory):
171 item = QListWidgetItem(QIcon(":/images/ok.png"), bertos_utils.bertosVersion(directory) + " (\"" + os.path.normpath(directory) + "\")")
172 item.setData(Qt.UserRole, qvariant_converter.convertString(directory))
173 self.pageContent.versionList.addItem(item)
175 elif len(directory) > 0:
176 item = QListWidgetItem(QIcon(":/images/warning.png"), "UNKNOWN" + " (\"" + os.path.normpath(directory) + "\")")
177 item.setData(Qt.UserRole, qvariant_converter.convertString(directory))
178 self.pageContent.versionList.addItem(item)
181 def fillVersionList(self):
183 Fills the version list with all the BeRTOS versions founded in the QSettings.
187 import winreg_importer
188 versions |= set([os.path.normpath(dir) for dir in winreg_importer.getBertosDirs()])
189 versions |= set([os.path.normpath(dir) for dir in self.versions()])
190 selected = self.projectInfo("BERTOS_PATH")
191 for directory in versions:
192 item = self.insertListElement(directory)
193 if selected and selected == directory:
194 self.setCurrentItem(item)
196 latest_version_item = self.latestVersionItem()
197 if latest_version_item:
198 self.setCurrentItem(latest_version_item)
200 def disableRemoveButton(self):
202 Disable the Remove button.
204 self.pageContent.removeButton.setEnabled(False)
206 def enableRemoveButton(self):
208 Enable the Remove button.
210 self.pageContent.removeButton.setEnabled(True)
212 def latestVersionItem(self):
214 Returns the latest BeRTOS version founded.
216 latest_version_item = None
217 for index in range(self.pageContent.versionList.count()):
218 item = self.pageContent.versionList.item(index)
219 if not latest_version_item:
220 latest_version_item = item
221 version = item.text().split(" (")[0]
222 latest = latest_version_item.text().split(" (")[0]
223 if version != "UNKNOWN" and version > latest:
224 latest_version_item = item
225 return latest_version_item
227 def setCurrentItem(self, item):
229 Select the given item in the version list.
231 self.pageContent.versionList.setCurrentItem(item)
233 def currentItem(self):
235 Returns the current selected item.
237 return self.pageContent.versionList.currentItem()
239 def currentVersion(self):
241 Return the path of the selected version.
243 current = self.currentItem()
245 return qvariant_converter.getString(current.data(Qt.UserRole))
249 def isDefaultVersion(self, version):
251 Returns True if the given version is one of the default versions.
254 import winreg_importer
255 if version in winreg_importer.getBertosDirs():