eric7/Project/AddFileDialog.py

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

eric ide

mercurial