eric7/E5Gui/E5PathPicker.py

branch
eric7
changeset 8327
666c2b81cbb7
parent 8322
b422b4e77d19
child 8350
74a3b2a6a944
equal deleted inserted replaced
8325:547319e56c60 8327:666c2b81cbb7
23 23
24 class E5PathPickerModes(enum.Enum): 24 class E5PathPickerModes(enum.Enum):
25 """ 25 """
26 Class implementing the path picker modes. 26 Class implementing the path picker modes.
27 """ 27 """
28 # TODO: eric7: convert these to all uppercase without "Mode" when doing 28 OPEN_FILE_MODE = 0
29 # the port to PyQt6 29 OPEN_FILES_MODE = 1
30 OpenFileMode = 0 30 SAVE_FILE_MODE = 2
31 OpenFilesMode = 1 31 SAVE_FILE_ENSURE_EXTENSION_MODE = 3
32 SaveFileMode = 2 32 SAVE_FILE_OVERWRITE_MODE = 4
33 SaveFileEnsureExtensionMode = 3 33 DIRECTORY_MODE = 5
34 SaveFileOverwriteMode = 4 34 DIRECTORY_SHOW_FILES_MODE = 6
35 DirectoryMode = 5 35 CUSTOM_MODE = 99
36 DirectoryShowFilesMode = 6 36 NO_MODE = 100
37 CustomMode = 99
38 NoMode = 100
39 37
40 38
41 class E5PathPickerBase(QWidget): 39 class E5PathPickerBase(QWidget):
42 """ 40 """
43 Class implementing the base of a path picker widget consisting of a 41 Class implementing the base of a path picker widget consisting of a
51 file dialog 49 file dialog
52 @signal aboutToShowPathPickerDialog emitted before the file dialog is shown 50 @signal aboutToShowPathPickerDialog emitted before the file dialog is shown
53 @signal pickerButtonClicked emitted when the picker button was pressed and 51 @signal pickerButtonClicked emitted when the picker button was pressed and
54 the widget mode is custom 52 the widget mode is custom
55 """ 53 """
56 DefaultMode = E5PathPickerModes.NoMode 54 DefaultMode = E5PathPickerModes.NO_MODE
57 55
58 textChanged = pyqtSignal(str) 56 textChanged = pyqtSignal(str)
59 editTextChanged = pyqtSignal(str) 57 editTextChanged = pyqtSignal(str)
60 pathSelected = pyqtSignal(str) 58 pathSelected = pyqtSignal(str)
61 aboutToShowPathPickerDialog = pyqtSignal() 59 aboutToShowPathPickerDialog = pyqtSignal()
113 self.setFocusProxy(self._editor) 111 self.setFocusProxy(self._editor)
114 self.setFocusPolicy(Qt.FocusPolicy.StrongFocus) 112 self.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
115 self.setSizePolicy(QSizePolicy.Policy.Expanding, 113 self.setSizePolicy(QSizePolicy.Policy.Expanding,
116 QSizePolicy.Policy.Preferred) 114 QSizePolicy.Policy.Preferred)
117 115
118 self.__button.setEnabled(self.__mode != E5PathPickerModes.NoMode) 116 self.__button.setEnabled(self.__mode != E5PathPickerModes.NO_MODE)
119 117
120 def __pathEdited(self, path): 118 def __pathEdited(self, path):
121 """ 119 """
122 Private slot handling editing of the path. 120 Private slot handling editing of the path.
123 121
145 if self.__lineEditKind and self._completer: 143 if self.__lineEditKind and self._completer:
146 # Remove current completer 144 # Remove current completer
147 self._editor.setCompleter(None) 145 self._editor.setCompleter(None)
148 self._completer = None 146 self._completer = None
149 147
150 if mode != E5PathPickerModes.NoMode: 148 if mode != E5PathPickerModes.NO_MODE:
151 if self.__lineEditKind: 149 if self.__lineEditKind:
152 # Set a new completer 150 # Set a new completer
153 if mode == E5PathPickerModes.DirectoryMode: 151 if mode == E5PathPickerModes.DIRECTORY_MODE:
154 self._completer = E5DirCompleter(self._editor) 152 self._completer = E5DirCompleter(self._editor)
155 else: 153 else:
156 self._completer = E5FileCompleter(self._editor) 154 self._completer = E5FileCompleter(self._editor)
157 155
158 # set inactive text 156 # set inactive text
159 if mode == E5PathPickerModes.OpenFilesMode: 157 if mode == E5PathPickerModes.OPEN_FILES_MODE:
160 self._editor.setInactiveText( 158 self._editor.setInactiveText(
161 self.tr("Enter Path Names separated by ';'")) 159 self.tr("Enter Path Names separated by ';'"))
162 else: 160 else:
163 self._editor.setInactiveText( 161 self._editor.setInactiveText(
164 self.tr("Enter Path Name")) 162 self.tr("Enter Path Name"))
165 self.__button.setEnabled(self.__mode != E5PathPickerModes.NoMode) 163 self.__button.setEnabled(self.__mode != E5PathPickerModes.NO_MODE)
166 164
167 def mode(self): 165 def mode(self):
168 """ 166 """
169 Public method to get the path picker mode. 167 Public method to get the path picker mode.
170 168
238 @type str 236 @type str
239 @param toNative flag indicating to convert the path into 237 @param toNative flag indicating to convert the path into
240 a native format 238 a native format
241 @type bool 239 @type bool
242 """ 240 """
243 if self.__mode == E5PathPickerModes.OpenFilesMode: 241 if self.__mode == E5PathPickerModes.OPEN_FILES_MODE:
244 self._setEditorText(path) 242 self._setEditorText(path)
245 else: 243 else:
246 if toNative: 244 if toNative:
247 path = QDir.toNativeSeparators(path) 245 path = QDir.toNativeSeparators(path)
248 self._setEditorText(path) 246 self._setEditorText(path)
257 a native format 255 a native format
258 @type bool 256 @type bool
259 @return current path 257 @return current path
260 @rtype str 258 @rtype str
261 """ 259 """
262 if self.__mode == E5PathPickerModes.OpenFilesMode: 260 if self.__mode == E5PathPickerModes.OPEN_FILES_MODE:
263 if toNative: 261 if toNative:
264 return ";".join( 262 return ";".join(
265 [QDir.toNativeSeparators(path) 263 [QDir.toNativeSeparators(path)
266 for path in self._editorText().split(";")]) 264 for path in self._editorText().split(";")])
267 else: 265 else:
329 a native format 327 a native format
330 @type bool 328 @type bool
331 @return entered paths 329 @return entered paths
332 @rtype list of str 330 @rtype list of str
333 """ 331 """
334 if self.__mode == E5PathPickerModes.OpenFilesMode: 332 if self.__mode == E5PathPickerModes.OPEN_FILES_MODE:
335 return self.path(toNative=toNative).split(";") 333 return self.path(toNative=toNative).split(";")
336 else: 334 else:
337 return [self.path(toNative=toNative)] 335 return [self.path(toNative=toNative)]
338 336
339 def firstPath(self, toNative=True): 337 def firstPath(self, toNative=True):
484 482
485 def __showPathPickerDialog(self): 483 def __showPathPickerDialog(self):
486 """ 484 """
487 Private slot to show the path picker dialog. 485 Private slot to show the path picker dialog.
488 """ 486 """
489 if self.__mode == E5PathPickerModes.NoMode: 487 if self.__mode == E5PathPickerModes.NO_MODE:
490 return 488 return
491 489
492 if self.__mode == E5PathPickerModes.CustomMode: 490 if self.__mode == E5PathPickerModes.CUSTOM_MODE:
493 self.pickerButtonClicked.emit() 491 self.pickerButtonClicked.emit()
494 return 492 return
495 493
496 self.aboutToShowPathPickerDialog.emit() 494 self.aboutToShowPathPickerDialog.emit()
497 495
498 windowTitle = self.__windowTitle 496 windowTitle = self.__windowTitle
499 if not windowTitle: 497 if not windowTitle:
500 if self.__mode == E5PathPickerModes.OpenFileMode: 498 if self.__mode == E5PathPickerModes.OPEN_FILE_MODE:
501 windowTitle = self.tr("Choose a file to open") 499 windowTitle = self.tr("Choose a file to open")
502 elif self.__mode == E5PathPickerModes.OpenFilesMode: 500 elif self.__mode == E5PathPickerModes.OPEN_FILES_MODE:
503 windowTitle = self.tr("Choose files to open") 501 windowTitle = self.tr("Choose files to open")
504 elif self.__mode in [ 502 elif self.__mode in [
505 E5PathPickerModes.SaveFileMode, 503 E5PathPickerModes.SAVE_FILE_MODE,
506 E5PathPickerModes.SaveFileEnsureExtensionMode, 504 E5PathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE,
507 E5PathPickerModes.SaveFileOverwriteMode]: 505 E5PathPickerModes.SAVE_FILE_OVERWRITE_MODE]:
508 windowTitle = self.tr("Choose a file to save") 506 windowTitle = self.tr("Choose a file to save")
509 elif self.__mode == E5PathPickerModes.DirectoryMode: 507 elif self.__mode == E5PathPickerModes.DIRECTORY_MODE:
510 windowTitle = self.tr("Choose a directory") 508 windowTitle = self.tr("Choose a directory")
511 509
512 directory = self._editorText() 510 directory = self._editorText()
513 if not directory and self.__defaultDirectory: 511 if not directory and self.__defaultDirectory:
514 directory = self.__defaultDirectory 512 directory = self.__defaultDirectory
515 directory = ( 513 directory = (
516 os.path.expanduser(directory.split(";")[0]) 514 os.path.expanduser(directory.split(";")[0])
517 if self.__mode == E5PathPickerModes.OpenFilesMode else 515 if self.__mode == E5PathPickerModes.OPEN_FILES_MODE else
518 os.path.expanduser(directory) 516 os.path.expanduser(directory)
519 ) 517 )
520 if not os.path.isabs(directory) and self.__defaultDirectory: 518 if not os.path.isabs(directory) and self.__defaultDirectory:
521 directory = os.path.join(self.__defaultDirectory, directory) 519 directory = os.path.join(self.__defaultDirectory, directory)
522 directory = QDir.fromNativeSeparators(directory) 520 directory = QDir.fromNativeSeparators(directory)
523 521
524 if self.__mode == E5PathPickerModes.OpenFileMode: 522 if self.__mode == E5PathPickerModes.OPEN_FILE_MODE:
525 path = E5FileDialog.getOpenFileName( 523 path = E5FileDialog.getOpenFileName(
526 self, 524 self,
527 windowTitle, 525 windowTitle,
528 directory, 526 directory,
529 self.__filters) 527 self.__filters)
530 path = QDir.toNativeSeparators(path) 528 path = QDir.toNativeSeparators(path)
531 elif self.__mode == E5PathPickerModes.OpenFilesMode: 529 elif self.__mode == E5PathPickerModes.OPEN_FILES_MODE:
532 paths = E5FileDialog.getOpenFileNames( 530 paths = E5FileDialog.getOpenFileNames(
533 self, 531 self,
534 windowTitle, 532 windowTitle,
535 directory, 533 directory,
536 self.__filters) 534 self.__filters)
537 path = ";".join([QDir.toNativeSeparators(path) 535 path = ";".join([QDir.toNativeSeparators(path)
538 for path in paths]) 536 for path in paths])
539 elif self.__mode == E5PathPickerModes.SaveFileMode: 537 elif self.__mode == E5PathPickerModes.SAVE_FILE_MODE:
540 path = E5FileDialog.getSaveFileName( 538 path = E5FileDialog.getSaveFileName(
541 self, 539 self,
542 windowTitle, 540 windowTitle,
543 directory, 541 directory,
544 self.__filters, 542 self.__filters,
545 E5FileDialog.DontConfirmOverwrite) 543 E5FileDialog.DontConfirmOverwrite)
546 path = QDir.toNativeSeparators(path) 544 path = QDir.toNativeSeparators(path)
547 elif self.__mode == E5PathPickerModes.SaveFileEnsureExtensionMode: 545 elif self.__mode == E5PathPickerModes.SAVE_FILE_ENSURE_EXTENSION_MODE:
548 path, selectedFilter = E5FileDialog.getSaveFileNameAndFilter( 546 path, selectedFilter = E5FileDialog.getSaveFileNameAndFilter(
549 self, 547 self,
550 windowTitle, 548 windowTitle,
551 directory, 549 directory,
552 self.__filters, 550 self.__filters,
557 ext = QFileInfo(path).suffix() 555 ext = QFileInfo(path).suffix()
558 if not ext: 556 if not ext:
559 ex = selectedFilter.split("(*")[1].split(")")[0] 557 ex = selectedFilter.split("(*")[1].split(")")[0]
560 if ex: 558 if ex:
561 path += ex 559 path += ex
562 elif self.__mode == E5PathPickerModes.SaveFileOverwriteMode: 560 elif self.__mode == E5PathPickerModes.SAVE_FILE_OVERWRITE_MODE:
563 path = E5FileDialog.getSaveFileName( 561 path = E5FileDialog.getSaveFileName(
564 self, 562 self,
565 windowTitle, 563 windowTitle,
566 directory, 564 directory,
567 self.__filters) 565 self.__filters)
568 path = QDir.toNativeSeparators(path) 566 path = QDir.toNativeSeparators(path)
569 elif self.__mode == E5PathPickerModes.DirectoryMode: 567 elif self.__mode == E5PathPickerModes.DIRECTORY_MODE:
570 path = E5FileDialog.getExistingDirectory( 568 path = E5FileDialog.getExistingDirectory(
571 self, 569 self,
572 windowTitle, 570 windowTitle,
573 directory, 571 directory,
574 E5FileDialog.ShowDirsOnly) 572 E5FileDialog.ShowDirsOnly)
575 path = QDir.toNativeSeparators(path) 573 path = QDir.toNativeSeparators(path)
576 while path.endswith(os.sep): 574 while path.endswith(os.sep):
577 path = path[:-1] 575 path = path[:-1]
578 elif self.__mode == E5PathPickerModes.DirectoryShowFilesMode: 576 elif self.__mode == E5PathPickerModes.DIRECTORY_SHOW_FILES_MODE:
579 path = E5FileDialog.getExistingDirectory( 577 path = E5FileDialog.getExistingDirectory(
580 self, 578 self,
581 windowTitle, 579 windowTitle,
582 directory, 580 directory,
583 E5FileDialog.DontUseNativeDialog) 581 E5FileDialog.DontUseNativeDialog)

eric ide

mercurial