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