E5Gui/E5PathPicker.py

changeset 4578
5d611151cdec
parent 4576
a258569d44db
child 4581
76999ca7bbf1
equal deleted inserted replaced
4577:e79a139aacc4 4578:5d611151cdec
20 from PyQt5.QtWidgets import QWidget, QHBoxLayout, QToolButton 20 from PyQt5.QtWidgets import QWidget, QHBoxLayout, QToolButton
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 26
26 import UI.PixmapCache 27 import UI.PixmapCache
27 import Utilities 28 import Utilities
28 29
29 30
326 self, 327 self,
327 windowTitle, 328 windowTitle,
328 directory, 329 directory,
329 E5FileDialog.Options(E5FileDialog.ShowDirsOnly)) 330 E5FileDialog.Options(E5FileDialog.ShowDirsOnly))
330 path = Utilities.toNativeSeparators(path) 331 path = Utilities.toNativeSeparators(path)
332 while path.endswith(os.sep):
333 path = path[:-1]
331 334
332 if path: 335 if path:
333 self.__editor.setText(path) 336 self.__editor.setText(path)
337
338
339 class E5ComboPathPicker(QWidget):
340 """
341 Class implementing a path picker widget consisting of a combobox and a
342 tool button to open a file dialog.
343
344 @signal editTextChanged(path) emitted when the entered path has changed
345 """
346 DefaultMode = E5PathPickerModes.OpenFileMode
347
348 editTextChanged = pyqtSignal(str)
349
350 def __init__(self, parent=None):
351 """
352 Constructor
353
354 @param parent reference to the parent widget
355 @type QWidget
356 """
357 super(E5ComboPathPicker, self).__init__(parent)
358
359 self.__mode = E5PathPicker.DefaultMode
360 self.__editorEnabled = True
361
362 self.__completer = None
363 self.__filters = ""
364 self.__defaultDirectory = ""
365 self.__windowTitle = ""
366
367 self.__layout = QHBoxLayout()
368 self.__layout.setSpacing(0)
369 self.__layout.setContentsMargins(0, 0, 0, 0)
370 self.setLayout(self.__layout)
371
372 self.__editor = E5ClearableComboBox(self, self.tr("Enter Path Name"))
373
374 self.__button = QToolButton(self)
375 self.__button.setToolButtonStyle(Qt.ToolButtonIconOnly)
376 self.__button.setIcon(UI.PixmapCache.getIcon("open.png"))
377
378 self.__layout.addWidget(self.__editor)
379 self.__layout.addWidget(self.__button)
380
381 self.__button.clicked.connect(self.__showPathPickerDialog)
382 self.__editor.editTextChanged.connect(self.editTextChanged)
383
384 def setMode(self, mode):
385 """
386 Public method to set the path picker mode.
387
388 @param mode picker mode
389 @type E5PathPickerModes
390 """
391 assert mode in E5PathPickerModes
392
393 oldMode = self.__mode
394 self.__mode = mode
395
396 if mode != oldMode:
397 # Remove current completer
398 self.__editor.setCompleter(None)
399 self.__completer = None
400
401 # Set a new completer
402 if mode == E5PathPickerModes.DiretoryMode:
403 self.__completer = E5DirCompleter(self.__editor)
404 else:
405 self.__completer = E5FileCompleter(self.__editor)
406
407 def mode(self):
408 """
409 Public method to get the path picker mode.
410
411 @return path picker mode
412 @rtype E5PathPickerModes
413 """
414 return self.__mode
415
416 def clear(self):
417 """
418 Public method to clear the list of paths.
419 """
420 self.__editor.clear()
421
422 def clearEditText(self):
423 """
424 Public method to clear the current path.
425 """
426 self.__editor.clearEditText()
427
428 def setEditText(self, path):
429 """
430 Public method to set the current path.
431
432 @param path path to be set
433 @type str
434 """
435 if self.__mode == E5PathPickerModes.OpenFilesMode:
436 self.__editor.setEditText(path)
437 else:
438 self.__editor.setEditText(Utilities.toNativeSeparators(path))
439
440 def currentText(self):
441 """
442 Public method to get the current path.
443
444 @return current path
445 @rtype str
446 """
447 if self.__mode == E5PathPickerModes.OpenFilesMode:
448 return self.__editor.currentText()
449 else:
450 return os.path.expanduser(
451 Utilities.toNativeSeparators(self.__editor.currentText()))
452
453 def setPath(self, path):
454 """
455 Public method to set the current path.
456
457 @param path path to be set
458 @type str
459 """
460 self.setEditText(path)
461
462 def path(self):
463 """
464 Public method to get the current path.
465
466 @return current path
467 @rtype str
468 """
469 return self.currentText()
470
471 def addItems(self, pathsList):
472 """
473 Public method to add paths to the current list.
474
475 @param pathsList list of paths to add
476 @type list of str
477 """
478 self.__editor.addItems(pathsList)
479
480 def addItem(self, path):
481 """
482 Public method to add a paths to the current list.
483
484 @param path path to add
485 @type str
486 """
487 self.__editor.addItem(path)
488
489 def setPathsList(self, pathsList):
490 """
491 Public method to set the paths list.
492
493 @param pathsList list of paths
494 @type list of str
495 """
496 self.clear()
497 self.addItems(pathsList)
498
499 def setCurrentIndex(self, index):
500 """
501 Public slot to set the current index.
502
503 @param index index of the item to set current
504 @type int
505 """
506 self.__editor.setCurrentIndex(index)
507
508 def setEditorEnabled(self, enable):
509 """
510 Public method to set the path editor's enabled state.
511
512 @param enable flag indicating the enable state
513 @type bool
514 """
515 if enable != self.__editorEnabled:
516 self.__editorEnabled = enable
517 self.__editor.setEnabled(enable)
518
519 def editorEnabled(self):
520 """
521 Public method to get the path editor's enabled state.
522
523 @return flag indicating the enabled state
524 @rtype bool
525 """
526 return self.__editorEnabled
527
528 def setDefaultDirectory(self, directory):
529 """
530 Public method to set the default directory.
531
532 @param directory default directory
533 @type str
534 """
535 self.__defaultDirectory = directory
536
537 def defaultDirectory(self):
538 """
539 Public method to get the default directory.
540
541 @return default directory
542 @rtype str
543 """
544 return self.__defaultDirectory
545
546 def setWindowTitle(self, title):
547 """
548 Public method to set the path picker dialog window title.
549
550 @param title window title
551 @type str
552 """
553 self.__windowTitle = title
554
555 def windowTitle(self):
556 """
557 Public method to get the path picker dialog's window title.
558
559 @return window title
560 @rtype str
561 """
562 return self.__windowTitle
563
564 def setFilters(self, filters):
565 """
566 Public method to set the filters for the path picker dialog.
567
568 Note: Multiple filters must be separated by ';;'.
569
570 @param filters string containing the file filters
571 @type str
572 """
573 self.__filters = filters
574
575 def filters(self):
576 """
577 Public methods to get the filter string.
578
579 @return filter string
580 @rtype str
581 """
582 return self.__filters
583
584 def setButtonToolTip(self, tooltip):
585 """
586 Public method to set the tool button tool tip.
587
588 @param tooltip text to be set as a tool tip
589 @type str
590 """
591 self.__button.setToolTip(tooltip)
592
593 def buttonToolTip(self):
594 """
595 Public method to get the tool button tool tip.
596
597 @return tool tip text
598 @rtype str
599 """
600 return self.__button.toolTip()
601
602 def setEditorToolTip(self, tooltip):
603 """
604 Public method to set the editor tool tip.
605
606 @param tooltip text to be set as a tool tip
607 @type str
608 """
609 self.__editor.setToolTip(tooltip)
610
611 def editorToolTip(self):
612 """
613 Public method to get the editor tool tip.
614
615 @return tool tip text
616 @rtype str
617 """
618 return self.__editor.toolTip()
619
620 def __showPathPickerDialog(self):
621 """
622 Private slot to show the path picker dialog.
623 """
624 windowTitle = self.__windowTitle
625 if not windowTitle:
626 if self.__mode == E5PathPickerModes.OpenFileMode:
627 windowTitle = self.tr("Choose a file to open")
628 elif self.__mode == E5PathPickerModes.OpenFilesMode:
629 windowTitle = self.tr("Choose files to open")
630 elif self.__mode == E5PathPickerModes.SaveFileMode:
631 windowTitle = self.tr("Choose a file to save")
632 elif self.__mode == E5PathPickerModes.DiretoryMode:
633 windowTitle = self.tr("Choose a directory")
634
635 directory = self.__editor.currentText()
636 if self.__mode == E5PathPickerModes.OpenFilesMode:
637 directory = os.path.expanduser(
638 Utilities.fromNativeSeparators(directory.split(";")[0]))
639 else:
640 directory = os.path.expanduser(
641 Utilities.fromNativeSeparators(directory))
642
643 if self.__mode == E5PathPickerModes.OpenFileMode:
644 path = E5FileDialog.getOpenFileName(
645 self,
646 windowTitle,
647 directory,
648 self.__filters)
649 path = Utilities.toNativeSeparators(path)
650 elif self.__mode == E5PathPickerModes.OpenFilesMode:
651 paths = E5FileDialog.getOpenFileNames(
652 self,
653 windowTitle,
654 directory,
655 self.__filters)
656 path = ";".join([Utilities.toNativeSeparators(path)
657 for path in paths])
658 elif self.__mode == E5PathPickerModes.SaveFileMode:
659 path = E5FileDialog.getSaveFileName(
660 self,
661 windowTitle,
662 directory,
663 self.__filters,
664 E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
665 path = Utilities.toNativeSeparators(path)
666 elif self.__mode == E5PathPickerModes.DiretoryMode:
667 path = E5FileDialog.getExistingDirectory(
668 self,
669 windowTitle,
670 directory,
671 E5FileDialog.Options(E5FileDialog.ShowDirsOnly))
672 path = Utilities.toNativeSeparators(path)
673 while path.endswith(os.sep):
674 path = path[:-1]
675
676 if path:
677 self.__editor.setEditText(path)

eric ide

mercurial