eric6/Project/AddFileDialog.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2002 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a dialog to add a file to the project.
8 """
9
10 from __future__ import unicode_literals
11
12 import os
13
14 from PyQt5.QtCore import pyqtSlot
15 from PyQt5.QtWidgets import QDialog
16
17 from E5Gui.E5PathPicker import E5PathPickerModes
18
19 from .Ui_AddFileDialog import Ui_AddFileDialog
20
21
22 class AddFileDialog(QDialog, Ui_AddFileDialog):
23 """
24 Class implementing a dialog to add a file to the project.
25 """
26 def __init__(self, pro, parent=None, fileTypeFilter=None, name=None,
27 startdir=None):
28 """
29 Constructor
30
31 @param pro reference to the project object
32 @param parent parent widget of this dialog (QWidget)
33 @param fileTypeFilter filter specification for the file to add (string)
34 @param name name of this dialog (string)
35 @param startdir start directory for the selection dialog
36 """
37 super(AddFileDialog, self).__init__(parent)
38 if name:
39 self.setObjectName(name)
40 self.setupUi(self)
41
42 self.sourceFilesPicker.setMode(E5PathPickerModes.OpenFilesMode)
43 self.targetDirPicker.setMode(E5PathPickerModes.DirectoryMode)
44 self.targetDirPicker.setDefaultDirectory(startdir)
45
46 self.targetDirPicker.setText(pro.ppath)
47 self.fileTypeFilter = fileTypeFilter
48 self.ppath = pro.ppath
49 self.startdir = startdir
50 self.filetypes = pro.pdata["FILETYPES"]
51 # save a reference to the filetypes dict
52
53 if self.fileTypeFilter is not None:
54 self.sourcecodeCheckBox.hide()
55
56 msh = self.minimumSizeHint()
57 self.resize(max(self.width(), msh.width()), msh.height())
58
59 @pyqtSlot()
60 def on_sourceFilesPicker_aboutToShowPathPickerDialog(self):
61 """
62 Private slot to perform actions before the source files selection
63 dialog is shown.
64 """
65 path = self.targetDirPicker.text()
66 if not path:
67 path = self.startdir
68 self.sourceFilesPicker.setDefaultDirectory(path)
69
70 if self.fileTypeFilter is None:
71 patterns = {
72 "SOURCES": [],
73 "FORMS": [],
74 "RESOURCES": [],
75 "INTERFACES": [],
76 "PROTOCOLS": [],
77 "TRANSLATIONS": [],
78 }
79 for pattern, filetype in list(self.filetypes.items()):
80 if filetype in patterns:
81 patterns[filetype].append(pattern)
82 dfilter = self.tr(
83 "Source Files ({0});;"
84 "Forms Files ({1});;"
85 "Resource Files ({2});;"
86 "Interface Files ({3});;"
87 "Protocol Files ({4});;"
88 "Translation Files ({5});;"
89 "All Files (*)")\
90 .format(
91 " ".join(patterns["SOURCES"]),
92 " ".join(patterns["FORMS"]),
93 " ".join(patterns["RESOURCES"]),
94 " ".join(patterns["INTERFACES"]),
95 " ".join(patterns["PROTOCOLS"]),
96 " ".join(patterns["TRANSLATIONS"]))
97 caption = self.tr("Select Files")
98 elif self.fileTypeFilter == 'form':
99 patterns = []
100 for pattern, filetype in list(self.filetypes.items()):
101 if filetype == "FORMS":
102 patterns.append(pattern)
103 dfilter = self.tr("Forms Files ({0})")\
104 .format(" ".join(patterns))
105 caption = self.tr("Select user-interface files")
106 elif self.fileTypeFilter == "resource":
107 patterns = []
108 for pattern, filetype in list(self.filetypes.items()):
109 if filetype == "RESOURCES":
110 patterns.append(pattern)
111 dfilter = self.tr("Resource Files ({0})")\
112 .format(" ".join(patterns))
113 caption = self.tr("Select resource files")
114 elif self.fileTypeFilter == 'source':
115 patterns = []
116 for pattern, filetype in list(self.filetypes.items()):
117 if filetype == "SOURCES":
118 patterns.append(pattern)
119 dfilter = self.tr("Source Files ({0});;All Files (*)")\
120 .format(" ".join(patterns))
121 caption = self.tr("Select source files")
122 elif self.fileTypeFilter == 'interface':
123 patterns = []
124 for pattern, filetype in list(self.filetypes.items()):
125 if filetype == "INTERFACES":
126 patterns.append(pattern)
127 dfilter = self.tr("Interface Files ({0})")\
128 .format(" ".join(patterns))
129 caption = self.tr("Select interface files")
130 elif self.fileTypeFilter == 'protocol':
131 patterns = []
132 for pattern, filetype in list(self.filetypes.items()):
133 if filetype == "PROTOCOLS":
134 patterns.append(pattern)
135 dfilter = self.tr("Protocol Files ({0})")\
136 .format(" ".join(patterns))
137 caption = self.tr("Select protocol files")
138 elif self.fileTypeFilter == 'translation':
139 patterns = []
140 for pattern, filetype in list(self.filetypes.items()):
141 if filetype == "TRANSLATIONS":
142 patterns.append(pattern)
143 dfilter = self.tr("Translation Files ({0})")\
144 .format(" ".join(patterns))
145 caption = self.tr("Select translation files")
146 elif self.fileTypeFilter == 'others':
147 dfilter = self.tr("All Files (*)")
148 caption = self.tr("Select files")
149 else:
150 dfilter = ""
151 caption = ""
152
153 self.sourceFilesPicker.setWindowTitle(caption)
154 self.sourceFilesPicker.setFilters(dfilter)
155
156 @pyqtSlot(str)
157 def on_sourceFilesPicker_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 picker (string)
167 """
168 sfile = self.sourceFilesPicker.firstPath()
169 if sfile.startswith(self.ppath):
170 if os.path.isdir(sfile):
171 directory = sfile
172 else:
173 directory = os.path.dirname(sfile)
174 self.targetDirPicker.setText(directory)
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)
181 giving the source files, the target directory and a flag
182 telling, whether the files shall be added as source code
183 """
184 return (
185 self.sourceFilesPicker.paths(),
186 self.targetDirPicker.text(),
187 self.sourcecodeCheckBox.isChecked())

eric ide

mercurial