Use the directory written in the lineedit as base directory for the file dialog
[bertos.git] / wizard / BFolderPage.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 #
4 # Copyright 2008 Develer S.r.l. (http://www.develer.com/)
5 # All rights reserved.
6 #
7 # $Id$
8 #
9 # Author: Lorenzo Berni <duplo@develer.com>
10 #
11
12 import os
13
14 from PyQt4.QtGui import *
15 from BWizardPage import *
16 import bertos_utils
17
18 from const import *
19
20 class BFolderPage(BWizardPage):
21     """
22     Initial page of the wizard. Permit to select the project name and the directory
23     where the project will be created.
24     """
25     
26     def __init__(self):
27         BWizardPage.__init__(self, UI_LOCATION + "/dir_select.ui")
28         self.setTitle(self.tr("Select the project name"))
29         self.initializeAttributes()
30     
31     ## Overloaded QWizardPage methods ##
32
33     def isComplete(self):
34         """
35         Overload of the QWizardPage isComplete method.
36         """
37         self.setDefaultFolder(self._destination_folder)
38         if self.pageContent.projectPath.text() != "None":
39             self.setProjectInfo("PROJECT_PATH", unicode(self.pageContent.projectPath.text()))
40             return True
41         else:
42             return False
43     
44     ####
45
46     ## Overloaded BWizardPage methods ##
47
48     def setupUi(self):
49         """
50         Overload of the BWizardPage setupUi method.
51         """
52         self.pageContent.warningLabel.setVisible(False)
53     
54     def connectSignals(self):
55         """
56         Overload of the BWizardPage connectSignals method.
57         """
58         self.connect(self.pageContent.nameEdit, SIGNAL("textChanged(const QString)"), self.nameChanged)
59         self.connect(self.pageContent.directoryEdit, SIGNAL("textChanged(const QString)"), self.directoryChanged)
60         self.connect(self.pageContent.directoryButton, SIGNAL("clicked()"), self.selectDirectory)
61     
62     ####
63
64     ## Slots ##
65     
66     def nameChanged(self, name):
67         """
68         Slot called when the project name is changed manually by the user.
69         """
70         self._project_name = str(name).replace(" ", "_")
71         self.setProjectPath()
72     
73     def directoryChanged(self, directory):
74         """
75         Slot called when the project folder is changed manually by the user.
76         """
77         self._destination_folder = str(QDir.toNativeSeparators(directory))
78         self.setProjectPath()
79
80     def selectDirectory(self):
81         """
82         Slot called when the project folder is changed using the file dialog.
83         """
84         directory = unicode(QFileDialog.getExistingDirectory(self, self.tr("Open Directory"), self.pageContent.directoryEdit.text(), QFileDialog.ShowDirsOnly))
85         if len(directory) > 0:
86             self.pageContent.directoryEdit.setText(directory)
87
88     ####
89     
90     def initializeAttributes(self):
91         """
92         Initializes the page attributes to the default values.
93         """
94         self._project_name = ""
95         stored_folder = self.defaultFolder()
96         if stored_folder != "":
97             self._destination_folder = stored_folder
98         elif os.name == "nt":
99             from win32com.shell import shell, shellcon
100             self._destination_folder = shell.SHGetFolderPath(0, shellcon.CSIDL_PERSONAL, 0, 0)
101             del shell
102             del shellcon
103         else:
104             self._destination_folder = os.path.expanduser("~")
105         self.pageContent.directoryEdit.setText(self._destination_folder)
106     
107     def setProjectPath(self):
108         """
109         Analyzes the page attributes and generates the path string.
110         """
111         if self._destination_folder != "" and self._project_name <> "":
112             if not self._destination_folder.endswith(os.sep):
113                 self._destination_folder += os.sep
114             self.pageContent.projectPath.setText(QDir.toNativeSeparators(self._destination_folder + self._project_name))
115             if os.path.exists(self._destination_folder + self._project_name):
116                 self.pageContent.warningLabel.setVisible(True)
117                 self.pageContent.warningLabel.setText(self.tr("<font color='#FF0000'>Warning: the selected directory exists, \
118                     it will be destroyed with all contained subdirectories and files...</font>"))
119             else:
120                 self.pageContent.warningLabel.setVisible(False)
121                 self.pageContent.warningLabel.setText("")
122         else:
123             self.pageContent.projectPath.setText("None")
124             self.pageContent.warningLabel.setVisible(False)
125             self.pageContent.warningLabel.setText("")
126         self.emit(SIGNAL("completeChanged()"))