|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2002 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to add a file to the project. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt4.QtCore import * |
|
13 from PyQt4.QtGui import * |
|
14 |
|
15 from E4Gui.E4Completers import E4DirCompleter |
|
16 |
|
17 from Ui_AddFileDialog import Ui_AddFileDialog |
|
18 |
|
19 import Utilities |
|
20 |
|
21 class AddFileDialog(QDialog, Ui_AddFileDialog): |
|
22 """ |
|
23 Class implementing a dialog to add a file to the project. |
|
24 """ |
|
25 def __init__(self, pro, parent = None, filter = None, name = None, |
|
26 startdir = None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param pro reference to the project object |
|
31 @param parent parent widget of this dialog (QWidget) |
|
32 @param filter filter specification for the file to add (string) |
|
33 @param name name of this dialog (string) |
|
34 @param startdir start directory for the selection dialog |
|
35 """ |
|
36 QDialog.__init__(self, parent) |
|
37 if name: |
|
38 self.setObjectName(name) |
|
39 self.setupUi(self) |
|
40 |
|
41 self.targetDirCompleter = E4DirCompleter(self.targetDirEdit) |
|
42 |
|
43 self.targetDirEdit.setText(pro.ppath) |
|
44 self.filter = filter |
|
45 self.ppath = pro.ppath |
|
46 self.startdir = startdir |
|
47 self.filetypes = pro.pdata["FILETYPES"] # save a reference to the filetypes dict |
|
48 |
|
49 if self.filter is not None and self.filter != 'source': |
|
50 self.sourcecodeCheckBox.hide() |
|
51 |
|
52 @pyqtSlot() |
|
53 def on_targetDirButton_clicked(self): |
|
54 """ |
|
55 Private slot to display a directory selection dialog. |
|
56 """ |
|
57 startdir = self.targetDirEdit.text() |
|
58 if not startdir and self.startdir is not None: |
|
59 startdir = self.startdir |
|
60 directory = QFileDialog.getExistingDirectory( |
|
61 self, |
|
62 self.trUtf8("Select target directory"), |
|
63 startdir, |
|
64 QFileDialog.Options(QFileDialog.Option(0))) |
|
65 |
|
66 if directory: |
|
67 self.targetDirEdit.setText(Utilities.toNativeSeparators(directory)) |
|
68 |
|
69 @pyqtSlot() |
|
70 def on_sourceFileButton_clicked(self): |
|
71 """ |
|
72 Private slot to display a file selection dialog. |
|
73 """ |
|
74 dir = self.sourceFileEdit.text().split(os.pathsep, 1)[0] |
|
75 if not dir: |
|
76 if self.startdir is not None: |
|
77 dir = self.startdir |
|
78 else: |
|
79 dir = self.targetDirEdit.text() |
|
80 if self.filter is None: |
|
81 patterns = { |
|
82 "SOURCES" : [], |
|
83 "FORMS" : [], |
|
84 "RESOURCES" : [], |
|
85 "INTERFACES" : [], |
|
86 "TRANSLATIONS" : [], |
|
87 } |
|
88 for pattern, filetype in self.filetypes.items(): |
|
89 if filetype in patterns: |
|
90 patterns[filetype].append(pattern) |
|
91 dfilter = self.trUtf8( |
|
92 "Source Files ({0});;" |
|
93 "Forms Files ({1});;" |
|
94 "Resource Files ({2});;" |
|
95 "Interface Files ({3});;" |
|
96 "Translation Files ({4});;" |
|
97 "All Files (*)").format( |
|
98 " ".join(patterns["SOURCES"]), |
|
99 " ".join(patterns["FORMS"]), |
|
100 " ".join(patterns["RESOURCES"]), |
|
101 " ".join(patterns["INTERFACES"]), |
|
102 " ".join(patterns["TRANSLATIONS"])) |
|
103 caption = self.trUtf8("Select Files") |
|
104 elif self.filter == 'form': |
|
105 patterns = [] |
|
106 for pattern, filetype in self.filetypes.items(): |
|
107 if filetype == "FORMS": |
|
108 patterns.append(pattern) |
|
109 dfilter = self.trUtf8("Forms Files ({0})")\ |
|
110 .format(" ".join(patterns)) |
|
111 caption = self.trUtf8("Select user-interface files") |
|
112 elif self.filter == "resource": |
|
113 patterns = [] |
|
114 for pattern, filetype in self.filetypes.items(): |
|
115 if filetype == "RESOURCES": |
|
116 patterns.append(pattern) |
|
117 dfilter = self.trUtf8("Resource Files ({0})")\ |
|
118 .format(" ".join(patterns)) |
|
119 caption = self.trUtf8("Select resource files") |
|
120 elif self.filter == 'source': |
|
121 patterns = [] |
|
122 for pattern, filetype in self.filetypes.items(): |
|
123 if filetype == "SOURCES": |
|
124 patterns.append(pattern) |
|
125 dfilter = self.trUtf8("Source Files ({0});;All Files (*)")\ |
|
126 .format(" ".join(patterns)) |
|
127 caption = self.trUtf8("Select source files") |
|
128 elif self.filter == 'interface': |
|
129 patterns = [] |
|
130 for pattern, filetype in self.filetypes.items(): |
|
131 if filetype == "INTERFACES": |
|
132 patterns.append(pattern) |
|
133 dfilter = self.trUtf8("Interface Files ({0})")\ |
|
134 .format(" ".join(patterns)) |
|
135 caption = self.trUtf8("Select interface files") |
|
136 elif self.filter == 'translation': |
|
137 patterns = [] |
|
138 for pattern, filetype in self.filetypes.items(): |
|
139 if filetype == "TRANSLATIONS": |
|
140 patterns.append(pattern) |
|
141 dfilter = self.trUtf8("Translation Files ({0})")\ |
|
142 .format(" ".join(patterns)) |
|
143 caption = self.trUtf8("Select translation files") |
|
144 elif self.filter == 'others': |
|
145 dfilter = self.trUtf8("All Files (*)") |
|
146 caption = self.trUtf8("Select files") |
|
147 else: |
|
148 return |
|
149 |
|
150 fnames = QFileDialog.getOpenFileNames(self, caption, dir, dfilter) |
|
151 |
|
152 if len(fnames): |
|
153 self.sourceFileEdit.setText(Utilities.toNativeSeparators( |
|
154 os.pathsep.join(fnames))) |
|
155 |
|
156 @pyqtSlot(str) |
|
157 def on_sourceFileEdit_textChanged(self, sfile): |
|
158 """ |
|
159 Private slot to handle the source file text changed. |
|
160 |
|
161 If the entered source directory is a subdirectory of the current |
|
162 projects main directory, the target directory path is synchronized. |
|
163 It is assumed, that the user wants to add a bunch of files to |
|
164 the project in place. |
|
165 |
|
166 @param sfile the text of the source file line edit (string) |
|
167 """ |
|
168 sfile = sfile.split(os.pathsep, 1)[0] |
|
169 if sfile.startswith(self.ppath): |
|
170 if os.path.isdir(sfile): |
|
171 dir = sfile |
|
172 else: |
|
173 dir = os.path.dirname(sfile) |
|
174 self.targetDirEdit.setText(dir) |
|
175 |
|
176 def getData(self): |
|
177 """ |
|
178 Public slot to retrieve the dialogs data. |
|
179 |
|
180 @return tuple of three values (list of string, string, boolean) giving the |
|
181 source files, the target directory and a flag telling, whether |
|
182 the files shall be added as source code |
|
183 """ |
|
184 return (self.sourceFileEdit.text().split(os.pathsep), |
|
185 self.targetDirEdit.text(), self.sourcecodeCheckBox.isChecked()) |