src/eric7/EricWidgets/EricPathPicker.py

branch
server
changeset 10605
b6f5e27daeb5
parent 10439
21c28b0f9e41
child 10610
bb0149571d94
equal deleted inserted replaced
10604:0f4017309f35 10605:b6f5e27daeb5
20 QToolButton, 20 QToolButton,
21 QWidget, 21 QWidget,
22 ) 22 )
23 23
24 from eric7.EricGui import EricPixmapCache 24 from eric7.EricGui import EricPixmapCache
25 from eric7.RemoteServerInterface import EricServerFileDialog
26 from eric7.SystemUtilities import FileSystemUtilities
25 27
26 from . import EricFileDialog 28 from . import EricFileDialog
29 from .EricApplication import ericApp
27 from .EricCompleters import EricDirCompleter, EricFileCompleter 30 from .EricCompleters import EricDirCompleter, EricFileCompleter
28 31
29 32
30 class EricPathPickerModes(enum.Enum): 33 class EricPathPickerModes(enum.Enum):
31 """ 34 """
81 84
82 self.__lineEditKind = useLineEdit 85 self.__lineEditKind = useLineEdit
83 86
84 self.__mode = EricPathPicker.DefaultMode 87 self.__mode = EricPathPicker.DefaultMode
85 self.__editorEnabled = True 88 self.__editorEnabled = True
89 self.__remote = False
90 self.__remotefsInterface = None
86 91
87 self._completer = None 92 self._completer = None
88 self.__filters = "" 93 self.__filters = ""
89 self.__defaultDirectory = "" 94 self.__defaultDirectory = ""
90 self.__windowTitle = "" 95 self.__windowTitle = ""
185 @return path picker mode 190 @return path picker mode
186 @rtype EricPathPickerModes 191 @rtype EricPathPickerModes
187 """ 192 """
188 return self.__mode 193 return self.__mode
189 194
195 def setRemote(self, remote):
196 """
197 Public method to set the remote mode of the path picker.
198
199 @param remote flag indicating the remote mode
200 @type bool
201 """
202 self.__remote = remote
203 if remote:
204 self.__remotefsInterface = (
205 ericApp().getObject("EricServer").getServiceInterface("FileSystem")
206 )
207 else:
208 self.__remotefsInterface = None
209
210 def isRemote(self):
211 """
212 Public method to get the path picker remote mode.
213
214 @return flag indicating the remote mode
215 @rtype bool
216 """
217 return self.__remote
218
190 def setPickerEnabled(self, enable): 219 def setPickerEnabled(self, enable):
191 """ 220 """
192 Public method to set the enabled state of the file dialog button. 221 Public method to set the enabled state of the file dialog button.
193 222
194 @param enable flag indicating the enabled state 223 @param enable flag indicating the enabled state
288 ] 317 ]
289 ) 318 )
290 else: 319 else:
291 return self._editorText() 320 return self._editorText()
292 else: 321 else:
293 if toNative: 322 if self.__remote:
294 return os.path.expanduser(QDir.toNativeSeparators(self._editorText())) 323 return self.__remotefsInterface.expanduser(self._editorText())
295 else: 324 else:
296 return os.path.expanduser(self._editorText()) 325 if toNative:
326 return os.path.expanduser(
327 QDir.toNativeSeparators(self._editorText())
328 )
329 else:
330 return os.path.expanduser(self._editorText())
297 331
298 def setEditText(self, fpath, toNative=True): 332 def setEditText(self, fpath, toNative=True):
299 """ 333 """
300 Public method to set the current path. 334 Public method to set the current path.
301 335
367 401
368 @return last path 402 @return last path
369 @rtype pathlib.Path 403 @rtype pathlib.Path
370 """ 404 """
371 return self.paths()[-1] 405 return self.paths()[-1]
406
407 def strPaths(self):
408 """
409 Public method to get the list of entered paths as strings.
410
411 @return entered paths
412 @rtype list of str
413 """
414 if self.__mode in (
415 EricPathPickerModes.OPEN_FILES_MODE,
416 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE,
417 ):
418 return self.text().split(";")
419 else:
420 return [self.text()]
421
422 def firstStrPath(self):
423 """
424 Public method to get the first path of a list of entered paths as a string.
425
426 @return first path
427 @rtype pathlib.Path
428 """
429 return self.strPaths()[0]
430
431 def lastStrPath(self):
432 """
433 Public method to get the last path of a list of entered paths as a string.
434
435 @return last path
436 @rtype pathlib.Path
437 """
438 return self.strPaths()[-1]
372 439
373 def setEditorEnabled(self, enable): 440 def setEditorEnabled(self, enable):
374 """ 441 """
375 Public method to set the path editor's enabled state. 442 Public method to set the path editor's enabled state.
376 443
532 windowTitle = self.tr("Choose a directory") 599 windowTitle = self.tr("Choose a directory")
533 600
534 directory = self._editorText() 601 directory = self._editorText()
535 if not directory and self.__defaultDirectory: 602 if not directory and self.__defaultDirectory:
536 directory = self.__defaultDirectory 603 directory = self.__defaultDirectory
537 directory = ( 604 if self.__remote:
538 os.path.expanduser(directory.split(";")[0]) 605 directory = (
539 if self.__mode 606 self.__remotefsInterface.expanduser(directory.split(";")[0])
540 in ( 607 if self.__mode == EricPathPickerModes.OPEN_FILES_MODE
541 EricPathPickerModes.OPEN_FILES_MODE, 608 else self.__remotefsInterface.expanduser(directory)
542 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE,
543 ) 609 )
544 else os.path.expanduser(directory) 610 else:
545 ) 611 directory = (
546 if not os.path.isabs(directory) and self.__defaultDirectory: 612 os.path.expanduser(directory.split(";")[0])
547 directory = os.path.join(self.__defaultDirectory, directory) 613 if self.__mode
548 directory = QDir.fromNativeSeparators(directory) 614 in (
615 EricPathPickerModes.OPEN_FILES_MODE,
616 EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE,
617 )
618 else os.path.expanduser(directory)
619 )
620 if not os.path.isabs(directory) and self.__defaultDirectory:
621 directory = os.path.join(self.__defaultDirectory, directory)
622 directory = QDir.fromNativeSeparators(directory)
549 623
550 if self.__mode == EricPathPickerModes.OPEN_FILE_MODE: 624 if self.__mode == EricPathPickerModes.OPEN_FILE_MODE:
551 fpath = EricFileDialog.getOpenFileName( 625 if self.__remote:
552 self, windowTitle, directory, self.__filters 626 fpath = EricServerFileDialog.getOpenFileName(
553 ) 627 self, windowTitle, directory, self.__filters
554 fpath = QDir.toNativeSeparators(fpath) 628 )
629 else:
630 fpath = EricFileDialog.getOpenFileName(
631 self, windowTitle, directory, self.__filters
632 )
633 fpath = QDir.toNativeSeparators(fpath)
555 elif self.__mode == EricPathPickerModes.OPEN_FILES_MODE: 634 elif self.__mode == EricPathPickerModes.OPEN_FILES_MODE:
556 fpaths = EricFileDialog.getOpenFileNames( 635 if self.__remote:
557 self, windowTitle, directory, self.__filters 636 fpaths = EricServerFileDialog.getOpenFileNames(
558 ) 637 self, windowTitle, directory, self.__filters
559 fpath = ";".join([QDir.toNativeSeparators(fpath) for fpath in fpaths]) 638 )
639 fpath = ";".join(fpaths)
640 else:
641 fpaths = EricFileDialog.getOpenFileNames(
642 self, windowTitle, directory, self.__filters
643 )
644 fpath = ";".join([QDir.toNativeSeparators(fpath) for fpath in fpaths])
560 elif self.__mode == EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE: 645 elif self.__mode == EricPathPickerModes.OPEN_FILES_AND_DIRS_MODE:
646 # that is not supported for 'remote' pickers
561 fpaths = EricFileDialog.getOpenFileAndDirNames( 647 fpaths = EricFileDialog.getOpenFileAndDirNames(
562 self, windowTitle, directory, self.__filters 648 self, windowTitle, directory, self.__filters
563 ) 649 )
564 fpath = ";".join([QDir.toNativeSeparators(fpath) for fpath in fpaths]) 650 fpath = ";".join([QDir.toNativeSeparators(fpath) for fpath in fpaths])
565 elif self.__mode == EricPathPickerModes.SAVE_FILE_MODE: 651 elif self.__mode == EricPathPickerModes.SAVE_FILE_MODE:
566 fpath = EricFileDialog.getSaveFileName( 652 if self.__remote:
567 self, 653 fpath = EricServerFileDialog.getSaveFileName(
568 windowTitle, 654 self,
569 directory, 655 windowTitle,
570 self.__filters, 656 directory,
571 EricFileDialog.DontConfirmOverwrite, 657 self.__filters,
572 ) 658 )
573 fpath = QDir.toNativeSeparators(fpath) 659 else:
660 fpath = EricFileDialog.getSaveFileName(
661 self,
662 windowTitle,
663 directory,
664 self.__filters,
665 EricFileDialog.DontConfirmOverwrite,
666 )
667 fpath = QDir.toNativeSeparators(fpath)
574 elif self.__mode == EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE: 668 elif self.__mode == EricPathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE:
575 fpath, selectedFilter = EricFileDialog.getSaveFileNameAndFilter( 669 if self.__remote:
576 self, 670 fpath, selectedFilter = EricServerFileDialog.getSaveFileNameAndFilter(
577 windowTitle, 671 self,
578 directory, 672 windowTitle,
579 self.__filters, 673 directory,
580 None, 674 self.__filters,
581 EricFileDialog.DontConfirmOverwrite, 675 )
582 ) 676 fn, ext = self.__remotefsInterface.splitext(fpath)
583 fpath = pathlib.Path(fpath) 677 if not ext:
584 if not fpath.suffix: 678 ex = selectedFilter.split("(*")[1].split(")")[0].split()[0]
585 ex = selectedFilter.split("(*")[1].split(")")[0].split()[0] 679 if ex:
586 if ex: 680 fpath = f"{fn}{ex}"
587 fpath = fpath.with_suffix(ex) 681 else:
682 fpath, selectedFilter = EricFileDialog.getSaveFileNameAndFilter(
683 self,
684 windowTitle,
685 directory,
686 self.__filters,
687 None,
688 EricFileDialog.DontConfirmOverwrite,
689 )
690 fpath = pathlib.Path(fpath)
691 if not fpath.suffix:
692 ex = selectedFilter.split("(*")[1].split(")")[0].split()[0]
693 if ex:
694 fpath = fpath.with_suffix(ex)
588 elif self.__mode == EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE: 695 elif self.__mode == EricPathPickerModes.SAVE_FILE_OVERWRITE_MODE:
589 fpath = EricFileDialog.getSaveFileName( 696 if self.__remote:
590 self, windowTitle, directory, self.__filters 697 fpath = EricServerFileDialog.getSaveFileName(
591 ) 698 self,
592 fpath = QDir.toNativeSeparators(fpath) 699 windowTitle,
700 directory,
701 self.__filters,
702 )
703 else:
704 fpath = EricFileDialog.getSaveFileName(
705 self, windowTitle, directory, self.__filters
706 )
707 fpath = QDir.toNativeSeparators(fpath)
593 elif self.__mode == EricPathPickerModes.DIRECTORY_MODE: 708 elif self.__mode == EricPathPickerModes.DIRECTORY_MODE:
594 fpath = EricFileDialog.getExistingDirectory( 709 if self.__remote:
595 self, windowTitle, directory, EricFileDialog.ShowDirsOnly 710 fpath = EricServerFileDialog.getExistingDirectory(
596 ) 711 self, windowTitle, directory, True
597 fpath = QDir.toNativeSeparators(fpath) 712 )
598 while fpath.endswith(os.sep): 713 while fpath.endswith(self.__remotefsInterface.separator()):
599 fpath = fpath[:-1] 714 fpath = fpath[:-1]
715 else:
716 fpath = EricFileDialog.getExistingDirectory(
717 self, windowTitle, directory, EricFileDialog.ShowDirsOnly
718 )
719 fpath = QDir.toNativeSeparators(fpath)
720 while fpath.endswith(os.sep):
721 fpath = fpath[:-1]
600 elif self.__mode == EricPathPickerModes.DIRECTORY_SHOW_FILES_MODE: 722 elif self.__mode == EricPathPickerModes.DIRECTORY_SHOW_FILES_MODE:
601 fpath = EricFileDialog.getExistingDirectory( 723 if self.__remote:
602 self, windowTitle, directory, EricFileDialog.DontUseNativeDialog 724 fpath = EricServerFileDialog.getExistingDirectory(
603 ) 725 self, windowTitle, directory, False
604 fpath = QDir.toNativeSeparators(fpath) 726 )
605 while fpath.endswith(os.sep): 727 while fpath.endswith(self.__remotefsInterface):
606 fpath = fpath[:-1] 728 fpath = fpath[:-1]
729 else:
730 fpath = EricFileDialog.getExistingDirectory(
731 self, windowTitle, directory, EricFileDialog.DontUseNativeDialog
732 )
733 fpath = QDir.toNativeSeparators(fpath)
734 while fpath.endswith(os.sep):
735 fpath = fpath[:-1]
607 736
608 if fpath: 737 if fpath:
609 self._setEditorText(str(fpath)) 738 self._setEditorText(str(fpath))
610 self.pathSelected.emit(str(fpath)) 739 self.pathSelected.emit(str(fpath))
611 740
764 """ 893 """
765 paths = [] 894 paths = []
766 for index in range(self._editor.count()): 895 for index in range(self._editor.count()):
767 paths.append(pathlib.Path(self._editor.itemText(index))) 896 paths.append(pathlib.Path(self._editor.itemText(index)))
768 return paths 897 return paths
898
899 def getRemotePathItems(self):
900 """
901 Public method to get the list of remembered remote paths.
902
903 @return list of remembered paths
904 @rtype list of str
905 """
906 paths = []
907 for index in range(self._editor.count()):
908 paths.append(
909 FileSystemUtilities.remoteFileName(self._editor.itemText(index))
910 )
911 return paths

eric ide

mercurial