|
| 1 | +import os |
| 2 | +import typing |
| 3 | +from PySide6.QtWidgets import * |
| 4 | +from PySide6.QtCore import * |
| 5 | +from PySide6.QtGui import * |
| 6 | +from ..forms.generated.ui_fileselectionwidget import Ui_FileSelectionWidget |
| 7 | +from preppipe.language import * |
| 8 | +from ..translatablewidgetinterface import * |
| 9 | + |
| 10 | +class FileSelectionWidget(QWidget, TranslatableWidgetInterface): |
| 11 | + # Signal to indicate that the file path has been updated. |
| 12 | + filePathUpdated = Signal(str) |
| 13 | + |
| 14 | + TR_gui_fileselectionwidget = TranslationDomain("gui_fileselectionwidget") |
| 15 | + |
| 16 | + tr_pleaseselect = TR_gui_fileselectionwidget.tr("pleaseselect", |
| 17 | + en="Please select {fieldname}", |
| 18 | + zh_cn="请选择{fieldname}", |
| 19 | + zh_hk="請選擇{fieldname}", |
| 20 | + ) |
| 21 | + tr_notselected = TR_gui_fileselectionwidget.tr("notselected", |
| 22 | + en="({fieldname}: Not selected)", |
| 23 | + zh_cn="({fieldname}: 未选择)", |
| 24 | + zh_hk="({fieldname}: 未選擇)", |
| 25 | + ) |
| 26 | + tr_select = TR_gui_fileselectionwidget.tr("select", |
| 27 | + en="Select", |
| 28 | + zh_cn="选择", |
| 29 | + zh_hk="選擇", |
| 30 | + ) |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + def default_file_checker(path: str) -> bool: |
| 34 | + return os.path.exists(path) and os.path.isfile(path) |
| 35 | + |
| 36 | + @staticmethod |
| 37 | + def default_directory_checker(path: str) -> bool: |
| 38 | + return os.path.exists(path) and os.path.isdir(path) |
| 39 | + |
| 40 | + ui : Ui_FileSelectionWidget |
| 41 | + isDirectoryMode : bool |
| 42 | + isOutputInsteadofInput : bool |
| 43 | + isExistingOnly : bool |
| 44 | + verifyCB : typing.Callable[[str], bool] | None |
| 45 | + currentPath : str |
| 46 | + fieldName : Translatable | str |
| 47 | + filter : Translatable | str |
| 48 | + defaultName : Translatable | str |
| 49 | + |
| 50 | + def __init__(self, parent=None): |
| 51 | + super().__init__(parent) |
| 52 | + # Instantiate the UI from the compiled .ui file. |
| 53 | + self.ui = Ui_FileSelectionWidget() |
| 54 | + self.ui.setupUi(self) |
| 55 | + |
| 56 | + # Initial state |
| 57 | + self.isDirectoryMode = False |
| 58 | + self.isOutputInsteadofInput = False |
| 59 | + self.isExistingOnly = False |
| 60 | + self.verifyCB = None |
| 61 | + self.currentPath = "" |
| 62 | + self.fieldName = "" |
| 63 | + self.filter = "" |
| 64 | + self.defaultName = "" |
| 65 | + |
| 66 | + # Connect the push button's clicked signal to open the dialog. |
| 67 | + self.ui.pushButton.clicked.connect(self.requestOpenDialog) |
| 68 | + self.bind_text(self.ui.pushButton.setText, self.tr_select) |
| 69 | + |
| 70 | + # Enable drag and drop. |
| 71 | + self.setAcceptDrops(True) |
| 72 | + # Initialize the verifier based on the directory mode. |
| 73 | + self.setDirectoryMode(self.isDirectoryMode) |
| 74 | + |
| 75 | + def setDirectoryMode(self, v: bool): |
| 76 | + self.isDirectoryMode = v |
| 77 | + self.verifyCB = FileSelectionWidget.getDefaultVerifier(self.isDirectoryMode) |
| 78 | + |
| 79 | + def getIsDirectoryMode(self) -> bool: |
| 80 | + return self.isDirectoryMode |
| 81 | + |
| 82 | + @staticmethod |
| 83 | + def getDefaultVerifier(isDirectoryMode: bool): |
| 84 | + if isDirectoryMode: |
| 85 | + return FileSelectionWidget.default_directory_checker |
| 86 | + else: |
| 87 | + return FileSelectionWidget.default_file_checker |
| 88 | + |
| 89 | + def setVerifyCallBack(self, cb): |
| 90 | + self.verifyCB = cb |
| 91 | + |
| 92 | + def setIsOutputInsteadofInput(self, v: bool): |
| 93 | + self.isOutputInsteadofInput = v |
| 94 | + |
| 95 | + def getIsOutputInsteadofInput(self) -> bool: |
| 96 | + return self.isOutputInsteadofInput |
| 97 | + |
| 98 | + def setExistingOnly(self, v: bool): |
| 99 | + self.isExistingOnly = v |
| 100 | + |
| 101 | + def setFieldName(self, name: Translatable | str): |
| 102 | + self.fieldName = name |
| 103 | + self.updateLabelText() |
| 104 | + |
| 105 | + def getFieldName(self) -> Translatable | str: |
| 106 | + return self.fieldName |
| 107 | + |
| 108 | + def setFilter(self, filter_str: Translatable | str): |
| 109 | + self.filter = filter_str |
| 110 | + |
| 111 | + def getFilter(self) -> Translatable | str: |
| 112 | + return self.filter |
| 113 | + |
| 114 | + def getCurrentPath(self) -> str: |
| 115 | + return self.currentPath |
| 116 | + |
| 117 | + def setDefaultName(self, name: Translatable | str): |
| 118 | + self.defaultName = name |
| 119 | + |
| 120 | + def getDefaultName(self) -> Translatable | str: |
| 121 | + return self.defaultName |
| 122 | + |
| 123 | + def setCurrentPath(self, newpath: str): |
| 124 | + """Slot to update the current path and refresh the label.""" |
| 125 | + self.currentPath = newpath |
| 126 | + self.updateLabelText() |
| 127 | + self.filePathUpdated.emit(self.currentPath) |
| 128 | + |
| 129 | + def requestOpenDialog(self): |
| 130 | + dialogTitle = self.tr_pleaseselect.format(fieldname=str(self.fieldName)) |
| 131 | + dialog = QFileDialog(self, dialogTitle, self.currentPath, str(self.filter)) |
| 132 | + if self.isDirectoryMode: |
| 133 | + dialog.setFileMode(QFileDialog.Directory) |
| 134 | + dialog.setOption(QFileDialog.ShowDirsOnly, True) |
| 135 | + else: |
| 136 | + dialog.setFileMode(QFileDialog.ExistingFile if self.isExistingOnly else QFileDialog.AnyFile) |
| 137 | + if self.isOutputInsteadofInput: |
| 138 | + dialog.setAcceptMode(QFileDialog.AcceptSave) |
| 139 | + else: |
| 140 | + dialog.setAcceptMode(QFileDialog.AcceptOpen) |
| 141 | + dialog.fileSelected.connect(self.setCurrentPath) |
| 142 | + dialog.finished.connect(dialog.deleteLater) |
| 143 | + dialog.show() |
| 144 | + |
| 145 | + def updateLabelText(self): |
| 146 | + if not self.currentPath: |
| 147 | + self.ui.pathLabel.setText(self.tr_notselected.format(fieldname=str(self.fieldName))) |
| 148 | + else: |
| 149 | + self.ui.pathLabel.setText(self.currentPath) |
| 150 | + |
| 151 | + def update_text(self): |
| 152 | + super().update_text() |
| 153 | + self.updateLabelText() |
| 154 | + |
| 155 | + def dragEnterEvent(self, e: QDragEnterEvent): |
| 156 | + if e.mimeData().hasUrls(): |
| 157 | + path = e.mimeData().urls()[0].toLocalFile() |
| 158 | + if not self.verifyCB or self.verifyCB(path): |
| 159 | + e.acceptProposedAction() |
| 160 | + else: |
| 161 | + super().dragEnterEvent(e) |
| 162 | + |
| 163 | + def dropEvent(self, event: QDropEvent): |
| 164 | + for url in event.mimeData().urls(): |
| 165 | + path = url.toLocalFile() |
| 166 | + if not self.verifyCB or self.verifyCB(path): |
| 167 | + self.setCurrentPath(path) |
| 168 | + return |
0 commit comments