|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a path picker widget. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 |
|
14 try: |
|
15 from enum import Enum |
|
16 except ImportError: |
|
17 from ThirdParty.enum import Enum |
|
18 |
|
19 from PyQt5.QtCore import Qt |
|
20 from PyQt5.QtWidgets import QWidget, QHBoxLayout, QToolButton |
|
21 |
|
22 from . import E5FileDialog |
|
23 from .E5LineEdit import E5ClearableLineEdit |
|
24 from .E5Completers import E5FileCompleter, E5DirCompleter |
|
25 |
|
26 import UI.PixmapCache |
|
27 import Utilities |
|
28 |
|
29 |
|
30 class E5PathPickerModes(Enum): |
|
31 """ |
|
32 Class implementing the path picker modes. |
|
33 """ |
|
34 OpenFileMode = 0 |
|
35 OpenFilesMode = 1 |
|
36 SaveFileMode = 2 |
|
37 DiretoryMode = 3 |
|
38 |
|
39 |
|
40 class E5PathPicker(QWidget): |
|
41 """ |
|
42 Class implementing a path picker widget consisting of a line edit and a |
|
43 tool button to open a file dialog. |
|
44 """ |
|
45 DefaultMode = E5PathPickerModes.OpenFileMode |
|
46 |
|
47 def __init__(self, parent=None): |
|
48 """ |
|
49 Constructor |
|
50 |
|
51 @param parent reference to the parent widget |
|
52 @type QWidget |
|
53 """ |
|
54 super(E5PathPicker, self).__init__(parent) |
|
55 |
|
56 self.__mode = E5PathPicker.DefaultMode |
|
57 self.__editorEnabled = True |
|
58 |
|
59 self.__completer = None |
|
60 self.__filters = "" |
|
61 self.__defaultDirectory = "" |
|
62 self.__windowTitle = "" |
|
63 |
|
64 self.__layout = QHBoxLayout() |
|
65 self.__layout.setSpacing(0) |
|
66 self.__layout.setContentsMargins(0, 0, 0, 0) |
|
67 self.setLayout(self.__layout) |
|
68 |
|
69 self.__editor = E5ClearableLineEdit(self, self.tr("Enter Path Name")) |
|
70 |
|
71 self.__button = QToolButton(self) |
|
72 self.__button.setToolButtonStyle(Qt.ToolButtonIconOnly) |
|
73 self.__button.setIcon(UI.PixmapCache.getIcon("open.png")) |
|
74 |
|
75 self.__layout.addWidget(self.__editor) |
|
76 self.__layout.addWidget(self.__button) |
|
77 |
|
78 self.__button.clicked.connect(self.__showPathPickerDialog) |
|
79 |
|
80 def setMode(self, mode): |
|
81 """ |
|
82 Public method to set the path picker mode. |
|
83 |
|
84 @param mode picker mode |
|
85 @type E5PathPickerModes |
|
86 """ |
|
87 assert mode in E5PathPickerModes |
|
88 |
|
89 oldMode = self.__mode |
|
90 self.__mode = mode |
|
91 |
|
92 if mode != oldMode: |
|
93 # Remove current completer |
|
94 self.__editor.setCompleter(None) |
|
95 self.__completer = None |
|
96 |
|
97 # Set a new completer |
|
98 if mode == E5PathPicker.DiretoryMode: |
|
99 self.__completer = E5DirCompleter(self.__editor) |
|
100 else: |
|
101 self.__completer = E5FileCompleter(self.__editor) |
|
102 |
|
103 def mode(self): |
|
104 """ |
|
105 Public method to get the path picker mode. |
|
106 |
|
107 @return path picker mode |
|
108 @rtype E5PathPickerModes |
|
109 """ |
|
110 return self.__mode |
|
111 |
|
112 def setText(self, path): |
|
113 """ |
|
114 Public method to set the current path. |
|
115 |
|
116 @param path path to be set |
|
117 @type str |
|
118 """ |
|
119 if self.__mode == E5PathPickerModes.OpenFilesMode: |
|
120 self.__editor.setText(path) |
|
121 else: |
|
122 self.__editor.setText(Utilities.toNativeSeparators(path)) |
|
123 |
|
124 def text(self): |
|
125 """ |
|
126 Public method to get the current path. |
|
127 |
|
128 @return current path |
|
129 @rtype str |
|
130 """ |
|
131 if self.__mode == E5PathPickerModes.OpenFilesMode: |
|
132 return self.__editor.text() |
|
133 else: |
|
134 return os.path.expanduser( |
|
135 Utilities.toNativeSeparators(self.__editor.text())) |
|
136 |
|
137 def setPath(self, path): |
|
138 """ |
|
139 Public method to set the current path. |
|
140 |
|
141 @param path path to be set |
|
142 @type str |
|
143 """ |
|
144 self.setText(path) |
|
145 |
|
146 def path(self): |
|
147 """ |
|
148 Public method to get the current path. |
|
149 |
|
150 @return current path |
|
151 @rtype str |
|
152 """ |
|
153 return self.text() |
|
154 |
|
155 def setEditorEnabled(self, enable): |
|
156 """ |
|
157 Public method to set the path editor's enabled state. |
|
158 |
|
159 @param enable flag indicating the enable state |
|
160 @type bool |
|
161 """ |
|
162 if enable != self.__editorEnabled: |
|
163 self.__editorEnabled = enable |
|
164 self.__editor.setEnabled(enable) |
|
165 |
|
166 def editorEnabled(self): |
|
167 """ |
|
168 Public method to get the path editor's enabled state. |
|
169 |
|
170 @return flag indicating the enabled state |
|
171 @rtype bool |
|
172 """ |
|
173 return self.__editorEnabled |
|
174 |
|
175 def setDefaultDirectory(self, directory): |
|
176 """ |
|
177 Public method to set the default directory. |
|
178 |
|
179 @param directory default directory |
|
180 @type str |
|
181 """ |
|
182 self.__defaultDirectory = directory |
|
183 |
|
184 def defaultDirectory(self): |
|
185 """ |
|
186 Public method to get the default directory. |
|
187 |
|
188 @return default directory |
|
189 @rtype str |
|
190 """ |
|
191 return self.__defaultDirectory |
|
192 |
|
193 def setWindowTitle(self, title): |
|
194 """ |
|
195 Public method to set the path picker dialog window title. |
|
196 |
|
197 @param title window title |
|
198 @type str |
|
199 """ |
|
200 self.__windowTitle = title |
|
201 |
|
202 def windowTitle(self): |
|
203 """ |
|
204 Public method to get the path picker dialog's window title. |
|
205 |
|
206 @return window title |
|
207 @rtype str |
|
208 """ |
|
209 return self.__windowTitle |
|
210 |
|
211 def setFilters(self, filters): |
|
212 """ |
|
213 Public method to set the filters for the path picker dialog. |
|
214 |
|
215 Note: Multiple filters must be separated by ';;'. |
|
216 |
|
217 @param filters string containing the file filters |
|
218 @type str |
|
219 """ |
|
220 self.__filters = filters |
|
221 |
|
222 def filters(self): |
|
223 """ |
|
224 Public methods to get the filter string. |
|
225 |
|
226 @return filter string |
|
227 @rtype str |
|
228 """ |
|
229 return self.__filters |
|
230 |
|
231 def setButtonToolTip(self, tooltip): |
|
232 """ |
|
233 Public method to set the tool button tool tip. |
|
234 |
|
235 @param tooltip text to be set as a tool tip |
|
236 @type str |
|
237 """ |
|
238 self.__button.setToolTip(tooltip) |
|
239 |
|
240 def buttonToolTip(self): |
|
241 """ |
|
242 Public method to get the tool button tool tip. |
|
243 |
|
244 @return tool tip text |
|
245 @rtype str |
|
246 """ |
|
247 return self.__button.toolTip() |
|
248 |
|
249 def setEditorToolTip(self, tooltip): |
|
250 """ |
|
251 Public method to set the editor tool tip. |
|
252 |
|
253 @param tooltip text to be set as a tool tip |
|
254 @type str |
|
255 """ |
|
256 self.__editor.setToolTip(tooltip) |
|
257 |
|
258 def editorToolTip(self): |
|
259 """ |
|
260 Public method to get the editor tool tip. |
|
261 |
|
262 @return tool tip text |
|
263 @rtype str |
|
264 """ |
|
265 return self.__editor.toolTip() |
|
266 |
|
267 def __showPathPickerDialog(self): |
|
268 """ |
|
269 Private slot to show the path picker dialog. |
|
270 """ |
|
271 windowTitle = self.__windowTitle |
|
272 if not windowTitle: |
|
273 if self.__mode == E5PathPickerModes.OpenFileMode: |
|
274 windowTitle = self.tr("Choose a file to open") |
|
275 elif self.__mode == E5PathPickerModes.OpenFilesMode: |
|
276 windowTitle = self.tr("Choose files to open") |
|
277 elif self.__mode == E5PathPickerModes.SaveFileMode: |
|
278 windowTitle = self.tr("Choose a file to save") |
|
279 elif self.__mode == E5PathPickerModes.DiretoryMode: |
|
280 windowTitle = self.tr("Choose a directory") |
|
281 |
|
282 directory = self.__editor.text() |
|
283 if self.__mode == E5PathPickerModes.OpenFilesMode: |
|
284 directory = os.path.expanduser( |
|
285 Utilities.fromNativeSeparators(directory.split(";")[0])) |
|
286 else: |
|
287 directory = os.path.expanduser( |
|
288 Utilities.fromNativeSeparators(directory)) |
|
289 |
|
290 if self.__mode == E5PathPickerModes.OpenFileMode: |
|
291 path = E5FileDialog.getOpenFileName( |
|
292 self, |
|
293 windowTitle, |
|
294 directory, |
|
295 self.__filters) |
|
296 path = Utilities.toNativeSeparators(path) |
|
297 elif self.__mode == E5PathPickerModes.OpenFilesMode: |
|
298 paths = E5FileDialog.getOpenFileNames( |
|
299 self, |
|
300 windowTitle, |
|
301 directory, |
|
302 self.__filters) |
|
303 path = ";".join([Utilities.toNativeSeparators(path) |
|
304 for path in paths]) |
|
305 elif self.__mode == E5PathPickerModes.SaveFileMode: |
|
306 path = E5FileDialog.getSaveFileName( |
|
307 self, |
|
308 windowTitle, |
|
309 directory, |
|
310 self.__filters, |
|
311 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) |
|
312 path = Utilities.toNativeSeparators(path) |
|
313 elif self.__mode == E5PathPickerModes.DiretoryMode: |
|
314 path = E5FileDialog.getExistingDirectory( |
|
315 self, |
|
316 windowTitle, |
|
317 directory, |
|
318 E5FileDialog.Options(E5FileDialog.ShowDirsOnly)) |
|
319 path = Utilities.toNativeSeparators(path) |
|
320 |
|
321 if path: |
|
322 self.__editor.setText(path) |