Add stub of new interface with radio for custom/predefined boards.
[bertos.git] / wizard / BFolderPage.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
42 from BVersionPage import BVersionPage
43
44 from const import *
45
46 class BFolderPage(BWizardPage):
47     """
48     Initial page of the wizard. Permit to select the project name and the directory
49     where the project will be created.
50     """
51     
52     def __init__(self):
53         BWizardPage.__init__(self, UI_LOCATION + "/dir_select.ui")
54         self.setTitle(self.tr("Select the project name"))
55         self.initializeAttributes()
56     
57     ## Overloaded QWizardPage methods ##
58
59     def isComplete(self):
60         """
61         Overload of the QWizardPage isComplete method.
62         """
63         self.setDefaultFolder(self._destination_folder)
64         if self.pageContent.projectPath.text() != "None":
65             self.setProjectInfo("PROJECT_PATH", unicode(self.pageContent.projectPath.text()))
66             self.setProjectInfo("PROJECT_NAME", os.path.basename(unicode(self.pageContent.projectPath.text())))
67             return True
68         else:
69             return False
70
71     def nextId(self):
72         """
73         Overload of the QWizardPage nextId method.
74         """
75         return self.wizard().pageIndex(self.next_page)
76         
77     
78     ####
79
80     ## Overloaded BWizardPage methods ##
81
82     def setupUi(self):
83         """
84         Overload of the BWizardPage setupUi method.
85         """
86         self.pageContent.warningLabel.setVisible(False)
87     
88     def connectSignals(self):
89         """
90         Overload of the BWizardPage connectSignals method.
91         """
92         self.connect(self.pageContent.nameEdit, SIGNAL("textChanged(const QString)"), self.nameChanged)
93         self.connect(self.pageContent.directoryEdit, SIGNAL("textChanged(const QString)"), self.directoryChanged)
94         self.connect(self.pageContent.directoryButton, SIGNAL("clicked()"), self.selectDirectory)
95     
96     ####
97
98     ## Slots ##
99     
100     def nameChanged(self, name):
101         """
102         Slot called when the project name is changed manually by the user.
103         """
104         try:
105             name = unicode(name).encode("ascii")
106         except UnicodeEncodeError:
107             name = self._project_name
108             self.pageContent.nameEdit.setText(name)
109         self._project_name = unicode(name).replace(" ", "_")
110         self.setProjectPath()
111     
112     def directoryChanged(self, directory):
113         """
114         Slot called when the project folder is changed manually by the user.
115         """
116         try:
117             directory = unicode(directory).encode("ascii")
118         except UnicodeEncodeError:
119             directory = self._destination_folder
120             self.pageContent.directoryEdit.setText(directory)
121         self._destination_folder = directory
122         self.setProjectPath()
123
124     def selectDirectory(self):
125         """
126         Slot called when the project folder is changed using the file dialog.
127         """
128         directory = unicode(QFileDialog.getExistingDirectory(self, self.tr("Open Directory"), self.pageContent.directoryEdit.text(), QFileDialog.ShowDirsOnly))
129         if len(directory) > 0:
130             self.pageContent.directoryEdit.setText(QDir.toNativeSeparators(directory))
131
132     ####
133
134     @property
135     def next_page(self):
136         """
137         Contains the next page class.
138         """
139         # Actually it does nothing.
140         if self.pageContent.customButton.isChecked():
141             return BVersionPage
142         else:
143             # TODO: change it with the predefined board selection page.
144             # return BBoardPage
145             return BVersionPage
146     
147     def initializeAttributes(self):
148         """
149         Initializes the page attributes to the default values.
150         """
151         self._project_name = ""
152         stored_folder = self.defaultFolder()
153         if stored_folder != "":
154             self._destination_folder = stored_folder
155         elif os.name == "nt":
156             from win32com.shell import shell, shellcon
157             self._destination_folder = shell.SHGetFolderPath(0, shellcon.CSIDL_PERSONAL, 0, 0)
158             del shell
159             del shellcon
160         else:
161             self._destination_folder = os.path.expanduser("~")
162         self.pageContent.directoryEdit.setText(self._destination_folder)
163     
164     def setProjectPath(self):
165         """
166         Analyzes the page attributes and generates the path string.
167         """
168         if self._destination_folder != "" and self._project_name <> "":
169             if not self._destination_folder.endswith(os.sep):
170                 self._destination_folder += os.sep
171             self.pageContent.projectPath.setText(QDir.toNativeSeparators(self._destination_folder + self._project_name))
172             if os.path.exists(self._destination_folder + self._project_name):
173                 self.pageContent.warningLabel.setVisible(True)
174                 self.pageContent.warningLabel.setText(self.tr("<font color='#FF0000'>Warning: the selected directory exists, \
175                     it will be destroyed with all contained subdirectories and files...</font>"))
176             else:
177                 self.pageContent.warningLabel.setVisible(False)
178                 self.pageContent.warningLabel.setText("")
179         else:
180             self.pageContent.projectPath.setText("None")
181             self.pageContent.warningLabel.setVisible(False)
182             self.pageContent.warningLabel.setText("")
183         self.emit(SIGNAL("completeChanged()"))