9623394d1d423227e6dd09524876fceb5fc4de41
[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 #
32 # Author: Lorenzo Berni <duplo@develer.com>
33 #
34
35 import os
36
37 from PyQt4.QtGui import *
38 from BWizardPage import *
39 import bertos_utils
40
41 from BCpuPage import BCpuPage
42 from BBoardPage import BBoardPage
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             self.setProjectInfo("PROJECT_SRC_PATH", os.path.join(self.projectInfo("PROJECT_PATH"), self.projectInfo("PROJECT_NAME")))
68             self.setProjectInfo("PROJECT_HW_PATH", os.path.join(self.projectInfo("PROJECT_PATH"), self.projectInfo("PROJECT_NAME")))
69             return True
70         else:
71             return False
72     
73     ####
74
75     ## Overloaded BWizardPage methods ##
76
77     def setupUi(self):
78         """
79         Overload of the BWizardPage setupUi method.
80         """
81         self.pageContent.warningLabel.setVisible(False)
82     
83     def connectSignals(self):
84         """
85         Overload of the BWizardPage connectSignals method.
86         """
87         self.connect(self.pageContent.nameEdit, SIGNAL("textChanged(const QString)"), self.nameChanged)
88         self.connect(self.pageContent.directoryEdit, SIGNAL("textChanged(const QString)"), self.directoryChanged)
89         self.connect(self.pageContent.directoryButton, SIGNAL("clicked()"), self.selectDirectory)
90         # self.connect(self.pageContent.customButton, SIGNAL("toggled(bool)"), self.isComplete)
91     
92     ####
93
94     ## Slots ##
95     
96     def nameChanged(self, name):
97         """
98         Slot called when the project name is changed manually by the user.
99         """
100         try:
101             name = unicode(name).encode("ascii")
102         except UnicodeEncodeError:
103             name = self._project_name
104             self.pageContent.nameEdit.setText(name)
105         self._project_name = unicode(name).replace(" ", "_")
106         self.setProjectPath()
107     
108     def directoryChanged(self, directory):
109         """
110         Slot called when the project folder is changed manually by the user.
111         """
112         try:
113             directory = unicode(directory).encode("ascii")
114         except UnicodeEncodeError:
115             directory = self._destination_folder
116             self.pageContent.directoryEdit.setText(directory)
117         self._destination_folder = directory
118         self.setProjectPath()
119
120     def selectDirectory(self):
121         """
122         Slot called when the project folder is changed using the file dialog.
123         """
124         directory = unicode(QFileDialog.getExistingDirectory(self, self.tr("Open Directory"), self.pageContent.directoryEdit.text(), QFileDialog.ShowDirsOnly))
125         if len(directory) > 0:
126             self.pageContent.directoryEdit.setText(QDir.toNativeSeparators(directory))
127
128     ####
129     
130     def initializeAttributes(self):
131         """
132         Initializes the page attributes to the default values.
133         """
134         self._project_name = ""
135         stored_folder = self.defaultFolder()
136         if stored_folder != "":
137             self._destination_folder = stored_folder
138         elif os.name == "nt":
139             def _winGetSpecialFolder(csidl):
140                 from ctypes import windll, create_unicode_buffer
141                 MAX_PATH = 4096
142                 buf = create_unicode_buffer(MAX_PATH)
143                 if not windll.shell32.SHGetSpecialFolderPathW(0, buf, csidl, False):
144                     raise WindowsError("cannot get special folder location")
145                 return buf.value
146             CSIDL_PERSONAL = 5
147             self._destination_folder = _winGetSpecialFolder(CSIDL_PERSONAL)
148         else:
149             self._destination_folder = os.path.expanduser("~")
150         self.pageContent.directoryEdit.setText(self._destination_folder)
151     
152     def setProjectPath(self):
153         """
154         Analyzes the page attributes and generates the path string.
155         """
156         if self._destination_folder != "" and self._project_name <> "":
157             if not self._destination_folder.endswith(os.sep):
158                 self._destination_folder += os.sep
159             self.pageContent.projectPath.setText(QDir.toNativeSeparators(self._destination_folder + self._project_name))
160             if os.path.exists(self._destination_folder + self._project_name):
161                 self.pageContent.warningLabel.setVisible(True)
162                 self.pageContent.warningLabel.setText(self.tr("<font color='#FF0000'>Warning: the selected directory exists, \
163                     it will be destroyed with all contained subdirectories and files...</font>"))
164             else:
165                 self.pageContent.warningLabel.setVisible(False)
166                 self.pageContent.warningLabel.setText("")
167         else:
168             self.pageContent.projectPath.setText("None")
169             self.pageContent.warningLabel.setVisible(False)
170             self.pageContent.warningLabel.setText("")
171         self.emit(SIGNAL("completeChanged()"))