14 try: |
14 try: |
15 from enum import Enum |
15 from enum import Enum |
16 except ImportError: |
16 except ImportError: |
17 from ThirdParty.enum import Enum |
17 from ThirdParty.enum import Enum |
18 |
18 |
19 from PyQt5.QtCore import pyqtSignal, Qt, QFileInfo, QCoreApplication |
19 from PyQt5.QtCore import pyqtSignal, Qt, QFileInfo, QCoreApplication, QDir |
20 from PyQt5.QtWidgets import QWidget, QHBoxLayout, QToolButton, QSizePolicy |
20 from PyQt5.QtWidgets import QWidget, QHBoxLayout, QToolButton, QSizePolicy |
21 |
21 |
22 from . import E5FileDialog |
22 from . import E5FileDialog |
23 from .E5LineEdit import E5ClearableLineEdit |
23 from .E5LineEdit import E5ClearableLineEdit |
24 from .E5Completers import E5FileCompleter, E5DirCompleter |
24 from .E5Completers import E5FileCompleter, E5DirCompleter |
25 from .E5ComboBox import E5ClearableComboBox |
25 from .E5ComboBox import E5ClearableComboBox |
26 |
26 |
27 import UI.PixmapCache |
27 import UI.PixmapCache |
28 import Utilities |
|
29 |
28 |
30 |
29 |
31 class E5PathPickerModes(Enum): |
30 class E5PathPickerModes(Enum): |
32 """ |
31 """ |
33 Class implementing the path picker modes. |
32 Class implementing the path picker modes. |
127 |
126 |
128 @param path current text of the path line edit |
127 @param path current text of the path line edit |
129 @type str |
128 @type str |
130 """ |
129 """ |
131 if self._completer and not self._completer.popup().isVisible(): |
130 if self._completer and not self._completer.popup().isVisible(): |
132 self._completer.setRootPath(Utilities.toNativeSeparators(path)) |
131 self._completer.setRootPath(QDir.toNativeSeparators(path)) |
133 |
132 |
134 def setMode(self, mode): |
133 def setMode(self, mode): |
135 """ |
134 """ |
136 Public method to set the path picker mode. |
135 Public method to set the path picker mode. |
137 |
136 |
244 """ |
243 """ |
245 if self.__mode == E5PathPickerModes.OpenFilesMode: |
244 if self.__mode == E5PathPickerModes.OpenFilesMode: |
246 self._setEditorText(path) |
245 self._setEditorText(path) |
247 else: |
246 else: |
248 if toNative: |
247 if toNative: |
249 path = Utilities.toNativeSeparators(path) |
248 path = QDir.toNativeSeparators(path) |
250 self._setEditorText(path) |
249 self._setEditorText(path) |
251 if self._completer: |
250 if self._completer: |
252 self._completer.setRootPath(path) |
251 self._completer.setRootPath(path) |
253 |
252 |
254 def text(self, toNative=True): |
253 def text(self, toNative=True): |
262 @rtype str |
261 @rtype str |
263 """ |
262 """ |
264 if self.__mode == E5PathPickerModes.OpenFilesMode: |
263 if self.__mode == E5PathPickerModes.OpenFilesMode: |
265 if toNative: |
264 if toNative: |
266 return ";".join( |
265 return ";".join( |
267 [Utilities.toNativeSeparators(path) |
266 [QDir.toNativeSeparators(path) |
268 for path in self._editorText().split(";")]) |
267 for path in self._editorText().split(";")]) |
269 else: |
268 else: |
270 return self._editorText() |
269 return self._editorText() |
271 else: |
270 else: |
272 if toNative: |
271 if toNative: |
273 return os.path.expanduser( |
272 return os.path.expanduser( |
274 Utilities.toNativeSeparators(self._editorText())) |
273 QDir.toNativeSeparators(self._editorText())) |
275 else: |
274 else: |
276 return os.path.expanduser(self._editorText()) |
275 return os.path.expanduser(self._editorText()) |
277 |
276 |
278 def setEditText(self, path, toNative=True): |
277 def setEditText(self, path, toNative=True): |
279 """ |
278 """ |
518 directory = os.path.expanduser(directory.split(";")[0]) |
517 directory = os.path.expanduser(directory.split(";")[0]) |
519 else: |
518 else: |
520 directory = os.path.expanduser(directory) |
519 directory = os.path.expanduser(directory) |
521 if not os.path.isabs(directory) and self.__defaultDirectory: |
520 if not os.path.isabs(directory) and self.__defaultDirectory: |
522 directory = os.path.join(self.__defaultDirectory, directory) |
521 directory = os.path.join(self.__defaultDirectory, directory) |
523 directory = Utilities.fromNativeSeparators(directory) |
522 directory = QDir.fromNativeSeparators(directory) |
524 |
523 |
525 if self.__mode == E5PathPickerModes.OpenFileMode: |
524 if self.__mode == E5PathPickerModes.OpenFileMode: |
526 path = E5FileDialog.getOpenFileName( |
525 path = E5FileDialog.getOpenFileName( |
527 self, |
526 self, |
528 windowTitle, |
527 windowTitle, |
529 directory, |
528 directory, |
530 self.__filters) |
529 self.__filters) |
531 path = Utilities.toNativeSeparators(path) |
530 path = QDir.toNativeSeparators(path) |
532 elif self.__mode == E5PathPickerModes.OpenFilesMode: |
531 elif self.__mode == E5PathPickerModes.OpenFilesMode: |
533 paths = E5FileDialog.getOpenFileNames( |
532 paths = E5FileDialog.getOpenFileNames( |
534 self, |
533 self, |
535 windowTitle, |
534 windowTitle, |
536 directory, |
535 directory, |
537 self.__filters) |
536 self.__filters) |
538 path = ";".join([Utilities.toNativeSeparators(path) |
537 path = ";".join([QDir.toNativeSeparators(path) |
539 for path in paths]) |
538 for path in paths]) |
540 elif self.__mode == E5PathPickerModes.SaveFileMode: |
539 elif self.__mode == E5PathPickerModes.SaveFileMode: |
541 path = E5FileDialog.getSaveFileName( |
540 path = E5FileDialog.getSaveFileName( |
542 self, |
541 self, |
543 windowTitle, |
542 windowTitle, |
544 directory, |
543 directory, |
545 self.__filters, |
544 self.__filters, |
546 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) |
545 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) |
547 path = Utilities.toNativeSeparators(path) |
546 path = QDir.toNativeSeparators(path) |
548 elif self.__mode == E5PathPickerModes.SaveFileEnsureExtensionMode: |
547 elif self.__mode == E5PathPickerModes.SaveFileEnsureExtensionMode: |
549 path, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( |
548 path, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( |
550 self, |
549 self, |
551 windowTitle, |
550 windowTitle, |
552 directory, |
551 directory, |
553 self.__filters, |
552 self.__filters, |
554 None, |
553 None, |
555 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) |
554 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite)) |
556 path = Utilities.toNativeSeparators(path) |
555 path = QDir.toNativeSeparators(path) |
557 if path: |
556 if path: |
558 ext = QFileInfo(path).suffix() |
557 ext = QFileInfo(path).suffix() |
559 if not ext: |
558 if not ext: |
560 ex = selectedFilter.split("(*")[1].split(")")[0] |
559 ex = selectedFilter.split("(*")[1].split(")")[0] |
561 if ex: |
560 if ex: |
564 path = E5FileDialog.getSaveFileName( |
563 path = E5FileDialog.getSaveFileName( |
565 self, |
564 self, |
566 windowTitle, |
565 windowTitle, |
567 directory, |
566 directory, |
568 self.__filters) |
567 self.__filters) |
569 path = Utilities.toNativeSeparators(path) |
568 path = QDir.toNativeSeparators(path) |
570 elif self.__mode == E5PathPickerModes.DirectoryMode: |
569 elif self.__mode == E5PathPickerModes.DirectoryMode: |
571 path = E5FileDialog.getExistingDirectory( |
570 path = E5FileDialog.getExistingDirectory( |
572 self, |
571 self, |
573 windowTitle, |
572 windowTitle, |
574 directory, |
573 directory, |
575 E5FileDialog.Options(E5FileDialog.ShowDirsOnly)) |
574 E5FileDialog.Options(E5FileDialog.ShowDirsOnly)) |
576 path = Utilities.toNativeSeparators(path) |
575 path = QDir.toNativeSeparators(path) |
577 while path.endswith(os.sep): |
576 while path.endswith(os.sep): |
578 path = path[:-1] |
577 path = path[:-1] |
579 elif self.__mode == E5PathPickerModes.DirectoryShowFilesMode: |
578 elif self.__mode == E5PathPickerModes.DirectoryShowFilesMode: |
580 path = E5FileDialog.getExistingDirectory( |
579 path = E5FileDialog.getExistingDirectory( |
581 self, |
580 self, |
582 windowTitle, |
581 windowTitle, |
583 directory, |
582 directory, |
584 E5FileDialog.Options(E5FileDialog.DontUseNativeDialog)) |
583 E5FileDialog.Options(E5FileDialog.DontUseNativeDialog)) |
585 path = Utilities.toNativeSeparators(path) |
584 path = QDir.toNativeSeparators(path) |
586 while path.endswith(os.sep): |
585 while path.endswith(os.sep): |
587 path = path[:-1] |
586 path = path[:-1] |
588 |
587 |
589 if path: |
588 if path: |
590 self._setEditorText(path) |
589 self._setEditorText(path) |